Fix go-to-def for shadowed include*!

This commit is contained in:
Maybe Waffle 2022-10-06 06:10:28 +00:00
parent 476d043874
commit a57ef6b0b1

View file

@ -95,6 +95,14 @@ fn try_lookup_include_path(
if !matches!(&*name.text(), "include" | "include_str" | "include_bytes") { if !matches!(&*name.text(), "include" | "include_str" | "include_bytes") {
return None; return None;
} }
// Ignore non-built-in macros to account for shadowing
if let Some(it) = sema.resolve_macro_call(&macro_call) {
if !matches!(it.kind(sema.db), hir::MacroKind::BuiltIn) {
return None;
}
}
let file_id = sema.db.resolve_path(AnchoredPath { anchor: file_id, path: &path })?; let file_id = sema.db.resolve_path(AnchoredPath { anchor: file_id, path: &path })?;
let size = sema.db.file_text(file_id).len().try_into().ok()?; let size = sema.db.file_text(file_id).len().try_into().ok()?;
Some(NavigationTarget { Some(NavigationTarget {
@ -156,9 +164,6 @@ mod tests {
fn check(ra_fixture: &str) { fn check(ra_fixture: &str) {
let (analysis, position, expected) = fixture::annotations(ra_fixture); let (analysis, position, expected) = fixture::annotations(ra_fixture);
let navs = analysis.goto_definition(position).unwrap().expect("no definition found").info; let navs = analysis.goto_definition(position).unwrap().expect("no definition found").info;
if navs.is_empty() {
panic!("unresolved reference")
}
let cmp = |&FileRange { file_id, range }: &_| (file_id, range.start()); let cmp = |&FileRange { file_id, range }: &_| (file_id, range.start());
let navs = navs let navs = navs
@ -1348,6 +1353,10 @@ fn f(e: Enum) {
check( check(
r#" r#"
//- /main.rs //- /main.rs
#[rustc_builtin_macro]
macro_rules! include_str {}
fn main() { fn main() {
let str = include_str!("foo.txt$0"); let str = include_str!("foo.txt$0");
} }
@ -1357,6 +1366,24 @@ fn main() {
"#, "#,
); );
} }
#[test]
fn goto_shadow_include() {
check(
r#"
//- /main.rs
macro_rules! include {
("included.rs") => {}
}
include!("included.rs$0");
//- /included.rs
// empty
"#,
);
}
#[cfg(test)] #[cfg(test)]
mod goto_impl_of_trait_fn { mod goto_impl_of_trait_fn {
use super::check; use super::check;