checker: verify use of blank identifier (#6412)

pull/6424/head^2
Enzo 2020-09-18 23:47:50 +02:00 committed by GitHub
parent bc28801993
commit 3126ae305c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 21 deletions

View File

@ -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,29 +2831,26 @@ 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
}
} }
if ident.kind == .unresolved && ident.mod != 'builtin' { }
// search in the `builtin` idents, for example if ident.kind == .unresolved && ident.mod != 'builtin' {
// main.compare_f32 may actually be builtin.compare_f32 // search in the `builtin` idents, for example
saved_mod := ident.mod // main.compare_f32 may actually be builtin.compare_f32
ident.mod = 'builtin' saved_mod := ident.mod
builtin_type := c.ident(ident) ident.mod = 'builtin'
if builtin_type != table.void_type { builtin_type := c.ident(ident)
return builtin_type if builtin_type != table.void_type {
} return builtin_type
ident.mod = saved_mod
}
if ident.tok_kind == .assign {
c.error('undefined ident: `$ident.name` (use `:=` to declare a variable)',
ident.pos)
} else {
c.error('undefined ident: `$ident.name`', ident.pos)
} }
ident.mod = saved_mod
}
if ident.tok_kind == .assign {
c.error('undefined ident: `$ident.name` (use `:=` to declare a variable)', ident.pos)
} else {
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, '...')`

View File

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

View File

@ -0,0 +1,3 @@
fn main() {
_ := [_]
}