racer-rust / racer

Rust Code Completion utility
MIT License
3.36k stars 278 forks source link

Ignore duplicate definitions due to cfg #700

Open Wilfred opened 7 years ago

Wilfred commented 7 years ago

Given the code:

#[cfg(target_pointer_width = "32")]
const SIZE_OF_PTR: usize = 4;
#[cfg(target_pointer_width = "64")]
const SIZE_OF_PTR: usize = 8;

fn main() {
    SIZE
}

Racer gives:

PREFIX 149,153,SIZE
MATCH SIZE_OF_PTR,2,6,/home/wilfred/projects/remacs/rust_src/src/foo.rs,Const,const SIZE_OF_PTR: usize = 4;
MATCH SIZE_OF_PTR,4,6,/home/wilfred/projects/remacs/rust_src/src/foo.rs,Const,const SIZE_OF_PTR: usize = 8;
END

Ideally racer would not offer both of these.

TedDriggs commented 7 years ago

I'm not sure how racer would decide which cfg item to return, especially in the case of feature flags or other things.

jwilm commented 7 years ago

I'm not convinced find-definition shouldn't return "duplicate" definitions, at least by default. They are both real definitions for that identifier.

Perhaps there should be an option for looking into cfg? Big question then is where cfg definitions would come from. Compiler knows some of them; others are derived based on flags, Cargo.toml, etc. One option is to not have Racer attempt to figure out such definitions but instead provide such a table up-front.

Wilfred commented 7 years ago

Reasonable points. Since we're trying to complete rather than find a definition in this example, it's not useful having duplicates offered. We might just filter duplicates before showing the list to the user.

I agree that looking at cfg would be desirable in an ideal world :)

jwilm commented 7 years ago

Hmm, deduping for completions seems reasonable.

TedDriggs commented 7 years ago

PR #778 deliberately doesn't handle this case, since the two occur at different points and therefore may have different doc strings.