Bug 22651 – undefined reference to ModuleInfo when using imported

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-01-04T17:27:57Z
Last change time
2022-11-16T09:05:14Z
Keywords
patch
Assigned to
No Owner
Creator
Tim
See also
https://issues.dlang.org/show_bug.cgi?id=17181

Attachments

IDFilenameSummaryContent-TypeSize
1850modulefix.patchfixtext/plain995

Comments

Comment #0 by tim.dlang — 2022-01-04T17:27:57Z
Compiling the following files with dmd nightly results in a linker error: ///////////////////// a.d ///////////////////// import c; import std; imported!q{b}.C obj; void main() { Appender!string appender; std.file.write("test.txt", appender.data); } ///////////////////// b.d ///////////////////// import c; class C { } ///////////////////// c.d ///////////////////// import std; /////////////////////////////////////////////// Compiling it with dmd a.d b.d c.d produces the following output: /usr/bin/ld: a.o:(.data.rel.ro+0x50): undefined reference to `_D3std8internal6memory12__ModuleInfoZ' collect2: error: ld returned 1 exit status Error: linker exited with status 1 The error goes away if a custom definition of imported is used. dmd nightly has to be used, because imported is not available with dmd 2.098.1.
Comment #1 by duser — 2022-04-28T05:38:17Z
Created attachment 1850 fix
Comment #2 by duser — 2022-04-28T05:40:48Z
smaller reproducer: /// common.d template myimported() { import myctor; } /// myctor.d shared static this(){} /// empty.d import common; /// main.d import common; alias x = myimported!(); import empty; void main(){} compile: "dmd main.d common.d myctor.d", gives an undefined symbol error for _D5empty12__ModuleInfoZ i'm not 1000% familiar with this stuff but i think what happens in the original example is 1. using imported!"", a module is imported that has a module constructor - this causes the "needmoduleinfo" flag to be set for a bunch of modules including "object" (it's not normally set for object) 2. a module is imported that doesn't have a ModuleInfo symbol and isn't included in compilation - it inherits the needmoduleinfo flag from object (which it implicitly imports) and linking fails because there's no ModuleInfo symbol for it the thing the patch does to fix this is copied from here: https://github.com/dlang/dmd/blob/375ed51/src/dmd/dsymbolsem.d#L1283-L1292 - it causes "needmoduleinfo" to be set for the module that uses imported!"" instead of the module that defines it the link error in the original example is for std.internal.memory which indeed doesn't have a ModuleInfo symbol inside libphobos2.so
Comment #3 by razvan.nitu1305 — 2022-11-16T09:05:14Z
I cannot reproduce this with the latest version of the compiler: neither the initial test case, nor the reduction.