Fix Issue 11006 - Subtraction of pointers for void and non-void type compiles#7332
Fix Issue 11006 - Subtraction of pointers for void and non-void type compiles#7332dlang-bot merged 6 commits intodlang:masterfrom JinShil:fix_11006
Conversation
|
Thanks for your pull request, @JinShil! We are looking forward to reviewing it, and you should be hearing from a maintainer soon. Some tips to help speed things up:
Bear in mind that large or tricky changes may require multiple rounds of review and revision. Please see CONTRIBUTING.md for more information. Bugzilla references
|
|
This PR may have revealed the following problem: Deprecation messages occur in Perhaps See output of auto-tester for more details. |
|
@andralex @wilzbach @ZombineDev As code owners of |
|
@JinShil thanks for pointing that out. The culprit seems to be the snippet: auto start = _control.rep.ptr, end = _payload.ptr + _payload.length;
parent.deallocate(start[0 .. end - start]);The void* start = _control.rep.ptr, end = _payload.ptr + _payload.length;
parent.deallocate(start[0 .. end - start]);To my own surprise I managed to create dlang/phobos#5864 just by using the web interface - nice job github. |
|
As an aside, the code was actually incorrect. It was equivalent to: ulong* start = _control.rep.ptr;
void* end = _payload.ptr + _payload.length;
parent.deallocate(start[0 .. end - start]);The computation |
|
From my perspective, this is ready to go. |
PetarKirov
left a comment
There was a problem hiding this comment.
LGTM, modulo one question about how strict we want to be.
src/ddmd/expressionsem.d
Outdated
| if (!p1.equivalent(p2)) | ||
| { | ||
| // Types don't match unless the have the same size | ||
| if (p1.size() != p2.size()) |
There was a problem hiding this comment.
According to spec, subtracting pointers pointing to objects of different types (regardless of size) is an error. In the case where we have pointers to different types, but with equal sizes, the result obviously would be the same if you cast one of the pointers to the same type as the other one, but such code may be actually be a mistake on the user's part. What are your opinions on being more strict, as the spec dictates?
(I just saw that @LemonBoy is also in favor of the more strict approach #6440 (comment).)
There was a problem hiding this comment.
Yeah, I'm for following the spec. I'll update the PR.
There was a problem hiding this comment.
My understanding is the Type.equivalent() function is all that's needed. I think the size comparison in the previous commit was unnecessary.
Again, I think this is ready to go unless I'm missing something.
|
Is |
|
@andralex it is nice that this PR found a bug. |
| if (t2.ty == Tpointer) | ||
| { | ||
| // https://issues.dlang.org/show_bug.cgi?id=11006 | ||
| Type p1 = t1.nextOf(); |
There was a problem hiding this comment.
Not much point here to linking to the bug report. Instead link to the relevant spec
https://dlang.org/spec/expression.html#add_expressions and add the quote: "If both operands are pointers, and the operator is -, the pointers are subtracted and the result is divided by the size of the type pointed to by the operands. It is an error if the pointers point to different types."
Linking to and quoting from the spec should be a good way to clarify what's going on in the implementation.
Revival of #6440