Skip to content

Commit

Permalink
Merge pull request #8738 from wolframw/issue_19246
Browse files Browse the repository at this point in the history
Fix Issue 19246 - Binary literal `0b_` allowed
  • Loading branch information
thewilsonator committed Nov 6, 2018
2 parents 3074a3d + 1caed61 commit d6e4bbb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
9 changes: 9 additions & 0 deletions changelog/deprecated_binary_literals.dd
@@ -0,0 +1,9 @@
Deprecate invalid binary literals

Prior to this release, binary literals without any digits after the prefix `0b`
were considered valid. This has now been deprecated.
---
auto foo = 0b; // deprecated
auto bar = 0b_; // deprecated
auto baz = 0b0; // conforming equivalent
---
7 changes: 3 additions & 4 deletions src/dmd/lexer.d
Expand Up @@ -1803,7 +1803,7 @@ class Lexer : ErrorHandler
int d;
bool err = false;
bool overflow = false;
bool anyBinaryDigitsUS = false;
bool anyBinaryDigitsNoSingleUS = false;
bool anyHexDigitsNoSingleUS = false;
dchar c = *p;
if (c == '0')
Expand Down Expand Up @@ -1914,15 +1914,14 @@ class Lexer : ErrorHandler
p = start;
return inreal(t);
case '_':
anyBinaryDigitsUS = true;
++p;
continue;
default:
goto Ldone;
}
// got a digit here, set any necessary flags, check for errors
anyHexDigitsNoSingleUS = true;
anyBinaryDigitsUS = true;
anyBinaryDigitsNoSingleUS = true;
if (!err && d >= base)
{
error("%s digit expected, not `%c`", base == 2 ? "binary".ptr :
Expand Down Expand Up @@ -1950,7 +1949,7 @@ class Lexer : ErrorHandler
// Deprecated in 2018-06.
// Change to error in 2019-06.
// @@@DEPRECATED_2019-06@@@
if ((base == 2 && !anyBinaryDigitsUS) ||
if ((base == 2 && !anyBinaryDigitsNoSingleUS) ||
(base == 16 && !anyHexDigitsNoSingleUS))
deprecation("`%.*s` isn't a valid integer literal, use `%.*s0` instead", cast(int)(p - start), start, 2, start);
enum FLAGS : int
Expand Down
20 changes: 20 additions & 0 deletions test/fail_compilation/fix19246.d
@@ -0,0 +1,20 @@
/* REQUIRED_ARGS: -de
* PERMUTE_ARGS:
* TEST_OUTPUT:
---
fail_compilation/fix19246.d(16): Deprecation: `0b_` isn't a valid integer literal, use `0b0` instead
fail_compilation/fix19246.d(17): Deprecation: `0B_` isn't a valid integer literal, use `0B0` instead
fail_compilation/fix19246.d(18): Deprecation: `0b` isn't a valid integer literal, use `0b0` instead
fail_compilation/fix19246.d(19): Deprecation: `0B` isn't a valid integer literal, use `0B0` instead
---
*/

// https://issues.dlang.org/show_bug.cgi?id=19246

void foo()
{
auto a = 0b_;
auto b = 0B_;
auto c = 0b;
auto d = 0B;
}

0 comments on commit d6e4bbb

Please sign in to comment.