parser: check closure var name conflict (#14823)

master
yuyi 2022-06-22 14:04:15 +08:00 committed by GitHub
parent e2e3992e0d
commit c64c4907a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 0 deletions

View File

@ -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

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

View File

@ -0,0 +1,10 @@
fn main() {
x := 1
y := fn [x] (x int) {
println(x)
}
y(x)
y(2)
}