aptos-core
aptos-core copied to clipboard
[Bug] Differing behaviors based on use statement regarding function values
🐛 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.
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
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
}
}
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.