-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler-v2] Some fixes to enum variant type checking (#14411)
* [compiler-v2] Some fixes to enum variant type checking Those fixes resulted from testing various scenarios for the Move book - Ensured that simple variant names are inferred for pack and not just unpack. This makes scenarios like `let c: Color = Blue` work. - Ensured that 0-ary constructors can be used as `Blue` and not need `Blue()` or `Blue{}`. Fixes #14365 A few test cases also now produce better error messages because types are earlier specialized. Added some new test cases. * Fix tests
- Loading branch information
Showing
17 changed files
with
366 additions
and
126 deletions.
There are no files selected for viewing
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
29 changes: 29 additions & 0 deletions
29
third_party/move/move-compiler-v2/tests/bytecode-generator/matching_refutable_err.exp
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 @@ | ||
// -- Model dump before bytecode pipeline | ||
module 0x815::m { | ||
enum E { | ||
None, | ||
Some { | ||
0: u64, | ||
} | ||
} | ||
private fun t(self: m::E): u64 { | ||
{ | ||
let m::E::Some{ 0: x } = self; | ||
x | ||
} | ||
} | ||
} // end 0x815::m | ||
|
||
============ initial bytecode ================ | ||
|
||
[variant baseline] | ||
fun m::t($t0: m::E): u64 { | ||
var $t1: u64 | ||
var $t2: u64 | ||
0: $t2 := unpack_variant m::E::Some($t0) | ||
1: $t1 := infer($t2) | ||
2: return $t1 | ||
} | ||
|
||
|
||
============ bytecode verification succeeded ======== |
13 changes: 13 additions & 0 deletions
13
third_party/move/move-compiler-v2/tests/bytecode-generator/matching_refutable_err.move
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,13 @@ | ||
module 0x815::m { | ||
|
||
enum E { | ||
None, | ||
Some(u64) | ||
} | ||
|
||
fun t(self: E): u64 { | ||
// We currently allow matching refutable patterns with let | ||
let Some(x) = self; | ||
x | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
third_party/move/move-compiler-v2/tests/checking/indexing/examples_book.exp
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,26 @@ | ||
// -- Model dump before bytecode pipeline | ||
module 0x1::m { | ||
struct R { | ||
value: bool, | ||
} | ||
private fun f1() | ||
acquires m::R(*) | ||
{ | ||
{ | ||
let x: &mut m::R = BorrowGlobal(Mutable)<m::R>(0x1); | ||
select m::R.value<&mut m::R>(x) = false; | ||
if Eq<bool>(select m::R.value<m::R>(BorrowGlobal(Immutable)<m::R>(0x1)), false) { | ||
Tuple() | ||
} else { | ||
Abort(1) | ||
}; | ||
select m::R.value<m::R>(BorrowGlobal(Mutable)<m::R>(0x1)) = true; | ||
if Eq<bool>(select m::R.value<m::R>(BorrowGlobal(Immutable)<m::R>(0x1)), true) { | ||
Tuple() | ||
} else { | ||
Abort(2) | ||
}; | ||
Tuple() | ||
} | ||
} | ||
} // end 0x1::m |
12 changes: 12 additions & 0 deletions
12
third_party/move/move-compiler-v2/tests/checking/indexing/examples_book.move
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,12 @@ | ||
module 0x1::m { | ||
|
||
struct R has key, drop { value: bool } | ||
|
||
fun f1() acquires R { | ||
let x = &mut R[@0x1]; | ||
x.value = false; | ||
assert!(R[@0x1].value == false, 1); | ||
R[@0x1].value = true; | ||
assert!(R[@0x1].value == true, 2); | ||
} | ||
} |
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
10 changes: 2 additions & 8 deletions
10
third_party/move/move-compiler-v2/tests/checking/typing/native_structs_pack_unpack.exp
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
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
47 changes: 47 additions & 0 deletions
47
third_party/move/move-compiler-v2/tests/checking/variants/variants_constants.exp
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,47 @@ | ||
// -- Model dump before bytecode pipeline | ||
module 0x815::m { | ||
enum Color { | ||
RGB { | ||
red: u64, | ||
green: u64, | ||
blue: u64, | ||
} | ||
Red, | ||
Blue, | ||
} | ||
private fun t0(): bool { | ||
{ | ||
let c: m::Color = pack m::Color::Red(); | ||
Eq<u64>(select_variants m::Color.RGB.red<m::Color>(c), 1) | ||
} | ||
} | ||
private fun t1(): bool { | ||
{ | ||
let c: m::Color = pack m::Color::Red(); | ||
Eq<u64>(select_variants m::Color.RGB.red<m::Color>(c), 1) | ||
} | ||
} | ||
private fun t2(): bool { | ||
{ | ||
let c: m::Color = pack m::Color::Blue(); | ||
Eq<u64>(select_variants m::Color.RGB.red<m::Color>(c), 1) | ||
} | ||
} | ||
private fun t3(): bool { | ||
{ | ||
let c: m::Color = pack m::Color::Blue(); | ||
Eq<u64>(select_variants m::Color.RGB.red<m::Color>(c), 1) | ||
} | ||
} | ||
private fun t4(c: &m::Color) { | ||
match (c) { | ||
m::Color::Red => { | ||
Abort(1) | ||
} | ||
m::Color::Blue => { | ||
Abort(2) | ||
} | ||
} | ||
|
||
} | ||
} // end 0x815::m |
31 changes: 31 additions & 0 deletions
31
third_party/move/move-compiler-v2/tests/checking/variants/variants_constants.move
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,31 @@ | ||
module 0x815::m { | ||
|
||
enum Color { | ||
RGB{red: u64, green: u64, blue: u64}, | ||
Red, | ||
Blue(), | ||
} | ||
|
||
fun t0(): bool { | ||
let c = Color::Red; // no error expected | ||
c.red == 1 | ||
} | ||
fun t1(): bool { | ||
let c : Color = Red; // no error expected | ||
c.red == 1 | ||
} | ||
|
||
fun t2(): bool { | ||
let c : Color = Blue; // no error expected | ||
c.red == 1 | ||
} | ||
|
||
fun t3(): bool { | ||
let c : Color = Blue(); // no error expected | ||
c.red == 1 | ||
} | ||
|
||
fun t4(c: &Color) { | ||
match (c) { Red => abort 1, Blue => abort 2 } // no error | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
third_party/move/move-compiler-v2/tests/checking/variants/variants_inference.exp
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,19 @@ | ||
|
||
Diagnostics: | ||
error: undeclared struct `m::Red` | ||
┌─ tests/checking/variants/variants_inference.move:17:14 | ||
│ | ||
17 │ take(Red{}) // errors expected because of bottom-up type inference | ||
│ ^^^ | ||
|
||
error: undeclared struct `m::Red` | ||
┌─ tests/checking/variants/variants_inference.move:21:17 | ||
│ | ||
21 │ let c = Red{}; // error expected | ||
│ ^^^ | ||
|
||
error: enum `m::Color` must be used with one of its variants | ||
┌─ tests/checking/variants/variants_inference.move:26:17 | ||
│ | ||
26 │ let c = Color{}; // error expected | ||
│ ^^^^^ |
34 changes: 34 additions & 0 deletions
34
third_party/move/move-compiler-v2/tests/checking/variants/variants_inference.move
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,34 @@ | ||
module 0x815::m { | ||
|
||
enum Color { | ||
RGB{red: u64, green: u64, blue: u64}, | ||
Red, | ||
Blue(u64), | ||
} | ||
|
||
fun take(_c: Color){} | ||
|
||
fun t1(): bool { | ||
let c : Color = Red{}; // no error expected | ||
c.red == 1 | ||
} | ||
|
||
fun t2() { | ||
take(Red{}) // errors expected because of bottom-up type inference | ||
} | ||
|
||
fun t3(): Color { | ||
let c = Red{}; // error expected | ||
c | ||
} | ||
|
||
fun t4(): Color { | ||
let c = Color{}; // error expected | ||
c | ||
} | ||
|
||
fun t5(): Color { | ||
let c : Color = Blue(0); // no error expected | ||
c | ||
} | ||
} |
Oops, something went wrong.