checker: check division by zero
parent
ef4f4825ac
commit
f2060d431e
|
@ -355,6 +355,10 @@ pub fn (mut c Checker) infix_expr(infix_expr mut ast.InfixExpr) table.Type {
|
|||
return table.bool_type
|
||||
}
|
||||
.plus, .minus, .mul, .div {
|
||||
if infix_expr.op == .div && infix_expr.right is ast.IntegerLiteral &&
|
||||
infix_expr.right.str().int() == 0 {
|
||||
c.error('division by zero', infix_expr.right.position())
|
||||
}
|
||||
if left.kind in [.array, .array_fixed, .map, .struct_] && !left.has_method(infix_expr.op.str()) {
|
||||
c.error('mismatched types `$left.name` and `$right.name`', infix_expr.left.position())
|
||||
} else if right.kind in [.array, .array_fixed, .map, .struct_] && !right.has_method(infix_expr.op.str()) {
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
vlib/v/checker/tests/division_by_zero_err.v:2:12: error: division by zero
|
||||
1| fn main() {
|
||||
2| println(1/0)
|
||||
^
|
||||
3| }
|
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
println(1/0)
|
||||
}
|
Loading…
Reference in New Issue