Diagnostics

gg check reports a use-after-consume error, in text and in JSON.

Source: use-after-consume.gg. JSON: use-after-consume.json. Text: use-after-consume.txt.

gg check use-after-consume.gg
gg check use-after-consume.gg --json

Apply a fix only when its applicability is safe or likely and verified is true. In this example the first fix meets that bar. The second is manual.

gg check --json
{
  "schema_version": 1,
  "tool": {
    "name": "gg",
    "version": "0.3.0"
  },
  "command": "check",
  "ok": false,
  "summary": {
    "errors": 1,
    "warnings": 0,
    "notes": 0,
    "suppressed": 0
  },
  "diagnostics": [
    {
      "id": "d1",
      "code": "GG0301",
      "rule": "use-after-consume",
      "severity": "error",
      "category": "ownership",
      "message": "`c` variable was moved (`consume`), can no longer be used.",
      "explanation": "A value passed to a `consume` parameter moves into the callee. The caller no longer owns it.",
      "span": {
        "file": "website\\diagnostics\\use-after-consume.gg",
        "start": {
          "line": 10,
          "column": 11,
          "offset": 147
        },
        "end": {
          "line": 10,
          "column": 18,
          "offset": 154
        }
      },
      "caused_by": null,
      "doc": "gg explain GG0301",
      "labels": [
        {
          "role": "cause",
          "message": "`c` is consumed here",
          "span": {
            "file": "website\\diagnostics\\use-after-consume.gg",
            "start": {
              "line": 9,
              "column": 12,
              "offset": 134
            },
            "end": {
              "line": 9,
              "column": 13,
              "offset": 135
            }
          }
        },
        {
          "role": "definition",
          "message": "`c` is declared here",
          "span": {
            "file": "website\\diagnostics\\use-after-consume.gg",
            "start": {
              "line": 8,
              "column": 5,
              "offset": 98
            },
            "end": {
              "line": 8,
              "column": 29,
              "offset": 122
            }
          }
        },
        {
          "role": "expected",
          "message": "`finish` takes `c` as `consume`",
          "span": {
            "file": "website\\diagnostics\\use-after-consume.gg",
            "start": {
              "line": 4,
              "column": 14,
              "offset": 45
            },
            "end": {
              "line": 4,
              "column": 22,
              "offset": 53
            }
          }
        }
      ],
      "details": {
        "value": "c",
        "state": "consumed",
        "consumed_at": {
          "line": 9,
          "column": 12
        },
        "used_at": {
          "line": 10,
          "column": 11
        },
        "callee": "finish",
        "parameter": "c"
      },
      "fixes": [
        {
          "id": "f1",
          "description": "Remove `consume` from `c` so the caller keeps ownership",
          "applicability": "likely",
          "verified": true,
          "edits": [
            {
              "file": "website\\diagnostics\\use-after-consume.gg",
              "start": {
                "line": 4,
                "column": 14,
                "offset": 45
              },
              "end": {
                "line": 4,
                "column": 22,
                "offset": 53
              },
              "replacement": ""
            }
          ]
        },
        {
          "id": "f2",
          "description": "Use `c` before it is consumed, or do not use it afterwards",
          "applicability": "manual",
          "verified": false,
          "edits": []
        }
      ]
    }
  ]
}
gg check
Error: `c` variable was moved (`consume`), can no longer be used.
  --> website\diagnostics\use-after-consume.gg:10:11  GG0301 use-after-consume
   |
10 |     print(c.value)
   |           ^^^^^^^
   |
 9 |     finish(c)
   |            ^ `c` is consumed here
   |
 8 |     c := Counter{ value: 1 }
   |     ^^^^^^^^^^^^^^^^^^^^^^^^ `c` is declared here
   |
 4 | fn finish(c: consume Counter):
   |              ^^^^^^^^ `finish` takes `c` as `consume`
   = help: Use before moving; or if `c` is `var`, reassign a value after moving.
   = fix (verified): Remove `consume` from `c` so the caller keeps ownership
   = fix (manual): Use `c` before it is consumed, or do not use it afterwards

1 error found.