Also note struct access, and fix macro expansion from foreign crates

This commit is contained in:
Michael Goulet 2025-02-24 21:37:01 +00:00
parent 06072468fe
commit 09e584671b
7 changed files with 72 additions and 18 deletions

View file

@ -1071,9 +1071,17 @@ impl Span {
///
/// If "self" is the span of the outer_ident, and "within" is the span of the `($ident,)`
/// expr, then this will return the span of the `$ident` macro variable.
pub fn within_macro(self, within: Span) -> Option<Span> {
pub fn within_macro(self, within: Span, sm: &SourceMap) -> Option<Span> {
match Span::prepare_to_combine(self, within) {
Ok((self_, _, parent)) if self_.lo != self.lo() && self.hi() != self_.hi => {
// Only return something if it doesn't overlap with the original span,
// and the span isn't "imported" (i.e. from unavailable sources).
// FIXME: This does limit the usefulness of the error when the macro is
// from a foreign crate; we could also take into account `-Zmacro-backtrace`,
// which doesn't redact this span (but that would mean passing in even more
// args to this function, lol).
Ok((self_, _, parent))
if self_.hi < self.lo() || self.hi() < self_.lo && !sm.is_imported(within) =>
{
Some(Span::new(self_.lo, self_.hi, self_.ctxt, parent))
}
_ => None,