Skip to content

Commit

Permalink
handle index directly
Browse files Browse the repository at this point in the history
  • Loading branch information
rahxephon89 committed Nov 13, 2024
1 parent 399a51b commit 0657b4b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ module 0x42::m {
{
m::merge(Borrow(Mutable)(select m::T.w<T>(select m::S.t<S>(BorrowGlobal(Mutable)<S>(account)))), w)
}
private fun foo_2(account: address,w: W)
acquires W(*)
{
m::merge(BorrowGlobal(Mutable)<W>(account), w)
}
private fun foo_3(account: address,w: W)
acquires W(*)
{
m::merge(BorrowGlobal(Mutable)<W>(account), w)
}
private fun foo_greater(account: address,w: W): bool
acquires S(*)
{
Expand Down Expand Up @@ -58,13 +68,13 @@ module 0x42::m {

// -- Sourcified model before bytecode pipeline
module 0x42::m {
struct T has drop, store {
struct T has drop, store, key {
w: W,
}
struct S has drop, key {
t: T,
}
struct W has drop, store {
struct W has drop, store, key {
x: u64,
}
fun boo(v: vector<S>, w: W) {
Expand All @@ -89,6 +99,16 @@ module 0x42::m {
{
merge(&mut borrow_global_mut<S>(account).t.w, w)
}
fun foo_2(account: address, w: W)
acquires W
{
merge(borrow_global_mut<W>(account), w)
}
fun foo_3(account: address, w: W)
acquires W
{
merge(borrow_global_mut<W>(account), w)
}
fun foo_greater(account: address, w: W): bool
acquires S
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module 0x42::m {

struct S has key, drop { t: T }

struct T has store, drop {
struct T has key, store, drop {
w: W
}

struct W has store, drop {
struct W has key, store, drop {
x: u64
}

Expand All @@ -18,6 +18,14 @@ module 0x42::m {
S[account].t.w.merge(w)
}

fun foo_2(account: address, w: W) acquires W {
W[account].merge(w)
}

fun foo_3(account: address, w: W) acquires W {
borrow_global_mut<W>(account).merge(w)
}

fun boo(v: vector<S>, w: W) {
v[0].t.w.merge(w)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ module 0x42::test {

struct S has key, drop, copy { t: T }

struct T has store, drop, copy {
struct T has key, store, drop, copy {
w: W
}

struct W has store, drop, copy {
struct W has key, store, drop, copy {
x: u64
}

Expand All @@ -290,6 +290,7 @@ module 0x42::test {
let s = S {
t
};
move_to(signer, w);
move_to(signer, s);
}

Expand All @@ -301,18 +302,30 @@ module 0x42::test {
S[account].t.w.merge(w)
}

fun foo_2(account: address, w: W) acquires W {
W[account].merge(w)
}

fun boo_1(v: vector<S>, w: W): u64 {
v[0].t.w.merge(w);
v[0].t.w.x
}

fun boo_2(v: vector<W>, w: W) {
v[0].merge(w);
assert!(v[0].x == 8, 0);
}

fun test_receiver() {
let w = W {
x: 3
};
foo_1(@0x1, w);
assert!(S[@0x1].t.w.x == 5, 0);
assert!(boo_1(vector[S[@0x1]], w) == 8, 1);
foo_2(@0x1, w);
assert!(W[@0x1].x == 5, 0);
boo_2(vector[W[@0x1]], w);
}

}
Expand Down
51 changes: 37 additions & 14 deletions third_party/move/move-model/src/builder/exp_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4136,7 +4136,7 @@ impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'mo
context: &ErrorMessageContext,
) -> ExpData {
// Translate arguments.
let (arg_types, mut translated_args) = self.translate_exp_list(args);
let (mut arg_types, mut translated_args) = self.translate_exp_list(args);

// Special handling of receiver call functions
if kind == CallKind::Receiver {
Expand All @@ -4149,19 +4149,42 @@ impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'mo
"receiver call needs to have at least one parameter"
);
let receiver_call_opt = self.get_receiver_function(&arg_types[0], name);
if let (Some(receiver_call), Exp_::ExpDotted(dotted)) =
(receiver_call_opt, &args[0].value)
{
// special case for the receiver call S[x].f.fun(&mut...)
// making sure the first argument is mutable ref
if receiver_call.arg_types[0].is_mutable_reference() {
let first_arg = self.translate_dotted(
dotted,
&arg_types[0],
true,
&ErrorMessageContext::General,
);
translated_args[0] = first_arg.into_exp();
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
if receiver_call.arg_types[0].is_mutable_reference() {
let first_arg = self.translate_dotted(
dotted,
&arg_types[0],
true,
&ErrorMessageContext::General,
);
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
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(
loc,
target,
index,
&ErrorMessageContext::General,
&Type::Reference(
ReferenceKind::from_is_mut(index_mutate),
Box::new(arg_types[0].clone()),
),
index_mutate,
) {
translated_args[0] = first_arg.into_exp();
arg_types[0] = Type::Reference(
ReferenceKind::from_is_mut(index_mutate),
Box::new(arg_types[0].clone()),
);
}
}
}
}
return self.translate_receiver_call(
Expand Down

0 comments on commit 0657b4b

Please sign in to comment.