Skip to content

Commit 0c76421

Browse files
committed
Auto merge of rust-lang#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 rust-lang/rust-analyzer#16328
2 parents 67cfbf2 + f9b5e0d commit 0c76421

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs

+46
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,13 @@ fn reference_to_node(
412412
) -> Option<(ast::PathSegment, SyntaxNode, hir::Module)> {
413413
let segment =
414414
reference.name.as_name_ref()?.syntax().parent().and_then(ast::PathSegment::cast)?;
415+
416+
// filter out the reference in marco
417+
let segment_range = segment.syntax().text_range();
418+
if segment_range != reference.range {
419+
return None;
420+
}
421+
415422
let parent = segment.parent_path().syntax().parent()?;
416423
let expr_or_pat = match_ast! {
417424
match parent {
@@ -432,6 +439,45 @@ mod tests {
432439

433440
use super::*;
434441

442+
#[test]
443+
fn test_with_marco() {
444+
check_assist(
445+
extract_struct_from_enum_variant,
446+
r#"
447+
macro_rules! foo {
448+
($x:expr) => {
449+
$x
450+
};
451+
}
452+
453+
enum TheEnum {
454+
TheVariant$0 { the_field: u8 },
455+
}
456+
457+
fn main() {
458+
foo![TheEnum::TheVariant { the_field: 42 }];
459+
}
460+
"#,
461+
r#"
462+
macro_rules! foo {
463+
($x:expr) => {
464+
$x
465+
};
466+
}
467+
468+
struct TheVariant{ the_field: u8 }
469+
470+
enum TheEnum {
471+
TheVariant(TheVariant),
472+
}
473+
474+
fn main() {
475+
foo![TheEnum::TheVariant { the_field: 42 }];
476+
}
477+
"#,
478+
);
479+
}
480+
435481
#[test]
436482
fn issue_16197() {
437483
check_assist(

0 commit comments

Comments
 (0)