-
Notifications
You must be signed in to change notification settings - Fork 532
Update object safety to match impl around self as receiver #1248
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
Open
upsuper
wants to merge
4
commits into
rust-lang:master
Choose a base branch
from
upsuper-forks:trait-obj-receiver-by-value
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 make it explicitly non-dispatchable currently though, so having a receiver of
Self
would be explicitly non-dispatchable and placed into the other list, yes? It made sense when the unstable feature comment was here, but without it, it doesn't.Uh oh!
There was an error while loading. Please reload this page.
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.
That was exactly the reason that I referenced the unstable feature.
You can't place it into the other list, because
Self
as receiver doesn't make the method explicitly non-dispatchable, as that would fail to explain whyfn b(self) -> Self;
doesn't compile.There is basically no simple way otherwise to explain the current situation, and I think the code basically said
self
as receiver is considered object safe: https://github.com/rust-lang/rust/blob/d70c0ecfae6dd9cb00ffae9ad806c47256c9ef15/compiler/rustc_trait_selection/src/traits/object_safety.rs#L456-L460There 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.
Also if you see this example: https://godbolt.org/z/TPz4WEWh5
It is pretty clear that
fn a(self);
is treated as dispatchable, and it has an item in the vtable.For future reference the code and output is the following:
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.
Alright. This is too weird for me to reason about at 2am. I'll think about it tomorrow unless ehuss wants to step in.
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.
Just to follow up on this: I also don't think it looks correct to include this in this list of dispatchable functions. And I believe that there is an implied
self: Sized
when implementing, which is already covered in the points below.I think it would be good to clear up the parentheticals, though, as they seemed a little confusing to me. Perhaps remove them and place the elaboration inside a note block? Maybe something like:
Uh oh!
There was an error while loading. Please reload this page.
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.
All I'm arguing here is that this statement doesn't match the current behavior and can't explain the examples in the issue linked.
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.
Ah, I see. Sorry, trying to catch up. This is a part of the compiler I am unfamiliar with.
So it looks like there are more complex rules that are not covered here. It looks like
Self
can be a receiver (for a non-dispatchable method). But there are several other restrictions that would cover what was raised in the issue:Self
cannot be used in any parameterSelf
cannot be used anywhere in a return typewhere
clause cannot referenceSelf
(currently a future-incompatibility lint). Although this strangely allows Copy and Clone. That needs more investigation.contains_illegal_self_type_reference
has a long description.I think removing the parentheticals, and trying to incorporate the additional restrictions might be the way to go. For the future-incompat thing, I would lean towards just documenting the intended behavior, with a footnote pointing to the tracking issue.
Uh oh!
There was an error while loading. Please reload this page.
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.
That would be much more complicated to document than simply saying it's dispatchable but you just can't call it through trait object right now. You would efficiently need to duplicate all the rules again.
It's especially the case that vtable actually includes entry for such method indicating the compiler does consider it dispatchable.
Uh oh!
There was an error while loading. Please reload this page.
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.
I believe the intented behavior is that it's dispatchable but just not fully supported yet.