Bug 24282 – Error in the code is not detected by the compiler and only fails at the linking stage

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2023-12-15T06:32:09Z
Last change time
2024-01-05T14:09:46Z
Assigned to
No Owner
Creator
Siarhei Siamashka

Comments

Comment #0 by siarhei.siamashka — 2023-12-15T06:32:09Z
Testcase: import std.stdio, std.algorithm; int dummyhash(string s) { return 0; } void main() { auto input = ["a", "b"]; input.map!(s => s.map!dummyhash).writeln; } The code is incorrect (the "map!" part shouldn't be there), but the compiler can't see this and only the linker fails: $ dmd-2.106.0 testcase.d /usr/lib/gcc/x86_64-pc-linux-gnu/13/../../../../x86_64-pc-linux-gnu/bin/ld: testcase.o: in function `_D3std9algorithm9iteration__T3mapS_D8testcase9dummyhashFAyaZiZ__TQBkTQnZQBrFNaNbNiNfQBcZSQDjQDiQDb__T9MapResultS_DQDaQCuQCnTQCqZQBb': testcase.d:(.text._D3std9algorithm9iteration__T3mapS_D8testcase9dummyhashFAyaZiZ__TQBkTQnZQBrFNaNbNiNfQBcZSQDjQDiQDb__T9MapResultS_DQDaQCuQCnTQCqZQBb[_D3std9algorithm9iteration__T3mapS_D8testcase9dummyhashFAyaZiZ__TQBkTQnZQBrFNaNbNiNfQBcZSQDjQDiQDb__T9MapResultS_DQDaQCuQCnTQCqZQBb]+0x30): undefined reference to `_D3std9algorithm9iteration__T9MapResultS_D8testcase9dummyhashFAyaZiTQgZQBq6__ctorMFNaNbNcNiNfQBfZSQDsQDrQDk__TQDdS_DQCwQCqQCjTQCmZQDx' collect2: error: ld returned 1 exit status Error: linker exited with status 1 Also it's interesting that this triggers an ICE for GDC with the old frontend: $ gdc-11.3.0 testcase.d d21: internal compiler error: in ErrorStatement, at d/dmd/statement.c:290 Please submit a full bug report, with preprocessed source if appropriate. See <https://bugs.gentoo.org/> for instructions.
Comment #1 by dkorpel — 2023-12-15T10:27:49Z
Reduced to remove imports: ```D template map(fun...) { auto map(Range)(Range r) { alias RE = ElementType!Range; alias _fun = unaryFun!fun; assert(!is(typeof(_fun(RE.init)) )); MapResult!(_fun, Range)(r); } } struct MapResult(alias fun, Range) { Range input; this(Range range) { } void front() { fun(input.front); } } template unaryFun(alias fun) { alias unaryFun = fun; } template ElementType(R) { static if (is(typeof(R.init.front) T)) alias ElementType = T; } string front(string[] s) { return ""; } int dummyhash(string s) { return 0; } void main() { auto input = ["a", "b"]; input.map!(s => s.map!dummyhash); } ```
Comment #2 by razvan.nitu1305 — 2024-01-05T14:09:46Z
*** This issue has been marked as a duplicate of issue 15459 ***