Skip to content

Commit b39d044

Browse files
authored
Unrolled build for rust-lang#126142
Rollup merge of rust-lang#126142 - compiler-errors:trait-ref-split, r=jackh726 Harmonize using root or leaf obligation in trait error reporting When rust-lang#121826 changed the error reporting to use root obligation and not the leafmost obligation, it didn't actually make sure that all the other diagnostics helper functions used the right obligation. Specifically, when reporting similar impl candidates we are looking for impls of the root obligation, but trying to match them against the trait ref of the leaf obligation. This does a few other miscellaneous changes. There's a lot more clean-up that could be done here, but working with this code is really grief-inducing due to how messy it has become over the years. Someone really needs to show it love. 😓 r? ``@estebank`` Fixes rust-lang#126129
2 parents f6b4b71 + ae24ebe commit b39d044

24 files changed

+163
-119
lines changed

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ impl<T> Trait<T> for X {
546546
for pred in hir_generics.bounds_for_param(def_id) {
547547
if self.constrain_generic_bound_associated_type_structured_suggestion(
548548
diag,
549-
&trait_ref,
549+
trait_ref,
550550
pred.bounds,
551551
assoc,
552552
assoc_args,
@@ -715,7 +715,7 @@ fn foo(&self) -> Self::T { String::new() }
715715

716716
self.constrain_generic_bound_associated_type_structured_suggestion(
717717
diag,
718-
&trait_ref,
718+
trait_ref,
719719
opaque_hir_ty.bounds,
720720
assoc,
721721
assoc_args,
@@ -869,7 +869,7 @@ fn foo(&self) -> Self::T { String::new() }
869869
fn constrain_generic_bound_associated_type_structured_suggestion(
870870
&self,
871871
diag: &mut Diag<'_>,
872-
trait_ref: &ty::TraitRef<'tcx>,
872+
trait_ref: ty::TraitRef<'tcx>,
873873
bounds: hir::GenericBounds<'_>,
874874
assoc: ty::AssocItem,
875875
assoc_args: &[ty::GenericArg<'tcx>],

compiler/rustc_infer/src/traits/util.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,7 @@ impl<'tcx, O: Elaboratable<'tcx>> Elaborator<'tcx, O> {
285285
let obligations =
286286
predicates.predicates.iter().enumerate().map(|(index, &(clause, span))| {
287287
elaboratable.child_with_derived_cause(
288-
clause
289-
.instantiate_supertrait(tcx, &bound_clause.rebind(data.trait_ref)),
288+
clause.instantiate_supertrait(tcx, bound_clause.rebind(data.trait_ref)),
290289
span,
291290
bound_clause.rebind(data),
292291
index,

compiler/rustc_middle/src/traits/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'tcx> Elaborator<'tcx> {
3737
let super_predicates =
3838
self.tcx.super_predicates_of(trait_ref.def_id()).predicates.iter().filter_map(
3939
|&(pred, _)| {
40-
let clause = pred.instantiate_supertrait(self.tcx, &trait_ref);
40+
let clause = pred.instantiate_supertrait(self.tcx, trait_ref);
4141
self.visited.insert(clause).then_some(clause)
4242
},
4343
);

compiler/rustc_middle/src/ty/predicate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ impl<'tcx> Clause<'tcx> {
313313
pub fn instantiate_supertrait(
314314
self,
315315
tcx: TyCtxt<'tcx>,
316-
trait_ref: &ty::PolyTraitRef<'tcx>,
316+
trait_ref: ty::PolyTraitRef<'tcx>,
317317
) -> Clause<'tcx> {
318318
// The interaction between HRTB and supertraits is not entirely
319319
// obvious. Let me walk you (and myself) through an example.

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

+8-10
Original file line numberDiff line numberDiff line change
@@ -3597,7 +3597,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
35973597
&self,
35983598
obligation: &PredicateObligation<'tcx>,
35993599
err: &mut Diag<'_>,
3600-
trait_ref: &ty::PolyTraitRef<'tcx>,
3600+
trait_ref: ty::PolyTraitRef<'tcx>,
36013601
) {
36023602
let rhs_span = match obligation.cause.code() {
36033603
ObligationCauseCode::BinOp { rhs_span: Some(span), rhs_is_lit, .. } if *rhs_is_lit => {
@@ -4592,7 +4592,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
45924592
&self,
45934593
obligation: &PredicateObligation<'tcx>,
45944594
err: &mut Diag<'_>,
4595-
trait_ref: ty::PolyTraitRef<'tcx>,
4595+
trait_pred: ty::PolyTraitPredicate<'tcx>,
45964596
) {
45974597
if ObligationCauseCode::QuestionMark != *obligation.cause.code().peel_derives() {
45984598
return;
@@ -4602,10 +4602,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
46024602
if let hir::Node::Item(item) = node
46034603
&& let hir::ItemKind::Fn(sig, _, body_id) = item.kind
46044604
&& let hir::FnRetTy::DefaultReturn(ret_span) = sig.decl.output
4605-
&& self.tcx.is_diagnostic_item(sym::FromResidual, trait_ref.def_id())
4606-
&& let ty::Tuple(l) = trait_ref.skip_binder().args.type_at(0).kind()
4607-
&& l.len() == 0
4608-
&& let ty::Adt(def, _) = trait_ref.skip_binder().args.type_at(1).kind()
4605+
&& self.tcx.is_diagnostic_item(sym::FromResidual, trait_pred.def_id())
4606+
&& trait_pred.skip_binder().trait_ref.args.type_at(0).is_unit()
4607+
&& let ty::Adt(def, _) = trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
46094608
&& self.tcx.is_diagnostic_item(sym::Result, def.did())
46104609
{
46114610
let body = self.tcx.hir().body(body_id);
@@ -4863,14 +4862,13 @@ impl<'a, 'hir> hir::intravisit::Visitor<'hir> for ReplaceImplTraitVisitor<'a> {
48634862
pub(super) fn get_explanation_based_on_obligation<'tcx>(
48644863
tcx: TyCtxt<'tcx>,
48654864
obligation: &PredicateObligation<'tcx>,
4866-
trait_ref: ty::PolyTraitRef<'tcx>,
4867-
trait_predicate: &ty::PolyTraitPredicate<'tcx>,
4865+
trait_predicate: ty::PolyTraitPredicate<'tcx>,
48684866
pre_message: String,
48694867
) -> String {
48704868
if let ObligationCauseCode::MainFunctionType = obligation.cause.code() {
48714869
"consider using `()`, or a `Result`".to_owned()
48724870
} else {
4873-
let ty_desc = match trait_ref.skip_binder().self_ty().kind() {
4871+
let ty_desc = match trait_predicate.self_ty().skip_binder().kind() {
48744872
ty::FnDef(_, _) => Some("fn item"),
48754873
ty::Closure(_, _) => Some("closure"),
48764874
_ => None,
@@ -4895,7 +4893,7 @@ pub(super) fn get_explanation_based_on_obligation<'tcx>(
48954893
format!(
48964894
"{pre_message}the trait `{}` is not implemented for{desc} `{}`{post}",
48974895
trait_predicate.print_modifiers_and_trait_path(),
4898-
tcx.short_ty_string(trait_ref.skip_binder().self_ty(), &mut None),
4896+
tcx.short_ty_string(trait_predicate.self_ty().skip_binder(), &mut None),
48994897
)
49004898
} else {
49014899
// "the trait bound `T: !Send` is not satisfied" reads better than "`!Send` is

0 commit comments

Comments
 (0)