1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: working is none type check + no crash when checking with non types (#9793)

This commit is contained in:
Henrixounez 2021-04-18 15:28:39 +02:00 committed by GitHub
parent ee7bcfd05c
commit 4a1e2f9dcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1000,6 +1000,7 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) ast.Type {
ast.Type(0)
}
}
if typ != ast.Type(0) {
typ_sym := c.table.get_type_symbol(typ)
op := infix_expr.op.str()
if typ_sym.kind == .placeholder {
@ -1012,6 +1013,7 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) ast.Type {
c.error('`$left.name` has no variant `$right.name`', infix_expr.pos)
}
}
}
return ast.bool_type
}
.arrow { // `chan <- elem`
@ -5387,15 +5389,29 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
pos := branch.cond.position()
if branch.cond is ast.InfixExpr {
if branch.cond.op == .key_is {
right_expr := branch.cond.right as ast.TypeNode
right_expr := branch.cond.right
right_type := match right_expr {
ast.TypeNode {
right_expr.typ
}
ast.None {
ast.none_type_idx
}
else {
c.error('invalid type `$right_expr`', right_expr.position())
ast.Type(0)
}
}
if right_type != ast.Type(0) {
left_sym := c.table.get_type_symbol(branch.cond.left_type)
expr_type := c.expr(branch.cond.left)
if left_sym.kind == .interface_ {
c.type_implements(right_expr.typ, expr_type, pos)
} else if !c.check_types(right_expr.typ, expr_type) {
expect_str := c.table.type_to_str(right_expr.typ)
c.type_implements(right_type, expr_type, pos)
} else if !c.check_types(right_type, expr_type) {
expect_str := c.table.type_to_str(right_type)
expr_str := c.table.type_to_str(expr_type)
c.error('cannot use type `$expect_str` as type `$expr_str`', pos)
c.error('cannot use type `$expect_str` as type `$expr_str`',
pos)
}
if (branch.cond.left is ast.Ident || branch.cond.left is ast.SelectorExpr)
&& branch.cond.right is ast.TypeNode {
@ -5406,8 +5422,9 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
}
if is_variable {
if left_sym.kind in [.interface_, .sum_type] {
c.smartcast(branch.cond.left, branch.cond.left_type, right_expr.typ, mut
branch.scope)
c.smartcast(branch.cond.left, branch.cond.left_type,
right_type, mut branch.scope)
}
}
}
}