checker: check that right `is` type exists

pull/4480/head
Enzo Baldisserri 2020-04-18 00:19:33 +02:00 committed by GitHub
parent 73073cd954
commit f2be3d7ffb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 0 deletions

View File

@ -24,6 +24,7 @@ pub type ScopeObject = ConstField | GlobalDecl | Var
pub struct Type {
pub:
typ table.Type
pos token.Position
}
pub struct Block {

View File

@ -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)

View File

@ -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| }

View File

@ -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 !')
}
}

View File

@ -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'