Improve error message for 'out' on foreach

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
```
This commit is contained in:
Geod24 2021-01-07 11:44:21 +09:00 committed by The Dlang Bot
parent 96569c9a95
commit 0befa1bc6a
2 changed files with 19 additions and 0 deletions

View file

@ -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;

View file

@ -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) {}
}