-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Allow method resolution to work on reference receiver types with Self : Sized bounds with reference trait implementations #83205
Conversation
r? @davidtwco (rust-highfive has picked a reviewer for you, use r? to override) |
r? @eddyb maybe? |
42284f8
to
b3ac42b
Compare
This comment has been minimized.
This comment has been minimized.
Oh... looking at the diff, there's already a hack for a different situation involving non-object-safe methods, that special-cases But maybe I'm misunderstanding the existing code, so maybe ignore me until you get a proper review. |
@eddyb That's what the compiler currently does. The existing hack you refer to is for diagnostics purposes. It's very much possible that I misunderstood the intent of the issue. The way I understood it is that @guswynn wanted the compiler to autoref here to make Self correspond to the Maybe @guswynn can also weigh in on this? |
@eddyb I think I understand what you meant now. Instead of using the hack to test whether an illegal sized bound was encountered, we could test whether the sized bound holds (or object safety in general) during method probing. If that fails (during the |
@b-naber I believe your most recently comment is how I think of this problem |
One problem I see with ignoring non object-safe methods during method probing is that the resulting diagnostics would be less informative. Instead of being given information on what bounds aren't satisfied we would only give out a 'method not found' error. But keeping track of potential candidates, which have unfulfilled obligations, during method probing for diagnostics purposes would probably affect performance negatively. Not sure what's the right trade-off here. |
@b-naber is it possible in the event of a diagnostic to backtrack and recollect the methods that were tried? As far as I can tell, the diagnostic path in rustc prefers accuracy/helpfulness over performance |
I think we would need to re-run method probing and add an option to collect method candidates. But you're right, once we're not on the hot path anymore because of an error, performance matters a lot less. I can try that approach, thanks. |
fn foo(x: &dyn Trait) { | ||
x.static_call(); | ||
} |
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.
@b-naber can we add a &mut test here? that was the motivating usecase
also, is there a way we can test that this has actually regressed diagnostics? ci appears to pass just fine
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.
Added the test, but we have to declare x as mutable in order for the mutable autoref to work, which might be confusing given that the autoref is implicit.
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.
We don't have diagnostics problems in this version (using the existing illegal_sized_bound
hack.
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.
@b-naber oh thats very confusing...is the diagnostic around that good? as in, does it explicitly tell you to add the mut
before the parameter?
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.
Hm not really great, the borrow part is confusing, I don't think we should expect the user to know that an autoref occurs here:
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> src/test/ui/resolve/issue-82825-mut.rs:17:5
|
17 | x.static_call();
| ^
| |
| cannot borrow as mutable
| try removing `&mut` here
error: aborting due to previous error
What do you think?
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.
oh hmm, yeah we would likely need to improve this diagnostic
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.
Oh also we should make sure we can still call it with (x).static_call()
so that this is backwards compatible
Trying to solve this without the existing |
This comment has been minimized.
This comment has been minimized.
c88ad1e
to
84c371d
Compare
☔ The latest upstream changes (presumably #83580) made this pull request unmergeable. Please resolve the merge conflicts. |
I owe a review here, I know |
Ping again from triage: |
Triage: |
Any progress on this fix? |
Fixes #82825
In situations in which we have a trait method that takes a reference receiver type, a Self : Sized bound and a trait implementation for a reference type, the adjustments that the compiler applies are insufficient to fulfill the Sized bound on Self. To allow the compiler to resolve the correct method we autoref during method probing, after a sized bound violation was found, try to confirm the new method candidate and check if all resulting obligations can be fulfilled.