Compiler reports an internal error if an address of a temporary struct's method is taken: ---- begin test.d void main() { struct A { void foo() {} static A opCall() {A a; return a;} } void delegate() x = &A().foo; } ---- end test.d >dmd test.d Internal error: ..\ztc\cgcs.c 358 Workaround is to use an explicit stack variable for A(): auto y = A(); void delegate() x = &y.foo; compiles and works correctly.
This bug also applies to D1. And it silently generates bad code if compiled with -O. Reduced test case: ---- struct A { void foo() {} } A bar() {A a; return a;} void main() { void delegate() x = &bar().foo; } --- Root cause: should not be able to make a delegate from something which isn't an lvalue (just as you can't take address of a function return). PATCH: add one line in expression.c, DelegateExp::semantic(Scope *sc) if (func->needThis()) e1 = getRightThis(loc, sc, ad, e1, func); + e1->toLvalue(sc, e1); // add this line }
Sergey is right, and the fix is to create a temp on the stack, copy the struct in, and take the address of that. One line fix in e2ir.c.
Fixed dmd 1.046 and 2.031
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル