Skip to content

Commit

Permalink
Merge branch 'master' into luc/rust-upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-blaeser committed May 22, 2023
2 parents 2394c95 + 66c55a1 commit f2aaa0b
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 135 deletions.
12 changes: 6 additions & 6 deletions nix/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"homepage": "",
"owner": "dfinity",
"repo": "candid",
"rev": "55d34ec060bd9dc735ed577e6924ddac79a415bf",
"sha256": "0v7lzp36h4xjg5qx7qcvh5jawxmc80ph1x5rmaddifp9mzb6vjhi",
"rev": "f7166f47d895e411a74de1eba4b347ac75f5fd26",
"sha256": "151nn11hn5h3bnj4h5wx37b5ggda7mdk0lclx03bc88imv1mh6zl",
"type": "tarball",
"url": "https://github.com/dfinity/candid/archive/55d34ec060bd9dc735ed577e6924ddac79a415bf.tar.gz",
"url": "https://github.com/dfinity/candid/archive/f7166f47d895e411a74de1eba4b347ac75f5fd26.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"esm": {
Expand Down Expand Up @@ -64,10 +64,10 @@
"homepage": null,
"owner": "dfinity",
"repo": "motoko-base",
"rev": "e31f984e225fbb94d9b2790129705ec4034a990f",
"sha256": "01f9242ycp1ky2icrn5i440k5wh2zjpg7wkrzjwr4wz9idcmxjr0",
"rev": "f319adb0283e4cb3e7fd4cfd57f2615946f735dc",
"sha256": "0bvhhcmdvl3k1zzz1xsfdsjkb6wzsqfqmham0kmarmg5dyk37a1s",
"type": "tarball",
"url": "https://github.com/dfinity/motoko-base/archive/e31f984e225fbb94d9b2790129705ec4034a990f.tar.gz",
"url": "https://github.com/dfinity/motoko-base/archive/f319adb0283e4cb3e7fd4cfd57f2615946f735dc.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"motoko-matchers": {
Expand Down
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
2 changes: 1 addition & 1 deletion src/lang_utils/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
(wrapped false)

(preprocess (pps ppx_blob))
(preprocessor_deps (glob_files error_codes/*.adoc))
(preprocessor_deps (glob_files error_codes/*.md))
(instrumentation (backend bisect_ppx --bisect-silent yes))
)
22 changes: 11 additions & 11 deletions src/lang_utils/error_codes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let error_codes : (string * string option) list =
"M0000", None; (* Internal compiler error *)
"M0001", None; (* Parsing errors *)
"M0002", None; (* Lexer errors *)
"M0003", Some([%blob "lang_utils/error_codes/M0003.adoc"]); (* Self-import *)
"M0003", Some([%blob "lang_utils/error_codes/M0003.md"]); (* Self-import *)
"M0004", None; (* IDL file doesn't define a service *)
"M0005", None; (* Case mismatch between import and filename *)
"M0006", None; (* Failed to parse import URL *)
Expand Down Expand Up @@ -140,7 +140,7 @@ let error_codes : (string * string option) list =
"M0134", None; (* Class body type mismatch *)
"M0135", None; (* Actor class has non-async return type *)
"M0136", None; (* Empty block type mismatch *)
"M0137", Some([%blob "lang_utils/error_codes/M0137.adoc"]); (* Type definition references type parameter from outer scope *)
"M0137", Some([%blob "lang_utils/error_codes/M0137.md"]); (* Type definition references type parameter from outer scope *)
"M0138", None; (* Actor classes are not supported *)
"M0139", None; (* Inner actor classes are not supported *)
"M0140", None; (* Actor classes with type parameters are not supported *)
Expand All @@ -152,16 +152,16 @@ let error_codes : (string * string option) list =
"M0146", None; (* Pattern is never matched *)
(* "M0147" DEFUNCT Object syntax is deprecated in this position *)
(* "M0148" DEFUNCT Block syntax is deprecated in this position *)
"M0149", Some([%blob "lang_utils/error_codes/M0149.adoc"]); (* Expected mutable 'var' field, found immutable field *)
"M0150", Some([%blob "lang_utils/error_codes/M0150.adoc"]); (* Expected immutable field, found mutable 'var' field *)
"M0151", Some([%blob "lang_utils/error_codes/M0151.adoc"]); (* missing field in object literal *)
"M0149", Some([%blob "lang_utils/error_codes/M0149.md"]); (* Expected mutable 'var' field, found immutable field *)
"M0150", Some([%blob "lang_utils/error_codes/M0150.md"]); (* Expected immutable field, found mutable 'var' field *)
"M0151", Some([%blob "lang_utils/error_codes/M0151.md"]); (* missing field in object literal *)
(* "M0152" DEFUNCT Word field deprecation *)
"M0153", Some([%blob "lang_utils/error_codes/M0153.adoc"]); (* IDL types not expressible in Motoko *)
"M0154", Some([%blob "lang_utils/error_codes/M0154.adoc"]); (* Deprecation annotation *)
"M0155", Some([%blob "lang_utils/error_codes/M0155.adoc"]); (* Inferred type Nat for subtraction *)
"M0156", Some([%blob "lang_utils/error_codes/M0156.adoc"]); (* block contains expansive type definitions *)
"M0157", Some([%blob "lang_utils/error_codes/M0157.adoc"]); (* block contains non-productive type definitions *)
"M0158", Some([%blob "lang_utils/error_codes/M0158.adoc"]); (* a public class cannot be anonymous, please provide a name *)
"M0153", Some([%blob "lang_utils/error_codes/M0153.md"]); (* IDL types not expressible in Motoko *)
"M0154", Some([%blob "lang_utils/error_codes/M0154.md"]); (* Deprecation annotation *)
"M0155", Some([%blob "lang_utils/error_codes/M0155.md"]); (* Inferred type Nat for subtraction *)
"M0156", Some([%blob "lang_utils/error_codes/M0156.md"]); (* block contains expansive type definitions *)
"M0157", Some([%blob "lang_utils/error_codes/M0157.md"]); (* block contains non-productive type definitions *)
"M0158", Some([%blob "lang_utils/error_codes/M0158.md"]); (* a public class cannot be anonymous, please provide a name *)
(* "M0159" DEFUNCT Word deprecation *)
"M0160", None; (* Candid method name is not a valid Motoko identifier *)
"M0161", None; (* Candid float32 type cannot be imported as a Motoko type *)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
= M0003
# M0003

This error means that a module tried to import itself.

Erroneous code example (file is called `Self.mo`):

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

If you encounter this error you should probably remove the offending import.
30 changes: 0 additions & 30 deletions src/lang_utils/error_codes/M0137.adoc

This file was deleted.

38 changes: 38 additions & 0 deletions src/lang_utils/error_codes/M0137.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# M0137

This error means that you declared a type or class that explicitly or implicitly references
an outer type parameter.

Erroneous code examples:

```motoko
class C<T>(){
type U = T; // type U mentions parameter T of class C
};
```

```motoko
class D<T>(){
class E(x : T) {
public let y : T = x; // class E mentions parameter T of class D in a field
};
}
```

To avoid this error, try parameterizing the inner types.

```motoko
class C<T>(){
type U<T1> = T1;
};
```

```motoko
class D<T>(){
class E<T1>(x : T1) {
public let y : T1 = x;
};
}
```

This is a temporary restriction of Motoko that we hope to remove in future.
13 changes: 0 additions & 13 deletions src/lang_utils/error_codes/M0149.adoc

This file was deleted.

15 changes: 15 additions & 0 deletions src/lang_utils/error_codes/M0149.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# M0149

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:

```motoko
{ count = 0 } : { var count : Nat }
```

If you encounter this error, you should probably insert the `var` keyword:

```motoko
{ var count = 1 } : { var count : Nat }
```
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
= M0150
# M0150

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

Erroneous code example:


{ var name = "Fred" } : { name : Text }

```motoko
{ var name = "Fred" } : { name : Text }
```

If you encounter this error, you should probably omit `var`:

{ name = "Fred" } : { name : Text }
```motoko
{ name = "Fred" } : { name : Text }
```
14 changes: 0 additions & 14 deletions src/lang_utils/error_codes/M0151.adoc

This file was deleted.

18 changes: 18 additions & 0 deletions src/lang_utils/error_codes/M0151.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# M0151

This error means that a object literal is missing some fields, maybe because of a typo.

Erroneous code examples:

```motoko
{ first_name = "Fred" } : { firstName : Text }
{ firstName = "Fred" } : { firstName : Text; lastName : Text }
```

If you encounter this error, you need to add the missing field name to the
object literal.

```motoko
{ firstName = "Fred" } : { firstName : Text }
{ firstName = "Fred"; lastName = "Flintstone" } : { firstName : Text; lastName : Text }
```
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
= M0153
# M0153

This error means that an imported Candid file (`.did`) mentions types that
cannot be represented in Motoko. These are

* `float32`
* `service` types with method names that are not identifiers, e.g. because
they contain special characters.
* `float32`
* `service` types with method names that are not identifiers, e.g. because
they contain special characters.

If you encounter this error, and you can, you should avoid these types in the
service’s interface. If you have no control over the interface, you cannot
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
= M0154
# M0154

You are using a field (typically a module field) that has a deprecation annotation
attached to its definition, e.g.

module SomeModule {
```motoko
module SomeModule {
/// @deprecated The foo function is deprecated and will be removed next release
public func foo() {}
/// @deprecated The foo function is deprecated and will be removed next release
public func foo() {}
}
}
```

The warning should include an explanation provided by the author of that code.
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
= M0155
# 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.

Offending code examples:

```
func f(n : Nat) {
if (n < 10) { return };
let m = 2 * (n - 1);
};
```motoko
func f(n : Nat) {
if (n < 10) { return };
let m = 2 * (n - 1);
};
func g(n : Nat) {
if (n - 1 < 10) { return };
};
func g(n : Nat) {
if (n - 1 < 10) { return };
};
```

If the subtraction was indeed intended to have a `Nat` result, you can let the compiler know by annotating the intended type explicitly:

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

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 };
};
```
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
= M0156
# M0156

This error means that a parameterized type definition, or set of type definitions, is too complicated for Motoko to accept.

Motoko rejects type definitions that are expansive, in the sense that unfolding type definitions may produce an ever-expanding set of types.

For example, the type definition:

type List<T> = ?(T, List<T>),
```motoko
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:

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

that recursively instantiates `Seq` with a larger type, `[T]`, containing `T`, is *expansive* and rejected.

Expand Down
Loading

0 comments on commit f2aaa0b

Please sign in to comment.