From 5ad7598484f9fa36d64e2eba06c898404507b76d Mon Sep 17 00:00:00 2001 From: Teng Zhang Date: Wed, 13 Nov 2024 09:56:35 -0800 Subject: [PATCH] add more test cases --- .../tests/checking/receiver/calls_index.exp | 44 +++++++++++++++++++ .../tests/checking/receiver/calls_index.move | 24 ++++++++++ .../tests/no-v1-comparison/index.exp | 2 +- .../tests/no-v1-comparison/index.move | 37 ++++++++++++++++ .../move-model/src/builder/exp_builder.rs | 9 ++-- 5 files changed, 111 insertions(+), 5 deletions(-) diff --git a/third_party/move/move-compiler-v2/tests/checking/receiver/calls_index.exp b/third_party/move/move-compiler-v2/tests/checking/receiver/calls_index.exp index ab49c2dd513727..643cdd7d3ac089 100644 --- a/third_party/move/move-compiler-v2/tests/checking/receiver/calls_index.exp +++ b/third_party/move/move-compiler-v2/tests/checking/receiver/calls_index.exp @@ -9,6 +9,9 @@ module 0x42::m { struct W { x: u64, } + struct Wrapper { + inner: T, + } private fun boo(v: vector,w: W) { m::merge(Borrow(Mutable)(select m::T.w(select m::S.t(vector::borrow_mut(Borrow(Mutable)(v), 0)))), w) } @@ -21,6 +24,17 @@ module 0x42::m { private fun boo_greater_(v: vector,w: W): bool { m::greater(Freeze(false)(Borrow(Mutable)(select m::T.w(select m::S.t(vector::borrow_mut(Borrow(Mutable)(v), 0))))), w) } + private fun boo_greater_2(v: vector,w: W): bool { + m::greater(vector::borrow(Borrow(Immutable)(v), 0), w) + } + private fun boo_greater_2_(v: vector,w: W): bool { + m::greater(vector::borrow(Borrow(Immutable)(v), 0), w) + } + private fun dispatch(account: address): T + acquires Wrapper(*) + { + m::unwrap(BorrowGlobal(Immutable)>(account)) + } private fun foo(account: address,w: W) acquires S(*) { @@ -51,6 +65,11 @@ module 0x42::m { { m::greater(Freeze(false)(Borrow(Mutable)(select m::T.w(select m::S.t(BorrowGlobal(Mutable)(account))))), w) } + private fun foo_greater_2(account: address,w: W): bool + acquires S(*) + { + m::greater(BorrowGlobal(Immutable)(account), w) + } private fun greater(self: &W,s: W): bool { Gt(select m::W.x<&W>(self), select m::W.x(s)) } @@ -64,6 +83,9 @@ module 0x42::m { }; Tuple() } + private fun unwrap(self: &Wrapper): T { + select m::Wrapper.inner<&Wrapper>(self) + } } // end 0x42::m // -- Sourcified model before bytecode pipeline @@ -77,6 +99,9 @@ module 0x42::m { struct W has drop, store, key { x: u64, } + struct Wrapper has copy, drop, store, key { + inner: T, + } fun boo(v: vector, w: W) { merge(&mut 0x1::vector::borrow_mut(&mut v, 0).t.w, w) } @@ -89,6 +114,17 @@ module 0x42::m { fun boo_greater_(v: vector, w: W): bool { greater(/*freeze*/&mut 0x1::vector::borrow_mut(&mut v, 0).t.w, w) } + fun boo_greater_2(v: vector, w: W): bool { + greater(0x1::vector::borrow(&v, 0), w) + } + fun boo_greater_2_(v: vector, w: W): bool { + greater(0x1::vector::borrow(&v, 0), w) + } + fun dispatch(account: address): T + acquires Wrapper + { + unwrap(borrow_global>(account)) + } fun foo(account: address, w: W) acquires S { @@ -119,6 +155,11 @@ module 0x42::m { { greater(/*freeze*/&mut borrow_global_mut(account).t.w, w) } + fun foo_greater_2(account: address, w: W): bool + acquires S + { + greater(borrow_global(account), w) + } fun greater(self: &W, s: W): bool { self.x > s.x } @@ -129,4 +170,7 @@ module 0x42::m { *$t1 = *$t1 + $t2 }; } + fun unwrap(self: &Wrapper): T { + self.inner + } } diff --git a/third_party/move/move-compiler-v2/tests/checking/receiver/calls_index.move b/third_party/move/move-compiler-v2/tests/checking/receiver/calls_index.move index ca23d76f65368c..8f0726ce369a10 100644 --- a/third_party/move/move-compiler-v2/tests/checking/receiver/calls_index.move +++ b/third_party/move/move-compiler-v2/tests/checking/receiver/calls_index.move @@ -46,10 +46,18 @@ module 0x42::m { S[account].t.w.greater(w) } + fun foo_greater_2(account: address, w: W): bool acquires S { + W[account].greater(w) + } + fun boo_greater(v: vector, w: W): bool { v[0].t.w.greater(w) } + fun boo_greater_2(v: vector, w: W): bool { + v[0].greater(w) + } + fun foo_greater_(account: address, w: W): bool acquires S { (&mut S[account].t.w).greater(w) } @@ -58,4 +66,20 @@ module 0x42::m { (&mut v[0].t.w).greater(w) } + fun boo_greater_2_(v: vector, w: W): bool { + (&v[0]).greater(w) + } + + struct Wrapper has drop, key, store, copy { + inner: T + } + + fun unwrap(self: &Wrapper): T { + self.inner + } + + fun dispatch(account: address): T acquires Wrapper { + Wrapper[account].unwrap() + } + } diff --git a/third_party/move/move-compiler-v2/transactional-tests/tests/no-v1-comparison/index.exp b/third_party/move/move-compiler-v2/transactional-tests/tests/no-v1-comparison/index.exp index 44057d6e2212b5..eb1ba78aa4fa99 100644 --- a/third_party/move/move-compiler-v2/transactional-tests/tests/no-v1-comparison/index.exp +++ b/third_party/move/move-compiler-v2/transactional-tests/tests/no-v1-comparison/index.exp @@ -1 +1 @@ -processed 27 tasks +processed 29 tasks diff --git a/third_party/move/move-compiler-v2/transactional-tests/tests/no-v1-comparison/index.move b/third_party/move/move-compiler-v2/transactional-tests/tests/no-v1-comparison/index.move index 5c185c2a81a5ea..66093ecfc1e562 100644 --- a/third_party/move/move-compiler-v2/transactional-tests/tests/no-v1-comparison/index.move +++ b/third_party/move/move-compiler-v2/transactional-tests/tests/no-v1-comparison/index.move @@ -298,6 +298,10 @@ module 0x42::test { self.x += s.x; } + fun greater(self: &W, s: W): bool { + self.x > s.x + } + fun foo_1(account: address, w: W) acquires S { S[account].t.w.merge(w) } @@ -320,6 +324,7 @@ module 0x42::test { let w = W { x: 3 }; + assert!(!W[@0x1].greater(w), 0); foo_1(@0x1, w); assert!(S[@0x1].t.w.x == 5, 0); assert!(boo_1(vector[S[@0x1]], w) == 8, 1); @@ -328,6 +333,34 @@ module 0x42::test { boo_2(vector[W[@0x1]], w); } + struct Wrapper has drop, key, store, copy { + inner: T + } + + fun unwrap(self: &Wrapper): T { + self.inner + } + + fun dispatch(account: address): T acquires Wrapper { + Wrapper[account].unwrap() + } + + fun init_receiver_2(signer: &signer) { + let wrapper = Wrapper { + inner: 2 + }; + move_to(signer, wrapper); + } + + fun test_receiver_2() { + assert!(dispatch(@0x1) == 2, 0); + let wrapper = Wrapper { + inner: 2 + }; + let v = vector[wrapper]; + assert!(v[0].unwrap() == 2, 0); + } + } //# run --verbose --signers 0x1 -- 0x42::test::init @@ -381,3 +414,7 @@ module 0x42::test { //# run --verbose --signers 0x1 -- 0x42::test::init_receiver //# run --verbose -- 0x42::test::test_receiver + +//# run --verbose --signers 0x1 -- 0x42::test::init_receiver_2 + +//# run --verbose -- 0x42::test::test_receiver_2 diff --git a/third_party/move/move-model/src/builder/exp_builder.rs b/third_party/move/move-model/src/builder/exp_builder.rs index 8956ab5c64e4c1..9b6427c19a2609 100644 --- a/third_party/move/move-model/src/builder/exp_builder.rs +++ b/third_party/move/move-model/src/builder/exp_builder.rs @@ -4151,8 +4151,9 @@ impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'mo let receiver_call_opt = self.get_receiver_function(&arg_types[0], name); if let Some(receiver_call) = receiver_call_opt { if let Exp_::ExpDotted(dotted) = &args[0].value { - // special case for the receiver call S[x].f.fun(&mut...) - // making sure the first argument is mutable ref + // we need a special case for the receiver call S[x].f.fun(&mut...) + // when the first argument is a dotted expression with index notation: + // S[x].y because the reference type is by default set immutable ref if receiver_call.arg_types[0].is_mutable_reference() { let first_arg = self.translate_dotted( dotted, @@ -4163,8 +4164,8 @@ impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'mo translated_args[0] = first_arg.into_exp(); } } else if let Exp_::Index(target, index) = &args[0].value { - // special case for the receiver call S[x].fun(&...) - // S[x] will be translated into a reference + // special case for the receiver call S[x].fun(&...), S[x].fun(&mut...) + // so that it behaves the same as (&S[x]).fun(&...), (&mut S[x]).fun(&mut...) if receiver_call.arg_types[0].is_reference() { let index_mutate = receiver_call.arg_types[0].is_mutable_reference(); if let Some(first_arg) = self.try_resource_or_vector_index(