Bug 7453 – Can't return value from within opApply

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-02-06T14:12:00Z
Last change time
2012-04-20T13:15:32Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2012-02-06T14:12:31Z
import std.file; void main() { foreach (i; 0 .. 10) return; // ok foreach (string entry; dirEntries(".", SpanMode.shallow)) { return; // Error: long has no effect in expression (0) // test.d(10): Error: cannot return non-void from void function } } This caught me off-guard. In fact: import std.file; import std.stdio; void main() { writeln(test()); // writes bar } string test() { foreach (string entry; dirEntries(".", SpanMode.shallow)) { return "foo"; } return "bar"; } The foreach loop *is* entered, but the return statement just makes the foreach loop break out, and then the 'return "bar";' statement is reached instead. This almost looks like a hole in the design of opApply?
Comment #1 by andrej.mitrovich — 2012-04-19T19:07:56Z
The second sample is fixed because it was a bug in dirEntry's opApply function. There's just one thing left: import std.file; void main(){ // Error: long has no effect in expression (0) foreach (_; dirEntries(".", SpanMode.shallow)) { return; } } void test() { // works fine foreach (_; dirEntries(".", SpanMode.shallow)) { return; } } Why does the return fail if we're in main()?
Comment #2 by k.hara.pg — 2012-04-20T01:37:22Z
Reduced test case: struct S { int opApply(int delegate(string) dg) { return 0; } } void main() { foreach (_; S()) { return; // Error: long has no effect in expression (0) // test.d(8): Error: cannot return non-void from void function } } Pull request: https://github.com/D-Programming-Language/dmd/pull/892
Comment #3 by github-bugzilla — 2012-04-20T12:04:41Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/4d28102c7a20341db6239bd843f406d6a5a45725 fix Issue 7453 - Can't return value from within opApply Inside main function, and after first semantic, ReturnStatement::exp is filled with IntegerExp(0). Then in second semantic, implicit0 doesn't become 1, and error occurs. ReturnStatement object should keep implicit0 value for repeating call of semantic(). https://github.com/D-Programming-Language/dmd/commit/03e8c2656d03c90cd2bda72abb8b6392556f1017 Merge pull request #892 from 9rnsr/fix7453 Issue 7453 - Can't return value from within opApply