Skip to content

Commit

Permalink
Improve error message for 'out' on foreach
Browse files Browse the repository at this point in the history
This hopefully will provide a slightly better user experience,
as many other storage classes are accepted.

Before, the following error message was printed by DMD:
```
test/fail_compilation/foreach.d(12): Error: basic type expected, not `out`
test/fail_compilation/foreach.d(12): Error: no identifier for declarator `_error_`
test/fail_compilation/foreach.d(12): Error: found `out` when expecting `;`
test/fail_compilation/foreach.d(12): Error: found `;` when expecting `)`
test/fail_compilation/foreach.d(12): Error: found `)` when expecting `;` following statement
test/fail_compilation/foreach.d(13): Error: basic type expected, not `out`
test/fail_compilation/foreach.d(13): Error: no identifier for declarator `_error_`
test/fail_compilation/foreach.d(13): Error: found `out` when expecting `;`
test/fail_compilation/foreach.d(13): Error: expression expected, not `out`
test/fail_compilation/foreach.d(13): Error: found `val` when expecting `)`
test/fail_compilation/foreach.d(13): Error: use `{ }` for an empty statement, not `;`
test/fail_compilation/foreach.d(13): Error: found `)` when expecting `;` following statement
```
  • Loading branch information
Geod24 authored and dlang-bot committed Oct 18, 2021
1 parent 96569c9 commit 0befa1b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/dmd/parse.d
Original file line number Diff line number Diff line change
Expand Up @@ -5411,6 +5411,11 @@ class Parser(AST) : Lexer
stc = STC.scope_;
goto Lagain;

case TOK.out_:
error("cannot declare `out` loop variable, use `ref` instead");
stc = STC.out_;
goto Lagain;

case TOK.enum_:
stc = STC.manifest;
goto Lagain;
Expand Down
14 changes: 14 additions & 0 deletions test/fail_compilation/foreach.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
TEST_OUTPUT:
---
fail_compilation/foreach.d(12): Error: cannot declare `out` loop variable, use `ref` instead
fail_compilation/foreach.d(13): Error: cannot declare `out` loop variable, use `ref` instead
fail_compilation/foreach.d(13): Error: cannot declare `out` loop variable, use `ref` instead
---
*/
void main ()
{
int[] array;
foreach (out val; array) {}
foreach (out idx, out val; array) {}
}

0 comments on commit 0befa1b

Please sign in to comment.