From 2417e8a6e89e18b0c2c46c4f86f38c5bc9bf38aa 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 | 5 +++++ compiler/test/runnable/controlflow_init.d | 12 ++++++++++++ 2 files changed, 17 insertions(+) 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..d5c065fe2e5 100644 --- a/compiler/src/dmd/statementsem.d +++ b/compiler/src/dmd/statementsem.d @@ -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; +}