aptos-core icon indicating copy to clipboard operation
aptos-core copied to clipboard

[Bug] Differing behaviors based on use statement regarding function values

Open vineethk opened this issue 6 months ago • 2 comments
trafficstars

🐛 Bug

Consider the following transactional test:

//# publish
module 0xc0ffee::m {
    public fun foo() {}
}

//# publish
module 0xc0ffee::n_ok {
    fun main() {
        use 0xc0ffee::m;
        let f = m::foo;
        f();
    }
}


//# publish
module 0xc0ffee::n_should_also_be_ok {
    fun main() {
        use 0xc0ffee::m::foo;
        let f = foo;
        f();
    }
}

The first two modules compile successfully, but the last one fails with:

Error: compilation errors:
 warning: unused alias
   ┌─ TEMPFILE2:19:26
   │
19 │         use 0xc0ffee::m::foo;
   │                          ^^^ Unused 'use' of alias 'foo'. Consider removing it

error: undeclared `foo`
   ┌─ TEMPFILE2:20:17
   │
20 │         let f = foo;
   │                 ^^^

As far as the user is concerned, there is no difference between the modules n_ok and n_should_also_be_ok.

vineethk avatar May 12 '25 15:05 vineethk

When handling let f = foo;, since foo is a simple name, compiler treats foo as a term without trying to find its full name. However, to properly fix it, we need to add the information of local names in the expansion phase, which is not currently available. Related to #16495

rahxephon89 avatar May 16 '25 22:05 rahxephon89

This is another example from @zzjas:

//# publish
module 0xAA00::M0 {
    public fun f1() {}
}

//# publish
module 0xAA00::M1 {
    use 0xAA00::M0::f1;

    public fun f2() {
        // (f1)(); // Doesn't compile
        (0xAA00::M0::f1)(); // Compiles fine
    }
}

rahxephon89 avatar May 23 '25 00:05 rahxephon89

This issue is stale because it has been open 45 days with no activity. Remove the stale label or comment - otherwise this will be closed in 15 days.

github-actions[bot] avatar Jul 07 '25 02:07 github-actions[bot]