From f2be3d7ffb3476b5c2e5233e5b2eee9e60028cc4 Mon Sep 17 00:00:00 2001 From: Enzo Baldisserri Date: Sat, 18 Apr 2020 00:19:33 +0200 Subject: [PATCH] checker: check that right `is` type exists --- vlib/v/ast/ast.v | 1 + vlib/v/checker/checker.v | 5 +++++ vlib/v/checker/tests/inout/is_type_not_exist.out | 7 +++++++ vlib/v/checker/tests/inout/is_type_not_exist.vv | 11 +++++++++++ vlib/v/parser/parser.v | 3 +++ 5 files changed, 27 insertions(+) create mode 100644 vlib/v/checker/tests/inout/is_type_not_exist.out create mode 100644 vlib/v/checker/tests/inout/is_type_not_exist.vv diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 1e8abcd80e..2c1c027167 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -24,6 +24,7 @@ pub type ScopeObject = ConstField | GlobalDecl | Var pub struct Type { pub: typ table.Type + pos token.Position } pub struct Block { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 111a2305cc..3c14a7a0dc 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -198,6 +198,11 @@ pub fn (c mut Checker) infix_expr(infix_expr mut ast.InfixExpr) table.Type { infix_expr.left_type = left_type c.expected_type = left_type if infix_expr.op == .key_is { + type_expr := infix_expr.right as ast.Type + typ_sym := c.table.get_type_symbol(type_expr.typ) + if typ_sym.kind == .placeholder { + c.error('is: type `${typ_sym.name}` does not exist', type_expr.pos) + } return table.bool_type } right_type := c.expr(infix_expr.right) diff --git a/vlib/v/checker/tests/inout/is_type_not_exist.out b/vlib/v/checker/tests/inout/is_type_not_exist.out new file mode 100644 index 0000000000..b24533cbe4 --- /dev/null +++ b/vlib/v/checker/tests/inout/is_type_not_exist.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/inout/is_type_not_exist.v:8:10: error: is: type `SomethingThatDontExist` does not exist + 6| + 7| fn fn_with_sum_type_param(i Integer) { + 8| if i is SomethingThatDontExist { + ~~~~~~~~~~~~~~~~~~~~~~ + 9| println('It should fail !') + 10| } diff --git a/vlib/v/checker/tests/inout/is_type_not_exist.vv b/vlib/v/checker/tests/inout/is_type_not_exist.vv new file mode 100644 index 0000000000..e50859b348 --- /dev/null +++ b/vlib/v/checker/tests/inout/is_type_not_exist.vv @@ -0,0 +1,11 @@ +type Integer = i8 | i16 | int | i64 + +fn main() { + fn_with_sum_type_param(1) +} + +fn fn_with_sum_type_param(i Integer) { + if i is SomethingThatDontExist { + println('It should fail !') + } +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 50a4c2fdb5..76adc9a702 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -551,8 +551,11 @@ pub fn (var p Parser) name_expr() ast.Expr { var node := ast.Expr{} if p.inside_is { p.inside_is = false + // get type position before moving to next + type_pos := p.tok.position() return ast.Type{ typ: p.parse_type() + pos: type_pos } } is_c := p.tok.lit == 'C'