-
-
Notifications
You must be signed in to change notification settings - Fork 420
Fix issue 11725: AA.dup should return mutable AA where possible. #2838
Conversation
Since the original AA has been duplicated, it should be safe to mutate the copy. No need to needlessly restrict the copy to be const too.
Thanks for your pull request, @quickfur! Bugzilla references
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub fetch digger
dub run digger -- build "master + druntime#2838" |
Hmm. Running into trouble with Help, anybody? |
// bug 11725: return mutable AA if possible | ||
import core.internal.traits : Unconst; | ||
static if (is(V : Unconst!V) && is(K : Unconst!K)) | ||
alias Result = Unconst!V[Unconst!K]; |
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 should probably be done for K
and V
independently.
Furthermore this should handle indirections by only stripping head const, e.g. const int*
should be replaced with const(int)*
. This also raises the question on how to handle structs with indirections:
struct S
{
int* ptr;
}
void foo(const S[int] aa)
{
auto copy = aa.dup();
*(copy[0].ptr) = 2; // Can write const data
}
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.
Technically, keys should always be const. In fact, D makes sure of that for indirections:
int[char[]] aa;
pragma(msg, typeof(aa)); // int[const(char)[]]
And the is(V : Unconst!V)
should take care of the structs with indirection cases.
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.
You're right, Unconst was more elegant than I remembered.
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.
Well, Unconst
could be dumb or poorly implemented :) It's the is
expression that makes the difference.
Isn't it the purity? If you accept an immutable item, and return a mutable item in a pure function, it's assumed unique. Note that you have to accept a const parameter for this to work. So in the case of an AA with mutable values, you would have to accept it as a const parameter explicitly. This is why there are 2 overloads of T[].dup. |
Does anyone want to take this PR up? I've been busy and haven't been able to work on this. Would be nice if somebody took over who better understand how this all works. :-D |
Sorry, also busy. Should be pretty quick though, I may do it next week if I find some time. |
Druntime have been merged into DMD. Please re-submit your PR to |
Since the original AA has been duplicated, it should be safe to mutate the copy. No need to needlessly restrict the copy to be const too.