-
-
Notifications
You must be signed in to change notification settings - Fork 703
Make new AA allocate the associative array Impl
#14257
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
Changes from all commits
b338603
14bea2e
ba394c8
e8cdd13
a3ca165
7ac9c77
27509b8
1390a3c
8498f4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| --- | ||
|
|
||
| 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. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1301,6 +1301,16 @@ elem* toElem(Expression e, IRState *irs) | |
| } | ||
| e = el_combine(ezprefix, e); | ||
| } | ||
| else if (auto taa = t.isTypeAArray()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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(); | ||
| } | ||
|
|
@@ -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"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
|
|
||
This file was deleted.
| 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); | ||
| } |
| 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); | ||
| } |
There was a problem hiding this comment.
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