Skip to content

Commit

Permalink
Improve check_ref suggestions in macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Jun 2, 2021
1 parent 716394d commit 80828f2
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions compiler/rustc_typeck/src/check/demand.rs
Expand Up @@ -8,6 +8,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc_hir as hir;
use rustc_hir::lang_items::LangItem;
use rustc_hir::{is_range_literal, Node};
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::adjustment::AllowTwoPhase;
use rustc_middle::ty::{self, AssocItem, Ty, TypeAndMut};
use rustc_span::symbol::sym;
Expand Down Expand Up @@ -412,25 +413,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
checked_ty: Ty<'tcx>,
expected: Ty<'tcx>,
) -> Option<(Span, &'static str, String, Applicability)> {
let sm = self.sess().source_map();
let sess = self.sess();
let sp = expr.span;
if sm.is_imported(sp) {
// Ignore if span is from within a macro #41858, #58298. We previously used the macro
// call span, but that breaks down when the type error comes from multiple calls down.

// If the span is from an external macro, there's no suggestion we can make.
if in_external_macro(sess, sp) {
return None;
}

let sm = sess.source_map();

let replace_prefix = |s: &str, old: &str, new: &str| {
s.strip_prefix(old).map(|stripped| new.to_string() + stripped)
};

let is_struct_pat_shorthand_field =
self.is_hir_id_from_struct_pattern_shorthand_field(expr.hir_id, sp);

// If the span is from a macro, then it's hard to extract the text
// and make a good suggestion, so don't bother.
let is_macro = sp.from_expansion() && sp.desugaring_kind().is_none();

// `ExprKind::DropTemps` is semantically irrelevant for these suggestions.
let expr = expr.peel_drop_temps();

Expand Down Expand Up @@ -570,10 +569,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, _, ref expr),
_,
&ty::Ref(_, checked, _),
) if {
self.infcx.can_sub(self.param_env, checked, &expected).is_ok() && !is_macro
} =>
{
) if self.infcx.can_sub(self.param_env, checked, &expected).is_ok() => {
// We have `&T`, check if what was expected was `T`. If so,
// we may want to suggest removing a `&`.
if sm.is_imported(expr.span) {
Expand All @@ -589,13 +585,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
return None;
}
if let Ok(code) = sm.span_to_snippet(expr.span) {
return Some((
sp,
"consider removing the borrow",
code,
Applicability::MachineApplicable,
));
if sp.contains(expr.span) {
if let Ok(code) = sm.span_to_snippet(expr.span) {
return Some((
sp,
"consider removing the borrow",
code,
Applicability::MachineApplicable,
));
}
}
}
(
Expand Down Expand Up @@ -643,7 +641,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
}
_ if sp == expr.span && !is_macro => {
_ if sp == expr.span => {
if let Some(steps) = self.deref_steps(checked_ty, expected) {
let expr = expr.peel_blocks();

Expand Down

0 comments on commit 80828f2

Please sign in to comment.