-
-
Notifications
You must be signed in to change notification settings - Fork 668
unique pointer implicit conversions for + and - #1700
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
Conversation
|
What's a mutable unique pointer? |
|
Design docs and discussion before implementation please. You'd expect no less from a community contribution. |
|
A unique pointer is one for which no copies exist, so it can be safely converted to immutable or shared. Operator new, for example, creates a unique pointer (absent what a constructor might do). This was discussed on the D n.g. a couple months ago. It's not possible to do a test case for this particular one as the other stuff isn't there. |
|
http://www.digitalmars.com/d/archives/digitalmars/D/Immutable_and_unique_in_C_180572.html There are other related discussions on the n.g. if you search the forums for "unique". |
|
I skimmed through the threads and didn't see a clear proposal. I saw a lot of talking and a lot of thoughts. You need to do what we're strongly encouraging everyone else to do, formalize the plan. Let people poke at it, enhance it. If you want to start a branch and play with an implementation then great. Merging pieces into the master branch absent either or both of those is premature and exactly the behavior you get grief from the community about doing. |
|
You should be able to add test cases for this. How do you know it works if you haven't tested? This test case should work (it doesn't) You're checking that And then of this is unnecessary, because you've special cased it to only allow offsetting pointers, where the only thing you need to know is that the original pointer converts to the final type. If it does, no amount of offsetting will break that. This should already be handled, because you're calling
|
|
The test case doesn't work because there's no code to determine that pureMaker returns a unique pointer.
This is incorrect. The expression must be looked at to see if it generates a unique pointer or not. Looking at the type is insufficient. |
|
Yes, Brad, I'll do a dip on it. |
Actually, there is, and it's in
Please read my message again this is not what I said. There is already some support for unique expressions in the compiler (from pull #218), and with a couple of tweaks to your patch I made the testcase I posted work. |
|
You're right, yebblies, I'd missed that. What are the tweaks you made? |
|
@yebblies Your example must not work, because |
|
BTW, I think the pull 218 is incorrect. For example, is not implicitly convertible to immutable. |
|
Other implicit conversions that should be possible under varying circumstances:
The cases are rather complicated and not as simple as is/isnot a unique pointer. I'm unsure what to do about inout. |
|
@9rnsr Yes, I meant to put pure there.
I completely removed the castMod check. Changing it to use invariantOf also works. This is ok for pointer offsetting because we are not changing the type, just adjusting the value, and the conversion is checked later when calling
Pull 218 is correct. The conversion will only be allowed if foo is strongly pure, in which cast |
|
yebblies, please show your exact patch. I tried to generalize my code to more than just casting to immutable. |
|
I can agree that #218 is correct, and recently I have improved the concept in #1519 (not only PUREstrong return, and also PUREconst and PUREweak return may be convertible to immutable implicitly). I call the traits 'isolated return', but based on this pull summary, we can call it "unique return". And then, I also basically agree with @WalterBright that the returned object from But @WalterBright , please implement it carefully. If T has some mutable indirections, |
If Some test cases would help me understand what casts you plan to allow. |
|
Testing by converting to immutable is not sufficient. For example, given: a pointer to S can be converted to shared, but not to immutable. All the different cases of this hurts my brain :-) |
|
@9rnsr, you are correct in that if operator new is constructing an object that has a constructor, the constructor must be regarded like a function call in order to determine if the resulting new'd pointer is unique. The changes I made earlier sidestepped the issue by rejecting new expressions that call constructors. |
|
I made a start on a DIP: http://wiki.dlang.org/DIP29 It's very incomplete. |
I think it can still be converted to immutable if the expression is unique.
Mine too. |
|
This test case should work with the change, but doesn't. template TypeTuple(T...) { alias T TypeTuple; }
void main()
{
static pure T* makeUniqueHead(T)(size_t dim) { return (new T[](dim)).ptr; }
static pure T* makeUniqueLast(T)(size_t dim) { return (new T[](dim)).ptr + dim - 1; }
static struct S1 { int val; }
static struct S2 { int[] arr; }
foreach (T; TypeTuple!(int, S1, S2))
{
immutable T* p0 = makeUniqueHead!T(2); // OK
immutable T* p1 = makeUniqueHead!T(2) + 1; // should be OK, but NG
immutable T* q1 = makeUniqueLast!T(2); // OK
immutable T* q0 = makeUniqueLast!T(2) - 1; // should be OK, but NG
}
} |
|
Replaced by #3319 |
A start to the eventual improvement where unique pointers can be implicitly converted to immutable and shared.