Issue 21482 - dynamic indexing into enum dynamic array at ctfe generates TypeInfo in betterC
Summary: dynamic indexing into enum dynamic array at ctfe generates TypeInfo in betterC
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: betterC
Depends on:
Blocks:
 
Reported: 2020-12-15 01:15 UTC by dave287091
Modified: 2023-04-10 10:04 UTC (History)
2 users (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-15 01:15:23 UTC
Compiling with DMD, with -betterC

DMD64 D Compiler v2.094.2-336-g97aa2ae5e

// works
int f_1(int a){
    enum int[]foo = [1,2,3];
    if(__ctfe)
        return foo[1];
    return 1;
}

// works
int f_2(int a){
    static immutable int[] foo = [1,2,3];
    if(__ctfe)
        return foo[a];
    return 2;
}
// fails with TypeInfo cannot be used with -betterC
int f_3(int a){
    enum int[]foo = [1,2,3];
    if(__ctfe)
        return foo[a]; // Error is reported at this line.
    return 3;
}

All three work with ldc.
Comment 1 dave287091 2020-12-15 01:30:29 UTC
I have discovered that a workaround is as follows:


int f_4(int a){
    enum int[]foo_ = [1,2,3];
    enum int[foo_.length]foo = foo_;
    if(__ctfe)
        return foo[a];
    return 4;
}
Comment 2 Walter Bright 2023-01-15 07:36:28 UTC
The current error message is:

 Error: expression `[1, 2, 3]` uses the GC and cannot be used with switch `-betterC`
Comment 3 RazvanN 2023-04-10 10:04:09 UTC
This seems to have been fixed. this compiles successfully with the latest compiler.