From 49fed3490bf3dcd21e2662968a4ccba7384275d7 Mon Sep 17 00:00:00 2001 From: Nicholas Wilson Date: Wed, 8 Jan 2025 10:32:52 +0800 Subject: [PATCH] fix #20645 - implement C++ style `init-statement` for `if --- compiler/src/dmd/statementsem.d | 7 ++++++- compiler/test/runnable/controlflow_init.d | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 compiler/test/runnable/controlflow_init.d diff --git a/compiler/src/dmd/statementsem.d b/compiler/src/dmd/statementsem.d index b2617e44354..87b9985da93 100644 --- a/compiler/src/dmd/statementsem.d +++ b/compiler/src/dmd/statementsem.d @@ -605,7 +605,7 @@ Statement statementSemanticVisit(Statement s, Scope* sc) * } finally { v1.~this(); } * where controlflow = `for` or `if` */ - private Statment expandInit(S)(S cfs) + Statment expandInit(S)(S cfs) { auto ainit = new Statements(); ainit.push(cfs._init); @@ -1642,6 +1642,11 @@ Statement statementSemanticVisit(Statement s, Scope* sc) /* https://dlang.org/spec/statement.html#IfStatement */ + if (ifs._init) + { + result = expandInit(ifs); + return; + } // check in syntax level ifs.condition = checkAssignmentAsCondition(ifs.condition, sc); diff --git a/compiler/test/runnable/controlflow_init.d b/compiler/test/runnable/controlflow_init.d new file mode 100644 index 00000000000..1624d951454 --- /dev/null +++ b/compiler/test/runnable/controlflow_init.d @@ -0,0 +1,12 @@ + + +int main() +{ + if (int a = 4; a) + { + + } + else + assert(0); + return 0; +}