diff --git a/src/compiler/lexer.c b/src/compiler/lexer.c index 6b5d55808..3a09cffd1 100644 --- a/src/compiler/lexer.c +++ b/src/compiler/lexer.c @@ -3,6 +3,7 @@ // a copy of which can be found in the LICENSE file. #include "compiler_internal.h" +#include "errno.h" typedef enum { @@ -426,7 +427,12 @@ static inline bool scan_hex(Lexer *lexer) // Possibly we should move to a BigDecimal implementation or at least a soft float 256 // implementation for the constants. char *end = NULL; + errno = 0; long double fval = strtold(lexer->lexing_start, &end); + if (errno == ERANGE) + { + return add_error_token(lexer, "The float value is out of range."); + } if (end != lexer->current) { return add_error_token(lexer, "Invalid float value."); diff --git a/src/compiler/sema_casts.c b/src/compiler/sema_casts.c index 24fdc4b3b..53cdc6478 100644 --- a/src/compiler/sema_casts.c +++ b/src/compiler/sema_casts.c @@ -615,7 +615,7 @@ bool may_convert_float_const_implicit(Expr *expr, Type *to_type) default: UNREACHABLE } - if (expr->const_expr.f < -limit && expr->const_expr.f > limit) + if (expr->const_expr.f < -limit || expr->const_expr.f > limit) { SEMA_ERROR(expr, "The value '%Lg' is out of range for %s, so you need an explicit cast to truncate the value.", expr->const_expr.f, type_quoted_error_string(to_type)); return false; diff --git a/test/test_suite/floats/explicit_float_truncation_needed.c3 b/test/test_suite/floats/explicit_float_truncation_needed.c3 new file mode 100644 index 000000000..6bd4792fe --- /dev/null +++ b/test/test_suite/floats/explicit_float_truncation_needed.c3 @@ -0,0 +1,4 @@ +func void test() +{ + float x = 0x7FFFFFFFFFFF.0p+400; // #error: The value '3.63419e+134' is out of range for 'float', so you need an explicit cast to truncate the value +} \ No newline at end of file diff --git a/test/test_suite/floats/float_exceeding_size.c3 b/test/test_suite/floats/float_exceeding_size.c3 new file mode 100644 index 000000000..0d198b3d9 --- /dev/null +++ b/test/test_suite/floats/float_exceeding_size.c3 @@ -0,0 +1,5 @@ +func void test() +{ + float x = 0x7FFFFFFFFFFF.0p+40000; // #error: The float value is out of range. + +} \ No newline at end of file