-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Trait auto import may mistake inherent method calls for trait method calls #8468
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
Comments
I find this issue very frustrating as I work in a codebase that uses lots of On this screenshot, I'm just typing So if we're note very careful, then rust analyzer will add the |
I assume your editor of choice is VSCode here. |
Also as for the original issue here, I don't think what the issue describes is actually still happening for us(we now show both completions separately with a |
Right, it is So I think the problem is that I think it is unlikely that someone want to access these function, so if the type is not known, perhaps leave them out if the trait is not yet in scope? |
Ah ye that one is #10454 I believe. |
Closing as the original issue seems to have disappeared and the issue in the comment being tracked inother issue. |
I've ran into the exact same issue, and it's been bewildering. I don't think it should simply be closed in favor of #10454, because the problem here is auto-import of traits, not method suggestion. Suggesting |
Hmm @kornelski I think it would be helpful if you could provide a simple reproduction of the problem. |
I've had a struct like: struct Foo {
cell: Rc<RefCell<i32>>,
} and I wrote:
rust-analyzer auto-completed Instead of |
Can this issue be reopened? I'm very active teaching people rust, and this pain point comes up almost daily. It has not been solved. |
That is the reproduction. People click on the second option, and then they get weird errors everywhere. I think we just have a difference of opinion about what "fixed" means. What I see is that people (especially newcomers) still get confused by accidentally importing To me the correct fix seems to be to special-case |
That does not sound like a fix to me, special casing one specific case here that is. There are probably more occasions where this could occur, (On an unrelated note, I am also surprised as to why especially newcomers jump the gun with Rc and RefCell 😕) |
|
Well, why do people only get confused here? It's in no way special in regards to other inherent vs trait method collisions really. One thing that came to mind right now though, since I assume part of the confusion for newcomers at least could come from them not understanding what the |
People won't read a random popup. Installation time is wrong time to explain a problem users haven't run into yet, without relevant context. Even if you make users read it, there's very little chance they will recall this when they run into the problem. You could show the warning when users select the BorrowMut trait for the first time, but it's not as good as never suggesting the trap in the first place. |
Well from the looks of it people either don't read what completions say either or they don't know what the |
I agree with @kornelski here; a popup listing some gotchas is a bad idea, especially when you can avoid having that gotcha in the first place.
The main reason, I think, is that in general people tend to not read things. |
Well, if you think about it, the shared_cell.borrow_m<tab> And it'd be transformed into BorrowMut::borrow_mut(shared_cell) Which would probably be annoying, but it would prevent those confusing ambiguity errors. |
Fwiw this seems really bad? I would expect people read what they are about to complete since it can be something completely different. |
Let's move the discussions over to #13786, this issue wasn't about the confusion per se (the issue was about the inherent completion not appearing at all). Meant to open a new issue for this earlier but I got sidetracked, sorry. Though I am likely to yield now to special case and remove this collision, clearly issue here is with rust having made these methods opposed to just functions on the trait. |
A realistic use case which sometimes reproduces the issue:
Here, typing out the
.borrow_mut()
will importstd::borrow::BorrowMut
, which will try to use the implementation ofBorrowMut
onRc<RefCell<Option<&'static str>>>
because it'd take priority over deref coercion ofRc
down to theRefCell
with its own.borrow_mut()
, thus mutably borrowing the variable storing theRc
rather than its content. Assigning to that fails with a type mismatch (expects you to put a newRc
there but finds anOption<&'static str>
which should itself be wrapped in anRc
).This does not reliably reproduce if you encounter the issue and remove the import (typing out the same code won't trigger the
BorrowMut
auto import again), but one thing I noticed is that this happens only when hovering over the.borrow_mut()
you just typed out shows the documentation from theBorrowMut
trait rather than the method. However, even when you remove the import it added and it doesn't add it again (the bug essentially disappears), hovering over.borrow_mut()
will still show theBorrowMut
documentation, even thoughBorrowMut
isn't in scope and the method call refers toRefCell::borrow_mut()
.I'm using the VSCode extension (
matklad.rust-analyzer
, not the official one) if that matters at all.The text was updated successfully, but these errors were encountered: