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.
This commit is contained in:
commit
c94cb834d0
5 changed files with 198 additions and 51 deletions
|
@ -686,6 +686,12 @@ impl Span {
|
|||
}
|
||||
|
||||
/// Walk down the expansion ancestors to find a span that's contained within `outer`.
|
||||
///
|
||||
/// The span returned by this method may have a different [`SyntaxContext`] as `outer`.
|
||||
/// If you need to extend the span, use [`find_ancestor_inside_same_ctxt`] instead,
|
||||
/// because joining spans with different syntax contexts can create unexpected results.
|
||||
///
|
||||
/// [`find_ancestor_inside_same_ctxt`]: Self::find_ancestor_inside_same_ctxt
|
||||
pub fn find_ancestor_inside(mut self, outer: Span) -> Option<Span> {
|
||||
while !outer.contains(self) {
|
||||
self = self.parent_callsite()?;
|
||||
|
@ -693,11 +699,34 @@ impl Span {
|
|||
Some(self)
|
||||
}
|
||||
|
||||
/// Like `find_ancestor_inside`, but specifically for when spans might not
|
||||
/// overlaps. Take care when using this, and prefer `find_ancestor_inside`
|
||||
/// when you know that the spans are nested (modulo macro expansion).
|
||||
/// Walk down the expansion ancestors to find a span with the same [`SyntaxContext`] as
|
||||
/// `other`.
|
||||
///
|
||||
/// Like [`find_ancestor_inside_same_ctxt`], but specifically for when spans might not
|
||||
/// overlap. Take care when using this, and prefer [`find_ancestor_inside`] or
|
||||
/// [`find_ancestor_inside_same_ctxt`] when you know that the spans are nested (modulo
|
||||
/// macro expansion).
|
||||
///
|
||||
/// [`find_ancestor_inside`]: Self::find_ancestor_inside
|
||||
/// [`find_ancestor_inside_same_ctxt`]: Self::find_ancestor_inside_same_ctxt
|
||||
pub fn find_ancestor_in_same_ctxt(mut self, other: Span) -> Option<Span> {
|
||||
while !Span::eq_ctxt(self, other) {
|
||||
while !self.eq_ctxt(other) {
|
||||
self = self.parent_callsite()?;
|
||||
}
|
||||
Some(self)
|
||||
}
|
||||
|
||||
/// Walk down the expansion ancestors to find a span that's contained within `outer` and
|
||||
/// has the same [`SyntaxContext`] as `outer`.
|
||||
///
|
||||
/// This method is the combination of [`find_ancestor_inside`] and
|
||||
/// [`find_ancestor_in_same_ctxt`] and should be preferred when extending the returned span.
|
||||
/// If you do not need to modify the span, use [`find_ancestor_inside`] instead.
|
||||
///
|
||||
/// [`find_ancestor_inside`]: Self::find_ancestor_inside
|
||||
/// [`find_ancestor_in_same_ctxt`]: Self::find_ancestor_in_same_ctxt
|
||||
pub fn find_ancestor_inside_same_ctxt(mut self, outer: Span) -> Option<Span> {
|
||||
while !outer.contains(self) || !self.eq_ctxt(outer) {
|
||||
self = self.parent_callsite()?;
|
||||
}
|
||||
Some(self)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue