Bug 1759 – Closures and With Statements

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2008-01-01T10:48:00Z
Last change time
2015-06-17T21:03:07Z
Keywords
pull, wrong-code
Assigned to
nobody
Creator
xinok

Comments

Comment #0 by xinok — 2008-01-01T10:48:23Z
The following code doesn't crash. However, it does print an unexpected value. It should print 90, but instead it prints 4202678. The bug can be worked around by referencing 'obj' directly. struct SS{ int a, b, c; } int delegate() addss(SS obj){ with(obj) return { return a+b+c; }; } void main(){ SS obj = {15, 30, 45}; auto l = addss(obj); writeln(l()); // Prints unexpected value }
Comment #1 by salihdb — 2012-08-07T20:56:08Z
This is not a bug it's a feature, there's solution: void main() { struct SS { int a, b, c; } int delegate () addss(SS obj){ return { with(obj) return a+b+c; }; } addss(SS(7, 10, 60))().writeln("(total numbers 77)"); }
Comment #2 by timon.gehr — 2012-08-08T02:05:30Z
(In reply to comment #1) > This is not a bug it's a feature, This can lead to memory corruption in @safe code, therefore it is a bug. > there's solution: > This is a workaround.
Comment #3 by clugdbug — 2012-08-15T06:36:20Z
(In reply to comment #1) > This is not a bug it's a feature, No, it's a wrong-code bug. This one has been on my to-do list for years now.
Comment #4 by k.hara.pg — 2015-03-25T12:20:53Z
Comment #5 by github-bugzilla — 2015-03-27T05:07:37Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/38d906b8366a881d19a7f9267b536d372409da31 fix Issue 1759 - Closures and With Statements https://github.com/D-Programming-Language/dmd/commit/a1d857a4f46aa21722712c3014fd40c978ee28c5 Merge pull request #4518 from 9rnsr/fix1759 Issue 1759 - Closures and With Statements
Comment #6 by github-bugzilla — 2015-06-17T21:03:07Z