Skip to content

Commit aa40dfc

Browse files
author
Lukas Neubert
authored
checker: check __global type (#9804)
1 parent 8e45549 commit aa40dfc

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

vlib/v/ast/ast.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ pub:
519519
expr Expr
520520
has_expr bool
521521
pos token.Position
522+
typ_pos token.Position
522523
pub mut:
523524
typ Type
524525
comments []Comment

vlib/v/checker/checker.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3834,6 +3834,10 @@ fn (mut c Checker) global_decl(node ast.GlobalDecl) {
38343834
if field.name in c.global_names {
38353835
c.error('duplicate global `$field.name`', field.pos)
38363836
}
3837+
sym := c.table.get_type_symbol(field.typ)
3838+
if sym.kind == .placeholder {
3839+
c.error('unknown type `$sym.name`', field.typ_pos)
3840+
}
38373841
c.global_names << field.name
38383842
}
38393843
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
vlib/v/checker/tests/globals/unknown_typ.vv:1:12: error: unknown type `foo`
2+
1 | __global x foo
3+
| ~~~
4+
2 | __global (
5+
3 | y = float(5.0)
6+
vlib/v/checker/tests/globals/unknown_typ.vv:3:6: error: unknown type `float`
7+
1 | __global x foo
8+
2 | __global (
9+
3 | y = float(5.0)
10+
| ~~~~~
11+
4 | )
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
__global x foo
2+
__global (
3+
y = float(5.0)
4+
)

vlib/v/parser/parser.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,6 +2846,7 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
28462846
if has_expr {
28472847
p.next() // =
28482848
}
2849+
typ_pos := p.tok.position()
28492850
typ := p.parse_type()
28502851
if p.tok.kind == .assign {
28512852
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 {
28662867
has_expr: has_expr
28672868
expr: expr
28682869
pos: pos
2870+
typ_pos: typ_pos
28692871
typ: typ
28702872
comments: comments
28712873
}

0 commit comments

Comments
 (0)