Skip to content

Commit

Permalink
Issue 12652 - Non-constant hash initializers should have a special-ca…
Browse files Browse the repository at this point in the history
…se diagnostic (#11598)

Issue 12652 - Non-constant hash initializers should have a special-case diagnostic

Signed-off-by: Dennis <[email protected]>
Signed-off-by: Razvan Nitu <[email protected]>
Merged-on-behalf-of: Razvan Nitu <[email protected]>
  • Loading branch information
adelavais authored Aug 24, 2022
1 parent 919abac commit 817610b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions compiler/src/dmd/todt.d
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,15 @@ extern (C++) void Expression_toDt(Expression e, ref DtBuilder dtb)
}
}

/* https://issues.dlang.org/show_bug.cgi?id=12652
Non-constant hash initializers should have a special-case diagnostic
*/
void visitAssocArrayLiteral(AssocArrayLiteralExp e)
{
e.error("static initializations of associative arrays is not allowed.");
errorSupplemental(e.loc, "associative arrays must be initialized at runtime: https://dlang.org/spec/hash-map.html#runtime_initialization");
}

void visitStructLiteral(StructLiteralExp sle)
{
//printf("StructLiteralExp.toDt() %s, ctfe = %d\n", sle.toChars(), sle.ownedByCtfe);
Expand Down Expand Up @@ -674,6 +683,7 @@ extern (C++) void Expression_toDt(Expression e, ref DtBuilder dtb)
case EXP.typeid_: return visitTypeid (e.isTypeidExp());
case EXP.assert_: return visitNoreturn (e);
case EXP.slice: return visitSlice (e.isSliceExp());
case EXP.assocArrayLiteral: return visitAssocArrayLiteral(e.isAssocArrayLiteralExp());
}
}

Expand Down
24 changes: 24 additions & 0 deletions compiler/test/fail_compilation/issue12652.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
TEST_OUTPUT:
----
fail_compilation/issue12652.d(18): Error: static initializations of associative arrays is not allowed.
fail_compilation/issue12652.d(18): associative arrays must be initialized at runtime: https://dlang.org/spec/hash-map.html#runtime_initialization
---
*/

enum A
{
x,
y,
z
}

struct S
{
string[A] t = [A.x : "aaa", A.y : "bbb"];
}

void main ()
{
S s;
}

0 comments on commit 817610b

Please sign in to comment.