checker: check division by zero

pull/4631/head
yuyi 2020-04-28 15:52:01 +08:00 committed by GitHub
parent ef4f4825ac
commit f2060d431e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 0 deletions

View File

@ -355,6 +355,10 @@ pub fn (mut c Checker) infix_expr(infix_expr mut ast.InfixExpr) table.Type {
return table.bool_type return table.bool_type
} }
.plus, .minus, .mul, .div { .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()) { 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()) 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()) { } else if right.kind in [.array, .array_fixed, .map, .struct_] && !right.has_method(infix_expr.op.str()) {

View File

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

View File

@ -0,0 +1,3 @@
fn main() {
println(1/0)
}