From c5496ff4e5436a9d82d7e568fb6e14df3009d635 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sat, 17 Aug 2024 10:05:52 +0100 Subject: [PATCH] Fix Bugzilla 24706 - Missing errors for first operand of comma expression (#16787) --- compiler/src/dmd/expressionsem.d | 13 ++++++++----- compiler/test/fail_compilation/misc1.d | 17 ++++++++++------- compiler/test/fail_compilation/must_use.d | 7 +++++-- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/compiler/src/dmd/expressionsem.d b/compiler/src/dmd/expressionsem.d index 8e0c9adf77a1..9c46ecca20d8 100644 --- a/compiler/src/dmd/expressionsem.d +++ b/compiler/src/dmd/expressionsem.d @@ -9824,13 +9824,16 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor if (sc.inCfile) return; - if (e.type is Type.tvoid) + if (!e.isGenerated) { - checkMustUse(e.e1, sc); - discardValue(e.e1); + if (e.allowCommaExp) + { + checkMustUse(e.e1, sc); + discardValue(e.e1); + } + else + error(e.loc, "using the result of a comma expression is not allowed"); } - else if (!e.allowCommaExp && !e.isGenerated) - error(e.loc, "using the result of a comma expression is not allowed"); } override void visit(IntervalExp e) diff --git a/compiler/test/fail_compilation/misc1.d b/compiler/test/fail_compilation/misc1.d index 21d02cdae449..95bcc6d03d47 100644 --- a/compiler/test/fail_compilation/misc1.d +++ b/compiler/test/fail_compilation/misc1.d @@ -1,13 +1,14 @@ /* TEST_OUTPUT: --- -fail_compilation/misc1.d(108): Error: `5` has no effect -fail_compilation/misc1.d(109): Error: `1 + 2` has no effect -fail_compilation/misc1.d(115): Deprecation: `1 * 1` has no effect -fail_compilation/misc1.d(116): Deprecation: `__lambda3` has no effect -fail_compilation/misc1.d(122): Deprecation: `false` has no effect -fail_compilation/misc1.d(125): Deprecation: `*sp++` has no effect -fail_compilation/misc1.d(126): Deprecation: `j` has no effect +fail_compilation/misc1.d(109): Error: `5` has no effect +fail_compilation/misc1.d(110): Error: `1 + 2` has no effect +fail_compilation/misc1.d(111): Error: `x` has no effect +fail_compilation/misc1.d(117): Deprecation: `1 * 1` has no effect +fail_compilation/misc1.d(118): Deprecation: `__lambda3` has no effect +fail_compilation/misc1.d(124): Deprecation: `false` has no effect +fail_compilation/misc1.d(127): Deprecation: `*sp++` has no effect +fail_compilation/misc1.d(128): Deprecation: `j` has no effect --- */ @@ -20,8 +21,10 @@ void hasSideEffect12490(){} void issue12490() { + int x; 5, hasSideEffect12490(); 1 + 2, hasSideEffect12490(); + x, x++; } void issue23480() diff --git a/compiler/test/fail_compilation/must_use.d b/compiler/test/fail_compilation/must_use.d index e3b5fedfd4d7..f21c4a405d5e 100644 --- a/compiler/test/fail_compilation/must_use.d +++ b/compiler/test/fail_compilation/must_use.d @@ -1,16 +1,19 @@ /+ TEST_OUTPUT: --- -fail_compilation/must_use.d(15): Error: ignored value of `@mustuse` type `must_use.S`; prepend a `cast(void)` if intentional +fail_compilation/must_use.d(17): Error: ignored value of `@mustuse` type `must_use.S`; prepend a `cast(void)` if intentional +fail_compilation/must_use.d(18): Error: ignored value of `@mustuse` type `must_use.S`; prepend a `cast(void)` if intentional --- +/ import core.attribute; @mustuse struct S {} -S fun() { return S(); } +S fun(); void test() { + int x; fun(); + fun(), x++; }