Skip to content

Commit 0298e19

Browse files
committed
fix(#20456): detect enum type used as value at semantic stage
Add hasValidType() checks after resolveProperties() in visitIf, visitFor, and visitDo (statementsem.d) so using an enum type name as a boolean condition (e.g. 'if (Flags)') is caught during semantic analysis instead of being deferred to e2ir/codegen, which was silently skipped when compiling with -o-. Fixes #20456
1 parent dd4bb63 commit 0298e19

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

compiler/src/dmd/statementsem.d

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,8 @@ Statement statementSemanticVisit(Statement s, Scope* sc)
658658

659659
ds.condition = ds.condition.expressionSemantic(sc);
660660
ds.condition = resolveProperties(sc, ds.condition);
661+
if (!ds.condition.hasValidType())
662+
ds.condition = ErrorExp.get();
661663
if (checkNonAssignmentArrayOp(ds.condition))
662664
ds.condition = ErrorExp.get();
663665
ds.condition = ds.condition.optimize(WANTvalue);
@@ -730,6 +732,8 @@ Statement statementSemanticVisit(Statement s, Scope* sc)
730732

731733
fs.condition = fs.condition.expressionSemantic(sc);
732734
fs.condition = resolveProperties(sc, fs.condition);
735+
if (!fs.condition.hasValidType())
736+
fs.condition = ErrorExp.get();
733737
if (checkNonAssignmentArrayOp(fs.condition))
734738
fs.condition = ErrorExp.get();
735739
fs.condition = fs.condition.optimize(WANTvalue);
@@ -1798,6 +1802,8 @@ Statement statementSemanticVisit(Statement s, Scope* sc)
17981802
ifs.condition = resolveProperties(scd, ifs.condition);
17991803
ifs.condition = ifs.condition.addDtorHook(scd);
18001804
}
1805+
if (!ifs.condition.hasValidType())
1806+
ifs.condition = ErrorExp.get();
18011807
if (checkNonAssignmentArrayOp(ifs.condition))
18021808
ifs.condition = ErrorExp.get();
18031809

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
TEST_OUTPUT:
3+
---
4+
fail_compilation/fail20456.d(14): Error: type `Flags` is not an expression
5+
fail_compilation/fail20456.d(19): Error: type `Flags` is not an expression
6+
fail_compilation/fail20456.d(24): Error: type `Flags` is not an expression
7+
---
8+
*/
9+
10+
enum Flags : ubyte { Foo = 1, }
11+
12+
void testIf()
13+
{
14+
if (Flags) {}
15+
}
16+
17+
void testWhile()
18+
{
19+
while (Flags) {}
20+
}
21+
22+
void testDoWhile()
23+
{
24+
do {} while (Flags);
25+
}

0 commit comments

Comments
 (0)