checker: verify use of blank identifier (#6412)
parent
bc28801993
commit
3126ae305c
|
@ -2718,6 +2718,9 @@ pub fn (mut c Checker) ident(mut ident ast.Ident) table.Type {
|
||||||
c.const_deps << name
|
c.const_deps << name
|
||||||
}
|
}
|
||||||
if ident.kind == .blank_ident {
|
if ident.kind == .blank_ident {
|
||||||
|
if ident.tok_kind !in [.assign, .decl_assign] {
|
||||||
|
c.error('undefined ident: `_` (may only be used in assignments)', ident.pos)
|
||||||
|
}
|
||||||
return table.void_type
|
return table.void_type
|
||||||
}
|
}
|
||||||
// second use
|
// second use
|
||||||
|
@ -2828,7 +2831,6 @@ pub fn (mut c Checker) ident(mut ident ast.Ident) table.Type {
|
||||||
if ident.language == .c {
|
if ident.language == .c {
|
||||||
return table.int_type
|
return table.int_type
|
||||||
}
|
}
|
||||||
if ident.name != '_' {
|
|
||||||
if c.inside_sql {
|
if c.inside_sql {
|
||||||
if field := c.table.struct_find_field(c.cur_orm_ts, ident.name) {
|
if field := c.table.struct_find_field(c.cur_orm_ts, ident.name) {
|
||||||
return field.typ
|
return field.typ
|
||||||
|
@ -2846,12 +2848,10 @@ pub fn (mut c Checker) ident(mut ident ast.Ident) table.Type {
|
||||||
ident.mod = saved_mod
|
ident.mod = saved_mod
|
||||||
}
|
}
|
||||||
if ident.tok_kind == .assign {
|
if ident.tok_kind == .assign {
|
||||||
c.error('undefined ident: `$ident.name` (use `:=` to declare a variable)',
|
c.error('undefined ident: `$ident.name` (use `:=` to declare a variable)', ident.pos)
|
||||||
ident.pos)
|
|
||||||
} else {
|
} else {
|
||||||
c.error('undefined ident: `$ident.name`', ident.pos)
|
c.error('undefined ident: `$ident.name`', ident.pos)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if c.table.known_type(ident.name) {
|
if c.table.known_type(ident.name) {
|
||||||
// e.g. `User` in `json.decode(User, '...')`
|
// e.g. `User` in `json.decode(User, '...')`
|
||||||
return table.void_type
|
return table.void_type
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
vlib/v/checker/tests/blank_ident_invalid_use.vv:2:8: error: undefined ident: `_` (may only be used in assignments)
|
||||||
|
1 | fn main() {
|
||||||
|
2 | _ := [_]
|
||||||
|
| ^
|
||||||
|
3 | }
|
|
@ -0,0 +1,3 @@
|
||||||
|
fn main() {
|
||||||
|
_ := [_]
|
||||||
|
}
|
Loading…
Reference in New Issue