From 6a8bdda2a6f21a59eae75abe3a10866fe7e857f4 Mon Sep 17 00:00:00 2001 From: Dennis Korpel Date: Tue, 28 Jan 2025 15:57:51 +0100 Subject: [PATCH] Fix #20763 - Inconsistent handling of type + value in typeof expressions --- compiler/src/dmd/expressionsem.d | 11 ++++++---- compiler/test/fail_compilation/test20763.d | 25 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 compiler/test/fail_compilation/test20763.d diff --git a/compiler/src/dmd/expressionsem.d b/compiler/src/dmd/expressionsem.d index b7d44dddc93..2c798d3318b 100644 --- a/compiler/src/dmd/expressionsem.d +++ b/compiler/src/dmd/expressionsem.d @@ -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); return false; + } return e.checkValue(); } diff --git a/compiler/test/fail_compilation/test20763.d b/compiler/test/fail_compilation/test20763.d new file mode 100644 index 00000000000..d663da4f65b --- /dev/null +++ b/compiler/test/fail_compilation/test20763.d @@ -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); +}