Skip to content

Commit

Permalink
Auto merge of rust-lang#16396 - Young-Flash:fix_marco, r=Veykril
Browse files Browse the repository at this point in the history
fix panic with reference in macro

it panic at `builder.make_mut(segment)`, where segment is from macro expand. And the usage reference in orginal macro call isn't a `PathSegment` so we can't update it in `apply_references`, I can't find a way to deal with it properly so here just filter out the reference in macro. LMK if there are better way to fix this

try to close rust-lang/rust-analyzer#16328
  • Loading branch information
bors committed Jan 20, 2024
2 parents 67cfbf2 + f9b5e0d commit 0c76421
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@ fn reference_to_node(
) -> Option<(ast::PathSegment, SyntaxNode, hir::Module)> {
let segment =
reference.name.as_name_ref()?.syntax().parent().and_then(ast::PathSegment::cast)?;

// filter out the reference in marco
let segment_range = segment.syntax().text_range();
if segment_range != reference.range {
return None;
}

let parent = segment.parent_path().syntax().parent()?;
let expr_or_pat = match_ast! {
match parent {
Expand All @@ -432,6 +439,45 @@ mod tests {

use super::*;

#[test]
fn test_with_marco() {
check_assist(
extract_struct_from_enum_variant,
r#"
macro_rules! foo {
($x:expr) => {
$x
};
}
enum TheEnum {
TheVariant$0 { the_field: u8 },
}
fn main() {
foo![TheEnum::TheVariant { the_field: 42 }];
}
"#,
r#"
macro_rules! foo {
($x:expr) => {
$x
};
}
struct TheVariant{ the_field: u8 }
enum TheEnum {
TheVariant(TheVariant),
}
fn main() {
foo![TheEnum::TheVariant { the_field: 42 }];
}
"#,
);
}

#[test]
fn issue_16197() {
check_assist(
Expand Down

0 comments on commit 0c76421

Please sign in to comment.