mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: add more checks for modulo
This commit is contained in:
parent
9b6ee8e77d
commit
37cf46d67a
@ -448,12 +448,16 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type {
|
||||
c.error('mismatched types `$left.name` and `$right.name`', infix_expr.right.position())
|
||||
} else if !left.is_int() && right.is_int() {
|
||||
c.error('mismatched types `$left.name` and `$right.name`', infix_expr.left.position())
|
||||
} else if left.kind == .f32 && right.kind == .f32 || left.kind == .f64 && right.kind == .f64 {
|
||||
c.error('float modulo not allowed, use math.fmod() instead', infix_expr.left.position())
|
||||
} else if left.kind in [.f32, .f64, .string, .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 [.f32, .f64, .string, .array, .array_fixed, .map, .struct_] &&
|
||||
!right.has_method(infix_expr.op.str()) {
|
||||
c.error('mismatched types `$left.name` and `$right.name`', infix_expr.right.position())
|
||||
} else if infix_expr.right is ast.IntegerLiteral && infix_expr.right.str() == '0' {
|
||||
c.error('modulo by zero', infix_expr.right.position())
|
||||
}
|
||||
}
|
||||
else {}
|
||||
|
12
vlib/v/checker/tests/float_modulo_err.out
Normal file
12
vlib/v/checker/tests/float_modulo_err.out
Normal file
@ -0,0 +1,12 @@
|
||||
vlib/v/checker/tests/float_modulo_err.v:2:13: error: float modulo not allowed, use math.fmod() instead
|
||||
1 | fn main() {
|
||||
2 | println(3.0 % 2.0)
|
||||
| ~~~
|
||||
3 | println(f32(3.0) % f32(2.0))
|
||||
4 | }
|
||||
vlib/v/checker/tests/float_modulo_err.v:3:17: error: float modulo not allowed, use math.fmod() instead
|
||||
1 | fn main() {
|
||||
2 | println(3.0 % 2.0)
|
||||
3 | println(f32(3.0) % f32(2.0))
|
||||
| ~~~
|
||||
4 | }
|
4
vlib/v/checker/tests/float_modulo_err.vv
Normal file
4
vlib/v/checker/tests/float_modulo_err.vv
Normal file
@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
println(3.0 % 2.0)
|
||||
println(f32(3.0) % f32(2.0))
|
||||
}
|
5
vlib/v/checker/tests/int_modulo_by_zero_err.out
Normal file
5
vlib/v/checker/tests/int_modulo_by_zero_err.out
Normal file
@ -0,0 +1,5 @@
|
||||
vlib/v/checker/tests/int_modulo_by_zero_err.v:2:17: error: modulo by zero
|
||||
1 | fn main() {
|
||||
2 | println(3 % 0)
|
||||
| ^
|
||||
3 | }
|
3
vlib/v/checker/tests/int_modulo_by_zero_err.vv
Normal file
3
vlib/v/checker/tests/int_modulo_by_zero_err.vv
Normal file
@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println(3 % 0)
|
||||
}
|
Loading…
Reference in New Issue
Block a user