From 8a96c4745c15e9fce3747699320d898cbf49b853 Mon Sep 17 00:00:00 2001 From: Dennis Date: Sat, 22 Mar 2025 09:26:49 +0100 Subject: [PATCH] Fix #21054 - No location for array literal sliced from init symbol (#21055) --- compiler/src/dmd/mtype.d | 2 +- compiler/test/fail_compilation/nogc3.d | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/compiler/src/dmd/mtype.d b/compiler/src/dmd/mtype.d index 5ba866c9e5..52d8b77cd4 100644 --- a/compiler/src/dmd/mtype.d +++ b/compiler/src/dmd/mtype.d @@ -2234,7 +2234,7 @@ extern (C++) final class TypeSArray : TypeArray auto elements = new Expressions(d); foreach (ref e; *elements) e = null; - auto ae = new ArrayLiteralExp(Loc.initial, this, elementinit, elements); + auto ae = new ArrayLiteralExp(loc, this, elementinit, elements); return ae; } diff --git a/compiler/test/fail_compilation/nogc3.d b/compiler/test/fail_compilation/nogc3.d index e7186c72bd..f1a469b544 100644 --- a/compiler/test/fail_compilation/nogc3.d +++ b/compiler/test/fail_compilation/nogc3.d @@ -111,3 +111,16 @@ void f() @nogc DA da = DA.a; int i = *cast(int*)cast(char[4])['0', '0', '0', '0']; } + +/* +TEST_OUTPUT: +--- +fail_compilation/nogc3.d(125): Error: this array literal causes a GC allocation in `@nogc` function `g` +--- +*/ + +// https://github.com/dlang/dmd/issues/21054 +void g() @nogc +{ + int[] x = (int[2]).init[]; +}