Skip to content

Commit

Permalink
add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
rahxephon89 committed Nov 13, 2024
1 parent 0657b4b commit 5ad7598
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module 0x42::m {
struct W {
x: u64,
}
struct Wrapper<T> {
inner: T,
}
private fun boo(v: vector<S>,w: W) {
m::merge(Borrow(Mutable)(select m::T.w<T>(select m::S.t<S>(vector::borrow_mut<S>(Borrow(Mutable)(v), 0)))), w)
}
Expand All @@ -21,6 +24,17 @@ module 0x42::m {
private fun boo_greater_(v: vector<S>,w: W): bool {
m::greater(Freeze(false)(Borrow(Mutable)(select m::T.w<T>(select m::S.t<S>(vector::borrow_mut<S>(Borrow(Mutable)(v), 0))))), w)
}
private fun boo_greater_2(v: vector<W>,w: W): bool {
m::greater(vector::borrow<W>(Borrow(Immutable)(v), 0), w)
}
private fun boo_greater_2_(v: vector<W>,w: W): bool {
m::greater(vector::borrow<W>(Borrow(Immutable)(v), 0), w)
}
private fun dispatch<T>(account: address): T
acquires Wrapper(*)
{
m::unwrap<T>(BorrowGlobal(Immutable)<Wrapper<T>>(account))
}
private fun foo(account: address,w: W)
acquires S(*)
{
Expand Down Expand Up @@ -51,6 +65,11 @@ module 0x42::m {
{
m::greater(Freeze(false)(Borrow(Mutable)(select m::T.w<T>(select m::S.t<S>(BorrowGlobal(Mutable)<S>(account))))), w)
}
private fun foo_greater_2(account: address,w: W): bool
acquires S(*)
{
m::greater(BorrowGlobal(Immutable)<W>(account), w)
}
private fun greater(self: &W,s: W): bool {
Gt<u64>(select m::W.x<&W>(self), select m::W.x<W>(s))
}
Expand All @@ -64,6 +83,9 @@ module 0x42::m {
};
Tuple()
}
private fun unwrap<T>(self: &Wrapper<T>): T {
select m::Wrapper.inner<&Wrapper<T>>(self)
}
} // end 0x42::m

// -- Sourcified model before bytecode pipeline
Expand All @@ -77,6 +99,9 @@ module 0x42::m {
struct W has drop, store, key {
x: u64,
}
struct Wrapper<T: copy> has copy, drop, store, key {
inner: T,
}
fun boo(v: vector<S>, w: W) {
merge(&mut 0x1::vector::borrow_mut<S>(&mut v, 0).t.w, w)
}
Expand All @@ -89,6 +114,17 @@ module 0x42::m {
fun boo_greater_(v: vector<S>, w: W): bool {
greater(/*freeze*/&mut 0x1::vector::borrow_mut<S>(&mut v, 0).t.w, w)
}
fun boo_greater_2(v: vector<W>, w: W): bool {
greater(0x1::vector::borrow<W>(&v, 0), w)
}
fun boo_greater_2_(v: vector<W>, w: W): bool {
greater(0x1::vector::borrow<W>(&v, 0), w)
}
fun dispatch<T: copy + store>(account: address): T
acquires Wrapper
{
unwrap<T>(borrow_global<Wrapper<T>>(account))
}
fun foo(account: address, w: W)
acquires S
{
Expand Down Expand Up @@ -119,6 +155,11 @@ module 0x42::m {
{
greater(/*freeze*/&mut borrow_global_mut<S>(account).t.w, w)
}
fun foo_greater_2(account: address, w: W): bool
acquires S
{
greater(borrow_global<W>(account), w)
}
fun greater(self: &W, s: W): bool {
self.x > s.x
}
Expand All @@ -129,4 +170,7 @@ module 0x42::m {
*$t1 = *$t1 + $t2
};
}
fun unwrap<T: copy>(self: &Wrapper<T>): T {
self.inner
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>, w: W): bool {
v[0].t.w.greater(w)
}

fun boo_greater_2(v: vector<W>, w: W): bool {
v[0].greater(w)
}

fun foo_greater_(account: address, w: W): bool acquires S {
(&mut S[account].t.w).greater(w)
}
Expand All @@ -58,4 +66,20 @@ module 0x42::m {
(&mut v[0].t.w).greater(w)
}

fun boo_greater_2_(v: vector<W>, w: W): bool {
(&v[0]).greater(w)
}

struct Wrapper<T: copy> has drop, key, store, copy {
inner: T
}

fun unwrap<T: copy>(self: &Wrapper<T>): T {
self.inner
}

fun dispatch<T: store + copy>(account: address): T acquires Wrapper {
Wrapper<T>[account].unwrap()
}

}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
processed 27 tasks
processed 29 tasks
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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);
Expand All @@ -328,6 +333,34 @@ module 0x42::test {
boo_2(vector[W[@0x1]], w);
}

struct Wrapper<T: copy> has drop, key, store, copy {
inner: T
}

fun unwrap<T: copy>(self: &Wrapper<T>): T {
self.inner
}

fun dispatch<T: store + copy>(account: address): T acquires Wrapper {
Wrapper<T>[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
Expand Down Expand Up @@ -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
9 changes: 5 additions & 4 deletions third_party/move/move-model/src/builder/exp_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down

0 comments on commit 5ad7598

Please sign in to comment.