Skip to content

Commit

Permalink
treat _ as a wildcard, not a var, in function parameter in Move Lang …
Browse files Browse the repository at this point in the history
…V2+ (#14962)

In Move Language 2.0+, treat a function parameter named "_" as a wildcard value-sink, not a variable.
Fixes #14949.
  • Loading branch information
brmataptos authored Oct 18, 2024
1 parent 78b3331 commit 9bc37be
Show file tree
Hide file tree
Showing 22 changed files with 1,852 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use log::{debug, trace};
use move_model::{
ast::{Exp, ExpData, Operation, Pattern, Spec, SpecBlockTarget, TempIndex},
exp_rewriter::ExpRewriterFunctions,
metadata::LanguageVersion,
model::{FunId, GlobalEnv, Loc, NodeId, Parameter, QualifiedId},
symbol::Symbol,
ty::{ReferenceKind, Type},
Expand Down Expand Up @@ -833,7 +834,11 @@ impl<'env, 'rewriter> InlinedRewriter<'env, 'rewriter> {
.map(|param| {
let Parameter(sym, ty, loc) = *param;
let id = env.new_node(loc.clone(), ty.instantiate(self.type_args));
if let Some(new_sym) = self.shadow_stack.get_shadow_symbol(*sym, true) {
if env.language_version().is_at_least(LanguageVersion::V2_0)
&& env.symbol_pool().string(*sym).as_ref() == "_"
{
Pattern::Wildcard(id)
} else if let Some(new_sym) = self.shadow_stack.get_shadow_symbol(*sym, true) {
Pattern::Var(id, new_sym)
} else {
Pattern::Var(id, *sym)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Diagnostics:
error: undeclared `_`
┌─ tests/checking-lang-v1/underscore.move:38:9
38 │ _ // undefined
│ ^

error: duplicate declaration of `_`
┌─ tests/checking-lang-v1/underscore.move:41:30
41 │ public fun test8(_: u64, _: u64): u64 {
│ - ^
│ │
│ previous declaration of `_`

error: duplicate declaration of `_`
┌─ tests/checking-lang-v1/underscore.move:46:37
46 │ inline fun fun9(x: u64, _: u64, _: u64): u64 {
│ - ^
│ │
│ previous declaration of `_`

error: duplicate declaration of `_`
┌─ tests/checking-lang-v1/underscore.move:54:38
54 │ inline fun fun10(x: u64, _: u64, _: |u64|u64): u64 {
│ - ^
│ │
│ previous declaration of `_`

error: cannot pass `integer` to a function which expects argument of type `|u64|u64`
┌─ tests/checking-lang-v1/underscore.move:59:21
59 │ fun10(4, 3, 2)
│ ^
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//# publish
module 0xc0ffee::m {
fun test2(): (u32, u64) {
(1u32, 2u64)
}

public fun test(_: u64): u64 {
let x = _ + 3;
x + _
}

public fun test3(_: u64): u64 {
let (_, _) = test2();
let x = _ + 3;
_ = _ + 1;
x + _
}

public fun test4(_: u64): u64 {
let (_, _x) = test2();
_ = _ + 2;
_
}

public fun test5(_: u64): u64 {
let (_, _) = test2();
_ = _ + 3;
_
}

public fun test6(_: u64): u64 {
let (_x, _) = test2();
_
}

public fun test7(_y: u64): u64 {
let _ = _y;
_ // undefined
}

public fun test8(_: u64, _: u64): u64 {
let _ = _;
_
}

inline fun fun9(x: u64, _: u64, _: u64): u64 {
x + 3
}

public fun test9(): u64 {
fun9(4, 3, 2)
}

inline fun fun10(x: u64, _: u64, _: |u64|u64): u64 {
x + 3
}

public fun test10a(): u64 {
fun10(4, 3, 2)
}

public fun test10b(): u64 {
fun10(4, 3, |x|x + 1)
}

public fun test10c(): u64 {
fun10(4, 3, |_|1)
}

inline fun fun11(x: u64, _: u64, f: |u64|u64): u64 {
f(x)
}

public fun test11(): u64 {
fun11(4, 3, |_|1)
}
}

//# run 0xc0ffee::m::test --args 4

//# run 0xc0ffee::m::test3 --args 5

//# run 0xc0ffee::m::test4 --args 5

//# run 0xc0ffee::m::test5 --args 5

//# run 0xc0ffee::m::test6 --args 5

//# run 0xc0ffee::m::test7 --args 5

//# run 0xc0ffee::m::test8 --args 5

//# run 0xc0ffee::m::test9

//# run 0xc0ffee::m::test10a

//# run 0xc0ffee::m::test10b

//# run 0xc0ffee::m::test10c

//# run 0xc0ffee::m::test11
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

Diagnostics:
error: undeclared `_`
┌─ tests/checking/naming/underscore.move:8:17
8 │ let x = _ + 3;
│ ^

error: undeclared `_`
┌─ tests/checking/naming/underscore.move:9:13
9 │ x + _
│ ^

error: undeclared `_`
┌─ tests/checking/naming/underscore.move:14:17
14 │ let x = _ + 3;
│ ^

error: undeclared `_`
┌─ tests/checking/naming/underscore.move:15:13
15 │ _ = _ + 1;
│ ^

error: undeclared `_`
┌─ tests/checking/naming/underscore.move:16:13
16 │ x + _
│ ^

error: undeclared `_`
┌─ tests/checking/naming/underscore.move:21:13
21 │ _ = _ + 2;
│ ^

error: undeclared `_`
┌─ tests/checking/naming/underscore.move:22:9
22 │ _
│ ^

error: undeclared `_`
┌─ tests/checking/naming/underscore.move:27:13
27 │ _ = _ + 3;
│ ^

error: undeclared `_`
┌─ tests/checking/naming/underscore.move:28:9
28 │ _
│ ^

error: undeclared `_`
┌─ tests/checking/naming/underscore.move:33:10
33 │ _
│ ^

error: undeclared `_`
┌─ tests/checking/naming/underscore.move:38:9
38 │ _ // undefined
│ ^

error: undeclared `_`
┌─ tests/checking/naming/underscore.move:42:17
42 │ let _ = _;
│ ^

error: undeclared `_`
┌─ tests/checking/naming/underscore.move:43:9
43 │ _
│ ^

error: cannot pass `integer` to a function which expects argument of type `|u64|u64`
┌─ tests/checking/naming/underscore.move:59:21
59 │ fun10(4, 3, 2)
│ ^
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//# publish
module 0xc0ffee::m {
fun test2(): (u32, u64) {
(1u32, 2u64)
}

public fun test(_: u64): u64 {
let x = _ + 3;
x + _
}

public fun test3(_: u64): u64 {
let (_, _) = test2();
let x = _ + 3;
_ = _ + 1;
x + _
}

public fun test4(_: u64): u64 {
let (_, _x) = test2();
_ = _ + 2;
_
}

public fun test5(_: u64): u64 {
let (_, _) = test2();
_ = _ + 3;
_
}

public fun test6(_: u64): u64 {
let (_x, _) = test2();
_
}

public fun test7(_y: u64): u64 {
let _ = _y;
_ // undefined
}

public fun test8(_: u64, _: u64): u64 {
let _ = _;
_
}

inline fun fun9(x: u64, _: u64, _: u64): u64 {
x + 3
}

public fun test9(): u64 {
fun9(4, 3, 2)
}

inline fun fun10(x: u64, _: u64, _: |u64|u64): u64 {
x + 3
}

public fun test10a(): u64 {
fun10(4, 3, 2)
}

public fun test10b(): u64 {
fun10(4, 3, |x|x + 1)
}

public fun test10c(): u64 {
fun10(4, 3, |_|1)
}

inline fun fun11(x: u64, _: u64, f: |u64|u64): u64 {
f(x)
}

public fun test11(): u64 {
fun11(4, 3, |_|1)
}
}

//# run 0xc0ffee::m::test --args 4

//# run 0xc0ffee::m::test3 --args 5

//# run 0xc0ffee::m::test4 --args 5

//# run 0xc0ffee::m::test5 --args 5

//# run 0xc0ffee::m::test6 --args 5

//# run 0xc0ffee::m::test7 --args 5

//# run 0xc0ffee::m::test8 --args 5

//# run 0xc0ffee::m::test9

//# run 0xc0ffee::m::test10a

//# run 0xc0ffee::m::test10b

//# run 0xc0ffee::m::test10c

//# run 0xc0ffee::m::test11
Loading

0 comments on commit 9bc37be

Please sign in to comment.