Skip to content

Commit

Permalink
feat: include error categories and codes in moc.js diagnostics (#3991)
Browse files Browse the repository at this point in the history
- Sends more diagnostic information through `moc.js`
- Fixes various typos and formatting inconsistencies in the error code explanation files
  • Loading branch information
rvanasa authored May 19, 2023
1 parent 155d77e commit 66c55a1
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/js/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ let diagnostics_of_msg (msg : Diag.message) =
val source = Js.string msg.at.left.file
val severity = match msg.sev with Diag.Error -> 1 | (Diag.Warning | Diag.Info) -> 2
val range = range_of_region msg.at
val code = Js.string msg.code
val category = Js.string msg.cat
val message = Js.string msg.text
end)

Expand Down
3 changes: 2 additions & 1 deletion src/lang_utils/error_codes/M0003.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Erroneous code example (file is called `Self.mo`):

```motoko
import S "./Self"; // import error, file Self.mo must not depend on itself
module { }
module { ... }
```

If you encounter this error you should probably remove the offending import.
2 changes: 1 addition & 1 deletion src/lang_utils/error_codes/M0149.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# M0149

This error means that you supplied a immutable record field (declared without `var`), where a mutable record field (specified with `var`), was expected.
This error means that you supplied an immutable record field (declared without `var`), where a mutable record field (specified with `var`), was expected.

Erroneous code example:

Expand Down
19 changes: 10 additions & 9 deletions src/lang_utils/error_codes/M0155.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# M0155

This warning indicates that the type of a subtraction operation had to be deduced from its operands and was inferred to be `Nat`.
That implies that it traps when the result is negative, which may be unintentional.

Expand Down Expand Up @@ -26,23 +27,23 @@ func f(n : Nat) {
If the intended type was `Int`, however, you can either annotate it as such:

```
func f(n : Nat) {
let m : Int = 2 * (n - 1);
};
func f(n : Nat) {
let m : Int = 2 * (n - 1);
};
```

Or you can insert a sign operator `+`, which also forces the expression to be of type `Int`:

```
func f(n : Nat) {
let m = 2 * (+n - 1);
};
func f(n : Nat) {
let m = 2 * (+n - 1);
};
```

This latter possibility is particularly convenient in the case of comparisons, because it is always okay to perform them at type `Int`:

```
func g(n : Nat) {
if (+n - 1 < 10) { return };
};
func g(n : Nat) {
if (+n - 1 < 10) { return };
};
```
4 changes: 2 additions & 2 deletions src/lang_utils/error_codes/M0156.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Motoko rejects type definitions that are expansive, in the sense that unfolding
For example, the type definition:

```motoko
type List<T> = ?(T, List<T>),
type List<T> = ?(T, List<T>);
```

that recursively instantiates `List` at the same parameter `T`, is non-expansive and accepted, but the similar looking definition:

```motoko
type Seq<T> = ?(T, Seq<[T]>),
type Seq<T> = ?(T, Seq<[T]>);
```

that recursively instantiates `Seq` with a larger type, `[T]`, containing `T`, is *expansive* and rejected.
Expand Down
2 changes: 1 addition & 1 deletion src/lang_utils/error_codes/M0158.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class () {};
If you encounter this error, you should probably name the class or make it private.

```motoko
public class C () {};
public class C() {};
```

Public fields must be named since they determine the interface of the enclosing object.
11 changes: 7 additions & 4 deletions test/test-moc.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ moc.Motoko.saveFile(
};
}`
);
moc.Motoko.saveFile('text.mo', `let s = "${'⛔|'.repeat(10000)}"; s.size()`); // #3822
moc.Motoko.saveFile("text.mo", `let s = "${"⛔|".repeat(10000)}"; s.size()`); // #3822

try {
assert.equal(moc.Motoko.readFile("empty.mo"), "");
Expand Down Expand Up @@ -102,6 +102,8 @@ try {
},
severity: 1,
source: "bad.mo",
code: "M0001",
category: "syntax",
message:
"unexpected end of input, expected one of token or <phrase> sequence:\n <exp_bin(ob)>",
},
Expand Down Expand Up @@ -130,6 +132,8 @@ try {
},
severity: 1,
source: "bad.mo",
category: "syntax",
code: "M0001",
message:
"unexpected end of input, expected one of token or <phrase> sequence:\n <exp_bin(ob)>",
},
Expand Down Expand Up @@ -158,9 +162,8 @@ try {
// Check that long text literals type-check without error
assert.deepStrictEqual(moc.Motoko.check("text.mo"), {
code: null,
diagnostics: []
diagnostics: [],
});
}
catch (err) {
} catch (err) {
assert.fail(err);
}

0 comments on commit 66c55a1

Please sign in to comment.