Bug 5635 – Code inside 'foreach' using opApply cannot modify variable out of its scope in a template function.

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Mac OS X
Creation time
2011-02-21T12:13:00Z
Last change time
2014-06-08T02:42:33Z
Keywords
rejects-valid
Assigned to
nobody
Creator
kennytm
Blocks
4850

Comments

Comment #0 by kennytm — 2011-02-21T12:13:44Z
The following program should compile: pure bool foo(R=int)(string x) { bool result = false; foreach (dchar d; x) result = true; return result; } void main () { foo("hi"); } But as of v2.052 it raises the errors: x.d(4): Error: pure nested function '__foreachbody3' cannot access mutable data 'result' x.d(9): Error: template instance x.foo!(int) error instantiating If the template parameter (R=int) were removed, the program compiles successfully. (I think this is related to Issue 3316).
Comment #1 by github-bugzilla — 2014-06-08T02:42:25Z
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/17f3b7171dad7bb4d77822a878840191e630cb1c fix Issue 5635 - Code inside 'foreach' using opApply cannot modify variable out of its scope in a template function.