Skip to content

Commit c94cb83

Browse files
committed
Auto merge of #112500 - lukas-code:span-ctxt, r=petrochenkov
Fix argument removal suggestion around macros Fixes #112437. Fixes #113866. Helps with #114255. The issue was that `span.find_ancestor_inside(outer)` could previously return a span with a different expansion context from `outer`. This happens for example for the built-in macro `panic!`, which expands to another macro call of `panic_2021!` or `panic_2015!`. Because the call site of `panic_20xx!` has not associated source code, its span currently points to the call site of `panic!` instead. Something similar also happens items that get desugared in AST->HIR lowering. For example, `for` loops get two spans: One "inner" span that has the `.desugaring_kind()` kind set to `DesugaringKind::ForLoop` and one "outer" span that does not. Similar to the macro situation, both of these spans point to the same source code, but have different expansion contexts. This causes problems, because joining two spans with different expansion contexts will usually[^1] not actually join them together to avoid creating "spaghetti" spans that go from the macro definition to the macro call. For example, in the following snippet `full_span` might not actually contain the `adjusted_start` and `adjusted_end`. This caused the broken suggestion / debug ICE in the linked issues. ```rust let adjusted_start = start.find_ancestor_inside(shared_ancestor); let adjusted_end = end.find_ancestor_inside(shared_ancestor); let full_span = adjusted_start.to(adjusted_end) ``` To fix the issue, this PR introduces a new method, `find_ancestor_inside_same_ctxt`, which combines the functionality of `find_ancestor_inside` and `find_ancestor_in_same_ctxt`: It finds an ancestor span that is contained within the parent *and* has the same syntax context, and is therefore safe to extend. This new method should probably be used everywhere, where the returned span is extended, but for now it is just used for the argument removal suggestion. Additionally, this PR fixes a second issue where the function call itself is inside a macro but the arguments come from outside the macro. The test is added in the first commit to include stderr diff, so this is best reviewed commit by commit. [^1]: If one expansion context is the root context and the other is not.
2 parents 1ec628d + 985036b commit c94cb83

File tree

5 files changed

+198
-51
lines changed

5 files changed

+198
-51
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
519519
// suggestions and labels are (more) correct when an arg is a
520520
// macro invocation.
521521
let normalize_span = |span: Span| -> Span {
522-
let normalized_span = span.find_ancestor_inside(error_span).unwrap_or(span);
522+
let normalized_span = span.find_ancestor_inside_same_ctxt(error_span).unwrap_or(span);
523523
// Sometimes macros mess up the spans, so do not normalize the
524524
// arg span to equal the error span, because that's less useful
525525
// than pointing out the arg expr in the wrong context.
@@ -929,7 +929,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
929929
};
930930
labels.push((provided_span, format!("unexpected argument{provided_ty_name}")));
931931
let mut span = provided_span;
932-
if span.can_be_used_for_suggestions() {
932+
if span.can_be_used_for_suggestions()
933+
&& error_span.can_be_used_for_suggestions()
934+
{
933935
if arg_idx.index() > 0
934936
&& let Some((_, prev)) = provided_arg_tys
935937
.get(ProvidedIdx::from_usize(arg_idx.index() - 1)
@@ -1220,22 +1222,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12201222
};
12211223
if let Some(suggestion_text) = suggestion_text {
12221224
let source_map = self.sess().source_map();
1223-
let (mut suggestion, suggestion_span) =
1224-
if let Some(call_span) = full_call_span.find_ancestor_inside(error_span) {
1225-
("(".to_string(), call_span.shrink_to_hi().to(error_span.shrink_to_hi()))
1226-
} else {
1227-
(
1228-
format!(
1229-
"{}(",
1230-
source_map.span_to_snippet(full_call_span).unwrap_or_else(|_| {
1231-
fn_def_id.map_or("".to_string(), |fn_def_id| {
1232-
tcx.item_name(fn_def_id).to_string()
1233-
})
1225+
let (mut suggestion, suggestion_span) = if let Some(call_span) =
1226+
full_call_span.find_ancestor_inside_same_ctxt(error_span)
1227+
{
1228+
("(".to_string(), call_span.shrink_to_hi().to(error_span.shrink_to_hi()))
1229+
} else {
1230+
(
1231+
format!(
1232+
"{}(",
1233+
source_map.span_to_snippet(full_call_span).unwrap_or_else(|_| {
1234+
fn_def_id.map_or("".to_string(), |fn_def_id| {
1235+
tcx.item_name(fn_def_id).to_string()
12341236
})
1235-
),
1236-
error_span,
1237-
)
1238-
};
1237+
})
1238+
),
1239+
error_span,
1240+
)
1241+
};
12391242
let mut needs_comma = false;
12401243
for (expected_idx, provided_idx) in matched_inputs.iter_enumerated() {
12411244
if needs_comma {

compiler/rustc_span/src/lib.rs

+33-4
Original file line numberDiff line numberDiff line change
@@ -686,18 +686,47 @@ impl Span {
686686
}
687687

688688
/// Walk down the expansion ancestors to find a span that's contained within `outer`.
689+
///
690+
/// The span returned by this method may have a different [`SyntaxContext`] as `outer`.
691+
/// If you need to extend the span, use [`find_ancestor_inside_same_ctxt`] instead,
692+
/// because joining spans with different syntax contexts can create unexpected results.
693+
///
694+
/// [`find_ancestor_inside_same_ctxt`]: Self::find_ancestor_inside_same_ctxt
689695
pub fn find_ancestor_inside(mut self, outer: Span) -> Option<Span> {
690696
while !outer.contains(self) {
691697
self = self.parent_callsite()?;
692698
}
693699
Some(self)
694700
}
695701

696-
/// Like `find_ancestor_inside`, but specifically for when spans might not
697-
/// overlaps. Take care when using this, and prefer `find_ancestor_inside`
698-
/// when you know that the spans are nested (modulo macro expansion).
702+
/// Walk down the expansion ancestors to find a span with the same [`SyntaxContext`] as
703+
/// `other`.
704+
///
705+
/// Like [`find_ancestor_inside_same_ctxt`], but specifically for when spans might not
706+
/// overlap. Take care when using this, and prefer [`find_ancestor_inside`] or
707+
/// [`find_ancestor_inside_same_ctxt`] when you know that the spans are nested (modulo
708+
/// macro expansion).
709+
///
710+
/// [`find_ancestor_inside`]: Self::find_ancestor_inside
711+
/// [`find_ancestor_inside_same_ctxt`]: Self::find_ancestor_inside_same_ctxt
699712
pub fn find_ancestor_in_same_ctxt(mut self, other: Span) -> Option<Span> {
700-
while !Span::eq_ctxt(self, other) {
713+
while !self.eq_ctxt(other) {
714+
self = self.parent_callsite()?;
715+
}
716+
Some(self)
717+
}
718+
719+
/// Walk down the expansion ancestors to find a span that's contained within `outer` and
720+
/// has the same [`SyntaxContext`] as `outer`.
721+
///
722+
/// This method is the combination of [`find_ancestor_inside`] and
723+
/// [`find_ancestor_in_same_ctxt`] and should be preferred when extending the returned span.
724+
/// If you do not need to modify the span, use [`find_ancestor_inside`] instead.
725+
///
726+
/// [`find_ancestor_inside`]: Self::find_ancestor_inside
727+
/// [`find_ancestor_in_same_ctxt`]: Self::find_ancestor_in_same_ctxt
728+
pub fn find_ancestor_inside_same_ctxt(mut self, outer: Span) -> Option<Span> {
729+
while !outer.contains(self) || !self.eq_ctxt(outer) {
701730
self = self.parent_callsite()?;
702731
}
703732
Some(self)

tests/ui/argument-suggestions/extra_arguments.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
fn empty() {}
2-
fn one_arg(_a: i32) {}
2+
fn one_arg<T>(_a: T) {}
33
fn two_arg_same(_a: i32, _b: i32) {}
44
fn two_arg_diff(_a: i32, _b: &str) {}
55

66
macro_rules! foo {
7-
($x:expr) => {
7+
($x:expr, ~) => {
88
empty($x, 1); //~ ERROR function takes
9-
}
9+
};
10+
($x:expr, $y:expr) => {
11+
empty($x, $y); //~ ERROR function takes
12+
};
13+
(~, $y:expr) => {
14+
empty(1, $y); //~ ERROR function takes
15+
};
1016
}
1117

1218
fn main() {
@@ -39,5 +45,17 @@ fn main() {
3945
1,
4046
""
4147
);
42-
foo!(1);
48+
49+
// Check with macro expansions
50+
foo!(1, ~);
51+
foo!(~, 1);
52+
foo!(1, 1);
53+
one_arg(1, panic!()); //~ ERROR function takes
54+
one_arg(panic!(), 1); //~ ERROR function takes
55+
one_arg(stringify!($e), 1); //~ ERROR function takes
56+
57+
// Not a macro, but this also has multiple spans with equal source code,
58+
// but different expansion contexts.
59+
// https://github.com/rust-lang/rust/issues/114255
60+
one_arg(for _ in 1.. {}, 1); //~ ERROR function takes
4361
}

0 commit comments

Comments
 (0)