Skip to content

Commit 48afbd1

Browse files
committed
Fix issue #21630 - non-static range foreach ignores enum and alias
1 parent c397436 commit 48afbd1

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

compiler/src/dmd/parse.d

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5575,6 +5575,7 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
55755575
}
55765576

55775577
TOK op = token.value;
5578+
Loc aliasOrEnumLoc;
55785579

55795580
nextToken();
55805581
check(TOK.leftParenthesis);
@@ -5617,10 +5618,12 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
56175618

56185619
case TOK.enum_:
56195620
stc = STC.manifest;
5621+
aliasOrEnumLoc = token.loc;
56205622
goto Lagain;
56215623

56225624
case TOK.alias_:
56235625
storageClass = appendStorageClass(storageClass, STC.alias_);
5626+
aliasOrEnumLoc = token.loc;
56245627
nextToken();
56255628
break;
56265629

@@ -5705,6 +5708,16 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
57055708
AST.Expression upr = parseExpression();
57065709
check(TOK.rightParenthesis);
57075710
Loc endloc;
5711+
if (is(Foreach == AST.Statement) && (p.storageClass & STC.alias_))
5712+
{
5713+
error(aliasOrEnumLoc, "cannot use `alias` with a range `foreach`");
5714+
eSink.errorSupplemental(aliasOrEnumLoc, "use `static foreach` instead");
5715+
}
5716+
if (is(Foreach == AST.Statement) && (p.storageClass & STC.manifest))
5717+
{
5718+
error(aliasOrEnumLoc, "cannot use `enum` with a range `foreach`");
5719+
eSink.errorSupplemental(aliasOrEnumLoc, "use `static foreach` instead");
5720+
}
57085721
static if (is(Foreach == AST.Statement) || is(Foreach == AST.StaticForeachStatement))
57095722
{
57105723
AST.Statement _body = parseStatement(0, null, &endloc);
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 use `enum` with a range `foreach`
5+
fail_compilation/issue21630.d(15): Error: cannot use `alias` with a range `foreach`
6+
fail_compilation/issue21630.d(16): Error: cannot use `enum` with a range `foreach`
7+
fail_compilation/issue21630.d(17): Error: cannot use `alias` with a range `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)