Unnamed repository; edit this file 'description' to name the repository.
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
bors 2024-01-20
parent 67cfbf2 · parent f9b5e0d · commit 0c76421
-rw-r--r--crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs
index 65e2a01847..81a639e0b9 100644
--- a/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs
+++ b/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs
@@ -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 {
@@ -433,6 +440,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(
extract_struct_from_enum_variant,