← Back to index
|
Original Bugzilla link
Bug 23416 – Difference DMD/GDC: Error: non-constant expression `& foo`
Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2022-10-15T00:18:02Z
Last change time
2024-12-13T19:25:02Z
Keywords
wrong-code
Assigned to
No Owner
Creator
kdevel
Moved to GitHub: dmd#20165 →
Comments
Comment #0
by kdevel — 2022-10-15T00:18:02Z
``` module dmd_gdc_difference; template funcptr (alias method) { immutable funcptr = &method; } struct S { void function () fun; void foo () { fun = funcptr!foo; } } unittest { S m; } ``` $ dmd -unittest -main dmd-gdc-difference.d dmd-gdc-difference.d(4): Error: non-constant expression `& foo` $ gdc -funittest -fmain dmd-gdc-difference.d -o dmd-gdc-difference $ ./dmd-gdc-difference 1 modules passed unittests Versions checked: GDC 12.1, 11.3; DMD v2.100.2, 2.080.0 Found in:
https://forum.dlang.org/post/
[email protected]
(Generate a pointer to a method of a struct)
Comment #1
by ibuclaw — 2022-10-15T00:33:30Z
LDC also errors: dmd-gdc-difference.d(4): Error: need `this` to access `foo` Compiler returned: 1
Comment #2
by kdevel — 2022-10-15T00:47:20Z
This code should compile and pass its unittest: ```post2.d import std.stdio; struct S { void function () fp = &foo; void foo () { __PRETTY_FUNCTION__.writeln; fp = (&bar).funcptr; } void bar () { __PRETTY_FUNCTION__.writeln; fp = (&foo).funcptr; } auto fun () // invocation helper { void delegate () dg; dg.ptr = &this; dg.funcptr = fp; return dg (); } } unittest { S s; s.fun; s.fun; } ``` $ dmd -O -unittest -main -run post2 post2.d(3): Error: non-constant expression `& foo` But this code should compile and pass: $ gdc -O -funittest -fmain post2.d -o post2 && ./post2 void post2.S.foo() void post2.S.bar() 1 modules passed unittests
Comment #3
by robert.schadek — 2024-12-13T19:25:02Z
THIS ISSUE HAS BEEN MOVED TO GITHUB
https://github.com/dlang/dmd/issues/20165
DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB