December 19, 2020
https://issues.dlang.org/show_bug.cgi?id=21492

          Issue ID: 21492
           Summary: betterC: TypeInfo is generated for code guarded by
                    if(__ctfe)
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: betterC, CTFE
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: dave287091@gmail.com

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.

--