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