Bug 21492 – betterC: TypeInfo is generated for code guarded by if(__ctfe)

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-12-19T19:43:53Z
Last change time
2023-01-17T11:48:14Z
Keywords
betterC, CTFE, pull
Assigned to
No Owner
Creator
dave287091
See also
https://issues.dlang.org/show_bug.cgi?id=21482

Comments

Comment #0 by dave287091 — 2020-12-19T19:43:53Z
Example code that demonstrates the issue, compiled with dmd and the -betterC flag DMD64 D Compiler v2.094.2 int bar(){ if(__ctfe){ int[] foo = [1]; // Error: TypeInfo cannot be used with -betterC } return 0; } The same code compiles with ldc without issue. My understanding of the difference between the two compilers is that ldc doesn’t generate code at all for `if`s that have compile-time known constant-expressions with no labels. I don’t know how dmd works, but presumably it is instead relying on the backend optimizing out the dead branch? In other words, the __ctfe is not treated as a special case by ldc. The same result is seen with this code, which compiles with ldc but fails with dmd: int bar(){ if(false){ int[] foo = [1]; } return 0; } I’m posting this as an issue as it means that betterC code that compiles with ldc won’t compile with dmd. betterC is under-specified, so I don’t know if ldc is being overly-permissive or if dmd is improperly detecting betterC violations when it attempts to generate code instead of during semantic analysis. This issue crops up if you want CTFE-able code that calculates something inefficiently at ctfe while it just calls some external function at runtime.
Comment #1 by dave287091 — 2023-01-07T06:45:15Z
The error message has been change to “uses the GC”, but still fails. Dead branches like this should be pruned so that more things will work with betterC.
Comment #2 by bugzilla — 2023-01-15T07:41:55Z
What D does is generate the code, then prune away all the false code blocks. Looks like ldc does it is better.
Comment #3 by dlang-bot — 2023-01-16T21:37:51Z
@WalterBright created dlang/dmd pull request #14830 "fix Issue 21492 - betterC: TypeInfo is generated for code guarded by …" fixing this issue: - fix Issue 21492 - betterC: TypeInfo is generated for code guarded by if(__ctfe) https://github.com/dlang/dmd/pull/14830
Comment #4 by dlang-bot — 2023-01-17T11:48:14Z
dlang/dmd pull request #14830 "fix Issue 21492 - betterC: TypeInfo is generated for code guarded by …" was merged into master: - f90b298ca578c00104b0121ea2ecfacdd76f9827 by Walter Bright: fix Issue 21492 - betterC: TypeInfo is generated for code guarded by if(__ctfe) https://github.com/dlang/dmd/pull/14830