Skip to content

Commit

Permalink
introduce helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Jun 21, 2021
1 parent 186c09a commit aa3580b
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions compiler/rustc_typeck/src/check/method/prelude2021.rs
Expand Up @@ -137,28 +137,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
segment.ident.name
));

if let Ok(self_expr) = self.sess().source_map().span_to_snippet(self_expr.span)
{
let derefs = "*".repeat(pick.autoderefs);

let autoref = match pick.autoref_or_ptr_adjustment {
Some(probe::AutorefOrPtrAdjustment::Autoref {
mutbl: Mutability::Mut,
..
}) => "&mut ",
Some(probe::AutorefOrPtrAdjustment::Autoref {
mutbl: Mutability::Not,
..
}) => "&",
Some(probe::AutorefOrPtrAdjustment::ToConstPtr) | None => "",
};
let self_adjusted = if let Some(probe::AutorefOrPtrAdjustment::ToConstPtr) =
pick.autoref_or_ptr_adjustment
{
format!("{}{} as *const _", derefs, self_expr)
} else {
format!("{}{}{}", autoref, derefs, self_expr)
};
let (self_adjusted, precise) = self.adjust_expr(pick, self_expr);
if precise {
let args = args
.iter()
.skip(1)
Expand Down Expand Up @@ -317,4 +297,34 @@ 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) {
let derefs = "*".repeat(pick.autoderefs);

let autoref = match pick.autoref_or_ptr_adjustment {
Some(probe::AutorefOrPtrAdjustment::Autoref { mutbl: Mutability::Mut, .. }) => "&mut ",
Some(probe::AutorefOrPtrAdjustment::Autoref { mutbl: Mutability::Not, .. }) => "&",
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 {
(format!("(..)"), false)
};

let adjusted_text = if let Some(probe::AutorefOrPtrAdjustment::ToConstPtr) =
pick.autoref_or_ptr_adjustment
{
format!("{}{} as *const _", derefs, expr_text)
} else {
format!("{}{}{}", autoref, derefs, expr_text)
};

(adjusted_text, precise)
}
}

0 comments on commit aa3580b

Please sign in to comment.