rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.25k stars 1.61k forks source link

Go to Definition on Private Symbols #17122

Open DavidArchibald opened 6 months ago

DavidArchibald commented 6 months ago

It's a not wholly uncommon occurrence for me to import or otherwise use something that's private within my own code and it always feels clunky to have to manually go to the symbol because Go To Definition doesn't work on private symbols.

Here's a minimum example:

fn main() {
    public_module::accidentally_private;
}

pub mod public_module {
    fn accidentally_private() {}
}

If you try to use Go to Definition on public_module::accidentally_private you're unable to, despite the fact that rustc itself points out the line number. It's not a huge deal, it'd just be nice to have as an improvement.

I'd be willing to submit a PR for this but I'd appreciate first knowing if this is a tenable idea as well as a rough direction, e.g. where Go to Definition happens and possibly how to look up private symbols if that's tricky.

lnicola commented 6 months ago

Hmm, this feels like a bug to me? Anyway, as a workaround, you can trigger the "Change visibility to pub(crate)" quick assist, then press Undo, which will leave the caret on the function.

Veykril commented 6 months ago

The relevant entry point is here https://github.com/rust-lang/rust-analyzer/blob/47a901b9bf1f99b1ec5222d478684fc412d526a5/crates/hir/src/source_analyzer.rs#L1082-L1150, this is where the ide rederives the path resolutions. Presumabily we filter out by visibility inside the calls which is usually the correct thing, though here we'd just want to prefer visible over invisible definitions.