1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: verify use of blank identifier (#6412)

This commit is contained in:
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
}
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
}
// second use
@ -2828,29 +2831,26 @@ pub fn (mut c Checker) ident(mut ident ast.Ident) table.Type {
if ident.language == .c {
return table.int_type
}
if ident.name != '_' {
if c.inside_sql {
if field := c.table.struct_find_field(c.cur_orm_ts, ident.name) {
return field.typ
}
if c.inside_sql {
if field := c.table.struct_find_field(c.cur_orm_ts, ident.name) {
return field.typ
}
if ident.kind == .unresolved && ident.mod != 'builtin' {
// search in the `builtin` idents, for example
// main.compare_f32 may actually be builtin.compare_f32
saved_mod := ident.mod
ident.mod = 'builtin'
builtin_type := c.ident(ident)
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)
}
if ident.kind == .unresolved && ident.mod != 'builtin' {
// search in the `builtin` idents, for example
// main.compare_f32 may actually be builtin.compare_f32
saved_mod := ident.mod
ident.mod = 'builtin'
builtin_type := c.ident(ident)
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)
}
if c.table.known_type(ident.name) {
// 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() {
_ := [_]
}