checker: check devision by zero (int and float)

pull/4644/head
yuyi 2020-04-29 16:07:27 +08:00 committed by GitHub
parent 2b48ce21df
commit 117649f40d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 22 additions and 7 deletions

View File

@ -366,8 +366,9 @@ 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 && if infix_expr.op == .div &&
infix_expr.right.str().int() == 0 { (infix_expr.right is ast.IntegerLiteral && infix_expr.right.str() == '0' ||
infix_expr.right is ast.FloatLiteral && infix_expr.right.str().f64() == 0.0) {
c.error('division by zero', infix_expr.right.position()) 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()) {

View File

@ -1,5 +0,0 @@
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,5 @@
vlib/v/checker/tests/division_by_zero_float_err.v:2:14: error: division by zero
1| fn main() {
2| println(1.0/0.0)
~~~
3| }

View File

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

View File

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

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/division_by_zero_int_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)
}