Skip to content

Commit

Permalink
Auto merge of rust-lang#13356 - WaffleLapkin:go_to_def_shadow_include…
Browse files Browse the repository at this point in the history
…, r=Veykril

minor: Fix go-to-def for shadowed `include*!`

Add a check in go-to-def feature, so that we don't assume any macro named `include`/`include_str`/`include_bytes` is the builtin one.
  • Loading branch information
bors committed Oct 6, 2022
2 parents 476d043 + a57ef6b commit f2f3528
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions crates/ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ fn try_lookup_include_path(
if !matches!(&*name.text(), "include" | "include_str" | "include_bytes") {
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 size = sema.db.file_text(file_id).len().try_into().ok()?;
Some(NavigationTarget {
Expand Down Expand Up @@ -156,9 +164,6 @@ mod tests {
fn check(ra_fixture: &str) {
let (analysis, position, expected) = fixture::annotations(ra_fixture);
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 navs = navs
Expand Down Expand Up @@ -1348,6 +1353,10 @@ fn f(e: Enum) {
check(
r#"
//- /main.rs
#[rustc_builtin_macro]
macro_rules! include_str {}
fn main() {
let str = include_str!("foo.txt$0");
}
Expand All @@ -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)]
mod goto_impl_of_trait_fn {
use super::check;
Expand Down

0 comments on commit f2f3528

Please sign in to comment.