Skip to content

Commit

Permalink
[move] Add additional enums tests (MystenLabs#18567)
Browse files Browse the repository at this point in the history
## Description 

Adds a number of additional tests for enums. 

These tests test a couple things:
* All variations of "unpack wrong variant at runtime" (by value, mut
ref, and immut ref).
* Various incarnations of this (e.g., all switch arms branching to the
same branch).
* Various reference tests around unpacking enum variants. 

## Test plan 

It's all tests!
  • Loading branch information
tzakian authored Jul 8, 2024
1 parent 6d1fc34 commit 3d6e503
Show file tree
Hide file tree
Showing 20 changed files with 1,539 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
processed 2 tasks

task 0 'publish'. lines 1-22:
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::Enums'. Got VMError: {
major_status: WRITEREF_WITHOUT_DROP_ABILITY,
sub_status: None,
location: 0x1::Enums,
indices: [(FunctionDefinition, 0)],
offsets: [(FunctionDefinitionIndex(0), 10)],
}

task 1 'publish'. lines 24-47:
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::Enums'. Got VMError: {
major_status: WRITEREF_WITHOUT_DROP_ABILITY,
sub_status: None,
location: 0x1::Enums,
indices: [(FunctionDefinition, 0)],
offsets: [(FunctionDefinitionIndex(0), 10)],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//# publish
module 0x1.Enums {
enum PolyEnumWithTwoVariants<T> {
One { },
Two { x: T }
}

public g<T>(t: T, x2: T): T {
let e: Self.PolyEnumWithTwoVariants<T>;
let x: &mut T;
label b0:
e = PolyEnumWithTwoVariants.Two<T> { x: move(t) };
variant_switch PolyEnumWithTwoVariants (&e) {
One : b1,
Two : b1,
};
label b1:
&mut PolyEnumWithTwoVariants.Two<T> { x: x } = &mut e;
*move(x) = move(x2);
abort 0;
}
}

//# publish
module 0x1.Enums {
struct X { b: bool }

enum MonoEnumWithTwoVariants {
One { },
Two { x: Self.X }
}

public g(t: Self.X, x2: Self.X): Self.X {
let e: Self.MonoEnumWithTwoVariants;
let x: &mut Self.X;
label b0:
e = MonoEnumWithTwoVariants.Two { x: move(t) };
variant_switch MonoEnumWithTwoVariants (&e) {
One : b1,
Two : b1,
};
label b1:
&mut MonoEnumWithTwoVariants.Two { x: x } = &mut e;
*move(x) = move(x2);
abort 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 1 task
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//# publish
module 0x1.o {
enum X has drop {
One { x: u64, y: u64 },
Two { x: u64, y: u64},
}

a(e: &mut Self.X) {
let x: &mut u64;
let y: &mut u64;
label b0:
&mut X.One { x: x, y: y} = copy(e);
&mut X.Two { x: x, y: y} = move(e);
return;
}

b(e: &mut Self.X) {
let x: &mut u64;
let x2: &mut u64;
let y: &mut u64;
label b0:
&mut X.One { x: x, y: y} = copy(e);
*move(y) = 0;
&mut X.One { x: x2, y: y} = move(e);
// x and x2 are aliases of each other
*move(x) = 0;
*move(x2) = 0;
return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
processed 7 tasks

task 0 'publish'. lines 1-17:
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: {
major_status: FIELD_EXISTS_MUTABLE_BORROW_ERROR,
sub_status: None,
location: 0x1::o,
indices: [(FunctionDefinition, 0)],
offsets: [(FunctionDefinitionIndex(0), 5)],
}

task 1 'publish'. lines 19-36:
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: {
major_status: FIELD_EXISTS_MUTABLE_BORROW_ERROR,
sub_status: None,
location: 0x1::o,
indices: [(FunctionDefinition, 0)],
offsets: [(FunctionDefinitionIndex(0), 3)],
}

task 2 'publish'. lines 38-54:
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: {
major_status: FIELD_EXISTS_MUTABLE_BORROW_ERROR,
sub_status: None,
location: 0x1::o,
indices: [(FunctionDefinition, 0)],
offsets: [(FunctionDefinitionIndex(0), 3)],
}

task 3 'publish'. lines 56-74:
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: {
major_status: FIELD_EXISTS_MUTABLE_BORROW_ERROR,
sub_status: None,
location: 0x1::o,
indices: [(FunctionDefinition, 0)],
offsets: [(FunctionDefinitionIndex(0), 5)],
}

task 5 'publish'. lines 96-118:
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::invalid'. Got VMError: {
major_status: WRITEREF_EXISTS_BORROW_ERROR,
sub_status: None,
location: 0x1::invalid,
indices: [(FunctionDefinition, 0)],
offsets: [(FunctionDefinitionIndex(0), 21)],
}

task 6 'publish'. lines 120-142:
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::invalid'. Got VMError: {
major_status: READREF_EXISTS_MUTABLE_BORROW_ERROR,
sub_status: None,
location: 0x1::invalid,
indices: [(FunctionDefinition, 0)],
offsets: [(FunctionDefinitionIndex(0), 17)],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//# publish
module 0x1.o {
enum X has drop {
Empty { },
One { x: u64, y: u64 },
}

f(e: &mut Self.X) {
let a1: &mut Self.X;
let a2: &mut Self.X;
label b0:
a1 = copy(e);
a2 = copy(e);
&mut X.Empty{} = move(a2);
return;
}
}

//# publish
module 0x1.o {
enum X has drop {
One { x: u64, y: u64 },
Two { x: u64, y: u64},
}

c(e: &mut Self.X) {
let x: &mut u64;
let y: &mut u64;
let a1: &mut Self.X;
label b0:
a1 = copy(e);
&mut X.One { x: x, y: y} = copy(e);
return;
}

}

//# publish
module 0x1.o {
enum X has drop {
One { x: u64, y: u64 },
Two { x: u64, y: u64},
}

c(e: &mut Self.X) {
let x: &mut u64;
let y: &mut u64;
let a1: &mut Self.X;
label b0:
a1 = copy(e);
&mut X.One { x: x, y: y} = move(e);
return;
}
}

//# publish
module 0x1.o {
enum X has drop {
One { x: u64, y: u64 },
Two { x: u64, y: u64},
}

c(e: &mut Self.X) {
let x: &mut u64;
let y: &mut u64;
let a1: &mut Self.X;
let a2: &mut Self.X;
label b0:
a1 = copy(e);
a2 = copy(e);
&mut X.One { x: x, y: y} = move(a2);
return;
}
}

//# publish
module 0x1.o {
enum X has drop {
One { x: u64, y: u64 },
Two { x: u64, y: u64},
}

c(e: &mut Self.X) {
let x: &mut u64;
let y: &mut u64;
let a1: &mut Self.X;
let a2: &mut Self.X;
label b0:
a1 = copy(e);
a2 = copy(e);
&mut X.One { x: x, y: y} = move(a1);
return;
}
}

//# publish
module 0x1.invalid {
enum X has drop {
One { x: u64 },
Two { x: u64, y: u64},
}

h(e: &mut Self.X) {
let x: &mut u64;
let y: &mut u64;
let counter: &mut u64;
label b0:
&mut X.Two { x: x, y: y} = copy(e);
*move(x) = 1;
*copy(y) = 3;
counter = move(y);
&mut X.Two { x: x, y: y} = copy(e);
// Error: Invalid to write to `y` since it's factored to `counter` which is still a live mutable reference
*copy(y) = *copy(counter) - 1;
counter = move(y);
return;
}
}

//# publish
module 0x1.invalid {
enum X has drop {
One { x: u64 },
Two { x: u64, y: u64},
}

h(e: &mut Self.X) {
let x: &mut u64;
let y: &mut u64;
let counter: &mut u64;
label b0:
&mut X.Two { x: x, y: y} = copy(e);
*move(x) = 1;
*copy(y) = 3;
counter = move(y);
&mut X.Two { x: x, y: y} = copy(e);
// Error: Invalid to read `y` since it's factored to `counter` which is still a live mutable reference
*copy(y) = *copy(y) - 1;
counter = move(y);
return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
processed 7 tasks

task 0 'publish'. lines 1-20:
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: {
major_status: WRITEREF_EXISTS_BORROW_ERROR,
sub_status: None,
location: 0x1::o,
indices: [(FunctionDefinition, 0)],
offsets: [(FunctionDefinitionIndex(0), 13)],
}

task 1 'publish'. lines 22-47:
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: {
major_status: CALL_BORROWED_MUTABLE_REFERENCE_ERROR,
sub_status: None,
location: 0x1::o,
indices: [(FunctionDefinition, 0)],
offsets: [(FunctionDefinitionIndex(0), 11)],
}

task 2 'publish'. lines 49-68:
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: {
major_status: WRITEREF_EXISTS_BORROW_ERROR,
sub_status: None,
location: 0x1::o,
indices: [(FunctionDefinition, 0)],
offsets: [(FunctionDefinitionIndex(0), 10)],
}

task 3 'publish'. lines 70-95:
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: {
major_status: CALL_BORROWED_MUTABLE_REFERENCE_ERROR,
sub_status: None,
location: 0x1::o,
indices: [(FunctionDefinition, 0)],
offsets: [(FunctionDefinitionIndex(0), 8)],
}

task 4 'publish'. lines 97-114:
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: {
major_status: WRITEREF_EXISTS_BORROW_ERROR,
sub_status: None,
location: 0x1::o,
indices: [(FunctionDefinition, 0)],
offsets: [(FunctionDefinitionIndex(0), 10)],
}

task 5 'publish'. lines 116-134:
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: {
major_status: WRITEREF_EXISTS_BORROW_ERROR,
sub_status: None,
location: 0x1::o,
indices: [(FunctionDefinition, 0)],
offsets: [(FunctionDefinitionIndex(0), 10)],
}

task 6 'publish'. lines 136-160:
Error: Unable to publish module '0000000000000000000000000000000000000000000000000000000000000001::o'. Got VMError: {
major_status: CALL_BORROWED_MUTABLE_REFERENCE_ERROR,
sub_status: None,
location: 0x1::o,
indices: [(FunctionDefinition, 0)],
offsets: [(FunctionDefinitionIndex(0), 8)],
}
Loading

0 comments on commit 3d6e503

Please sign in to comment.