checker: check that right `is` type exists
parent
73073cd954
commit
f2be3d7ffb
|
@ -24,6 +24,7 @@ pub type ScopeObject = ConstField | GlobalDecl | Var
|
||||||
pub struct Type {
|
pub struct Type {
|
||||||
pub:
|
pub:
|
||||||
typ table.Type
|
typ table.Type
|
||||||
|
pos token.Position
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Block {
|
pub struct Block {
|
||||||
|
|
|
@ -198,6 +198,11 @@ pub fn (c mut Checker) infix_expr(infix_expr mut ast.InfixExpr) table.Type {
|
||||||
infix_expr.left_type = left_type
|
infix_expr.left_type = left_type
|
||||||
c.expected_type = left_type
|
c.expected_type = left_type
|
||||||
if infix_expr.op == .key_is {
|
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
|
return table.bool_type
|
||||||
}
|
}
|
||||||
right_type := c.expr(infix_expr.right)
|
right_type := c.expr(infix_expr.right)
|
||||||
|
|
|
@ -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| }
|
|
@ -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 !')
|
||||||
|
}
|
||||||
|
}
|
|
@ -551,8 +551,11 @@ pub fn (var p Parser) name_expr() ast.Expr {
|
||||||
var node := ast.Expr{}
|
var node := ast.Expr{}
|
||||||
if p.inside_is {
|
if p.inside_is {
|
||||||
p.inside_is = false
|
p.inside_is = false
|
||||||
|
// get type position before moving to next
|
||||||
|
type_pos := p.tok.position()
|
||||||
return ast.Type{
|
return ast.Type{
|
||||||
typ: p.parse_type()
|
typ: p.parse_type()
|
||||||
|
pos: type_pos
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
is_c := p.tok.lit == 'C'
|
is_c := p.tok.lit == 'C'
|
||||||
|
|
Loading…
Reference in New Issue