www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17605] New: __traits(compiles, closure) adds link-time

https://issues.dlang.org/show_bug.cgi?id=17605

          Issue ID: 17605
           Summary: __traits(compiles, closure) adds link-time reference
                    to _d_allocmemory
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: petar.p.kirov gmail.com

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 --
Jul 05