Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Compiler-v2] fix receiver call with index notation #15239

Merged
merged 5 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// -- Model dump before bytecode pipeline
module 0x42::m {
struct T {
w: W,
}
struct S {
t: T,
}
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)
}
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)
}
private fun boo_greater(v: vector<S>,w: W): bool {
m::greater(Borrow(Immutable)(select m::T.w<T>(select m::S.t<S>(vector::borrow<S>(Borrow(Immutable)(v), 0)))), w)
}
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(*)
{
m::merge(Borrow(Mutable)(select m::T.w<T>(select m::S.t<S>(BorrowGlobal(Mutable)<S>(account)))), w)
}
private fun foo_(account: address,w: W)
acquires S(*)
{
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(*)
{
m::greater(Borrow(Immutable)(select m::T.w<T>(select m::S.t<S>(BorrowGlobal(Immutable)<S>(account)))), w)
}
private fun foo_greater_(account: address,w: W): bool
acquires S(*)
{
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))
}
private fun merge(self: &mut W,s: W) {
{
let $t2: u64 = select m::W.x<W>(s);
{
let $t1: &mut u64 = Borrow(Mutable)(select m::W.x<&mut W>(self));
$t1 = Add<u64>(Deref($t1), $t2)
}
};
Tuple()
}
private fun unwrap<T>(self: &Wrapper<T>): T {
select m::Wrapper.inner<&Wrapper<T>>(self)
}
} // end 0x42::m

// -- Sourcified model before bytecode pipeline
module 0x42::m {
struct T has drop, store, key {
w: W,
}
struct S has drop, key {
t: T,
}
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)
}
fun boo_(v: vector<S>, w: W) {
merge(&mut 0x1::vector::borrow_mut<S>(&mut v, 0).t.w, w)
}
fun boo_greater(v: vector<S>, w: W): bool {
greater(&0x1::vector::borrow<S>(&v, 0).t.w, w)
}
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
{
merge(&mut borrow_global_mut<S>(account).t.w, w)
}
fun foo_(account: address, w: W)
acquires S
{
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
{
greater(&borrow_global<S>(account).t.w, w)
}
fun foo_greater_(account: address, w: W): bool
acquires S
{
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
}
fun merge(self: &mut W, s: W) {
{
let $t2 = s.x;
let $t1 = &mut self.x;
*$t1 = *$t1 + $t2
};
}
fun unwrap<T: copy>(self: &Wrapper<T>): T {
self.inner
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
module 0x42::m {

struct S has key, drop { t: T }

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

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

fun merge(self: &mut W, s: W) {
self.x += s.x;
}

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add the following test case (or similar) that tests:

  1. Non-mut ref receiver over resource indexing
  2. Resource indexing on a generic type that goes through the newly added translation.
    struct Wrapper<T> has drop, key, store, copy {
        inner: T
    }

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

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

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)
}

fun foo_(account: address, w: W) acquires S {
(&mut S[account].t.w).merge(w)
}

fun boo_(v: vector<S>, w: W) {
(&mut v[0].t.w).merge(w)
}

fun greater(self: &W, s: W): bool {
self.x > s.x
}

fun foo_greater(account: address, w: W): bool acquires S {
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)
}

fun boo_greater_(v: vector<S>, w: W): bool {
(&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
@@ -0,0 +1,19 @@

Diagnostics:
error: undeclared receiver function `merge_non_receiver` for type `W`
┌─ tests/checking/receiver/calls_index_errors.move:22:9
22 │ S[account].t.w.merge_non_receiver(w)
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: the function takes `&mut` but `&` was provided
┌─ tests/checking/receiver/calls_index_errors.move:26:9
26 │ (&S[account].t.w).merge(w)
│ ^^^^^^^^^^^^^^^^^

error: the function takes `&mut` but `&` was provided
┌─ tests/checking/receiver/calls_index_errors.move:30:9
30 │ (&v[0].t.w).merge(w)
│ ^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module 0x42::m {

struct S has key, drop { t: T }

struct T has store, drop {
w: W
}

struct W has store, drop {
x: u64
}

fun merge(self: &mut W, s: W) {
self.x += s.x;
}

fun merge_non_receiver(self1: &mut W, s: W) {
self1.x += s.x;
}

fun foo_non_receiver(account: address, w: W) acquires S {
S[account].t.w.merge_non_receiver(w)
}

fun foo_(account: address, w: W) acquires S {
(&S[account].t.w).merge(w)
}

fun boo_(v: vector<S>, w: W) {
(&v[0].t.w).merge(w)
}

}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
processed 25 tasks
processed 29 tasks
Loading
Loading