Skip to content

Commit

Permalink
fix #20645 - implement C++ style init-statement for `if
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilsonator committed Jan 8, 2025
1 parent 9803105 commit 49fed34
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion compiler/src/dmd/statementsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
12 changes: 12 additions & 0 deletions compiler/test/runnable/controlflow_init.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


int main()
{
if (int a = 4; a)
{

}
else
assert(0);
return 0;
}

0 comments on commit 49fed34

Please sign in to comment.