Skip to content

Commit

Permalink
Fix #20763 - Inconsistent handling of type + value in typeof expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Korpel committed Jan 28, 2025
1 parent 84c7b04 commit 6a8bdda
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
11 changes: 7 additions & 4 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -14771,11 +14771,14 @@ private bool checkArithmetic(Expression e, EXP op)
return true;
}

// FIXME: Existing code relies on adding / subtracting types in typeof() expressions:
// alias I = ulong; alias U = typeof(I + 1u);
// https://github.com/dlang/dmd/issues/20763
if (op == EXP.add || op == EXP.min)
if ((op == EXP.add || op == EXP.min) && e.isTypeExp())
{
// @@@DEPRECATED_2.121@@@
// Deprecated in 2.111
// In 2.121, remove this branch to let `checkValue` raise the error
deprecation(e.loc, "type `%s` has no value", e.toChars);

Check warning on line 14779 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L14779

Added line #L14779 was not covered by tests
return false;
}

return e.checkValue();
}
Expand Down
25 changes: 25 additions & 0 deletions compiler/test/fail_compilation/test20763.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
REQUIRED_ARGS: -de
TEST_OUTPUT:
---
fail_compilation\test20763.d(19): Deprecation: type `ulong` has no value
fail_compilation\test20763.d(20): Deprecation: type `ulong` has no value
fail_compilation\test20763.d(21): Error: type `ulong` has no value
fail_compilation\test20763.d(22): Error: type `ulong` has no value
fail_compilation\test20763.d(23): Error: type `ulong` has no value
fail_compilation\test20763.d(24): Error: type `ulong` has no value
---
*/

// https://github.com/dlang/dmd/issues/20763
void test()
{
alias I = ulong;
alias U0 = typeof(I + 1u);
alias U1 = typeof(1 - I);
alias U2 = typeof(+I);
alias U3 = typeof(I * 1);
alias U4 = typeof(I << 1);
alias U5 = typeof(I | 1);
}

0 comments on commit 6a8bdda

Please sign in to comment.