mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: check that right is
type exists
This commit is contained in:
parent
73073cd954
commit
f2be3d7ffb
@ -24,6 +24,7 @@ pub type ScopeObject = ConstField | GlobalDecl | Var
|
||||
pub struct Type {
|
||||
pub:
|
||||
typ table.Type
|
||||
pos token.Position
|
||||
}
|
||||
|
||||
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
|
||||
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)
|
||||
|
7
vlib/v/checker/tests/inout/is_type_not_exist.out
Normal file
7
vlib/v/checker/tests/inout/is_type_not_exist.out
Normal 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| }
|
11
vlib/v/checker/tests/inout/is_type_not_exist.vv
Normal file
11
vlib/v/checker/tests/inout/is_type_not_exist.vv
Normal 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 !')
|
||||
}
|
||||
}
|
@ -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'
|
||||
|
Loading…
Reference in New Issue
Block a user