Bug 17605 – [REG2.066.0] __traits(compiles, closure) adds link-time reference to _d_allocmemory

Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-07-05T17:11:50Z
Last change time
2018-02-17T10:15:40Z
Keywords
betterC
Assigned to
No Owner
Creator
ZombineDev

Comments

Comment #0 by petar.p.kirov — 2017-07-05T17:11:50Z
While playing with '-betterC', and investigating which parts of the language are usable without the compiler emitting link-time references to druntime, I noticed that the following code: --- extern (C) void main() { int a; enum bool works = __traits(compiles, { a = 1; }); a = 1; } --- Produces an undefined reference to _d_allocmemory from druntime: $ dmd --version DMD64 D Compiler v2.074.1-master-de3cb1a (This is dmd nightly from 2017-07-05.) $ dmd -c -betterC test.d && nm test.o 0000000000000000 t U _d_allocmemory U _GLOBAL_OFFSET_TABLE_ 0000000000000000 W main While the following does not: --- extern (C) void main() { int a; a = 1; } --- --- // Also ok, as long as no closure-allocating lambdas // are used extern (C) void main() { int a; enum bool works = __traits(compiles, a = 1); pragma (msg, works); a = 1; } --- $ dmd -c -betterC test.d && nm test.o 0000000000000000 t U _GLOBAL_OFFSET_TABLE_ 0000000000000000 W main The result is also reproducible on Windows: > dmd --version DMD32 D Compiler v2.075.0-b1 Copyright (c) 1999-2017 by Digital Mars written by Walter Bright > dmd -c -betterC -m32mscoff test.d $ nm test.obj 0000000000000000 b .bss$B U _d_allocmemory 0000000000000000 d .data$B 0000000000000000 N .debug$S 0000000000000000 i .drectve 0000000000000000 T main 0000000000000000 p .pdata 0000000000000000 p $pdata$main 0000000000000000 r .rdata 0000000000000000 t .text 0000000000000000 t .text 0000000000000000 r $unwind$main 0000000000000000 r .xdata
Comment #1 by destructionator — 2017-07-06T15:09:31Z
I think this is a general case of CT-only functions in use still ending up in the object file simply because they were defined in the code...
Comment #2 by petar.p.kirov — 2017-07-06T15:22:32Z
Agreed. From my superficial understanding of dmd's internals, I think the culprit is around `needsCodegen()`. AFAIR, changes to that code often led to regressions and undefined symbol references and in the past 3-4 years it was relatively often tweaked, though I think it was stable / unchanged over the last year or so. I don't remember if Walter and Kenji agreed that they have reached the perfect design, so that no further changes to the general emission strategy were necessary, but judging from these bug reports, there's still work to be done. See also: https://github.com/ldc-developers/ldc/issues/2168#issuecomment-312494490 and https://issues.dlang.org/show_bug.cgi?id=17577.
Comment #3 by dlang-bugzilla — 2017-07-07T01:10:44Z
It looks like this was temporarily fixed in https://github.com/dlang/dmd/pull/2845 and then broken again shortly after in https://github.com/dlang/dmd/pull/3625. I suppose that makes this a regression.
Comment #4 by bugzilla — 2018-02-15T08:24:46Z
Comment #5 by github-bugzilla — 2018-02-17T10:15:39Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/1c6169c719c3dba0664e315b42a481d013b14530 fix Issue 17605 - [REG2.066.0] __traits(compiles, closure) adds link-time reference to _d_allocmemory https://github.com/dlang/dmd/commit/2568f3dae7e5ab4631adc0aaf1e3d27358a3eb74 Merge pull request #7897 from WalterBright/fix17605 fix Issue 17605 - [REG2.066.0] __traits(compiles, closure) adds link-…