checker: check devision by zero (int and float)
parent
2b48ce21df
commit
117649f40d
|
@ -366,8 +366,9 @@ 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 {
|
||||
if infix_expr.op == .div &&
|
||||
(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())
|
||||
}
|
||||
if left.kind in [.array, .array_fixed, .map, .struct_] && !left.has_method(infix_expr.op.str()) {
|
||||
|
|
|
@ -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| }
|
|
@ -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| }
|
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
println(1.0/0.0)
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
println(1.0/0.0)
|
||||
}
|
|
@ -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| }
|
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
println(1/0)
|
||||
}
|
Loading…
Reference in New Issue