From ac9e8a5a700346ef0479a24e5bd5544395b3965c Mon Sep 17 00:00:00 2001 From: Dennis Date: Fri, 17 Jan 2025 01:50:17 +0100 Subject: [PATCH] Fix bypassing nothrow in debug statements (#20720) * Fix bypassing nothrow in debug statements * Fix debug walking null statements --- compiler/src/dmd/expressionsem.d | 5 ----- compiler/src/dmd/statementsem.d | 16 +++++++++++++--- ...{test16492.d => debug_statement_attributes.d} | 14 ++++++++++++++ compiler/test/compilable/test24017.d | 11 ----------- 4 files changed, 27 insertions(+), 19 deletions(-) rename compiler/test/compilable/{test16492.d => debug_statement_attributes.d} (73%) delete mode 100644 compiler/test/compilable/test24017.d diff --git a/compiler/src/dmd/expressionsem.d b/compiler/src/dmd/expressionsem.d index 6ebb20a56a..ee8eb36465 100644 --- a/compiler/src/dmd/expressionsem.d +++ b/compiler/src/dmd/expressionsem.d @@ -1363,11 +1363,6 @@ private Expression resolveUFCSProperties(Scope* sc, Expression e1, Expression e2 auto arguments = new Expressions(1); (*arguments)[0] = eleft; e = new CallExp(loc, e, arguments); - - // https://issues.dlang.org/show_bug.cgi?id=24017 - if (sc.flags & SCOPE.debug_) - e.isCallExp().inDebugStatement = true; - e = e.expressionSemantic(sc); return e; } diff --git a/compiler/src/dmd/statementsem.d b/compiler/src/dmd/statementsem.d index 0390892efe..502a4c2bd6 100644 --- a/compiler/src/dmd/statementsem.d +++ b/compiler/src/dmd/statementsem.d @@ -134,7 +134,16 @@ private Expression checkAssignmentAsCondition(Expression e, Scope* sc) return e; } -// Performs semantic analysis in Statement AST nodes +/** + * Performs semantic analysis in Statement AST nodes + * + * Params: + * s = statement to perform semantic analysis on + * sc = scope in which statement resides + * + * Returns: statement `s` after semantic analysis. + * Can be `null`, for example with `pragma(msg, "")` + */ Statement statementSemantic(Statement s, Scope* sc) { import dmd.compiler; @@ -3461,6 +3470,7 @@ Statement statementSemanticVisit(Statement s, Scope* sc) sc = sc.push(); sc.flags |= SCOPE.debug_; ds.statement = ds.statement.statementSemantic(sc); + debugThrowWalker(ds.statement); sc.pop(); } result = ds.statement; @@ -4736,7 +4746,6 @@ private Statements* flatten(Statement statement, Scope* sc) if (dc) { s = new DebugStatement(cs.loc, cs.ifbody); - debugThrowWalker(cs.ifbody); } else s = cs.ifbody; @@ -4908,7 +4917,8 @@ Params: */ private void debugThrowWalker(Statement s) { - + if (!s) + return; extern(C++) final class DebugWalker : SemanticTimeTransitiveVisitor { alias visit = SemanticTimeTransitiveVisitor.visit; diff --git a/compiler/test/compilable/test16492.d b/compiler/test/compilable/debug_statement_attributes.d similarity index 73% rename from compiler/test/compilable/test16492.d rename to compiler/test/compilable/debug_statement_attributes.d index 833be1d8cf..bf762c001d 100644 --- a/compiler/test/compilable/test16492.d +++ b/compiler/test/compilable/debug_statement_attributes.d @@ -85,3 +85,17 @@ void test6() nothrow () {throw new Exception("");}(); } } + +void writeln() {} +void writeln(string) {} + +void test7() nothrow +{ + debug writeln("Hello"); // https://issues.dlang.org/show_bug.cgi?id=24017 + debug "Hello".writeln; + debug writeln = "Hello"; // https://github.com/dlang/dmd/issues/20719 + debug writeln; + + // https://github.com/dlang/dmd/pull/20720#issuecomment-2596892489 + debug pragma(msg, ""); // Came up as segfault, pragma statement became null after semantic +} diff --git a/compiler/test/compilable/test24017.d b/compiler/test/compilable/test24017.d deleted file mode 100644 index 0d03978120..0000000000 --- a/compiler/test/compilable/test24017.d +++ /dev/null @@ -1,11 +0,0 @@ -// https://issues.dlang.org/show_bug.cgi?id=24017 - -// REQUIRED_ARGS: -debug - -void writeln(string) {} - -void main() nothrow -{ - debug writeln("Hello"); - debug "Hello".writeln; -}