Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix panic when extracting struct from enum variant #16396

Merged
merged 3 commits into from
Jan 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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