Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upImplement raw fat pointer comparisons #1135
Conversation
petrochenkov
reviewed
May 27, 2015
| type like a fat raw pointer should implement equality in some way. | ||
|
|
||
| However, there doesn't seem to be a sensible way to order raw fat pointers | ||
| unless we take vtable addresses into account, which is relatively weird. |
This comment has been minimized.
This comment has been minimized.
petrochenkov
May 27, 2015
Contributor
PartialOrd makes much more sense for raw slices *const [T] than for trait pointers, and the ordering is quite clear - (begin1, len1) < (begin2, len2).
(Speaking of weirdness, ordering for unrelated thin pointers doesn't make much sense too (C++, for example, doesn't even define it), but it exists just because having some ordering is useful.)
arielb1
referenced this pull request
May 27, 2015
Merged
Prevent comparison and dereferencing of raw pointers in constexprs #25840
nrc
self-assigned this
May 27, 2015
nrc
added
the
T-lang
label
May 27, 2015
This comment has been minimized.
This comment has been minimized.
|
It would be good to either make it more obvious this is specific to fat pointers, or explain that equality and ordering already exist for thin pointers (of the same type at least). The way it's current written made me think that you were suggesting removing ordering from all raw pointers. |
nrc
reviewed
May 28, 2015
| raw pointers and all fat raw pointers. I don't like this because equality | ||
| between fat raw pointers of different traits is false most of the | ||
| time (unless one of the traits is a supertrait of the other and/or the | ||
| only difference is in free lifetimes), and anyway you can always compare |
This comment has been minimized.
This comment has been minimized.
nrc
May 28, 2015
Member
Depending on your version of equality there are other ways this can be true. If you ignore the vtables, and only compare the pointers (which is how I would define fat-ptr equality) then you can have two fat pointers of unrelated types which are equal:
let x = &T;
let a: &Tr1 = x;
let b: &Tr2 = x;
a == b
As long as T implements both Tr1 and Tr2, then the two traits do not have to be related in any way.
We haven't yet implemented casting from &Tr1 to &Tr1 + Tr2, so any cast would have to be to *u8 or something.
nrc
changed the title
Implement raw pointer comparisons
Implement raw fat pointer comparisons
May 28, 2015
This comment has been minimized.
This comment has been minimized.
|
I think there is a deep question here which is, should we be able to compare pointers which point to different types. If we think yes, then we should allow heterogeneous fat ptr comparison. If not, then we should not allow any fat ptr comparison, since the 'real' type of a fat pointer is not known, so we might be comparing pointers to different types. I think the answer should be 'yes' for raw pointers, I don't see much of a downside. These are comparisons of the pointer, not the value pointed to, so they are clearly comparable things. I think we can afford to be a little less 'safe' here since we are only talking about raw pointers. I can imagine we might not allow any comparison (since we don't allow arithmetic) but if we are going to allow comparison I don't see why there should be a type restriction. The other point is whether we should involve the vtables (or array lengths) in the comparison. I think we should not do this. The organisation of the vtable is an implementation detail that we may change, so it should not be exposed in any way to the programmer. |
This comment has been minimized.
This comment has been minimized.
zkamsler
commented
May 28, 2015
|
I think you would have to include the vtable pointer in the comparison. For trait objects pointing to zero-sized types, the address of the fat pointer might be a sentinel (depending on allocator behavior). |
This comment has been minimized.
This comment has been minimized.
zkamsler
commented
May 28, 2015
|
Similarly, a struct and the first field in a struct could implement the same trait. Thus, the address could be the same, but the vtable would vary. (which I now see is explicitly mentioned in the RFC) |
This comment has been minimized.
This comment has been minimized.
comex
commented
May 29, 2015
|
What would be the defined semantics for this with zero-sized types? (I don't like implicitly-supported pointer comparisons in general, since they make difficult a potential future optimization to represent |
This comment has been minimized.
This comment has been minimized.
zkamsler
commented
May 29, 2015
|
@comex |
This comment has been minimized.
This comment has been minimized.
|
@comex well, |
This comment has been minimized.
This comment has been minimized.
|
I had thought that we should look only at the pointers for all kinds of comparison, but looking at the vtables too also seems a reasonable approach. In particular if two pointers have different vtables they would be not equal and could not be compared for ordering. I don't have a strong preference for either kind of comparison; including the vtables seems marginally superior. |
This comment has been minimized.
This comment has been minimized.
|
Another related thing is that for some HPC use cases, pointer equality is often a useful property. |
This comment has been minimized.
This comment has been minimized.
|
This would be extremely useful. I'm very much in favor. |
This was referenced Jun 25, 2015
nikomatsakis
added
the
final-comment-period
label
Jul 31, 2015
This comment has been minimized.
This comment has been minimized.
|
Hear ye, hear ye. This RFC is now entering final comment period. |
arielb1
referenced this pull request
Aug 1, 2015
Closed
ICE when comparing two *const Trait for equality #27449
This comment has been minimized.
This comment has been minimized.
|
@arielb1 there's a few comments in the thread about whether to include the vtable info in the comparison, to you have an opinion beyond what is in the RFC? I just wanted to check you didn't have more thoughts here, otherwise we (lang team) think this is ready to merge. |
This comment has been minimized.
This comment has been minimized.
|
that's it, merge the RFC as is (comparing vtable addresses), and prohibit |
This comment has been minimized.
This comment has been minimized.
|
I definitely think vtables should be part of the comparison. Two examples:
(We discussed this in the lang team meeting, these points were raised there.) |
This comment has been minimized.
This comment has been minimized.
|
That was my initial thought too. What should happen with |
This comment has been minimized.
This comment has been minimized.
Yes, I think the current system is just a poor man's obligation. I personally have no problem supporting |
This comment has been minimized.
This comment has been minimized.
|
[bikeshed] is the vtable or addr more significant? |
This comment has been minimized.
This comment has been minimized.
|
@arielb1 whichever comes first in our current representation :) |
nikomatsakis
referenced this pull request
Sep 4, 2015
Closed
Tracking issue for raw fat pointer comparisons (RFC #1135) #28236
nikomatsakis
merged commit c16c332
into
rust-lang:master
Sep 4, 2015
nikomatsakis
added a commit
that referenced
this pull request
Sep 4, 2015
This comment has been minimized.
This comment has been minimized.
|
Huzzah! The language design subteam has decided to accept this RFC. The canonical text is now in the repository and the tracking issue for implementation is at rust-lang/rust#28236. Note: there is still an unresolved question, perhaps suitable for a followup RFC or amendment, about ordering beyond equality. |
arielb1 commentedMay 27, 2015
Rendered