Skip to content

Commit

Permalink
Fix Issue 23284 - better floating point error messages. (#14354)
Browse files Browse the repository at this point in the history
* Fix Issue 23284 - better floating point error messages.

There was also a bug here in that real literals did not get a suffix attached.

* Replace an array lookup with a ternary, more tests
  • Loading branch information
maxhaton authored Aug 31, 2022
1 parent 1f244a4 commit 1b78c63
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 7 additions & 2 deletions compiler/src/dmd/lexer.d
Original file line number Diff line number Diff line change
Expand Up @@ -2582,8 +2582,13 @@ class Lexer
{
/* C11 6.4.4.2 doesn't actually care if it is not representable if it is not hex
*/
const char* suffix = (result == TOK.float32Literal || result == TOK.imaginary32Literal) ? "f" : "";
error(scanloc, "number `%s%s` is not representable", sbufptr, suffix);
const char* suffix = result == TOK.float32Literal ? "f" : result == TOK.float80Literal ? "L" : "";
const char* type = [TOK.float32Literal: "`float`".ptr,
TOK.float64Literal: "`double`".ptr,
TOK.float80Literal: "`real` for the current target".ptr][result];
error(scanloc, "number `%s%s` is not representable as a %s", sbufptr, suffix, type);
const char* extra = result == TOK.float64Literal ? "`real` literals can be written using the `L` suffix. " : "";
errorSupplemental(scanloc, "%shttps://dlang.org/spec/lex.html#floatliteral", extra);
}
debug
{
Expand Down
6 changes: 5 additions & 1 deletion compiler/test/fail_compilation/lexer5.d
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/*
TEST_OUTPUT:
---
fail_compilation/lexer5.d(8): Error: number `1e-100f` is not representable
fail_compilation/lexer5.d(11): Error: number `1e-100f` is not representable as a `float`
fail_compilation/lexer5.d(11): https://dlang.org/spec/lex.html#floatliteral
fail_compilation/lexer5.d(12): Error: number `1e-10024` is not representable as a `double`
fail_compilation/lexer5.d(12): `real` literals can be written using the `L` suffix. https://dlang.org/spec/lex.html#floatliteral
---
*/

static float f = 1e-100f;
static float f2 = 1e-10024;

0 comments on commit 1b78c63

Please sign in to comment.