2323 – ICE(cgcs.c): taking address of a method of a temporary struct

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2323 - ICE(cgcs.c): taking address of a method of a temporary struct
Summary: ICE(cgcs.c): taking address of a method of a temporary struct
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Windows
: P2 normal
Assignee: Walter Bright
URL:
Keywords: ice-on-valid-code, patch, wrong-code
Depends on:
Blocks:
Reported: 2008年08月30日 11:48 UTC by Sergey Gromov
Modified: 2014年03月01日 00:37 UTC (History)
1 user (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Sergey Gromov 2008年08月30日 11:48:43 UTC
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.
Comment 1 Don 2009年05月27日 05:43:58 UTC
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
 }
Comment 2 Walter Bright 2009年06月22日 22:27:23 UTC
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.
Comment 3 Walter Bright 2009年07月09日 02:46:38 UTC
Fixed dmd 1.046 and 2.031


AltStyle によって変換されたページ (->オリジナル) /