Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions changelog/dmd.foreach-shadow-error.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Foreach variable shadowing is now an error

A deprecation introduced in DMD 2.089.0 for foreach variables shadowing
outer scope symbols has been upgraded to an error.

This affects both variables declared inside a foreach body that shadow
outer variables, and duplicate variable names in foreach parameter lists
when using `opApply`.

---
void main()
{
int[int] arr;
int x;
foreach (i, j; arr)
{
int x; // Error: variable `x` is shadowing variable `...x`
}
}
---

---
struct Foo
{
int opApply(scope int delegate(size_t, size_t, ref uint)) => 0;
}

void main()
{
foreach (x, y, x; Foo()) {} // Error: variable `x` is shadowing variable `...x`
}
---
13 changes: 3 additions & 10 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -8731,17 +8731,10 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
auto decl = s2.isDeclaration();
if (decl && (decl.storage_class & STC.local))
continue;
if (sc.func.fes)
{
deprecation(e.loc, "%s `%s` is shadowing %s `%s`", s.kind(), s.ident.toChars(), s2.kind(), s2.toPrettyChars());
deprecationSupplemental(s2.loc, "declared here");
}
else
{
error(e.loc, "%s `%s` is shadowing %s `%s`", s.kind(), s.ident.toChars(), s2.kind(), s2.toPrettyChars());
errorSupplemental(s2.loc, "declared here");
error(e.loc, "%s `%s` is shadowing %s `%s`", s.kind(), s.ident.toChars(), s2.kind(), s2.toPrettyChars());
errorSupplemental(s2.loc, "declared here");
if (!sc.func.fes)
return setError();
}
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions compiler/test/fail_compilation/fail2195.d
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// https://issues.dlang.org/show_bug.cgi?id=2195
// REQUIRED_ARGS: -de
/*
TEST_OUTPUT:
---
fail_compilation/fail2195.d(17): Deprecation: variable `variable` is shadowing variable `fail2195.main.variable`
fail_compilation/fail2195.d(14): declared here
fail_compilation/fail2195.d(16): Error: variable `variable` is shadowing variable `fail2195.main.variable`
fail_compilation/fail2195.d(13): declared here
---
*/

Expand Down
15 changes: 15 additions & 0 deletions compiler/test/fail_compilation/fail22584.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// https://github.com/dlang/dmd/issues/22584
/*
TEST_OUTPUT:
---
fail_compilation/fail22584.d(14): Error: variable `x` is shadowing variable `fail22584.main.__foreachbody_L14_C5.x`
fail_compilation/fail22584.d(14): declared here
---
*/

struct Foo {
int opApply(scope int delegate(size_t, size_t, ref uint)) => 0;
}
void main() {
foreach (x, y, x; Foo()) {}
}
Loading