Skip to content

Commit a15dab9

Browse files
committed
Use right span in prelude collision suggestions with macros.
1 parent 2a06daa commit a15dab9

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

compiler/rustc_typeck/src/check/method/prelude2021.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
156156
segment.ident.name
157157
));
158158

159-
let (self_adjusted, precise) = self.adjust_expr(pick, self_expr);
159+
let (self_adjusted, precise) = self.adjust_expr(pick, self_expr, sp);
160160
if precise {
161161
let args = args
162162
.iter()
163163
.skip(1)
164164
.map(|arg| {
165+
let span = arg.span.find_ancestor_inside(sp).unwrap_or_default();
165166
format!(
166167
", {}",
167-
self.sess().source_map().span_to_snippet(arg.span).unwrap()
168+
self.sess().source_map().span_to_snippet(span).unwrap()
168169
)
169170
})
170171
.collect::<String>();
@@ -275,7 +276,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
275276
let mut self_ty_name = self
276277
.sess()
277278
.source_map()
278-
.span_to_snippet(self_ty_span)
279+
.span_to_snippet(self_ty_span.find_ancestor_inside(span).unwrap_or_default())
279280
.unwrap_or_else(|_| self_ty.to_string());
280281

281282
// Get the number of generics the self type has (if an Adt) unless we can determine that
@@ -370,7 +371,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
370371
/// Creates a string version of the `expr` that includes explicit adjustments.
371372
/// Returns the string and also a bool indicating whther this is a *precise*
372373
/// suggestion.
373-
fn adjust_expr(&self, pick: &Pick<'tcx>, expr: &hir::Expr<'tcx>) -> (String, bool) {
374+
fn adjust_expr(
375+
&self,
376+
pick: &Pick<'tcx>,
377+
expr: &hir::Expr<'tcx>,
378+
outer: Span,
379+
) -> (String, bool) {
374380
let derefs = "*".repeat(pick.autoderefs);
375381

376382
let autoref = match pick.autoref_or_ptr_adjustment {
@@ -379,12 +385,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
379385
Some(probe::AutorefOrPtrAdjustment::ToConstPtr) | None => "",
380386
};
381387

382-
let (expr_text, precise) =
383-
if let Ok(expr_text) = self.sess().source_map().span_to_snippet(expr.span) {
384-
(expr_text, true)
385-
} else {
386-
("(..)".to_string(), false)
387-
};
388+
let (expr_text, precise) = if let Ok(expr_text) = self
389+
.sess()
390+
.source_map()
391+
.span_to_snippet(expr.span.find_ancestor_inside(outer).unwrap_or_default())
392+
{
393+
(expr_text, true)
394+
} else {
395+
("(..)".to_string(), false)
396+
};
388397

389398
let adjusted_text = if let Some(probe::AutorefOrPtrAdjustment::ToConstPtr) =
390399
pick.autoref_or_ptr_adjustment

0 commit comments

Comments
 (0)