mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: check division by zero
This commit is contained in:
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()) {
|
||||
|
5
vlib/v/checker/tests/division_by_zero_err.out
Normal file
5
vlib/v/checker/tests/division_by_zero_err.out
Normal 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| }
|
3
vlib/v/checker/tests/division_by_zero_err.vv
Normal file
3
vlib/v/checker/tests/division_by_zero_err.vv
Normal file
@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println(1/0)
|
||||
}
|
Loading…
Reference in New Issue
Block a user