-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update contract optimization for ADT Before the introduction of ADT, enum types couldn't contain other type components. Now they can, as in e.g. `[| 'Foo (String -> Number) |]`. The optimizer is updated to recurse into those types as well. * Proper contract for extended enum types (ADTs) ADTs have been introduced (experimentally) in previous commits. In practice, this means that the enum types have become more expressive, where value can now be variant with an associated argument. The contract derived from an enum type hasn't been updated yet, and wasn't correctly handling more elaborate enums. This commit implements the proper behavior for full enum types. This commit doesn't change the previous status quo around polymorphic enum types, in that it doesn't enforce parametricity at run-time. While this is questionable, we are somehow stuck with this choice for now, because suddenly enforcing parametricity would break backward-compatibility, including the types of some functions from the stdlib. * Converts type annotations to contract annotations Since optimization has been introduced, eliding some contract application for type annotations, it's safer to use contract annotation in test whenever we really want to check the behavior of contracts at run-time. While this change shouldn't make any difference right now, it's probably more future-proof, if further optimizations one day elide those contracts. * Add tests for enum/adt contracts * Format internals.ncl * Apply suggestions from code review Co-authored-by: Viktor Kleen <[email protected]> * Remove trailing backtick in comment --------- Co-authored-by: Viktor Kleen <[email protected]>
- Loading branch information
Showing
10 changed files
with
516 additions
and
300 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
cli/tests/snapshot/inputs/errors/enum_contract_shape_mismatch.ncl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# capture = 'stderr' | ||
# command = ['eval'] | ||
(std.function.id | forall r. [| 'Foo Number, 'Bar String, 'Qux; r |] -> [| 'Foo Number, 'Bar String, 'Qux; r |]) 'Foo |
3 changes: 3 additions & 0 deletions
3
cli/tests/snapshot/inputs/errors/enum_contract_shape_mismatch_rev.ncl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# capture = 'stderr' | ||
# command = ['eval'] | ||
(std.function.id | forall r. [| 'Foo, 'Bar String, 'Qux Dyn; r |] -> [| 'Foo, 'Bar String, 'Qux Dyn; r |]) ('Foo..(5)) |
29 changes: 29 additions & 0 deletions
29
cli/tests/snapshot/snapshots/snapshot__eval_stderr_enum_contract_shape_mismatch.ncl.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
source: cli/tests/snapshot/main.rs | ||
expression: err | ||
--- | ||
error: contract broken by the caller | ||
shape mismatch for 'Foo | ||
┌─ [INPUTS_PATH]/errors/enum_contract_shape_mismatch.ncl:3:30 | ||
│ | ||
3 │ (std.function.id | forall r. [| 'Foo Number, 'Bar String, 'Qux; r |] -> [| 'Foo Number, 'Bar String, 'Qux; r |]) 'Foo | ||
│ --------------------------------------- ---- evaluated to this expression | ||
│ │ | ||
│ expected type of the argument provided by the caller | ||
│ | ||
= Found an enum with tag `'Foo` which is indeed part of the expected enum type | ||
= However, their shape differs: one is an enum variant that carries an argument while the other is a bare enum tag | ||
|
||
note: | ||
┌─ [INPUTS_PATH]/errors/enum_contract_shape_mismatch.ncl:3:1 | ||
│ | ||
3 │ (std.function.id | forall r. [| 'Foo Number, 'Bar String, 'Qux; r |] -> [| 'Foo Number, 'Bar String, 'Qux; r |]) 'Foo | ||
│ --------------------------------------------------------------------------------------------------------------------- (1) calling <func> | ||
|
||
note: | ||
┌─ [INPUTS_PATH]/errors/enum_contract_shape_mismatch.ncl:3:2 | ||
│ | ||
3 │ (std.function.id | forall r. [| 'Foo Number, 'Bar String, 'Qux; r |] -> [| 'Foo Number, 'Bar String, 'Qux; r |]) 'Foo | ||
│ --------------- (2) calling <func> | ||
|
||
|
29 changes: 29 additions & 0 deletions
29
cli/tests/snapshot/snapshots/snapshot__eval_stderr_enum_contract_shape_mismatch_rev.ncl.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
source: cli/tests/snapshot/main.rs | ||
expression: err | ||
--- | ||
error: contract broken by the caller | ||
shape mismatch for 'Foo | ||
┌─ [INPUTS_PATH]/errors/enum_contract_shape_mismatch_rev.ncl:3:30 | ||
│ | ||
3 │ (std.function.id | forall r. [| 'Foo, 'Bar String, 'Qux Dyn; r |] -> [| 'Foo, 'Bar String, 'Qux Dyn; r |]) ('Foo..(5)) | ||
│ ------------------------------------ ----------- evaluated to this expression | ||
│ │ | ||
│ expected type of the argument provided by the caller | ||
│ | ||
= Found an enum with tag `'Foo` which is indeed part of the expected enum type | ||
= However, their shape differs: one is an enum variant that carries an argument while the other is a bare enum tag | ||
|
||
note: | ||
┌─ [INPUTS_PATH]/errors/enum_contract_shape_mismatch_rev.ncl:3:1 | ||
│ | ||
3 │ (std.function.id | forall r. [| 'Foo, 'Bar String, 'Qux Dyn; r |] -> [| 'Foo, 'Bar String, 'Qux Dyn; r |]) ('Foo..(5)) | ||
│ ---------------------------------------------------------------------------------------------------------------------- (1) calling <func> | ||
|
||
note: | ||
┌─ [INPUTS_PATH]/errors/enum_contract_shape_mismatch_rev.ncl:3:2 | ||
│ | ||
3 │ (std.function.id | forall r. [| 'Foo, 'Bar String, 'Qux Dyn; r |] -> [| 'Foo, 'Bar String, 'Qux Dyn; r |]) ('Foo..(5)) | ||
│ --------------- (2) calling <func> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.