Fix #21052 - Missing @nogc check for enum variable initialization (#21053)

This commit is contained in:
Dennis 2025-03-22 00:40:34 +01:00 committed by GitHub
parent 649223bff5
commit dce0ebac9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 1 deletions

View file

@ -119,7 +119,7 @@ public:
override void visit(ArrayLiteralExp e)
{
if (e.type.ty != Tarray || !e.elements || !e.elements.length || e.onstack)
if (e.type.toBasetype().isTypeSArray() || !e.elements || !e.elements.length || e.onstack)
return;
if (setGC(e, "this array literal"))
return;

View file

@ -94,3 +94,20 @@ int[] bar13702(bool b) @nogc
auto aux = 1 ~ [2]; // error
return aux;
}
/********** Enum and pointer types ***************/
// https://github.com/dlang/dmd/issues/21052
/*
TEST_OUTPUT:
---
fail_compilation/nogc3.d(111): Error: this array literal causes a GC allocation in `@nogc` function `f`
fail_compilation/nogc3.d(112): Error: this array literal causes a GC allocation in `@nogc` function `f`
---
*/
void f() @nogc
{
enum DA : int[] { a = [1,2,3] }
DA da = DA.a;
int i = *cast(int*)cast(char[4])['0', '0', '0', '0'];
}