Skip to content
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

Completion suggestion only shows public traits and members #7728

Closed
villem opened this issue Feb 20, 2021 · 10 comments · Fixed by #7768
Closed

Completion suggestion only shows public traits and members #7728

villem opened this issue Feb 20, 2021 · 10 comments · Fixed by #7768
Assignees
Labels
A-completion autocompletion A-macro macro expansion S-actionable Someone could pick this issue up and work on it right now

Comments

@villem
Copy link

villem commented Feb 20, 2021

Environment: mac osx 10.15.7, VSCode 1.53.2,
Latest nightly Rust Analyzer

INFO [2/20/2021, 6:07:47 PM]: PersistentState: {
  lastCheck: 1613807019781,
  releaseId: 38343369,
  serverVersion: '0.3.494-nightly'
}

I don't know if this has been always the case?
We got simple struct and some traits.

#[derive(Debug, Default)]
struct Foo {
    pub name: Option<String>,
}
struct Template2 {
    pub foo: Foo,
}

impl Template2 {
    pub fn faa(&self) {
        let a: Foo = Foo {..Default::default()};
        
    }
    fn priv2(&self) {

    }
    pub fn bar(&self) -> () {
        let a: Foo = Foo { ..Default::default() };
        println!("foo {}", a.name);
        self.<*>

    }
}
fn baz() {
    let goo: Template2 = Template2{ foo: Foo{ name: None }};
    println!("goo {}", goo.faa())
}

goo.<*> get fn bar completion if bar is pub.
also bar's self.<*> find priv() only if it is pub.

Has rust analyser always followed visibility rules? I think it would be really nice if we could turn this of and let compiler fail when private part is tried to use.

@lnicola
Copy link
Member

lnicola commented Feb 20, 2021

Works for me:

image

Your original code had fn priv() without &self, does it still happen with the updated version?

@lnicola lnicola added A-completion autocompletion S-unactionable Issue requires feedback, design decisions or is blocked on other work labels Feb 20, 2021
@villem
Copy link
Author

villem commented Feb 21, 2021

I don't get completion in bar even after adding &self to priv2. I also added the empty main. Strange enough, I get priv2 suggestion when from baz() with goo.pri

@villem
Copy link
Author

villem commented Feb 21, 2021

With latest nigthly Feb 21. Things seems working better. I get priv2 within bar but also with goo.pri.

@lnicola
Copy link
Member

lnicola commented Feb 21, 2021

I don't recall this any recent changes that could have affected this, but it's good to hear that it works. Can we close it, then?

@villem
Copy link
Author

villem commented Feb 21, 2021

After playing bit more, I get still inconsistent suggestions. It seems that only reliable way to get member and trait suggestions is to mark everything pub.

@flodiebold
Copy link
Member

Can you provide a concrete example where you're missing suggestions?

@villem
Copy link
Author

villem commented Feb 21, 2021

The attached video shows how trait method bar only get suggestions faa and priv2() if they are pub. The goo variable get suggestion faa and priv2() whether they are pub or not.

rustanalyzer.mp4

@flodiebold
Copy link
Member

Huh, there seems to be something weird going on with macroexpansion here.

mod foo {
    #[derive(Debug, Default)]
    struct Template2 {}

    impl Template2 {
        fn private(&self) {}
    }
    fn baz() {
        let goo: Template2 = Template2 {};
        // goo.<|> -> here it works
        println!("goo");
        // goo.<|> -> here it doesn't work
    }
}

It seems like we detect the wrong module after the macro call. Note that this only fails if it's in a submodule; putting the code into the crate root makes it work.

@flodiebold flodiebold added A-macro macro expansion S-actionable Someone could pick this issue up and work on it right now and removed S-unactionable Issue requires feedback, design decisions or is blocked on other work labels Feb 21, 2021
@edwin0cheng
Copy link
Member

Um.. an empty macro is not working either:

macro_rules! empty {
    () => {};
}

mod foo {
    #[derive(Debug, Default)]
    struct Template2 {}

    impl Template2 {
        fn private(&self) {}
    }
    fn baz() {
        let goo: Template2 = Template2 {};
         // goo.<|> -> here it works
         empty!();
        // goo.<|> -> here it doesn't work        
    }
}

@villem
Copy link
Author

villem commented Feb 24, 2021

Latest working great. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completion autocompletion A-macro macro expansion S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants