Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions changelog/new-aa.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
`new` can now allocate an associative array

This allows two associative array references to point to the same
associative array instance before any keys have been inserted.

---
int[string] a = new int[string];
auto b = a;
...
a["seven"] = 7;
assert(b["seven"] == 7);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a good addition to the runnable test

---

Note: Calling `new` is not needed before inserting keys on a null
associative array reference - the instance will be allocated if it
doesn't exist.
10 changes: 10 additions & 0 deletions compiler/src/dmd/e2ir.d
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,16 @@ elem* toElem(Expression e, IRState *irs)
}
e = el_combine(ezprefix, e);
}
else if (auto taa = t.isTypeAArray())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why Codecov says this isn't covered, it has to be or else the runnable test wouldn't work right?

{
Symbol *s = aaGetSymbol(taa, "New", 0);
elem *ti = getTypeInfo(ne, t, irs);
// aaNew(ti)
elem *ep = el_params(ti, null);
e = el_bin(OPcall, TYnptr, el_var(s), ep);
elem_setLoc(e, ne.loc);
return e;
}
else
{
ne.error("internal compiler error: cannot new type `%s`\n", t.toChars());
Expand Down
35 changes: 21 additions & 14 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -3463,8 +3463,17 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
if (!exp.arguments && exp.newtype.isTypeSArray())
{
auto ts = exp.newtype.isTypeSArray();
edim = ts.dim;
exp.newtype = ts.next;
// check `new Value[Key]`
ts.dim = ts.dim.expressionSemantic(sc);
if (ts.dim.op == EXP.type)
{
exp.newtype = new TypeAArray(ts.next, ts.dim.isTypeExp().type);
}
else
{
edim = ts.dim;
exp.newtype = ts.next;
}
}

ClassDeclaration cdthis = null;
Expand Down Expand Up @@ -3518,18 +3527,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
{
return setError();
}
//https://issues.dlang.org/show_bug.cgi?id=20547
//exp.arguments are the "parameters" to [], not to a real function
//so the errors that come from preFunctionParameters are misleading
if (originalNewtype.ty == Tsarray)
{
if (preFunctionParameters(sc, exp.arguments, false))
{
exp.error("cannot create a `%s` with `new`", originalNewtype.toChars());
return setError();
}
}
else if (preFunctionParameters(sc, exp.arguments))
if (preFunctionParameters(sc, exp.arguments))
{
return setError();
}
Expand Down Expand Up @@ -3885,6 +3883,15 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor

exp.type = exp.type.pointerTo();
}
else if (tb.ty == Taarray)
{
// e.g. `new Alias(args)`
if (nargs)
{
exp.error("`new` cannot take arguments for an associative array");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for this

return setError();
}
}
else
{
exp.error("cannot create a `%s` with `new`", exp.type.toChars());
Expand Down
9 changes: 3 additions & 6 deletions compiler/src/dmd/parse.d
Original file line number Diff line number Diff line change
Expand Up @@ -9312,13 +9312,10 @@ LagainStc:
{
AST.TypeAArray taa = cast(AST.TypeAArray)t;
AST.Type index = taa.index;
// `new Type[expr]` is a static array
auto edim = AST.typeToExpression(index);
if (!edim)
{
error("cannot create a `%s` with `new`", t.toChars);
return new AST.NullExp(loc);
}
t = new AST.TypeSArray(taa.next, edim);
if (edim)
t = new AST.TypeSArray(taa.next, edim);
}
else if (token.value == TOK.leftParenthesis && t.ty != Tsarray)
{
Expand Down
15 changes: 0 additions & 15 deletions compiler/test/fail_compilation/fail20547.d

This file was deleted.

3 changes: 1 addition & 2 deletions compiler/test/fail_compilation/misc_parser_err_cov1.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ fail_compilation/misc_parser_err_cov1.d(31): Error: expression expected, not `)`
fail_compilation/misc_parser_err_cov1.d(32): Error: `type identifier : specialization` expected following `is`
fail_compilation/misc_parser_err_cov1.d(33): Error: semicolon expected following auto declaration, not `auto`
fail_compilation/misc_parser_err_cov1.d(33): Error: found `+` when expecting `(` following `mixin`
fail_compilation/misc_parser_err_cov1.d(34): Error: cannot create a `char[float]` with `new`
fail_compilation/misc_parser_err_cov1.d(35): Error: `key:value` expected for associative array literal
fail_compilation/misc_parser_err_cov1.d(36): Error: basic type expected, not `;`
fail_compilation/misc_parser_err_cov1.d(36): Error: `{ members }` expected for anonymous class
Expand Down Expand Up @@ -44,7 +43,7 @@ void main()
auto tt = __traits(<o<);
auto b = is ;
auto mx1 = mixin +);
auto aa1 = new char[float];

aa += [key:value, key];
auto anon1 = new class;
auto anon2 = new class {};
Expand Down
19 changes: 19 additions & 0 deletions compiler/test/fail_compilation/newaa.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
TEST_OUTPUT:
---
fail_compilation/newaa.d(14): Error: cannot implicitly convert expression `new string[string]` of type `string[string]` to `int[string]`
fail_compilation/newaa.d(15): Error: function expected before `()`, not `new int[int]` of type `int[int]`
fail_compilation/newaa.d(17): Error: `new` cannot take arguments for an associative array
---
*/
#line 9
void main()
{
//https://issues.dlang.org/show_bug.cgi?id=11790
string[string] crash = new string[string];
//https://issues.dlang.org/show_bug.cgi?id=20547
int[string] c = new typeof(crash);
auto d = new int[int](5);
alias AA = char[string];
auto e = new AA(5);
}
23 changes: 23 additions & 0 deletions test/runnable/newaa.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
void main()
{
alias AA = int[string];
// aa is not ref
static void test(AA aa)
{
aa[""] = 0;
}
auto aa = new AA();
auto ab = new int[string];
auto ac = new typeof(aa);
test(aa);
test(ab);
test(ac);
assert(aa.length);
assert(ab.length);
assert(ac.length);

int[string] a = new int[string];
auto b = a;
a["seven"] = 7;
assert(b["seven"] == 7);
}