Skip to content

Commit abee682

Browse files
committed
Fix suggestions for &&T == &T
1 parent 348b041 commit abee682

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,21 @@ impl<'tcx> ObligationCauseCode<'tcx> {
512512
base_cause
513513
}
514514

515+
/// Returns the base obligation and the base trait predicate, if any, ignoring
516+
/// derived obligations.
517+
pub fn peel_derives_with_predicate(&self) -> (&Self, Option<ty::PolyTraitPredicate<'tcx>>) {
518+
let mut base_cause = self;
519+
let mut base_trait_pred = None;
520+
while let Some((parent_code, parent_pred)) = base_cause.parent() {
521+
base_cause = parent_code;
522+
if let Some(parent_pred) = parent_pred {
523+
base_trait_pred = Some(parent_pred);
524+
}
525+
}
526+
527+
(base_cause, base_trait_pred)
528+
}
529+
515530
pub fn parent(&self) -> Option<(&Self, Option<ty::PolyTraitPredicate<'tcx>>)> {
516531
match self {
517532
FunctionArgumentObligation { parent_code, .. } => Some((parent_code, None)),

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -838,14 +838,16 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
838838
}
839839
}
840840
}
841-
} else if let ObligationCauseCode::BinOp { lhs_hir_id, .. } = code
841+
} else if let (ObligationCauseCode::BinOp { lhs_hir_id, .. }, predicate) =
842+
code.peel_derives_with_predicate()
842843
&& let hir::Node::Expr(lhs) = self.tcx.hir().get(*lhs_hir_id)
843844
{
845+
let trait_pred = predicate.unwrap_or(trait_pred);
844846
let lhs_ty = self.tcx.instantiate_bound_regions_with_erased(trait_pred.self_ty());
845847
if let ty::Ref(..) = *lhs_ty.kind() {
846-
let autoderef = (self.autoderef_steps)(lhs_ty);
848+
let lhs_autoderef = (self.autoderef_steps)(lhs_ty);
847849
if let Some(steps) =
848-
autoderef.into_iter().enumerate().find_map(|(steps, (ty, obligations))| {
850+
lhs_autoderef.into_iter().enumerate().find_map(|(steps, (ty, obligations))| {
849851
// Remapping bound vars here
850852
let trait_pred_and_ty =
851853
trait_pred.map_bound(|inner_trait_pred| (inner_trait_pred, ty));
@@ -890,7 +892,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
890892
}
891893
}
892894
}
893-
894895
false
895896
}
896897

0 commit comments

Comments
 (0)