Fixing and explaining errors

View source

Read an error code with actus explain, let actus fix apply the safe fixes, see the fixes that need your decision, and get the same data as JSON.

An error from the compiler has a code, such as E1005. Two commands help you with it. actus explain tells you what the code means. actus fix applies the fixes that the compiler is sure about. This page uses both.

Explain an error

A code tells you what kind of mistake it is. The same code always means the same thing. When a program uses a value after it was handed over, the error is:

actus
verb finish(dat data: Buffer) {
    drop(data);
}

verb main() {
    erg data: Buffer = Buffer[4];
    finish(data: dat data);
    println(int: buffer_length(buffer: abs data));
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1005] at 8:44: use of moved binding `data`

main handed data to finish with dat, so it no longer owns it. The last line uses it again. To read more, give the code to actus explain:

bash
actus explain E1005
text
E1005: use of a moved binding

A `dat` argument, an assignment to another owner and a returned binding all
move ownership. ADR-0016 says the source then becomes `Moved` and has no
cleanup of its own, so the compiler rejects every later use, including a
second move. The error points at the later use.
...

The output goes on with an example program that triggers the code and a fix. The text above is shortened here. The catalog is part of the actus program, so the command works without a network and always matches the compiler that you have installed.

A code that does not exist gives an error and the exit status 2:

bash
actus explain E9999
text
error: no entry for `E9999`

With --format json the entry is one JSON document. It has the fields schema_version, command, code, title and markdown, where markdown is the text unchanged. A program or an editor can read it.

Apply the safe fixes

Some errors have a fix that the compiler knows exactly. Here a verb is called with a dot, as the receiver's name and then the verb. Actus has one form for a call, and the error says which:

actus
struct Counter {
    count: Int,
}

verb main() {
    erg counter: Counter = Counter {
        count: 0,
    };
    counter.bump();
    counter.bump();
    println(int: counter.count);
}

verb bump(ins self: Counter) {
    self.count += 1;
}
bash
actus check
text
/home/you/hello/src/main.act: error[E1152] at 9:5: `receiver.bump(...)` does not call a verb: a call names every participant with its role, so write `bump(self: ins receiver, ...)`

actus fix checks the project the way actus check does, applies the fixes that are safe, and checks again. A safe fix cannot change what the program means. It repeats this in rounds until no safe fix is left. Add --dry-run first to see what it would do. Nothing is written:

bash
actus fix --dry-run
text
round 1: E1152 src/main.act:9:5: write the call as `bump(self: ...)`
round 2: E1152 src/main.act:10:5: write the call as `bump(self: ...)`
--- a/src/main.act
+++ b/src/main.act
@@ -6,8 +6,8 @@
     erg counter: Counter = Counter {
         count: 0,
     };
-    counter.bump();
-    counter.bump();
+    bump(self: ins counter);
+    bump(self: ins counter);
     println(int: counter.count);
 }
 
would apply 2 fixes in 2 rounds
files that would change:
  src/main.act: 2 fixes

The compiler found the role from the declaration, ins self: Counter, and wrote it in the call. The program lends counter to bump, as the verb declares. It needs two rounds because the compiler reports one error of this kind per check. Run actus fix without --dry-run to write the change:

bash
actus fix
text
round 1: E1152 src/main.act:9:5: write the call as `bump(self: ...)`
round 2: E1152 src/main.act:10:5: write the call as `bump(self: ...)`
applied 2 fixes in 2 rounds
changed files:
  src/main.act: 2 fixes

The program now is:

actus
struct Counter {
    count: Int,
}

verb main() {
    erg counter: Counter = Counter {
        count: 0,
    };
    bump(self: ins counter);
    bump(self: ins counter);
    println(int: counter.count);
}

verb bump(ins self: Counter) {
    self.count += 1;
}
bash
actus run
text
2
process exited with status 0

What actus fix writes:

  • It writes only the .act files of your project. It never writes Actus.toml or Actus.lock, and it never writes the standard library or a dependency. A safe fix in such a file is listed and left alone.
  • It writes the edits of the fixes and nothing else. It does not format. Run actus fmt afterwards if you want that.
  • Each file is replaced in one step, so a failure never leaves a file half written.
  • Every fix is applied whole or not at all. A fix may change several places.
  • It takes the same arguments as check: a file, and --strict.

The exit status is 0 when no error is left after the fixes, 1 when one is left, and 2 for a wrong use of the command.

Fixes that need your decision

Not every fix is safe. A review fix changes what the program does, so actus fix lists it and does not apply it. Here a call returns an Option and throws it away:

actus
verb main() {
    erg two = 2;
    first_even(a: abs two);
}

verb first_even(abs a: Int) -> Option[Int] {
    if a % 2 == 0 {
        return Option[Int].Some(a);
    }
    return Option[Int].None;
}
bash
actus fix
text
review fixes left (not applied):
  E1120 src/main.act:3:5: discard the result explicitly with `drop(...)`
no fixes to apply
/home/you/hello/src/main.act: error[E1120] at 3:5: must-use `Option` from `first_even` was ignored: an `Option` may hold no value, which must be handled
changed files: none

The status is 1, because the error is still there. The fix would write drop(first_even(a: abs two));, which says that you do not care about the outcome. Only you can say that. Maybe you want to look at the value with case instead, as in Errors: Result and Option. Make the choice and edit the file yourself.

The same data as JSON

actus check --format json and actus fix --format json print one JSON document and nothing else. Every error has its code, its message, its start and end in the file, and a list of fixes. Each fix has a title, its edits and an applicability that is safe or review. An editor shows the same fixes as quick fixes. A script should apply only the safe ones on its own, and show a review fix to a person first. Programs that read this output are described in Using Actus with AI assistants.

What you learned

  • An error code means the same thing every time. actus explain E#### prints what it means, an example and the fix, without a network.
  • actus fix applies the safe fixes of the project in rounds. Use --dry-run to read the changes first.
  • A review fix is listed and never applied. You decide.
  • --format json gives the errors and fixes as data for editors and scripts.

Next: Editor support.