v: limit -div-by-zero-is-zero influence to just the integer types (for f32 and f64, / produces a +inf or -inf value, and the program continues to run)

This commit is contained in:
Delyan Angelov 2025-07-29 07:37:53 +03:00
parent a5f9899ae3
commit 59d408f72c
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
4 changed files with 12 additions and 2 deletions

View File

@ -1204,8 +1204,6 @@ fn (mut g Gen) gen_plain_infix_expr(node ast.InfixExpr) {
needs_cast := node.left_type.is_number() && node.right_type.is_number()
&& node.op in [.plus, .minus, .mul, .div, .mod] && !(g.pref.translated
|| g.file.is_translated)
is_safe_div := node.op == .div && g.pref.div_by_zero_is_zero
is_safe_mod := node.op == .mod && g.pref.div_by_zero_is_zero
mut typ := node.promoted_type
mut typ_str := g.styp(typ)
if needs_cast {
@ -1219,6 +1217,8 @@ fn (mut g Gen) gen_plain_infix_expr(node ast.InfixExpr) {
typ_str = g.styp(typ)
g.write('(${typ_str})(')
}
is_safe_div := node.op == .div && g.pref.div_by_zero_is_zero && typ.is_int()
is_safe_mod := node.op == .mod && g.pref.div_by_zero_is_zero && typ.is_int()
if node.left_type.is_ptr() && node.left.is_auto_deref_var() && !node.right_type.is_pointer() {
g.write('*')
} else if !g.inside_interface_deref && node.left is ast.Ident

View File

@ -1,2 +1,3 @@
int c = (int)(VSAFE_DIV_int(a , z));
int d = (int)(VSAFE_MOD_int(a , z));
f32 fc = (f32)(fa / fz);

View File

@ -2,3 +2,6 @@ a == 42
z == 0
c == a / z == 0
d == a % z == 42
fa == 42.0
fz == 0.0
fc == fa / fz == +inf

View File

@ -7,3 +7,9 @@ println('a == ${a}')
println('z == ${z}')
println('c == a / z == ${c}')
println('d == a % z == ${d}')
fa := f32(42)
fz := f32(0)
fc := fa / fz
println('fa == ${fa}')
println('fz == ${fz}')
println('fc == fa / fz == ${fc}')