Skip to content

Commit

Permalink
Rollup merge of rust-lang#126142 - compiler-errors:trait-ref-split, r…
Browse files Browse the repository at this point in the history
…=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
  • Loading branch information
workingjubilee authored Jun 13, 2024
2 parents 8719cc2 + ae24ebe commit 25c55c5
Show file tree
Hide file tree
Showing 24 changed files with 163 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ impl<T> Trait<T> for X {
for pred in hir_generics.bounds_for_param(def_id) {
if self.constrain_generic_bound_associated_type_structured_suggestion(
diag,
&trait_ref,
trait_ref,
pred.bounds,
assoc,
assoc_args,
Expand Down Expand Up @@ -715,7 +715,7 @@ fn foo(&self) -> Self::T { String::new() }

self.constrain_generic_bound_associated_type_structured_suggestion(
diag,
&trait_ref,
trait_ref,
opaque_hir_ty.bounds,
assoc,
assoc_args,
Expand Down Expand Up @@ -869,7 +869,7 @@ fn foo(&self) -> Self::T { String::new() }
fn constrain_generic_bound_associated_type_structured_suggestion(
&self,
diag: &mut Diag<'_>,
trait_ref: &ty::TraitRef<'tcx>,
trait_ref: ty::TraitRef<'tcx>,
bounds: hir::GenericBounds<'_>,
assoc: ty::AssocItem,
assoc_args: &[ty::GenericArg<'tcx>],
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_infer/src/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ impl<'tcx, O: Elaboratable<'tcx>> Elaborator<'tcx, O> {
let obligations =
predicates.predicates.iter().enumerate().map(|(index, &(clause, span))| {
elaboratable.child_with_derived_cause(
clause
.instantiate_supertrait(tcx, &bound_clause.rebind(data.trait_ref)),
clause.instantiate_supertrait(tcx, bound_clause.rebind(data.trait_ref)),
span,
bound_clause.rebind(data),
index,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<'tcx> Elaborator<'tcx> {
let super_predicates =
self.tcx.super_predicates_of(trait_ref.def_id()).predicates.iter().filter_map(
|&(pred, _)| {
let clause = pred.instantiate_supertrait(self.tcx, &trait_ref);
let clause = pred.instantiate_supertrait(self.tcx, trait_ref);
self.visited.insert(clause).then_some(clause)
},
);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl<'tcx> Clause<'tcx> {
pub fn instantiate_supertrait(
self,
tcx: TyCtxt<'tcx>,
trait_ref: &ty::PolyTraitRef<'tcx>,
trait_ref: ty::PolyTraitRef<'tcx>,
) -> Clause<'tcx> {
// The interaction between HRTB and supertraits is not entirely
// obvious. Let me walk you (and myself) through an example.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3597,7 +3597,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
&self,
obligation: &PredicateObligation<'tcx>,
err: &mut Diag<'_>,
trait_ref: &ty::PolyTraitRef<'tcx>,
trait_ref: ty::PolyTraitRef<'tcx>,
) {
let rhs_span = match obligation.cause.code() {
ObligationCauseCode::BinOp { rhs_span: Some(span), rhs_is_lit, .. } if *rhs_is_lit => {
Expand Down Expand Up @@ -4592,7 +4592,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
&self,
obligation: &PredicateObligation<'tcx>,
err: &mut Diag<'_>,
trait_ref: ty::PolyTraitRef<'tcx>,
trait_pred: ty::PolyTraitPredicate<'tcx>,
) {
if ObligationCauseCode::QuestionMark != *obligation.cause.code().peel_derives() {
return;
Expand All @@ -4602,10 +4602,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
if let hir::Node::Item(item) = node
&& let hir::ItemKind::Fn(sig, _, body_id) = item.kind
&& let hir::FnRetTy::DefaultReturn(ret_span) = sig.decl.output
&& self.tcx.is_diagnostic_item(sym::FromResidual, trait_ref.def_id())
&& let ty::Tuple(l) = trait_ref.skip_binder().args.type_at(0).kind()
&& l.len() == 0
&& let ty::Adt(def, _) = trait_ref.skip_binder().args.type_at(1).kind()
&& self.tcx.is_diagnostic_item(sym::FromResidual, trait_pred.def_id())
&& trait_pred.skip_binder().trait_ref.args.type_at(0).is_unit()
&& let ty::Adt(def, _) = trait_pred.skip_binder().trait_ref.args.type_at(1).kind()
&& self.tcx.is_diagnostic_item(sym::Result, def.did())
{
let body = self.tcx.hir().body(body_id);
Expand Down Expand Up @@ -4863,14 +4862,13 @@ impl<'a, 'hir> hir::intravisit::Visitor<'hir> for ReplaceImplTraitVisitor<'a> {
pub(super) fn get_explanation_based_on_obligation<'tcx>(
tcx: TyCtxt<'tcx>,
obligation: &PredicateObligation<'tcx>,
trait_ref: ty::PolyTraitRef<'tcx>,
trait_predicate: &ty::PolyTraitPredicate<'tcx>,
trait_predicate: ty::PolyTraitPredicate<'tcx>,
pre_message: String,
) -> String {
if let ObligationCauseCode::MainFunctionType = obligation.cause.code() {
"consider using `()`, or a `Result`".to_owned()
} else {
let ty_desc = match trait_ref.skip_binder().self_ty().kind() {
let ty_desc = match trait_predicate.self_ty().skip_binder().kind() {
ty::FnDef(_, _) => Some("fn item"),
ty::Closure(_, _) => Some("closure"),
_ => None,
Expand All @@ -4895,7 +4893,7 @@ pub(super) fn get_explanation_based_on_obligation<'tcx>(
format!(
"{pre_message}the trait `{}` is not implemented for{desc} `{}`{post}",
trait_predicate.print_modifiers_and_trait_path(),
tcx.short_ty_string(trait_ref.skip_binder().self_ty(), &mut None),
tcx.short_ty_string(trait_predicate.self_ty().skip_binder(), &mut None),
)
} else {
// "the trait bound `T: !Send` is not satisfied" reads better than "`!Send` is
Expand Down
Loading

0 comments on commit 25c55c5

Please sign in to comment.