Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/fpy/semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2421,7 +2421,8 @@ def const_convert_type(
return FpyValue(to_type, raw_val)

assert False, (from_val, from_type, to_type)
except (ValueError, struct.error) as e:
except (ValueError, OverflowError, struct.error) as e:
# OverflowError is int(inf); ValueError is int(nan)
state.err(f"For type {from_type.display_name}: {e}", node)
return None

Expand Down
28 changes: 28 additions & 0 deletions test/fpy/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,34 @@ def test_const_complex_pow(self, fprime_test_api):

assert_compile_failure(fprime_test_api, seq)

def test_const_infinity_to_int(self, fprime_test_api):
"""An infinite float constant cast to an integer type is a compile
error, not an uncaught OverflowError."""
seq = """
x: I64 = I64(F64(1e308) * F64(10.0))
exit(0)
"""

assert_compile_failure(fprime_test_api, seq, match="infinity")

def test_const_negative_infinity_to_int(self, fprime_test_api):
"""The same holds for a negative infinity and an unsigned target."""
seq = """
x: U8 = U8(F64(-1e308) + F64(-1e308))
exit(0)
"""

assert_compile_failure(fprime_test_api, seq, match="infinity")

def test_const_nan_to_int(self, fprime_test_api):
"""The NaN counterpart reports a diagnostic too."""
seq = """
x: I64 = I64(F64(1e308) * F64(10.0) - F64(1e308) * F64(10.0))
exit(0)
"""

assert_compile_failure(fprime_test_api, seq, match="NaN")

def test_very_large_const_pow(self, fprime_test_api):
seq = """
10.0 ** 1000
Expand Down
Loading