Skip to content

Commit

Permalink
Use right span in prelude collision suggestions with macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Aug 31, 2021
1 parent 2a06daa commit a15dab9
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions compiler/rustc_typeck/src/check/method/prelude2021.rs
Expand Up @@ -156,15 +156,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
segment.ident.name
));

let (self_adjusted, precise) = self.adjust_expr(pick, self_expr);
let (self_adjusted, precise) = self.adjust_expr(pick, self_expr, sp);
if precise {
let args = args
.iter()
.skip(1)
.map(|arg| {
let span = arg.span.find_ancestor_inside(sp).unwrap_or_default();
format!(
", {}",
self.sess().source_map().span_to_snippet(arg.span).unwrap()
self.sess().source_map().span_to_snippet(span).unwrap()
)
})
.collect::<String>();
Expand Down Expand Up @@ -275,7 +276,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let mut self_ty_name = self
.sess()
.source_map()
.span_to_snippet(self_ty_span)
.span_to_snippet(self_ty_span.find_ancestor_inside(span).unwrap_or_default())
.unwrap_or_else(|_| self_ty.to_string());

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

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

let (expr_text, precise) =
if let Ok(expr_text) = self.sess().source_map().span_to_snippet(expr.span) {
(expr_text, true)
} else {
("(..)".to_string(), false)
};
let (expr_text, precise) = if let Ok(expr_text) = self
.sess()
.source_map()
.span_to_snippet(expr.span.find_ancestor_inside(outer).unwrap_or_default())
{
(expr_text, true)
} else {
("(..)".to_string(), false)
};

let adjusted_text = if let Some(probe::AutorefOrPtrAdjustment::ToConstPtr) =
pick.autoref_or_ptr_adjustment
Expand Down

0 comments on commit a15dab9

Please sign in to comment.