Issue 21492 - betterC: TypeInfo is generated for code guarded by if(__ctfe)
Summary: betterC: TypeInfo is generated for code guarded by if(__ctfe)
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: betterC, CTFE, pull
Depends on:
Blocks:
 
Reported: 2020-12-19 19:43 UTC by dave287091
Modified: 2023-01-17 11:48 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description dave287091 2020-12-19 19:43:53 UTC
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 dave287091 2023-01-07 06:45:15 UTC
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 Walter Bright 2023-01-15 07:41:55 UTC
What D does is generate the code, then prune away all the false code blocks. Looks like ldc does it is better.
Comment 3 Dlang Bot 2023-01-16 21:37:51 UTC
@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 Dlang Bot 2023-01-17 11:48:14 UTC
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