mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parser: check closure var name conflict (#14823)
This commit is contained in:
parent
e2e3992e0d
commit
c64c4907a2
@ -751,6 +751,17 @@ fn (mut p Parser) anon_fn() ast.AnonFn {
|
||||
typ := ast.new_type(idx)
|
||||
p.inside_defer = old_inside_defer
|
||||
// name := p.table.get_type_name(typ)
|
||||
if inherited_vars.len > 0 && args.len > 0 {
|
||||
for arg in args {
|
||||
for var in inherited_vars {
|
||||
if arg.name == var.name {
|
||||
p.error_with_pos('the parameter name `$arg.name` conflicts with the captured value name',
|
||||
arg.pos)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ast.AnonFn{
|
||||
decl: ast.FnDecl{
|
||||
name: name
|
||||
|
7
vlib/v/parser/tests/closure_var_name_conflict.out
Normal file
7
vlib/v/parser/tests/closure_var_name_conflict.out
Normal file
@ -0,0 +1,7 @@
|
||||
vlib/v/parser/tests/closure_var_name_conflict.vv:4:15: error: the parameter name `x` conflicts with the captured value name
|
||||
2 | x := 1
|
||||
3 |
|
||||
4 | y := fn [x] (x int) {
|
||||
| ^
|
||||
5 | println(x)
|
||||
6 | }
|
10
vlib/v/parser/tests/closure_var_name_conflict.vv
Normal file
10
vlib/v/parser/tests/closure_var_name_conflict.vv
Normal file
@ -0,0 +1,10 @@
|
||||
fn main() {
|
||||
x := 1
|
||||
|
||||
y := fn [x] (x int) {
|
||||
println(x)
|
||||
}
|
||||
|
||||
y(x)
|
||||
y(2)
|
||||
}
|
Loading…
Reference in New Issue
Block a user