Skip to content

Commit

Permalink
[move] Add large enum test (MystenLabs#19075)
Browse files Browse the repository at this point in the history
## Description 

- Fix compiler's sui-mode entry rules for enums
- Add large enum tests for entry functions 

## Test plan 

- Added tests 

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
tnowacki authored Aug 22, 2024
1 parent bd0dc2f commit 3170d58
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
processed 4 tasks

init:
A: object(0,0)

task 1, lines 8-35:
//# publish
created: object(1,0)
mutated: object(0,1)
gas summary: computation_cost: 1000000, storage_cost: 6452400, storage_rebate: 0, non_refundable_storage_fee: 0

task 2, lines 37-38:
//# programmable --sender A
//> test::m::x1()
mutated: object(0,0)
gas summary: computation_cost: 1000000, storage_cost: 988000, storage_rebate: 0, non_refundable_storage_fee: 0

task 3, lines 40-41:
//# programmable --sender A
//> test::m::x3()
Error: Transaction Effects Status: Move Bytecode Verification Error. Please run the Bytecode Verifier for more information.
Execution Error: ExecutionError: ExecutionError { inner: ExecutionErrorInner { kind: VMVerificationOrDeserializationError, source: Some(VMError { major_status: TOO_MANY_TYPE_NODES, sub_status: None, message: None, exec_state: None, location: Undefined, indices: [], offsets: [] }), command: Some(0) } }
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

// tests error after serializing a large enum return value

//# init --addresses test=0x0 --accounts A

//# publish

module test::m {

public enum X1 has drop {
Big1(u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8),
}

public enum X2 has drop {
V1(X1, X1, X1),
V2(X1, X1, X1),
V3(X1, X1, X1),
}

public enum X3 has drop {
X2(X2, X2, X2),
U64(u64),
}

entry fun x1(): X1 {
X1::Big1(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
}

entry fun x3(): X3 {
X3::U64(0)
}

}

//# programmable --sender A
//> test::m::x1()

//# programmable --sender A
//> test::m::x3()
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
processed 4 tasks

task 2, line 34:
//# run 0x42::m::x1
return values: |0|{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

task 3, line 36:
//# run 0x42::m::x3
Error: Function execution failed with VMError: {
major_status: VERIFICATION_ERROR,
sub_status: None,
location: undefined,
indices: [],
offsets: [],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// tests error after serializing a large enum return value

//# init --edition 2024.alpha

//# publish

module 0x42::m {

public enum X1 {
Big(u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8),
}

public enum X2 {
V1(X1, X1, X1),
V2(X1, X1, X1),
V3(X1, X1, X1),
}

public enum X3 {
X2(X2, X2, X2),
U64(u64),
}

entry fun x1(): X1 {
X1::Big(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
}

entry fun x3(): X3 {
X3::U64(0)
}

}

//# run 0x42::m::x1

//# run 0x42::m::x3
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,21 @@ impl<const AFTER_TYPING: bool> ProgramInfo<AFTER_TYPING> {
_ => panic!("ICE should have failed in naming"),
}
}

pub fn datatype_declared_loc(&self, m: &ModuleIdent, n: &DatatypeName) -> Loc {
match self.datatype_kind(m, n) {
DatatypeKind::Struct => self.struct_declared_loc_(m, &n.0.value),
DatatypeKind::Enum => self.enum_declared_loc_(m, &n.0.value),
}
}

pub fn datatype_declared_abilities(&self, m: &ModuleIdent, n: &DatatypeName) -> &AbilitySet {
match self.datatype_kind(m, n) {
DatatypeKind::Struct => self.struct_declared_abilities(m, n),
DatatypeKind::Enum => self.enum_declared_abilities(m, n),
}
}

pub fn struct_definition(&self, m: &ModuleIdent, n: &DatatypeName) -> &StructDefinition {
self.struct_definition_opt(m, n)
.expect("ICE should have failed in naming")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -868,8 +868,8 @@ fn entry_return(
let (declared_loc_opt, declared_abilities) = match tn_ {
TypeName_::Multiple(_) => (None, AbilitySet::collection(*tloc)),
TypeName_::ModuleType(m, n) => (
Some(context.info.struct_declared_loc(m, n)),
context.info.struct_declared_abilities(m, n).clone(),
Some(context.info.datatype_declared_loc(m, n)),
context.info.datatype_declared_abilities(m, n).clone(),
),
TypeName_::Builtin(b) => (None, b.value.declared_abilities(b.loc)),
};
Expand Down Expand Up @@ -1064,7 +1064,7 @@ fn check_private_transfer(context: &mut Context, loc: Loc, mcall: &ModuleCall) {
let store_loc = if let Some((first_ty_module, first_ty_name)) = &first_ty_tn {
let abilities = context
.info
.struct_declared_abilities(first_ty_module, first_ty_name);
.datatype_declared_abilities(first_ty_module, first_ty_name);
abilities.ability_loc_(Ability_::Store).unwrap()
} else {
first_ty
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
error[Sui E02007]: invalid object declaration
┌─ tests/sui_mode/move_2024/typing/enum_in_struct_position.move:11:13
11 │ public enum Obj has key, store {
│ ^^^ --- Enums cannot have the 'key' ability.
│ │
│ Invalid object 'Obj'

error[Sui E02002]: invalid 'entry' function signature
┌─ tests/sui_mode/move_2024/typing/enum_in_struct_position.move:18:1
3 │ public enum E {
│ - To satisfy the constraint, the 'drop' ability would need to be added here
·
18 │ entry fun ret(): E {
│ ^^^^^ - The type 'a::m::E' does not have the ability 'drop'
│ │
│ Invalid return type for entry function 'ret'

error[Sui E02002]: invalid 'entry' function signature
┌─ tests/sui_mode/move_2024/typing/enum_in_struct_position.move:22:14
22 │ entry fun x3(_: E) {
│ ----- ^ - 'entry' parameters must be primitives (by-value), vectors of primitives, objects (by-reference or by-value), vectors of objects, or 'Receiving' arguments (by-reference or by-value)
│ │ │
│ │ Invalid 'entry' parameter type for parameter '_'
│ 'x3' was declared 'entry' here

error[Sui E02009]: invalid private transfer call
┌─ tests/sui_mode/move_2024/typing/enum_in_struct_position.move:30:5
11 │ public enum Obj has key, store {
│ ----- The object has 'store' so 'sui::transfer::public_transfer' can be called instead
·
29 │ public fun transfer(o: a::m::Obj) {
│ --------- The type 'a::m::Obj' is not declared in the current module
30 │ transfer::transfer(o, @0)
│ ^^^^^^^^^^^^^^^^^^^^^^^^^ Invalid private transfer. The function 'sui::transfer::transfer' is restricted to being called in the object's module, 'a::m'

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module a::m {

public enum E {
V()
}

public enum M has drop {
V()
}

public enum Obj has key, store {
V()
}

fun init(_: M, _: &mut TxContext) {
}

entry fun ret(): E {
E::V()
}

entry fun x3(_: E) {
abort 0
}

}

module a::n {
public fun transfer(o: a::m::Obj) {
transfer::transfer(o, @0)
}
}

module sui::transfer {
public fun transfer<T: key>(_: T, _: address) {
abort 0
}
}

module sui::tx_context{
public struct TxContext has drop {}
}

0 comments on commit 3170d58

Please sign in to comment.