From aa40dfc1de070f83926a99631feec347592f34f2 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Fri, 23 Apr 2021 11:28:08 +0200 Subject: [PATCH] checker: check __global type (#9804) --- vlib/v/ast/ast.v | 1 + vlib/v/checker/checker.v | 4 ++++ vlib/v/checker/tests/globals/unknown_typ.out | 11 +++++++++++ vlib/v/checker/tests/globals/unknown_typ.vv | 4 ++++ vlib/v/parser/parser.v | 2 ++ 5 files changed, 22 insertions(+) create mode 100644 vlib/v/checker/tests/globals/unknown_typ.out create mode 100644 vlib/v/checker/tests/globals/unknown_typ.vv diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 6ad55ce105..801484919b 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -519,6 +519,7 @@ pub: expr Expr has_expr bool pos token.Position + typ_pos token.Position pub mut: typ Type comments []Comment diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index e5c225831b..db0d9742d5 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 } } diff --git a/vlib/v/checker/tests/globals/unknown_typ.out b/vlib/v/checker/tests/globals/unknown_typ.out new file mode 100644 index 0000000000..91d082dd6b --- /dev/null +++ b/vlib/v/checker/tests/globals/unknown_typ.out @@ -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 | ) diff --git a/vlib/v/checker/tests/globals/unknown_typ.vv b/vlib/v/checker/tests/globals/unknown_typ.vv new file mode 100644 index 0000000000..9d7fdd1dda --- /dev/null +++ b/vlib/v/checker/tests/globals/unknown_typ.vv @@ -0,0 +1,4 @@ +__global x foo +__global ( + y = float(5.0) +) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index e2285da6b8..130febd6a3 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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 }