-
-
Notifications
You must be signed in to change notification settings - Fork 707
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b338603
Make `new` on an associative array allocate the AA Impl
ntrel 14bea2e
Add test
ntrel ba394c8
Update getTypeInfo call
ntrel e8cdd13
Fix 2 fail_compilation tests
ntrel a3ca165
Add changelog
ntrel 7ac9c77
detab, make nested fn static
ntrel 27509b8
test 2 refs pointing to same new AA
ntrel 1390a3c
Test `new AA(arg)` fails
ntrel 8498f4d
Rename fail test
ntrel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| void main() | ||
| { | ||
| alias AA = int[string]; | ||
|
thewilsonator marked this conversation as resolved.
Outdated
|
||
| // aa is not ref | ||
| 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); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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