Bug 15332 – ICE in e2ir.c: assert(irs->sthis) in visit(ThisExp), function literal with keyword 'function' calls method

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-11-14T01:16:00Z
Last change time
2016-02-02T03:02:17Z
Keywords
ice, pull
Assigned to
nobody
Creator
eiderdaus

Comments

Comment #0 by eiderdaus — 2015-11-14T01:16:03Z
Reduced test case: class MyClass { int myMethod() { return 5; } this() { int a = function() { return myMethod; }(); } } On compiling this with dmd 64-bit, v2.069.0, I get this internal compiler error: dmd: e2ir.c:1209: virtual void toElem(Expression*, IRState*)::ToElemVisitor::visit(ThisExp*): Assertion `irs->sthis' failed. Aborted The code sample compiles with no problems when I remove the keyword 'function', turning the 4th line into: int a = () { return myMethod; }(); A different way of getting the code sample to compile is to make myMethod a non-method function outside of the class, or to remove myMethod altogether and replace the call to myMethod by the literal 5: int a = function() { return 5; }(); -- Simon
Comment #1 by eiderdaus — 2015-11-14T01:59:23Z
Speculation: 'function' doesn't allow to refer to information outside of the literal's body (here, the 'this' pointer), unlike 'delegate'. Removing the keyword makes my literal a delegate, which becomes legal D. dmd doesn't notice the illegal referral to outside information at first, but eventually ends up with 'this' being a null pointer, failing the assert in e2ir.c.
Comment #2 by k.hara.pg — 2015-11-15T13:04:09Z
Comment #3 by github-bugzilla — 2016-02-02T03:02:14Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/a81cdc4b1a7e288fbcab7c13cb0370356bde9385 fix Issue 15332 - ICE in e2ir.c: assert(irs->sthis) in visit(ThisExp), function literal with keyword 'function' calls method Set `FuncLiteralDeclaration.tok` to `TOKdelegate` if its `isNested()` actually returns `true`. This is not complete, temporary disable `compilable/test14973.d` case. https://github.com/D-Programming-Language/dmd/commit/76c6f8c2bebd4f285d20aeb887890e47eb9fb51d Merge pull request #5271 from 9rnsr/fix15332 Issue 15332 - ICE in e2ir.c: assert(irs->sthis) in visit(ThisExp), function literal with keyword 'function' calls method