You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Duplicate trait autoimport completions with trait objects #8520
A-completionautocompletionE-has-instructionsIssue has some instructions and pointers to code to get startedS-actionableSomeone could pick this issue up and work on it right now
mod baz {pubtraitDefDatabase{fnmethod1(&self);}pubtraitHirDatabase:DefDatabase{fnmethod2(&self);}}mod bar {fntest(db:&dyncrate::baz::HirDatabase){
db.metho<|>
}}
The methods are already inherently available, but they get suggested as autoimports anyway.
For the current example, import.item_to_import in the code is a trait that contains the method.
Looks like we don't consider such traits as definitions in scope?
They aren't in scope, but you can call the method anyway. We probably need to add a different check for method completions. (The same happens for impl crate::baz::HirDatabase or T where T: crate::baz::HirDatabase.)
Yes, composing the test from the example and dbg-printing items in scope_definitions method shows that besides ModuleDef(BuiltinType(...)) there are only two definitions in scope for the code snippet given:
Another funny observation is that if I choose the autoimport completion option and insert the use statement, no warning is emitted by rustc about the "unused" import statement, so technically it's a valid, different way of completing the method albeit I agree that it's odd to have both.
I'm rather clueless on how to look for such items and check that they are fine to use without the import, do you have any ideas?
A-completionautocompletionE-has-instructionsIssue has some instructions and pointers to code to get startedS-actionableSomeone could pick this issue up and work on it right now
Activity
SomeoneToIgnore commentedon Apr 14, 2021
Currently autoimoprts are getting scope definitions from
Semanticsand filter out any imports that are contained there:https://github.com/rust-analyzer/rust-analyzer/blob/c2a63b97a80cb738f800da61c64e748994709c31/crates/ide_db/src/helpers/import_assets.rs#L230
For the current example,
import.item_to_importin the code is a trait that contains the method.Looks like we don't consider such traits as definitions in scope?
flodiebold commentedon Apr 14, 2021
They aren't in scope, but you can call the method anyway. We probably need to add a different check for method completions. (The same happens for
impl crate::baz::HirDatabaseorTwhereT: crate::baz::HirDatabase.)SomeoneToIgnore commentedon Apr 14, 2021
Yes, composing the test from the example and
dbg-printing items inscope_definitionsmethod shows that besidesModuleDef(BuiltinType(...))there are only two definitions in scope for the code snippet given:Local(Local(..))with namedbModuleDef(Function(..))with nametestWe get
Semanticsfrom the completion context: https://github.com/rust-analyzer/rust-analyzer/blob/e1e6e3b925bf7e0f6ca6cbc0d63d59572752653d/crates/ide_completion/src/completions/flyimport.rs#L140so at least we're not utterly wrong on that side.
Another funny observation is that if I choose the autoimport completion option and insert the use statement, no warning is emitted by
rustcabout the "unused" import statement, so technically it's a valid, different way of completing the method albeit I agree that it's odd to have both.I'm rather clueless on how to look for such items and check that they are fine to use without the import, do you have any ideas?
flodiebold commentedon Apr 14, 2021
Here's the code which decides which traits are considered for a method call:
https://github.com/rust-analyzer/rust-analyzer/blob/e131bfc747df1b21ae6ea04eb9c55001e06b7bf0/crates/hir_ty/src/method_resolution.rs#L623-L635
Maybe the best approach would be to extract this and make it callable from completion, or add a method that checks whether a trait is an "inherent" candidate.
Merge #8539