Auto merge of #16396 - Young-Flash:fix_marco, r=Veykril

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 https://github.com/rust-lang/rust-analyzer/issues/16328
This commit is contained in:
bors 2024-01-20 08:12:44 +00:00
commit 0c764216d5

View File

@ -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 {
@ -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(