Issue 19169 - [betterC] bogus TypeInfo error for `enum string[] a = ["a"]; auto aa = a;`
Summary: [betterC] bogus TypeInfo error for `enum string[] a = ["a"]; auto aa = a;`
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Mac OS X
: P4 enhancement
Assignee: No Owner
URL:
Keywords: betterC
Depends on:
Blocks:
 
Reported: 2018-08-14 13:16 UTC by Nicholas Wilson
Modified: 2023-01-15 04:30 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 Nicholas Wilson 2018-08-14 13:16:24 UTC
extern(C) void main() {
    enum string[] a = ["a"];
    auto aa = a;
}

> onlineapp.d(3): Error: TypeInfo cannot be used with -betterC
Comment 1 Nicholas Wilson 2018-08-14 13:19:12 UTC
Interestingly 

enum a = ["a"];
__gshared auto aa = a;
extern(C) void main() {}

Passes compilation just fine.
Comment 2 Mike Franklin 2018-08-14 13:27:01 UTC
The problem is here:  https://github.com/dlang/dmd/blob/48726c875fbfe16fd93051570d149e893d2dc3dc/src/dmd/e2ir.d#L5047-L5050

---
// call _d_arrayliteralTX(ti, dim)
e = el_bin(OPcall, TYnptr,
    el_var(getRtlsym(RTLSYM_ARRAYLITERALTX)),
    el_param(el_long(TYsize_t, dim), getTypeInfo(ale.loc, ale.type, irs)));
---

The compiler is generating a call to `_d_arrayliteralTX` in druntime: https://github.com/dlang/druntime/blob/9a8edfb48e4842180c706ee26ebd8edb10be53f4/src/rt/lifetime.d#L2279

That runtime hook requires a `TypeInfo` parameter.

One solution would be to replace `_d_arrayliteralTX` with a template and a compile-time type parameter, but this issue does beg the question why it is even calling `_d_arrayliteralTX` in the first place.
Comment 3 Nicholas Wilson 2018-08-14 13:28:27 UTC
At the very least the error message should not suck.
Comment 4 Nicholas Wilson 2018-08-14 13:30:08 UTC
This triggers for ints as well.

extern(C) void main() {
    auto a = [1];
}

same problem.
Comment 5 Nicholas Wilson 2018-08-14 13:31:24 UTC
extern(C) void main() {
    immutable a = [1];
}

works fine though.
Comment 6 Walter Bright 2023-01-15 04:30:57 UTC
(In reply to Nicholas Wilson from comment #0)
> extern(C) void main() {
>     enum string[] a = ["a"];
>     auto aa = a;   // line 3
> }

Now produces the error:

test.d(3): Error: expression `["a"]` uses the GC and cannot be used with switch `-betterC`

which is not bad. If we write this:

  extern(C) void main() {
    enum string[] a = ["a"];
    auto aa = a[0];
  }

it compiles without complaint. As far as I can see, this is expected behavior. The former produces the error because ["a"] has to be allocated somewhere at runtime, but with no GC, where should it be placed?

> At the very least the error message should not suck.

This condition is met, so I shall mark this as fixed.