Skip to content

Commit

Permalink
Auto merge of rust-lang#16678 - roife:fix-issue-16660, r=lnicola
Browse files Browse the repository at this point in the history
fix: panic when inlining callsites inside macros' parameters

Close rust-lang#16660, rust-lang#12429, rust-lang#10695.

When `inline_into_callers` encounters callsites in macros parameters, it can lead to panics. Since there is no perfect way to handle macros, this PR directly filters out these cases.
  • Loading branch information
bors committed Feb 26, 2024
2 parents f7f63cc + 61b576c commit 9650578
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions crates/ide-assists/src/handlers/inline_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext<'_>) ->
let call_infos: Vec<_> = name_refs
.into_iter()
.filter_map(CallInfo::from_name_ref)
// FIXME: do not handle callsites in macros' parameters, because
// directly inlining into macros may cause errors.
.filter(|call_info| !ctx.sema.hir_file_for(call_info.node.syntax()).is_macro())
.map(|call_info| {
let mut_node = builder.make_syntax_mut(call_info.node.syntax().clone());
(call_info, mut_node)
Expand Down Expand Up @@ -1795,4 +1798,26 @@ fn _hash2(self_: &u64, state: &mut u64) {
"#,
)
}

#[test]
fn inline_into_callers_in_macros_not_applicable() {
check_assist_not_applicable(
inline_into_callers,
r#"
fn foo() -> u32 {
42
}
macro_rules! bar {
($x:expr) => {
$x
};
}
fn f() {
bar!(foo$0());
}
"#,
);
}
}

0 comments on commit 9650578

Please sign in to comment.