Skip to content

Commit 507be96

Browse files
committed
fix #21630: enum and alias ignored on runtime foreach loop variables
1 parent 1a51bc6 commit 507be96

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

compiler/src/dmd/expressionsem.d

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3953,8 +3953,8 @@ private extern(C++) final class IsMemcmpableVisitor : Visitor
39533953
/* We recursively check all variable declaration within the struct.
39543954
* The recursiveness is needed to handle cases like this:
39553955
* struct Test {
3956-
* nothrow:
3957-
* int[] contents;
3956+
* nothrow:
3957+
* int[] contents;
39583958
* }
39593959
* Here a `StorageClassDeclaration` symbol will be created, which wraps the variable declaration.
39603960
*/
@@ -17932,10 +17932,11 @@ void lowerNonArrayAggregate(StaticForeach sfe, Scope* sc)
1793217932
Parameters*[3] pparams = [new Parameters(), new Parameters(), new Parameters()];
1793317933
foreach (i; 0 .. nvars)
1793417934
{
17935-
foreach (params; pparams)
17935+
foreach (j, params; pparams)
1793617936
{
1793717937
auto p = sfe.aggrfe ? (*sfe.aggrfe.parameters)[i] : sfe.rangefe.param;
17938-
params.push(new Parameter(aloc, p.storageClass, p.type, p.ident, null, null));
17938+
auto storageClass = j == 2 ? p.storageClass : p.storageClass & ~(STC.manifest | STC.alias_);
17939+
params.push(new Parameter(aloc, storageClass, p.type, p.ident, null, null));
1793917940
}
1794017941
}
1794117942
Expression[2] res;

compiler/src/dmd/statementsem.d

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,16 @@ Statement statementSemanticVisit(Statement s, Scope* sc)
14581458
*/
14591459

14601460
//printf("ForeachRangeStatement::semantic() %p\n", fs);
1461+
1462+
if (fs.param.storageClass & STC.manifest)
1463+
{
1464+
error(fs.loc, "cannot declare `enum` loop variables for non-unrolled foreach");
1465+
}
1466+
if (fs.param.storageClass & STC.alias_)
1467+
{
1468+
error(fs.loc, "cannot declare `alias` loop variables for non-unrolled foreach");
1469+
}
1470+
14611471
auto loc = fs.loc;
14621472
fs.lwr = fs.lwr.expressionSemantic(sc);
14631473
fs.lwr = resolveProperties(sc, fs.lwr);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/+
2+
TEST_OUTPUT:
3+
---
4+
fail_compilation/issue21630.d(14): Error: cannot declare `enum` loop variables for non-unrolled foreach
5+
fail_compilation/issue21630.d(15): Error: cannot declare `alias` loop variables for non-unrolled foreach
6+
fail_compilation/issue21630.d(16): Error: cannot declare `enum` loop variables for non-unrolled foreach
7+
fail_compilation/issue21630.d(17): Error: cannot declare `alias` loop variables for non-unrolled foreach
8+
---
9+
+/
10+
11+
void main()
12+
{
13+
enum a = [1, 2, 3];
14+
foreach(enum i; a) { } // error
15+
foreach(alias i; a) { } // error
16+
foreach(enum i; 0 .. 3) { } // error
17+
foreach(alias i; 0 .. 3) { } // error
18+
}

0 commit comments

Comments
 (0)