checker: check __global type (#9804)

pull/9855/head
Lukas Neubert 2021-04-23 11:28:08 +02:00 committed by GitHub
parent 8e455495b2
commit aa40dfc1de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 0 deletions

View File

@ -519,6 +519,7 @@ pub:
expr Expr
has_expr bool
pos token.Position
typ_pos token.Position
pub mut:
typ Type
comments []Comment

View File

@ -3834,6 +3834,10 @@ fn (mut c Checker) global_decl(node ast.GlobalDecl) {
if field.name in c.global_names {
c.error('duplicate global `$field.name`', field.pos)
}
sym := c.table.get_type_symbol(field.typ)
if sym.kind == .placeholder {
c.error('unknown type `$sym.name`', field.typ_pos)
}
c.global_names << field.name
}
}

View File

@ -0,0 +1,11 @@
vlib/v/checker/tests/globals/unknown_typ.vv:1:12: error: unknown type `foo`
1 | __global x foo
| ~~~
2 | __global (
3 | y = float(5.0)
vlib/v/checker/tests/globals/unknown_typ.vv:3:6: error: unknown type `float`
1 | __global x foo
2 | __global (
3 | y = float(5.0)
| ~~~~~
4 | )

View File

@ -0,0 +1,4 @@
__global x foo
__global (
y = float(5.0)
)

View File

@ -2846,6 +2846,7 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
if has_expr {
p.next() // =
}
typ_pos := p.tok.position()
typ := p.parse_type()
if p.tok.kind == .assign {
p.error('global assign must have the type around the value, use `name = type(value)`')
@ -2866,6 +2867,7 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
has_expr: has_expr
expr: expr
pos: pos
typ_pos: typ_pos
typ: typ
comments: comments
}