Skip to content

Duplicate trait autoimport completions with trait objects #8520

Closed

Description

@flodiebold
mod baz {
    pub trait DefDatabase {
        fn method1(&self);
    }
    pub trait HirDatabase: DefDatabase {
        fn method2(&self);
    }
}

mod bar {
    fn test(db: &dyn crate::baz::HirDatabase) {
        db.metho<|>
    }
}

2021-04-14-205228_927x426_scrot

The methods are already inherently available, but they get suggested as autoimports anyway.

Activity

  1. added
    A-completion
    S-actionable
    on Apr 14, 2021
  2. SomeoneToIgnore commented on Apr 14, 2021

    @SomeoneToIgnore
    Contributor

    Currently autoimoprts are getting scope definitions from Semantics and 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_import in the code is a trait that contains the method.
    Looks like we don't consider such traits as definitions in scope?

  3. flodiebold commented on Apr 14, 2021

    @flodiebold
    MemberAuthor

    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.)

  4. SomeoneToIgnore commented on Apr 14, 2021

    @SomeoneToIgnore
    Contributor

    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:

    • Local(Local(..)) with name db
    • ModuleDef(Function(..)) with name test

    We get Semantics from the completion context: https://github.com/rust-analyzer/rust-analyzer/blob/e1e6e3b925bf7e0f6ca6cbc0d63d59572752653d/crates/ide_completion/src/completions/flyimport.rs#L140
    so 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 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?

  5. flodiebold commented on Apr 14, 2021

    @flodiebold
    MemberAuthor

    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.

  6. added
    E-has-instructions
    on Apr 14, 2021
  7. added a commit that references this issue on Apr 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-completionE-has-instructionsS-actionable

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions