Fix Issue 16213 - CTFE internal error with static array $ as template argument (#14964)

This commit is contained in:
Razvan Nitu 2023-03-10 14:12:50 +08:00 committed by GitHub
parent 9e3e295d8c
commit 23514c6256
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 4 deletions

View file

@ -2162,10 +2162,23 @@ extern (C++) final class ArrayScopeSymbol : ScopeDsymbol
* or a variable (in which case an expression is created in
* toir.c).
*/
auto e = new VoidInitializer(Loc.initial);
e.type = Type.tsize_t;
v = new VarDeclaration(loc, Type.tsize_t, Id.dollar, e);
v.storage_class |= STC.temp | STC.ctfe; // it's never a true static variable
// https://issues.dlang.org/show_bug.cgi?id=16213
// For static arrays $ is known at compile time,
// so declare it as a manifest constant.
auto tsa = ce.type ? ce.type.isTypeSArray() : null;
if (tsa)
{
auto e = new ExpInitializer(loc, tsa.dim);
v = new VarDeclaration(loc, tsa.dim.type, Id.dollar, e, STC.manifest);
}
else
{
auto e = new VoidInitializer(Loc.initial);
e.type = Type.tsize_t;
v = new VarDeclaration(loc, Type.tsize_t, Id.dollar, e);
v.storage_class |= STC.temp | STC.ctfe; // it's never a true static variable
}
}
*pvar = v;
}

View file

@ -0,0 +1,8 @@
// https://issues.dlang.org/show_bug.cgi?id=16213
enum Id(size_t i) = i;
void main()
{
int[5] y;
y[ Id!($) - 1 ] = 3;
}