-
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
improve error message when resolution via Deref actually required DerefMut #28419
Comments
Definitely it'd be better to report something like "Values of type |
This should be eminently doable though -- we start out with a |
I'm willing to take this. Where should I look first? This is going to be my first issue. |
@mkpankov if you can ping me on irc ( |
I hit this today. Took me a while to figure out where the problem was. |
@nikomatsakis I'm interested in working on this issue to get my feet wet working with rustc. Any pointers/links for where I should start looking? I'll be looking at the source to see what I can find on my own. |
@ryanq hmm. That error occurs in the "borrow check" code ( |
The error is caused by |
I'm interested in trying this. I've just started at the Recurse Center and making contributions to Rust, within the type system especially, is one of my goals. So this might be perfect. I may need mentorship, but let's see if I can get anywhere on my own first. |
@caipre Oh awesome! Sorry not to get back to you for so long. @nikomatsakis or @arielb1 can probably help mentor. |
@brson Thanks, and no worries. I've been reacquainting myself with Rust since I've been away from it for a bit; my head hurts. 😄 I've made a few smaller contributions to the project but nothing this close to the compiler. I'll probably reach out sometime next week for some guidance. |
@caipre I would indeed be hapy to mentor. Let me give a tips to get you started. I think @arielb1 was basically right in this comment, though the names may have shifted in the meantime. The thing about how this works in the compiler is that a lot of times we have to deref a pointer before we know whether it will be a "mut deref" (i.e., via the OK, now imagine There are various places where this happens but they all wind up at the method Currently, that method is written to "fallback" to a I'm not quite sure however how best to fix this. Perhaps the best thing is for it to report an error and then continue as it does today, but I feel a bit uncomfortable with that -- this routine's name suggests that it just "looks" for a method but doesn't commit to the choice yet. In other words, while I don't think we do any probing about for a I feel like the thing I would most expect is for the method to return |
Reading through the code, I definitely think the most minimal edit would be to continue behaving as it does but report an error if a One other bit of context you may be missing: the self.tables.borrow_mut().method_map.insert(method_call, method); It's...probably fine to skip doing this insert in the case of "requested deref-mut but found deref", but it may cause ICEs, I'm not sure. So I could imagine that if you did the |
FYI, started on this today after taking some time to re-acquaint myself with Rust. (Okay, I was distracted by another project for a bit as well). I think I understand the gist of what's happening and how to proceed. I'm definitely missing any level of comfort with the rust codebase, eg,
Will reach out if I get really stuck. |
@nikomatsakis do you think this code is a symptom of the same problem, or should I file it separately? fn main() {
let mut fns: Vec<Box<FnMut()>> = vec![Box::new(|| println!("called"))];
(fns[0])(); // error: expected function, found `Box<std::ops::FnMut()>`
(*&mut fns[0])();
} It appears to be incorrectly using |
Still working on this. I've managed to add a warning that fires when a I talked with @nikomatsakis briefly on IRC and he suggested that we check whether we're returning into a |
I think that's a separate bug -- did you wind up filing it? |
@nikomatsakis I have now: #36786 |
This doesn't just seem to be a diagnostics issue, but also possibly a coercion logic issue with the call operator: #51157 (comment) |
The error today is:
That seems pretty much perfect to me, so I'm going to close this (a minor improvement might be to note that |
try this
|
If a type
B
implementsDeref
to get out&A
, then auto-deref based method resolution of instances ofB
will include methods ofA
that require&mut self
, even when there is noDerefMut
that would be necessary for such auto-deref to work.We still reject the code, with the message "cannot borrow immutable borrowed content as mutable"
What I would prefer is for the message to say something like:
(I think @nikomatsakis is well aware of this problem; he's mentioned the issue about how the auto-deref code needs to use a conservative guess during method resolution and rely on later compiler stages to reject the method lookup.)
Here is an example of code exhibiting the problem playpen:
The error message when you run this (on nightly) is:
This message can be frustrating, especially when the content appears mutable at the point that is highlighted by the span of the error message; after all, I did use
let mut b = ...
The text was updated successfully, but these errors were encountered: