Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix 21756 - make cast(immutable)arrayLiteral @nogc #12329

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/dmd/expressionsem.d
Expand Up @@ -7331,6 +7331,29 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor

if (!exp.to) // Handle cast(const) and cast(immutable), etc.
{

// turns `cast(immutable)[<elems...>]` into a static decl, @nogc compatible
ArrayLiteralExp ale = exp.e1.isArrayLiteralExp();
if (exp.mod == MODFlags.immutable_ && ale && sc.func)
{
Identifier id = Identifier.generateId("__arr");
ExpInitializer ei = new ExpInitializer(exp.e1.loc, exp.e1);
VarDeclaration vd = new VarDeclaration(exp.e1.loc, exp.e1.type, id, ei, STC.immutable_ | STC.gshared );
VarExp ve = new VarExp(exp.e1.loc, vd);

ScopeDsymbol sds = sc.scopesym.toParentDecl().isScopeDsymbol();
assert(sds);
if (!sds.symtab)
sds.symtab = new DsymbolTable();
vd.addMember(sc, sds);

DeclarationExp de = new DeclarationExp(exp.e1.loc, vd);
CommaExp ce = new CommaExp(exp.e1.loc, de, ve);

result = expressionSemantic(ce, sc);
return;
}

exp.to = exp.e1.type.castMod(exp.mod);
exp.to = exp.to.typeSemantic(exp.loc, sc);

Expand Down
28 changes: 28 additions & 0 deletions test/runnable/issue21756.d
@@ -0,0 +1,28 @@
module issue21756;

auto test1() @nogc
{
return cast(immutable)['a', 'b', 'c'];
}

auto test2() @nogc
{
return cast(immutable)[1, 2, 3];
}

struct S
{
int a,b,c;
}

auto test3() @nogc
{
return cast(immutable)[S(1,2,3), S(1,2,3)];
}

void main() @nogc
{
assert(test1() == "abc");
assert(test2()[2] == 3);
assert(test3()[0] == test3()[1]);
}