From 04c049498d8c24b890bc22f8ee2d541d5e4ee2f6 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 9 May 2024 14:09:55 -0400 Subject: [PATCH 1/5] rename some variants in FulfillmentErrorCode --- .../rustc_borrowck/src/diagnostics/mod.rs | 2 +- .../src/diagnostics/mutability_errors.rs | 2 +- .../src/fn_ctxt/adjust_fulfillment_errors.rs | 9 ++--- .../src/fn_ctxt/suggestions.rs | 2 +- compiler/rustc_infer/src/traits/mod.rs | 16 ++++----- .../src/traits/structural_impls.rs | 8 ++--- .../src/solve/fulfill.rs | 16 ++++----- .../error_reporting/type_err_ctxt_ext.rs | 17 +++++---- .../src/traits/fulfill.rs | 36 ++++++++----------- 9 files changed, 46 insertions(+), 62 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/mod.rs b/compiler/rustc_borrowck/src/diagnostics/mod.rs index 0b4018a23ce..88172c62a3b 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mod.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mod.rs @@ -1344,7 +1344,7 @@ fn explain_captures( Applicability::MaybeIncorrect, ); for error in errors { - if let FulfillmentErrorCode::SelectionError( + if let FulfillmentErrorCode::Select( SelectionError::Unimplemented, ) = error.code && let ty::PredicateKind::Clause(ty::ClauseKind::Trait( diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index 5d74a01aa20..c5f37200fcc 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -1283,7 +1283,7 @@ fn suggest_make_local_mut(&self, err: &mut Diag<'_>, local: Local, name: Symbol) } // The type doesn't implement Clone because of unmet obligations. for error in errors { - if let traits::FulfillmentErrorCode::SelectionError( + if let traits::FulfillmentErrorCode::Select( traits::SelectionError::Unimplemented, ) = error.code && let ty::PredicateKind::Clause(ty::ClauseKind::Trait( diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs index 2d98f312f8b..c41ad40aa79 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs @@ -360,12 +360,9 @@ fn closure_span_overlaps_error( error: &traits::FulfillmentError<'tcx>, span: Span, ) -> bool { - if let traits::FulfillmentErrorCode::SelectionError( - traits::SelectionError::SignatureMismatch(box traits::SignatureMismatchData { - expected_trait_ref, - .. - }), - ) = error.code + if let traits::FulfillmentErrorCode::Select(traits::SelectionError::SignatureMismatch( + box traits::SignatureMismatchData { expected_trait_ref, .. }, + )) = error.code && let ty::Closure(def_id, _) | ty::Coroutine(def_id, ..) = expected_trait_ref.self_ty().kind() && span.overlaps(self.tcx.def_span(*def_id)) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index f00d9d0ae3f..f1b719f24c7 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -1700,7 +1700,7 @@ pub(crate) fn note_type_is_not_clone( } } for error in errors { - if let traits::FulfillmentErrorCode::SelectionError( + if let traits::FulfillmentErrorCode::Select( traits::SelectionError::Unimplemented, ) = error.code && let ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) = diff --git a/compiler/rustc_infer/src/traits/mod.rs b/compiler/rustc_infer/src/traits/mod.rs index f77a6115861..ce6b3e8f487 100644 --- a/compiler/rustc_infer/src/traits/mod.rs +++ b/compiler/rustc_infer/src/traits/mod.rs @@ -138,10 +138,10 @@ pub enum FulfillmentErrorCode<'tcx> { /// Inherently impossible to fulfill; this trait is implemented if and only /// if it is already implemented. Cycle(Vec>), - SelectionError(SelectionError<'tcx>), - ProjectionError(MismatchedProjectionTypes<'tcx>), - SubtypeError(ExpectedFound>, TypeError<'tcx>), // always comes from a SubtypePredicate - ConstEquateError(ExpectedFound>, TypeError<'tcx>), + Select(SelectionError<'tcx>), + Project(MismatchedProjectionTypes<'tcx>), + Subtype(ExpectedFound>, TypeError<'tcx>), // always comes from a SubtypePredicate + ConstEquate(ExpectedFound>, TypeError<'tcx>), Ambiguity { /// Overflow is only `Some(suggest_recursion_limit)` when using the next generation /// trait solver `-Znext-solver`. With the old solver overflow is eagerly handled by @@ -209,10 +209,10 @@ pub fn new( pub fn is_true_error(&self) -> bool { match self.code { - FulfillmentErrorCode::SelectionError(_) - | FulfillmentErrorCode::ProjectionError(_) - | FulfillmentErrorCode::SubtypeError(_, _) - | FulfillmentErrorCode::ConstEquateError(_, _) => true, + FulfillmentErrorCode::Select(_) + | FulfillmentErrorCode::Project(_) + | FulfillmentErrorCode::Subtype(_, _) + | FulfillmentErrorCode::ConstEquate(_, _) => true, FulfillmentErrorCode::Cycle(_) | FulfillmentErrorCode::Ambiguity { overflow: _ } => { false } diff --git a/compiler/rustc_infer/src/traits/structural_impls.rs b/compiler/rustc_infer/src/traits/structural_impls.rs index 064e09b8750..b616d37e5b5 100644 --- a/compiler/rustc_infer/src/traits/structural_impls.rs +++ b/compiler/rustc_infer/src/traits/structural_impls.rs @@ -39,12 +39,12 @@ impl<'tcx> fmt::Debug for traits::FulfillmentErrorCode<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use traits::FulfillmentErrorCode::*; match *self { - SelectionError(ref e) => write!(f, "{e:?}"), - ProjectionError(ref e) => write!(f, "{e:?}"), - SubtypeError(ref a, ref b) => { + Select(ref e) => write!(f, "{e:?}"), + Project(ref e) => write!(f, "{e:?}"), + Subtype(ref a, ref b) => { write!(f, "CodeSubtypeError({a:?}, {b:?})") } - ConstEquateError(ref a, ref b) => { + ConstEquate(ref a, ref b) => { write!(f, "CodeConstEquateError({a:?}, {b:?})") } Ambiguity { overflow: None } => write!(f, "Ambiguity"), diff --git a/compiler/rustc_trait_selection/src/solve/fulfill.rs b/compiler/rustc_trait_selection/src/solve/fulfill.rs index 39d2ec4417c..f92b1eb88cd 100644 --- a/compiler/rustc_trait_selection/src/solve/fulfill.rs +++ b/compiler/rustc_trait_selection/src/solve/fulfill.rs @@ -203,39 +203,35 @@ fn fulfillment_error_for_no_solution<'tcx>( let code = match obligation.predicate.kind().skip_binder() { ty::PredicateKind::Clause(ty::ClauseKind::Projection(_)) => { - FulfillmentErrorCode::ProjectionError( + FulfillmentErrorCode::Project( // FIXME: This could be a `Sorts` if the term is a type MismatchedProjectionTypes { err: TypeError::Mismatch }, ) } ty::PredicateKind::NormalizesTo(..) => { - FulfillmentErrorCode::ProjectionError(MismatchedProjectionTypes { - err: TypeError::Mismatch, - }) + FulfillmentErrorCode::Project(MismatchedProjectionTypes { err: TypeError::Mismatch }) } ty::PredicateKind::AliasRelate(_, _, _) => { - FulfillmentErrorCode::ProjectionError(MismatchedProjectionTypes { - err: TypeError::Mismatch, - }) + FulfillmentErrorCode::Project(MismatchedProjectionTypes { err: TypeError::Mismatch }) } ty::PredicateKind::Subtype(pred) => { let (a, b) = infcx.enter_forall_and_leak_universe( obligation.predicate.kind().rebind((pred.a, pred.b)), ); let expected_found = ExpectedFound::new(true, a, b); - FulfillmentErrorCode::SubtypeError(expected_found, TypeError::Sorts(expected_found)) + FulfillmentErrorCode::Subtype(expected_found, TypeError::Sorts(expected_found)) } ty::PredicateKind::Coerce(pred) => { let (a, b) = infcx.enter_forall_and_leak_universe( obligation.predicate.kind().rebind((pred.a, pred.b)), ); let expected_found = ExpectedFound::new(false, a, b); - FulfillmentErrorCode::SubtypeError(expected_found, TypeError::Sorts(expected_found)) + FulfillmentErrorCode::Subtype(expected_found, TypeError::Sorts(expected_found)) } ty::PredicateKind::Clause(_) | ty::PredicateKind::ObjectSafe(_) | ty::PredicateKind::Ambiguous => { - FulfillmentErrorCode::SelectionError(SelectionError::Unimplemented) + FulfillmentErrorCode::Select(SelectionError::Unimplemented) } ty::PredicateKind::ConstEquate(..) => { bug!("unexpected goal: {obligation:?}") diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs index 0989d499497..da2e4ab35d2 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs @@ -1504,13 +1504,12 @@ fn report_fulfillment_error(&self, error: &FulfillmentError<'tcx>) -> ErrorGuara } match error.code { - FulfillmentErrorCode::SelectionError(ref selection_error) => self - .report_selection_error( - error.obligation.clone(), - &error.root_obligation, - selection_error, - ), - FulfillmentErrorCode::ProjectionError(ref e) => { + FulfillmentErrorCode::Select(ref selection_error) => self.report_selection_error( + error.obligation.clone(), + &error.root_obligation, + selection_error, + ), + FulfillmentErrorCode::Project(ref e) => { self.report_projection_error(&error.obligation, e) } FulfillmentErrorCode::Ambiguity { overflow: None } => { @@ -1519,7 +1518,7 @@ fn report_fulfillment_error(&self, error: &FulfillmentError<'tcx>) -> ErrorGuara FulfillmentErrorCode::Ambiguity { overflow: Some(suggest_increasing_limit) } => { self.report_overflow_no_abort(error.obligation.clone(), suggest_increasing_limit) } - FulfillmentErrorCode::SubtypeError(ref expected_found, ref err) => self + FulfillmentErrorCode::Subtype(ref expected_found, ref err) => self .report_mismatched_types( &error.obligation.cause, expected_found.expected, @@ -1527,7 +1526,7 @@ fn report_fulfillment_error(&self, error: &FulfillmentError<'tcx>) -> ErrorGuara *err, ) .emit(), - FulfillmentErrorCode::ConstEquateError(ref expected_found, ref err) => { + FulfillmentErrorCode::ConstEquate(ref expected_found, ref err) => { let mut diag = self.report_mismatched_consts( &error.obligation.cause, expected_found.expected, diff --git a/compiler/rustc_trait_selection/src/traits/fulfill.rs b/compiler/rustc_trait_selection/src/traits/fulfill.rs index 1f10cb71543..e3497c646db 100644 --- a/compiler/rustc_trait_selection/src/traits/fulfill.rs +++ b/compiler/rustc_trait_selection/src/traits/fulfill.rs @@ -411,7 +411,7 @@ fn process_obligation( ty::PredicateKind::ObjectSafe(trait_def_id) => { if !self.selcx.tcx().check_is_object_safe(trait_def_id) { - ProcessResult::Error(FulfillmentErrorCode::SelectionError(Unimplemented)) + ProcessResult::Error(FulfillmentErrorCode::Select(Unimplemented)) } else { ProcessResult::Changed(vec![]) } @@ -435,7 +435,7 @@ fn process_obligation( ty, ) { Ok(inf_ok) => ProcessResult::Changed(mk_pending(inf_ok.into_obligations())), - Err(_) => ProcessResult::Error(FulfillmentErrorCode::SelectionError( + Err(_) => ProcessResult::Error(FulfillmentErrorCode::Select( SelectionError::Unimplemented, )), } @@ -493,10 +493,7 @@ fn process_obligation( Ok(Err(err)) => { let expected_found = ExpectedFound::new(subtype.a_is_expected, subtype.a, subtype.b); - ProcessResult::Error(FulfillmentErrorCode::SubtypeError( - expected_found, - err, - )) + ProcessResult::Error(FulfillmentErrorCode::Subtype(expected_found, err)) } } } @@ -516,10 +513,7 @@ fn process_obligation( Ok(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)), Ok(Err(err)) => { let expected_found = ExpectedFound::new(false, coerce.a, coerce.b); - ProcessResult::Error(FulfillmentErrorCode::SubtypeError( - expected_found, - err, - )) + ProcessResult::Error(FulfillmentErrorCode::Subtype(expected_found, err)) } } } @@ -542,7 +536,7 @@ fn process_obligation( Err( e @ NotConstEvaluatable::MentionsParam | e @ NotConstEvaluatable::Error(_), - ) => ProcessResult::Error(FulfillmentErrorCode::SelectionError( + ) => ProcessResult::Error(FulfillmentErrorCode::Select( SelectionError::NotConstEvaluatable(e), )), } @@ -638,7 +632,7 @@ fn process_obligation( ProcessResult::Changed(mk_pending(inf_ok.into_obligations())) } Err(err) => { - ProcessResult::Error(FulfillmentErrorCode::ConstEquateError( + ProcessResult::Error(FulfillmentErrorCode::ConstEquate( ExpectedFound::new(true, c1, c2), err, )) @@ -646,13 +640,11 @@ fn process_obligation( } } (Err(ErrorHandled::Reported(reported, _)), _) - | (_, Err(ErrorHandled::Reported(reported, _))) => { - ProcessResult::Error(FulfillmentErrorCode::SelectionError( - SelectionError::NotConstEvaluatable(NotConstEvaluatable::Error( - reported.into(), - )), - )) - } + | (_, Err(ErrorHandled::Reported(reported, _))) => ProcessResult::Error( + FulfillmentErrorCode::Select(SelectionError::NotConstEvaluatable( + NotConstEvaluatable::Error(reported.into()), + )), + ), (Err(ErrorHandled::TooGeneric(_)), _) | (_, Err(ErrorHandled::TooGeneric(_))) => { if c1.has_non_region_infer() || c2.has_non_region_infer() { @@ -660,7 +652,7 @@ fn process_obligation( } else { // Two different constants using generic parameters ~> error. let expected_found = ExpectedFound::new(true, c1, c2); - ProcessResult::Error(FulfillmentErrorCode::ConstEquateError( + ProcessResult::Error(FulfillmentErrorCode::ConstEquate( expected_found, TypeError::ConstMismatch(expected_found), )) @@ -741,7 +733,7 @@ fn process_trait_obligation( Err(selection_err) => { debug!("selecting trait at depth {} yielded Err", obligation.recursion_depth); - ProcessResult::Error(FulfillmentErrorCode::SelectionError(selection_err)) + ProcessResult::Error(FulfillmentErrorCode::Select(selection_err)) } } } @@ -793,7 +785,7 @@ fn process_projection_obligation( project_obligation.with(tcx, project_obligation.predicate), ])), ProjectAndUnifyResult::MismatchedProjectionTypes(e) => { - ProcessResult::Error(FulfillmentErrorCode::ProjectionError(e)) + ProcessResult::Error(FulfillmentErrorCode::Project(e)) } } } From 4bde8a8f4b039c44baae49dea2f728a28d152122 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 9 May 2024 20:05:59 -0400 Subject: [PATCH 2/5] Remove glob imports for ObligationCauseCode --- .../src/check/compare_impl_item.rs | 7 +- .../rustc_hir_analysis/src/check/dropck.rs | 3 +- .../rustc_hir_analysis/src/check/wfcheck.rs | 24 +++- compiler/rustc_hir_typeck/src/callee.rs | 11 +- compiler/rustc_hir_typeck/src/check.rs | 17 ++- compiler/rustc_hir_typeck/src/closure.rs | 13 +- compiler/rustc_hir_typeck/src/expr.rs | 40 ++++-- .../rustc_hir_typeck/src/fn_ctxt/_impl.rs | 18 ++- .../src/fn_ctxt/adjust_fulfillment_errors.rs | 4 +- .../rustc_hir_typeck/src/fn_ctxt/checks.rs | 6 +- .../rustc_hir_typeck/src/gather_locals.rs | 10 +- compiler/rustc_hir_typeck/src/lib.rs | 3 +- .../rustc_hir_typeck/src/method/confirm.rs | 2 +- compiler/rustc_hir_typeck/src/method/probe.rs | 9 +- compiler/rustc_hir_typeck/src/op.rs | 4 +- compiler/rustc_hir_typeck/src/pat.rs | 9 +- .../src/infer/error_reporting/mod.rs | 127 ++++++++++-------- compiler/rustc_infer/src/traits/util.rs | 16 ++- compiler/rustc_middle/src/traits/mod.rs | 23 ++-- .../src/solve/fulfill.rs | 19 +-- .../src/traits/project.rs | 15 ++- .../src/traits/select/confirmation.rs | 18 +-- .../src/traits/select/mod.rs | 20 +-- .../rustc_trait_selection/src/traits/wf.rs | 39 +++--- 24 files changed, 279 insertions(+), 178 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index 81178344407..55bf06c1ec8 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -2012,9 +2012,9 @@ pub(super) fn check_type_bounds<'tcx>( ); let mk_cause = |span: Span| { let code = if span.is_dummy() { - traits::ItemObligation(trait_ty.def_id) + ObligationCauseCode::ItemObligation(trait_ty.def_id) } else { - traits::BindingObligation(trait_ty.def_id, span) + ObligationCauseCode::BindingObligation(trait_ty.def_id, span) }; ObligationCause::new(impl_ty_span, impl_ty_def_id, code) }; @@ -2251,7 +2251,8 @@ fn try_report_async_mismatch<'tcx>( }; for error in errors { - if let traits::BindingObligation(def_id, _) = *error.root_obligation.cause.code() + if let ObligationCauseCode::BindingObligation(def_id, _) = + *error.root_obligation.cause.code() && def_id == async_future_def_id && let Some(proj) = error.root_obligation.predicate.to_opt_poly_projection_pred() && let Some(proj) = proj.no_bound_vars() diff --git a/compiler/rustc_hir_analysis/src/check/dropck.rs b/compiler/rustc_hir_analysis/src/check/dropck.rs index 82a6b6b6f2c..be412dde968 100644 --- a/compiler/rustc_hir_analysis/src/check/dropck.rs +++ b/compiler/rustc_hir_analysis/src/check/dropck.rs @@ -5,6 +5,7 @@ use rustc_errors::{codes::*, struct_span_code_err, ErrorGuaranteed}; use rustc_infer::infer::outlives::env::OutlivesEnvironment; use rustc_infer::infer::{RegionResolutionError, TyCtxtInferExt}; +use rustc_infer::traits::ObligationCauseCode; use rustc_middle::ty::util::CheckRegions; use rustc_middle::ty::GenericArgsRef; use rustc_middle::ty::{self, TyCtxt}; @@ -139,7 +140,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>( for (pred, span) in tcx.predicates_of(drop_impl_def_id).instantiate_identity(tcx) { let normalize_cause = traits::ObligationCause::misc(span, adt_def_id); let pred = ocx.normalize(&normalize_cause, param_env, pred); - let cause = traits::ObligationCause::new(span, adt_def_id, traits::DropImpl); + let cause = traits::ObligationCause::new(span, adt_def_id, ObligationCauseCode::DropImpl); ocx.register_obligation(traits::Obligation::new(tcx, cause, param_env, pred)); } diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index ddfb88d72d8..124de69140e 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -1136,7 +1136,7 @@ fn check_type_defn<'tcx>( traits::ObligationCause::new( hir_ty.span, wfcx.body_def_id, - traits::FieldSized { + ObligationCauseCode::FieldSized { adt_kind: match &item.kind { ItemKind::Struct(..) => AdtKind::Struct, ItemKind::Union(..) => AdtKind::Union, @@ -1161,7 +1161,7 @@ fn check_type_defn<'tcx>( let cause = traits::ObligationCause::new( tcx.def_span(discr_def_id), wfcx.body_def_id, - traits::MiscObligation, + ObligationCauseCode::MiscObligation, ); wfcx.register_obligation(traits::Obligation::new( tcx, @@ -1278,7 +1278,11 @@ fn check_item_type( wfcx.register_wf_obligation(ty_span, Some(WellFormedLoc::Ty(item_id)), item_ty.into()); if forbid_unsized { wfcx.register_bound( - traits::ObligationCause::new(ty_span, wfcx.body_def_id, traits::WellFormed(None)), + traits::ObligationCause::new( + ty_span, + wfcx.body_def_id, + ObligationCauseCode::WellFormed(None), + ), wfcx.param_env, item_ty, tcx.require_lang_item(LangItem::Sized, None), @@ -1293,7 +1297,11 @@ fn check_item_type( if should_check_for_sync { wfcx.register_bound( - traits::ObligationCause::new(ty_span, wfcx.body_def_id, traits::SharedStatic), + traits::ObligationCause::new( + ty_span, + wfcx.body_def_id, + ObligationCauseCode::SharedStatic, + ), wfcx.param_env, item_ty, tcx.require_lang_item(LangItem::Sync, Some(ty_span)), @@ -1542,7 +1550,7 @@ fn visit_const(&mut self, c: ty::Const<'tcx>) -> Self::Result { let cause = traits::ObligationCause::new( sp, wfcx.body_def_id, - traits::ItemObligation(def_id.to_def_id()), + ObligationCauseCode::ItemObligation(def_id.to_def_id()), ); traits::Obligation::new(tcx, cause, wfcx.param_env, pred) }); @@ -1982,7 +1990,11 @@ fn check_false_global_bounds(&mut self) { let obligation = traits::Obligation::new( tcx, - traits::ObligationCause::new(span, self.body_def_id, traits::TrivialBound), + traits::ObligationCause::new( + span, + self.body_def_id, + ObligationCauseCode::TrivialBound, + ), empty_env, pred, ); diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index 1a015eb0d96..defb557867b 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -9,6 +9,7 @@ use rustc_hir::def::{self, CtorKind, Namespace, Res}; use rustc_hir::def_id::DefId; use rustc_hir_analysis::autoderef::Autoderef; +use rustc_infer::traits::ObligationCauseCode; use rustc_infer::{ infer, traits::{self, Obligation, ObligationCause}, @@ -117,7 +118,11 @@ pub fn check_call( }; // we must check that return type of called functions is WF: - self.register_wf_obligation(output.into(), call_expr.span, traits::WellFormed(None)); + self.register_wf_obligation( + output.into(), + call_expr.span, + ObligationCauseCode::WellFormed(None), + ); output } @@ -524,9 +529,9 @@ fn confirm_builtin_call( self.register_bound( ty, self.tcx.require_lang_item(hir::LangItem::Tuple, Some(sp)), - traits::ObligationCause::new(sp, self.body_id, traits::RustCall), + traits::ObligationCause::new(sp, self.body_id, ObligationCauseCode::RustCall), ); - self.require_type_is_sized(ty, sp, traits::RustCall); + self.require_type_is_sized(ty, sp, ObligationCauseCode::RustCall); } else { self.dcx().emit_err(errors::RustCallIncorrectArgs { span: sp }); } diff --git a/compiler/rustc_hir_typeck/src/check.rs b/compiler/rustc_hir_typeck/src/check.rs index 386edc37765..89e64cf5f0d 100644 --- a/compiler/rustc_hir_typeck/src/check.rs +++ b/compiler/rustc_hir_typeck/src/check.rs @@ -14,7 +14,6 @@ use rustc_span::def_id::LocalDefId; use rustc_span::symbol::sym; use rustc_target::spec::abi::Abi; -use rustc_trait_selection::traits; use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode}; /// Helper used for fns and closures. Does the grungy work of checking a function @@ -76,7 +75,7 @@ pub(super) fn check_fn<'a, 'tcx>( fcx.register_wf_obligation( param_ty.into(), param.span, - traits::WellFormed(Some(WellFormedLoc::Param { + ObligationCauseCode::WellFormed(Some(WellFormedLoc::Param { function: fn_def_id, param_idx: idx, })), @@ -101,7 +100,7 @@ pub(super) fn check_fn<'a, 'tcx>( param.pat.span, // ty.span == binding_span iff this is a closure parameter with no type ascription, // or if it's an implicit `self` parameter - traits::SizedArgumentType( + ObligationCauseCode::SizedArgumentType( if ty_span == Some(param.span) && tcx.is_closure_like(fn_def_id.into()) { None } else { @@ -121,10 +120,18 @@ pub(super) fn check_fn<'a, 'tcx>( hir::FnRetTy::Return(ty) => ty.span, }; - fcx.require_type_is_sized(declared_ret_ty, return_or_body_span, traits::SizedReturnType); + fcx.require_type_is_sized( + declared_ret_ty, + return_or_body_span, + ObligationCauseCode::SizedReturnType, + ); // We checked the root's signature during wfcheck, but not the child. if fcx.tcx.is_typeck_child(fn_def_id.to_def_id()) { - fcx.require_type_is_sized(declared_ret_ty, return_or_body_span, traits::WellFormed(None)); + fcx.require_type_is_sized( + declared_ret_ty, + return_or_body_span, + ObligationCauseCode::WellFormed(None), + ); } fcx.is_whole_body.set(true); diff --git a/compiler/rustc_hir_typeck/src/closure.rs b/compiler/rustc_hir_typeck/src/closure.rs index 6c73c77bf52..f52f95db6d3 100644 --- a/compiler/rustc_hir_typeck/src/closure.rs +++ b/compiler/rustc_hir_typeck/src/closure.rs @@ -8,6 +8,7 @@ use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer; use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes}; use rustc_infer::infer::{InferOk, InferResult}; +use rustc_infer::traits::ObligationCauseCode; use rustc_macros::{TypeFoldable, TypeVisitable}; use rustc_middle::ty::visit::{TypeVisitable, TypeVisitableExt}; use rustc_middle::ty::GenericArgs; @@ -119,7 +120,11 @@ pub fn check_expr_closure( hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _) | hir::CoroutineKind::Coroutine(_) => { let yield_ty = self.next_ty_var(expr_span); - self.require_type_is_sized(yield_ty, expr_span, traits::SizedYieldType); + self.require_type_is_sized( + yield_ty, + expr_span, + ObligationCauseCode::SizedYieldType, + ); yield_ty } // HACK(-Ztrait-solver=next): In the *old* trait solver, we must eagerly @@ -128,7 +133,11 @@ pub fn check_expr_closure( // not a problem. hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _) => { let yield_ty = self.next_ty_var(expr_span); - self.require_type_is_sized(yield_ty, expr_span, traits::SizedYieldType); + self.require_type_is_sized( + yield_ty, + expr_span, + ObligationCauseCode::SizedYieldType, + ); Ty::new_adt( tcx, diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index a7e147b4b18..00371f21277 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -573,7 +573,7 @@ pub(crate) fn check_expr_path( self.require_type_is_sized_deferred( input, span, - traits::SizedArgumentType(None), + ObligationCauseCode::SizedArgumentType(None), ); } } @@ -591,7 +591,7 @@ pub(crate) fn check_expr_path( self.require_type_is_sized_deferred( output, call.map_or(expr.span, |e| e.span), - traits::SizedCallReturnType, + ObligationCauseCode::SizedCallReturnType, ); } @@ -1249,7 +1249,7 @@ fn check_expr_assign( } }); - self.require_type_is_sized(lhs_ty, lhs.span, traits::AssignmentLhsSized); + self.require_type_is_sized(lhs_ty, lhs.span, ObligationCauseCode::AssignmentLhsSized); if let Err(guar) = (lhs_ty, rhs_ty).error_reported() { Ty::new_error(self.tcx, guar) @@ -1471,7 +1471,7 @@ fn check_expr_const_block( crate::GatherLocalsVisitor::new(&fcx).visit_body(body); let ty = fcx.check_expr_with_expectation(body.value, expected); - fcx.require_type_is_sized(ty, body.value.span, traits::ConstSized); + fcx.require_type_is_sized(ty, body.value.span, ObligationCauseCode::ConstSized); fcx.write_ty(block.hir_id, ty); ty } @@ -1517,7 +1517,7 @@ fn check_expr_repeat( let ty = Ty::new_array_with_const_len(tcx, t, count); - self.register_wf_obligation(ty.into(), expr.span, traits::WellFormed(None)); + self.register_wf_obligation(ty.into(), expr.span, ObligationCauseCode::WellFormed(None)); ty } @@ -1607,7 +1607,11 @@ fn check_expr_tuple( if let Err(guar) = tuple.error_reported() { Ty::new_error(self.tcx, guar) } else { - self.require_type_is_sized(tuple, expr.span, traits::TupleInitializerSized); + self.require_type_is_sized( + tuple, + expr.span, + ObligationCauseCode::TupleInitializerSized, + ); tuple } } @@ -1646,7 +1650,7 @@ fn check_expr_struct( base_expr, ); - self.require_type_is_sized(adt_ty, expr.span, traits::StructInitializerSized); + self.require_type_is_sized(adt_ty, expr.span, ObligationCauseCode::StructInitializerSized); adt_ty } @@ -3079,7 +3083,7 @@ fn find_and_report_unsatisfied_index_impl( polarity: ty::PredicatePolarity::Positive, }), |derived| { - traits::ImplDerivedObligation(Box::new( + ObligationCauseCode::ImplDerivedObligation(Box::new( traits::ImplDerivedObligationCause { derived, impl_or_alias_def_id: impl_def_id, @@ -3177,7 +3181,7 @@ fn check_expr_yield( fn check_expr_asm_operand(&self, expr: &'tcx hir::Expr<'tcx>, is_input: bool) { let needs = if is_input { Needs::None } else { Needs::MutPlace }; let ty = self.check_expr_with_needs(expr, needs); - self.require_type_is_sized(ty, expr.span, traits::InlineAsmSized); + self.require_type_is_sized(ty, expr.span, ObligationCauseCode::InlineAsmSized); if !is_input && !expr.is_syntactic_place_expr() { self.dcx() @@ -3348,7 +3352,11 @@ fn check_offset_of( let field_ty = self.field_ty(expr.span, field, args); // FIXME: DSTs with static alignment should be allowed - self.require_type_is_sized(field_ty, expr.span, traits::MiscObligation); + self.require_type_is_sized( + field_ty, + expr.span, + ObligationCauseCode::MiscObligation, + ); if field.vis.is_accessible_from(sub_def_scope, self.tcx) { self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None); @@ -3376,7 +3384,11 @@ fn check_offset_of( let field_ty = self.field_ty(expr.span, field, args); // FIXME: DSTs with static alignment should be allowed - self.require_type_is_sized(field_ty, expr.span, traits::MiscObligation); + self.require_type_is_sized( + field_ty, + expr.span, + ObligationCauseCode::MiscObligation, + ); if field.vis.is_accessible_from(def_scope, self.tcx) { self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None); @@ -3397,7 +3409,11 @@ fn check_offset_of( && field.name == sym::integer(index) { for ty in tys.iter().take(index + 1) { - self.require_type_is_sized(ty, expr.span, traits::MiscObligation); + self.require_type_is_sized( + ty, + expr.span, + ObligationCauseCode::MiscObligation, + ); } if let Some(&field_ty) = tys.get(index) { field_indices.push((FIRST_VARIANT, index.into())); diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 552747bdc52..887c0c9f4f0 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -409,7 +409,7 @@ pub fn register_bound( pub fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> LoweredTy<'tcx> { let ty = self.lowerer().lower_ty(hir_ty); - self.register_wf_obligation(ty.into(), hir_ty.span, traits::WellFormed(None)); + self.register_wf_obligation(ty.into(), hir_ty.span, ObligationCauseCode::WellFormed(None)); LoweredTy::from_raw(self, hir_ty.span, ty) } @@ -520,7 +520,7 @@ pub fn add_wf_bounds(&self, args: GenericArgsRef<'tcx>, expr: &hir::Expr<'_>) { for arg in args.iter().filter(|arg| { matches!(arg.unpack(), GenericArgKind::Type(..) | GenericArgKind::Const(..)) }) { - self.register_wf_obligation(arg, expr.span, traits::WellFormed(None)); + self.register_wf_obligation(arg, expr.span, ObligationCauseCode::WellFormed(None)); } } @@ -775,7 +775,11 @@ pub fn resolve_ty_and_res_fully_qualified_call( }; if let Some(&cached_result) = self.typeck_results.borrow().type_dependent_defs().get(hir_id) { - self.register_wf_obligation(ty.raw.into(), qself.span, traits::WellFormed(None)); + self.register_wf_obligation( + ty.raw.into(), + qself.span, + ObligationCauseCode::WellFormed(None), + ); // Return directly on cache hit. This is useful to avoid doubly reporting // errors with default match binding modes. See #44614. let def = cached_result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id)); @@ -814,7 +818,7 @@ pub fn resolve_ty_and_res_fully_qualified_call( self.register_wf_obligation( ty.raw.into(), qself.span, - traits::WellFormed(None), + ObligationCauseCode::WellFormed(None), ); } @@ -848,7 +852,11 @@ pub fn resolve_ty_and_res_fully_qualified_call( }); if result.is_ok() { - self.register_wf_obligation(ty.raw.into(), qself.span, traits::WellFormed(None)); + self.register_wf_obligation( + ty.raw.into(), + qself.span, + ObligationCauseCode::WellFormed(None), + ); } // Write back the new resolution. diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs index c41ad40aa79..56745c1245b 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs @@ -14,8 +14,8 @@ pub fn adjust_fulfillment_error_for_expr_obligation( &self, error: &mut traits::FulfillmentError<'tcx>, ) -> bool { - let (traits::ExprItemObligation(def_id, hir_id, idx) - | traits::ExprBindingObligation(def_id, _, hir_id, idx)) = + let (ObligationCauseCode::ExprItemObligation(def_id, hir_id, idx) + | ObligationCauseCode::ExprBindingObligation(def_id, _, hir_id, idx)) = *error.obligation.cause.code().peel_derives() else { return false; diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 7266be17649..73bc5fd7d50 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -203,7 +203,11 @@ pub(in super::super) fn check_argument_types( // All the input types from the fn signature must outlive the call // so as to validate implied bounds. for (&fn_input_ty, arg_expr) in iter::zip(formal_input_tys, provided_args) { - self.register_wf_obligation(fn_input_ty.into(), arg_expr.span, traits::MiscObligation); + self.register_wf_obligation( + fn_input_ty.into(), + arg_expr.span, + ObligationCauseCode::MiscObligation, + ); } let mut err_code = E0061; diff --git a/compiler/rustc_hir_typeck/src/gather_locals.rs b/compiler/rustc_hir_typeck/src/gather_locals.rs index af5cbad32c8..13e4b625e2d 100644 --- a/compiler/rustc_hir_typeck/src/gather_locals.rs +++ b/compiler/rustc_hir_typeck/src/gather_locals.rs @@ -2,11 +2,11 @@ use rustc_hir as hir; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{HirId, PatKind}; +use rustc_infer::traits::ObligationCauseCode; use rustc_middle::ty::Ty; use rustc_middle::ty::UserType; use rustc_span::def_id::LocalDefId; use rustc_span::Span; -use rustc_trait_selection::traits; /// Provides context for checking patterns in declarations. More specifically this /// allows us to infer array types if the pattern is irrefutable and allows us to infer @@ -146,7 +146,7 @@ fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) { p.span, // ty_span == ident.span iff this is a closure parameter with no type // ascription, or if it's an implicit `self` parameter - traits::SizedArgumentType( + ObligationCauseCode::SizedArgumentType( if ty_span == ident.span && self.fcx.tcx.is_closure_like(self.fcx.body_id.into()) { @@ -159,7 +159,11 @@ fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) { } } else { if !self.fcx.tcx.features().unsized_locals { - self.fcx.require_type_is_sized(var_ty, p.span, traits::VariableType(p.hir_id)); + self.fcx.require_type_is_sized( + var_ty, + p.span, + ObligationCauseCode::VariableType(p.hir_id), + ); } } diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs index 53ccc841ca9..e270e589505 100644 --- a/compiler/rustc_hir_typeck/src/lib.rs +++ b/compiler/rustc_hir_typeck/src/lib.rs @@ -62,7 +62,6 @@ use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer; use rustc_infer::traits::{ObligationCauseCode, ObligationInspector, WellFormedLoc}; use rustc_middle::query::Providers; -use rustc_middle::traits; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_session::config; use rustc_span::def_id::LocalDefId; @@ -170,7 +169,7 @@ fn typeck_with_fallback<'tcx>( let wf_code = ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(def_id))); fcx.register_wf_obligation(expected_type.into(), body.value.span, wf_code); - fcx.require_type_is_sized(expected_type, body.value.span, traits::ConstSized); + fcx.require_type_is_sized(expected_type, body.value.span, ObligationCauseCode::ConstSized); // Gather locals in statics (because of block expressions). GatherLocalsVisitor::new(&fcx).visit_body(body); diff --git a/compiler/rustc_hir_typeck/src/method/confirm.rs b/compiler/rustc_hir_typeck/src/method/confirm.rs index 128e599618a..f844083b57c 100644 --- a/compiler/rustc_hir_typeck/src/method/confirm.rs +++ b/compiler/rustc_hir_typeck/src/method/confirm.rs @@ -589,7 +589,7 @@ fn add_obligations( // the function type must also be well-formed (this is not // implied by the args being well-formed because of inherent // impls and late-bound regions - see issue #28609). - self.register_wf_obligation(fty.into(), self.span, traits::WellFormed(None)); + self.register_wf_obligation(fty.into(), self.span, ObligationCauseCode::WellFormed(None)); } /////////////////////////////////////////////////////////////////////////// diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index 5708630ca3c..163fcbf8cbb 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -15,6 +15,7 @@ use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282; use rustc_infer::infer::DefineOpaqueTypes; use rustc_infer::infer::{self, InferOk, TyCtxtInferExt}; +use rustc_infer::traits::ObligationCauseCode; use rustc_middle::middle::stability; use rustc_middle::query::Providers; use rustc_middle::ty::fast_reject::{simplify_type, TreatParams}; @@ -1401,9 +1402,13 @@ fn consider_probe( ocx.register_obligations(traits::predicates_for_generics( |idx, span| { let code = if span.is_dummy() { - traits::ExprItemObligation(impl_def_id, self.scope_expr_id, idx) + ObligationCauseCode::ExprItemObligation( + impl_def_id, + self.scope_expr_id, + idx, + ) } else { - traits::ExprBindingObligation( + ObligationCauseCode::ExprBindingObligation( impl_def_id, span, self.scope_expr_id, diff --git a/compiler/rustc_hir_typeck/src/op.rs b/compiler/rustc_hir_typeck/src/op.rs index 3df553bb037..dca1cda0694 100644 --- a/compiler/rustc_hir_typeck/src/op.rs +++ b/compiler/rustc_hir_typeck/src/op.rs @@ -19,7 +19,7 @@ use rustc_span::Span; use rustc_trait_selection::infer::InferCtxtExt; use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt as _; -use rustc_trait_selection::traits::{self, FulfillmentError, ObligationCtxt}; +use rustc_trait_selection::traits::{FulfillmentError, ObligationCtxt}; use rustc_type_ir::TyKind::*; impl<'a, 'tcx> FnCtxt<'a, 'tcx> { @@ -884,7 +884,7 @@ fn lookup_op_method( let input_types = opt_rhs_ty.as_slice(); let cause = self.cause( span, - traits::BinOp { + ObligationCauseCode::BinOp { lhs_hir_id: lhs_expr.hir_id, rhs_hir_id: opt_rhs_expr.map(|expr| expr.hir_id), rhs_span: opt_rhs_expr.map(|expr| expr.span), diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index fe2deee378e..0e677d0560f 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -18,7 +18,7 @@ use rustc_span::symbol::{kw, sym, Ident}; use rustc_span::{BytePos, Span, DUMMY_SP}; use rustc_target::abi::FieldIdx; -use rustc_trait_selection::traits::{ObligationCause, Pattern}; +use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode}; use ty::VariantDef; use std::cmp; @@ -88,8 +88,11 @@ struct PatInfo<'tcx, 'a> { impl<'tcx> FnCtxt<'_, 'tcx> { fn pattern_cause(&self, ti: TopInfo<'tcx>, cause_span: Span) -> ObligationCause<'tcx> { - let code = - Pattern { span: ti.span, root_ty: ti.expected, origin_expr: ti.origin_expr.is_some() }; + let code = ObligationCauseCode::Pattern { + span: ti.span, + root_ty: ti.expected, + origin_expr: ti.origin_expr.is_some(), + }; self.cause(cause_span, code) } diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 6370794648a..56f8d4d34f6 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -2773,19 +2773,17 @@ pub enum FailureCode { #[extension(pub trait ObligationCauseExt<'tcx>)] impl<'tcx> ObligationCause<'tcx> { fn as_failure_code(&self, terr: TypeError<'tcx>) -> FailureCode { - use self::FailureCode::*; - use crate::traits::ObligationCauseCode::*; match self.code() { - IfExpressionWithNoElse => Error0317, - MainFunctionType => Error0580, - CompareImplItemObligation { .. } - | MatchExpressionArm(_) - | IfExpression { .. } - | LetElse - | StartFunctionType - | LangFunctionType(_) - | IntrinsicType - | MethodReceiver => Error0308, + ObligationCauseCode::IfExpressionWithNoElse => FailureCode::Error0317, + ObligationCauseCode::MainFunctionType => FailureCode::Error0580, + ObligationCauseCode::CompareImplItemObligation { .. } + | ObligationCauseCode::MatchExpressionArm(_) + | ObligationCauseCode::IfExpression { .. } + | ObligationCauseCode::LetElse + | ObligationCauseCode::StartFunctionType + | ObligationCauseCode::LangFunctionType(_) + | ObligationCauseCode::IntrinsicType + | ObligationCauseCode::MethodReceiver => FailureCode::Error0308, // In the case where we have no more specific thing to // say, also take a look at the error code, maybe we can @@ -2794,10 +2792,10 @@ fn as_failure_code(&self, terr: TypeError<'tcx>) -> FailureCode { TypeError::CyclicTy(ty) if ty.is_closure() || ty.is_coroutine() || ty.is_coroutine_closure() => { - Error0644 + FailureCode::Error0644 } - TypeError::IntrinsicCast => Error0308, - _ => Error0308, + TypeError::IntrinsicCast => FailureCode::Error0308, + _ => FailureCode::Error0308, }, } } @@ -2807,36 +2805,51 @@ fn as_failure_code_diag( span: Span, subdiags: Vec, ) -> ObligationCauseFailureCode { - use crate::traits::ObligationCauseCode::*; match self.code() { - CompareImplItemObligation { kind: ty::AssocKind::Fn, .. } => { + ObligationCauseCode::CompareImplItemObligation { kind: ty::AssocKind::Fn, .. } => { ObligationCauseFailureCode::MethodCompat { span, subdiags } } - CompareImplItemObligation { kind: ty::AssocKind::Type, .. } => { - ObligationCauseFailureCode::TypeCompat { span, subdiags } - } - CompareImplItemObligation { kind: ty::AssocKind::Const, .. } => { - ObligationCauseFailureCode::ConstCompat { span, subdiags } - } - BlockTailExpression(.., hir::MatchSource::TryDesugar(_)) => { + ObligationCauseCode::CompareImplItemObligation { + kind: ty::AssocKind::Type, .. + } => ObligationCauseFailureCode::TypeCompat { span, subdiags }, + ObligationCauseCode::CompareImplItemObligation { + kind: ty::AssocKind::Const, .. + } => ObligationCauseFailureCode::ConstCompat { span, subdiags }, + ObligationCauseCode::BlockTailExpression(.., hir::MatchSource::TryDesugar(_)) => { ObligationCauseFailureCode::TryCompat { span, subdiags } } - MatchExpressionArm(box MatchExpressionArmCause { source, .. }) => match source { + ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause { + source, .. + }) => match source { hir::MatchSource::TryDesugar(_) => { ObligationCauseFailureCode::TryCompat { span, subdiags } } _ => ObligationCauseFailureCode::MatchCompat { span, subdiags }, }, - IfExpression { .. } => ObligationCauseFailureCode::IfElseDifferent { span, subdiags }, - IfExpressionWithNoElse => ObligationCauseFailureCode::NoElse { span }, - LetElse => ObligationCauseFailureCode::NoDiverge { span, subdiags }, - MainFunctionType => ObligationCauseFailureCode::FnMainCorrectType { span }, - StartFunctionType => ObligationCauseFailureCode::FnStartCorrectType { span, subdiags }, - &LangFunctionType(lang_item_name) => { + ObligationCauseCode::IfExpression { .. } => { + ObligationCauseFailureCode::IfElseDifferent { span, subdiags } + } + ObligationCauseCode::IfExpressionWithNoElse => { + ObligationCauseFailureCode::NoElse { span } + } + ObligationCauseCode::LetElse => { + ObligationCauseFailureCode::NoDiverge { span, subdiags } + } + ObligationCauseCode::MainFunctionType => { + ObligationCauseFailureCode::FnMainCorrectType { span } + } + ObligationCauseCode::StartFunctionType => { + ObligationCauseFailureCode::FnStartCorrectType { span, subdiags } + } + &ObligationCauseCode::LangFunctionType(lang_item_name) => { ObligationCauseFailureCode::FnLangCorrectType { span, subdiags, lang_item_name } } - IntrinsicType => ObligationCauseFailureCode::IntrinsicCorrectType { span, subdiags }, - MethodReceiver => ObligationCauseFailureCode::MethodCorrectType { span, subdiags }, + ObligationCauseCode::IntrinsicType => { + ObligationCauseFailureCode::IntrinsicCorrectType { span, subdiags } + } + ObligationCauseCode::MethodReceiver => { + ObligationCauseFailureCode::MethodCorrectType { span, subdiags } + } // In the case where we have no more specific thing to // say, also take a look at the error code, maybe we can @@ -2856,22 +2869,21 @@ fn as_failure_code_diag( } fn as_requirement_str(&self) -> &'static str { - use crate::traits::ObligationCauseCode::*; match self.code() { - CompareImplItemObligation { kind: ty::AssocKind::Fn, .. } => { + ObligationCauseCode::CompareImplItemObligation { kind: ty::AssocKind::Fn, .. } => { "method type is compatible with trait" } - CompareImplItemObligation { kind: ty::AssocKind::Type, .. } => { - "associated type is compatible with trait" - } - CompareImplItemObligation { kind: ty::AssocKind::Const, .. } => { - "const is compatible with trait" - } - MainFunctionType => "`main` function has the correct type", - StartFunctionType => "`#[start]` function has the correct type", - LangFunctionType(_) => "lang item function has the correct type", - IntrinsicType => "intrinsic has the correct type", - MethodReceiver => "method receiver has the correct type", + ObligationCauseCode::CompareImplItemObligation { + kind: ty::AssocKind::Type, .. + } => "associated type is compatible with trait", + ObligationCauseCode::CompareImplItemObligation { + kind: ty::AssocKind::Const, .. + } => "const is compatible with trait", + ObligationCauseCode::MainFunctionType => "`main` function has the correct type", + ObligationCauseCode::StartFunctionType => "`#[start]` function has the correct type", + ObligationCauseCode::LangFunctionType(_) => "lang item function has the correct type", + ObligationCauseCode::IntrinsicType => "intrinsic has the correct type", + ObligationCauseCode::MethodReceiver => "method receiver has the correct type", _ => "types are compatible", } } @@ -2882,16 +2894,21 @@ fn as_requirement_str(&self) -> &'static str { impl IntoDiagArg for ObligationCauseAsDiagArg<'_> { fn into_diag_arg(self) -> rustc_errors::DiagArgValue { - use crate::traits::ObligationCauseCode::*; let kind = match self.0.code() { - CompareImplItemObligation { kind: ty::AssocKind::Fn, .. } => "method_compat", - CompareImplItemObligation { kind: ty::AssocKind::Type, .. } => "type_compat", - CompareImplItemObligation { kind: ty::AssocKind::Const, .. } => "const_compat", - MainFunctionType => "fn_main_correct_type", - StartFunctionType => "fn_start_correct_type", - LangFunctionType(_) => "fn_lang_correct_type", - IntrinsicType => "intrinsic_correct_type", - MethodReceiver => "method_correct_type", + ObligationCauseCode::CompareImplItemObligation { kind: ty::AssocKind::Fn, .. } => { + "method_compat" + } + ObligationCauseCode::CompareImplItemObligation { + kind: ty::AssocKind::Type, .. + } => "type_compat", + ObligationCauseCode::CompareImplItemObligation { + kind: ty::AssocKind::Const, .. + } => "const_compat", + ObligationCauseCode::MainFunctionType => "fn_main_correct_type", + ObligationCauseCode::StartFunctionType => "fn_start_correct_type", + ObligationCauseCode::LangFunctionType(_) => "fn_lang_correct_type", + ObligationCauseCode::IntrinsicType => "intrinsic_correct_type", + ObligationCauseCode::MethodReceiver => "method_correct_type", _ => "other", } .into(); diff --git a/compiler/rustc_infer/src/traits/util.rs b/compiler/rustc_infer/src/traits/util.rs index 6d43011d33c..11e0d8fac3c 100644 --- a/compiler/rustc_infer/src/traits/util.rs +++ b/compiler/rustc_infer/src/traits/util.rs @@ -1,7 +1,7 @@ use smallvec::smallvec; use crate::infer::outlives::components::{push_outlives_components, Component}; -use crate::traits::{self, Obligation, PredicateObligation}; +use crate::traits::{self, Obligation, ObligationCauseCode, PredicateObligation}; use rustc_data_structures::fx::FxHashSet; use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt}; use rustc_span::symbol::Ident; @@ -129,12 +129,14 @@ fn child_with_derived_cause( index: usize, ) -> Self { let cause = self.cause.clone().derived_cause(parent_trait_pred, |derived| { - traits::ImplDerivedObligation(Box::new(traits::ImplDerivedObligationCause { - derived, - impl_or_alias_def_id: parent_trait_pred.def_id(), - impl_def_predicate_index: Some(index), - span, - })) + ObligationCauseCode::ImplDerivedObligation(Box::new( + traits::ImplDerivedObligationCause { + derived, + impl_or_alias_def_id: parent_trait_pred.def_id(), + impl_def_predicate_index: Some(index), + span, + }, + )) }); Obligation { cause, diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs index 1ae037e09a7..50b85d895ad 100644 --- a/compiler/rustc_middle/src/traits/mod.rs +++ b/compiler/rustc_middle/src/traits/mod.rs @@ -33,8 +33,6 @@ pub use self::select::{EvaluationCache, EvaluationResult, OverflowError, SelectionCache}; -pub use self::ObligationCauseCode::*; - /// Depending on the stage of compilation, we want projection to be /// more or less conservative. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, HashStable, Encodable, Decodable)] @@ -129,7 +127,7 @@ pub fn new( } pub fn misc(span: Span, body_id: LocalDefId) -> ObligationCause<'tcx> { - ObligationCause::new(span, body_id, MiscObligation) + ObligationCause::new(span, body_id, ObligationCauseCode::MiscObligation) } #[inline(always)] @@ -189,8 +187,8 @@ pub fn derived_cause( pub fn to_constraint_category(&self) -> ConstraintCategory<'tcx> { match self.code() { - MatchImpl(cause, _) => cause.to_constraint_category(), - AscribeUserTypeProvePredicate(predicate_span) => { + ObligationCauseCode::MatchImpl(cause, _) => cause.to_constraint_category(), + ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span) => { ConstraintCategory::Predicate(*predicate_span) } _ => ConstraintCategory::BoringNoLocation, @@ -540,19 +538,22 @@ pub fn peel_derives_with_predicate(&self) -> (&Self, Option Option<(&Self, Option>)> { match self { - FunctionArgumentObligation { parent_code, .. } => Some((parent_code, None)), - BuiltinDerivedObligation(derived) - | WellFormedDerivedObligation(derived) - | ImplDerivedObligation(box ImplDerivedObligationCause { derived, .. }) => { - Some((&derived.parent_code, Some(derived.parent_trait_pred))) + ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } => { + Some((parent_code, None)) } + ObligationCauseCode::BuiltinDerivedObligation(derived) + | ObligationCauseCode::WellFormedDerivedObligation(derived) + | ObligationCauseCode::ImplDerivedObligation(box ImplDerivedObligationCause { + derived, + .. + }) => Some((&derived.parent_code, Some(derived.parent_trait_pred))), _ => None, } } pub fn peel_match_impls(&self) -> &Self { match self { - MatchImpl(cause, _) => cause.code(), + ObligationCauseCode::MatchImpl(cause, _) => cause.code(), _ => self, } } diff --git a/compiler/rustc_trait_selection/src/solve/fulfill.rs b/compiler/rustc_trait_selection/src/solve/fulfill.rs index f92b1eb88cd..893a6428a5e 100644 --- a/compiler/rustc_trait_selection/src/solve/fulfill.rs +++ b/compiler/rustc_trait_selection/src/solve/fulfill.rs @@ -7,7 +7,7 @@ use rustc_infer::traits::solve::{CandidateSource, GoalSource, MaybeCause}; use rustc_infer::traits::{ self, FulfillmentError, FulfillmentErrorCode, MismatchedProjectionTypes, Obligation, - ObligationCause, PredicateObligation, SelectionError, TraitEngine, + ObligationCause, ObligationCauseCode, PredicateObligation, SelectionError, TraitEngine, }; use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::{self, TyCtxt}; @@ -425,17 +425,20 @@ fn derive_cause<'tcx>( tcx.predicates_of(impl_def_id).instantiate_identity(tcx).iter().nth(idx) { cause = cause.derived_cause(parent_trait_pred, |derived| { - traits::ImplDerivedObligation(Box::new(traits::ImplDerivedObligationCause { - derived, - impl_or_alias_def_id: impl_def_id, - impl_def_predicate_index: Some(idx), - span, - })) + ObligationCauseCode::ImplDerivedObligation(Box::new( + traits::ImplDerivedObligationCause { + derived, + impl_or_alias_def_id: impl_def_id, + impl_def_predicate_index: Some(idx), + span, + }, + )) }) } } ProbeKind::TraitCandidate { source: CandidateSource::BuiltinImpl(..), result: _ } => { - cause = cause.derived_cause(parent_trait_pred, traits::BuiltinDerivedObligation); + cause = cause + .derived_cause(parent_trait_pred, ObligationCauseCode::BuiltinDerivedObligation); } _ => {} }; diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 587f2f72207..0ef007e30f1 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -13,6 +13,7 @@ use super::SelectionContext; use super::SelectionError; use super::{Normalized, NormalizedTy, ProjectionCacheEntry, ProjectionCacheKey}; +use rustc_infer::traits::ObligationCauseCode; use rustc_middle::traits::BuiltinImplSource; use rustc_middle::traits::ImplSource; use rustc_middle::traits::ImplSourceUserDefinedData; @@ -573,9 +574,9 @@ pub fn normalize_inherent_projection<'a, 'b, 'tcx>( // diagnostics which is not ideal. // Consider creating separate cause codes for this specific situation. if span.is_dummy() { - super::ItemObligation(alias_ty.def_id) + ObligationCauseCode::ItemObligation(alias_ty.def_id) } else { - super::BindingObligation(alias_ty.def_id, span) + ObligationCauseCode::BindingObligation(alias_ty.def_id, span) }, ); @@ -2113,22 +2114,22 @@ fn assoc_ty_own_obligations<'cx, 'tcx>( let nested_cause = if matches!( obligation.cause.code(), - super::CompareImplItemObligation { .. } - | super::CheckAssociatedTypeBounds { .. } - | super::AscribeUserTypeProvePredicate(..) + ObligationCauseCode::CompareImplItemObligation { .. } + | ObligationCauseCode::CheckAssociatedTypeBounds { .. } + | ObligationCauseCode::AscribeUserTypeProvePredicate(..) ) { obligation.cause.clone() } else if span.is_dummy() { ObligationCause::new( obligation.cause.span, obligation.cause.body_id, - super::ItemObligation(obligation.predicate.def_id), + ObligationCauseCode::ItemObligation(obligation.predicate.def_id), ) } else { ObligationCause::new( obligation.cause.span, obligation.cause.body_id, - super::BindingObligation(obligation.predicate.def_id, span), + ObligationCauseCode::BindingObligation(obligation.predicate.def_id, span), ) }; nested.push(Obligation::with_depth( diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 22e0ee38344..3746e56b7c9 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -11,6 +11,7 @@ use rustc_hir::lang_items::LangItem; use rustc_infer::infer::HigherRankedType; use rustc_infer::infer::{DefineOpaqueTypes, InferOk}; +use rustc_infer::traits::ObligationCauseCode; use rustc_middle::traits::{BuiltinImplSource, SignatureMismatchData}; use rustc_middle::ty::{ self, GenericArgs, GenericArgsRef, GenericParamDefKind, ToPolyTraitRef, ToPredicate, @@ -25,10 +26,9 @@ VtblSegment, }; use crate::traits::{ - BuiltinDerivedObligation, ImplDerivedObligation, ImplDerivedObligationCause, ImplSource, - ImplSourceUserDefinedData, Normalized, Obligation, ObligationCause, PolyTraitObligation, - PredicateObligation, Selection, SelectionError, SignatureMismatch, TraitNotObjectSafe, - TraitObligation, Unimplemented, + ImplDerivedObligationCause, ImplSource, ImplSourceUserDefinedData, Normalized, Obligation, + ObligationCause, PolyTraitObligation, PredicateObligation, Selection, SelectionError, + SignatureMismatch, TraitNotObjectSafe, TraitObligation, Unimplemented, }; use super::BuiltinImplConditions; @@ -275,7 +275,7 @@ fn confirm_builtin_candidate( bug!("obligation {:?} had matched a builtin impl but now doesn't", obligation); }; - let cause = obligation.derived_cause(BuiltinDerivedObligation); + let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerivedObligation); self.collect_predicates_for_types( obligation.param_env, cause, @@ -435,7 +435,7 @@ fn vtable_auto_impl( ) -> Vec> { debug!(?nested, "vtable_auto_impl"); ensure_sufficient_stack(|| { - let cause = obligation.derived_cause(BuiltinDerivedObligation); + let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerivedObligation); let poly_trait_ref = obligation.predicate.to_poly_trait_ref(); let trait_ref = self.infcx.enter_forall_and_leak_universe(poly_trait_ref); @@ -723,7 +723,7 @@ fn confirm_fn_pointer_candidate( let mut nested = self.equate_trait_refs(obligation.with(tcx, placeholder_predicate), trait_ref)?; - let cause = obligation.derived_cause(BuiltinDerivedObligation); + let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerivedObligation); // Confirm the `type Output: Sized;` bound that is present on `FnOnce` let output_ty = self.infcx.enter_forall_and_leak_universe(sig.output()); @@ -1381,7 +1381,7 @@ fn confirm_const_destruct_candidate( let self_ty = obligation.self_ty().map_bound(|ty| self.infcx.shallow_resolve(ty)); let mut nested = vec![]; - let cause = obligation.derived_cause(BuiltinDerivedObligation); + let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerivedObligation); // If we have a custom `impl const Drop`, then // first check it like a regular impl candidate. @@ -1396,7 +1396,7 @@ fn confirm_const_destruct_candidate( debug!(?args, "impl args"); let cause = obligation.derived_cause(|derived| { - ImplDerivedObligation(Box::new(ImplDerivedObligationCause { + ObligationCauseCode::ImplDerivedObligation(Box::new(ImplDerivedObligationCause { derived, impl_or_alias_def_id: impl_def_id, impl_def_predicate_index: None, diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index dc005982695..f9246d22bfe 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -13,9 +13,9 @@ use super::util::closure_trait_ref_and_return_type; use super::wf; use super::{ - ImplDerivedObligation, ImplDerivedObligationCause, Normalized, Obligation, ObligationCause, - ObligationCauseCode, Overflow, PolyTraitObligation, PredicateObligation, Selection, - SelectionError, SelectionResult, TraitQueryMode, + ImplDerivedObligationCause, Normalized, Obligation, ObligationCause, ObligationCauseCode, + Overflow, PolyTraitObligation, PredicateObligation, Selection, SelectionError, SelectionResult, + TraitQueryMode, }; use crate::infer::{InferCtxt, InferOk, TypeFreshener}; @@ -2771,12 +2771,14 @@ fn impl_or_trait_obligations( cause.clone() } else { cause.clone().derived_cause(parent_trait_pred, |derived| { - ImplDerivedObligation(Box::new(ImplDerivedObligationCause { - derived, - impl_or_alias_def_id: def_id, - impl_def_predicate_index: Some(index), - span, - })) + ObligationCauseCode::ImplDerivedObligation(Box::new( + ImplDerivedObligationCause { + derived, + impl_or_alias_def_id: def_id, + impl_def_predicate_index: Some(index), + span, + }, + )) }) }; let clause = normalize_with_depth_to( diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index 28ee76f7145..44c70f2be00 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -2,6 +2,7 @@ use crate::traits; use rustc_hir as hir; use rustc_hir::lang_items::LangItem; +use rustc_infer::traits::ObligationCauseCode; use rustc_middle::ty::{ self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, }; @@ -333,7 +334,7 @@ fn normalize(self, infcx: &InferCtxt<'tcx>) -> Vec) { &mut traits::SelectionContext::new(self.infcx), self.param_env, data, - self.cause(traits::WellFormed(None)), + self.cause(ObligationCauseCode::WellFormed(None)), self.recursion_depth, &mut self.out, ); @@ -498,7 +499,7 @@ fn compute_inherent_projection(&mut self, data: ty::AliasTy<'tcx>) { fn compute_projection_args(&mut self, args: GenericArgsRef<'tcx>) { let tcx = self.tcx(); - let cause = self.cause(traits::WellFormed(None)); + let cause = self.cause(ObligationCauseCode::WellFormed(None)); let param_env = self.param_env; let depth = self.recursion_depth; @@ -565,9 +566,9 @@ fn nominal_obligations( iter::zip(predicates, origins.into_iter().rev()) .map(|((pred, span), origin_def_id)| { let code = if span.is_dummy() { - traits::ItemObligation(origin_def_id) + ObligationCauseCode::ItemObligation(origin_def_id) } else { - traits::BindingObligation(origin_def_id, span) + ObligationCauseCode::BindingObligation(origin_def_id, span) }; let cause = self.cause(code); traits::Obligation::with_depth( @@ -626,7 +627,7 @@ fn from_object_ty( self.out.reserve(implicit_bounds.len()); for implicit_bound in implicit_bounds { - let cause = self.cause(traits::ObjectTypeBound(ty, explicit_bound)); + let cause = self.cause(ObligationCauseCode::ObjectTypeBound(ty, explicit_bound)); let outlives = ty::Binder::dummy(ty::OutlivesPredicate(explicit_bound, implicit_bound)); self.out.push(traits::Obligation::with_depth( @@ -644,7 +645,7 @@ fn from_object_ty( impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { type Result = (); - fn visit_ty(&mut self, t: as ty::Interner>::Ty) -> Self::Result { + fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result { debug!("wf bounds for t={:?} t.kind={:#?}", t, t.kind()); let tcx = self.tcx(); @@ -673,22 +674,22 @@ fn visit_ty(&mut self, t: as ty::Interner>::Ty) -> Self::Result { ty::Infer(ty::FloatVar(_)) => {} ty::Slice(subty) => { - self.require_sized(subty, traits::SliceOrArrayElem); + self.require_sized(subty, ObligationCauseCode::SliceOrArrayElem); } ty::Array(subty, _) => { - self.require_sized(subty, traits::SliceOrArrayElem); + self.require_sized(subty, ObligationCauseCode::SliceOrArrayElem); // Note that we handle the len is implicitly checked while walking `arg`. } ty::Pat(subty, _) => { - self.require_sized(subty, traits::MiscObligation); + self.require_sized(subty, ObligationCauseCode::MiscObligation); } ty::Tuple(tys) => { if let Some((_last, rest)) = tys.split_last() { for &elem in rest { - self.require_sized(elem, traits::TupleElem); + self.require_sized(elem, ObligationCauseCode::TupleElem); } } } @@ -728,7 +729,7 @@ fn visit_ty(&mut self, t: as ty::Interner>::Ty) -> Self::Result { ty::Ref(r, rty, _) => { // WfReference if !r.has_escaping_bound_vars() && !rty.has_escaping_bound_vars() { - let cause = self.cause(traits::ReferenceOutlivesReferent(t)); + let cause = self.cause(ObligationCauseCode::ReferenceOutlivesReferent(t)); self.out.push(traits::Obligation::with_depth( tcx, cause, @@ -825,7 +826,7 @@ fn visit_ty(&mut self, t: as ty::Interner>::Ty) -> Self::Result { if let Some(principal) = data.principal_def_id() { self.out.push(traits::Obligation::with_depth( tcx, - self.cause(traits::WellFormed(None)), + self.cause(ObligationCauseCode::WellFormed(None)), self.recursion_depth, self.param_env, ty::Binder::dummy(ty::PredicateKind::ObjectSafe(principal)), @@ -847,7 +848,7 @@ fn visit_ty(&mut self, t: as ty::Interner>::Ty) -> Self::Result { // See also the comment on `fn obligations`, describing "livelock" // prevention, which happens before this can be reached. ty::Infer(_) => { - let cause = self.cause(traits::WellFormed(None)); + let cause = self.cause(ObligationCauseCode::WellFormed(None)); self.out.push(traits::Obligation::with_depth( tcx, cause, @@ -863,7 +864,7 @@ fn visit_ty(&mut self, t: as ty::Interner>::Ty) -> Self::Result { t.super_visit_with(self) } - fn visit_const(&mut self, c: as ty::Interner>::Const) -> Self::Result { + fn visit_const(&mut self, c: ty::Const<'tcx>) -> Self::Result { let tcx = self.tcx(); match c.kind() { @@ -875,7 +876,7 @@ fn visit_const(&mut self, c: as ty::Interner>::Const) -> Self::Res let predicate = ty::Binder::dummy(ty::PredicateKind::Clause( ty::ClauseKind::ConstEvaluatable(c), )); - let cause = self.cause(traits::WellFormed(None)); + let cause = self.cause(ObligationCauseCode::WellFormed(None)); self.out.push(traits::Obligation::with_depth( tcx, cause, @@ -886,7 +887,7 @@ fn visit_const(&mut self, c: as ty::Interner>::Const) -> Self::Res } } ty::ConstKind::Infer(_) => { - let cause = self.cause(traits::WellFormed(None)); + let cause = self.cause(ObligationCauseCode::WellFormed(None)); self.out.push(traits::Obligation::with_depth( tcx, @@ -909,7 +910,7 @@ fn visit_const(&mut self, c: as ty::Interner>::Const) -> Self::Res let predicate = ty::Binder::dummy(ty::PredicateKind::Clause( ty::ClauseKind::ConstEvaluatable(c), )); - let cause = self.cause(traits::WellFormed(None)); + let cause = self.cause(ObligationCauseCode::WellFormed(None)); self.out.push(traits::Obligation::with_depth( tcx, cause, @@ -933,7 +934,7 @@ fn visit_const(&mut self, c: as ty::Interner>::Const) -> Self::Res c.super_visit_with(self) } - fn visit_predicate(&mut self, _p: as ty::Interner>::Predicate) -> Self::Result { + fn visit_predicate(&mut self, _p: ty::Predicate<'tcx>) -> Self::Result { bug!("predicate should not be checked for well-formedness"); } } From 534e267d488a698ee8839452377e1d3201eede65 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 9 May 2024 20:12:47 -0400 Subject: [PATCH 3/5] Rename some ObligationCauseCode variants --- .../rustc_borrowck/src/region_infer/mod.rs | 7 +- .../src/transform/check_consts/check.rs | 2 +- .../src/check/compare_impl_item.rs | 19 ++-- .../rustc_hir_analysis/src/check/wfcheck.rs | 4 +- .../src/impl_wf_check/min_specialization.rs | 2 +- compiler/rustc_hir_typeck/src/expr.rs | 20 +---- .../rustc_hir_typeck/src/fn_ctxt/_impl.rs | 4 +- .../src/fn_ctxt/adjust_fulfillment_errors.rs | 20 ++--- .../rustc_hir_typeck/src/fn_ctxt/checks.rs | 2 +- compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs | 2 +- .../rustc_hir_typeck/src/method/confirm.rs | 9 +- compiler/rustc_hir_typeck/src/method/probe.rs | 4 +- .../rustc_hir_typeck/src/method/suggest.rs | 8 +- .../src/infer/error_reporting/mod.rs | 47 +++++----- .../mismatched_static_lifetime.rs | 6 +- .../nice_region_error/placeholder_error.rs | 4 +- .../nice_region_error/static_impl_trait.rs | 4 +- .../src/infer/error_reporting/note.rs | 7 +- .../infer/error_reporting/note_and_explain.rs | 3 +- .../src/infer/error_reporting/suggest.rs | 2 +- compiler/rustc_infer/src/infer/mod.rs | 2 +- .../src/infer/outlives/obligations.rs | 4 +- compiler/rustc_infer/src/traits/util.rs | 14 ++- .../src/for_loops_over_fallibles.rs | 7 +- compiler/rustc_middle/src/traits/mod.rs | 47 +++++----- .../src/solve/fulfill.rs | 14 ++- .../error_reporting/on_unimplemented.rs | 6 +- .../src/traits/error_reporting/suggestions.rs | 88 ++++++++----------- .../error_reporting/type_err_ctxt_ext.rs | 42 +++++---- .../src/traits/project.rs | 10 +-- .../src/traits/select/confirmation.rs | 2 +- .../src/traits/select/mod.rs | 14 ++- .../rustc_trait_selection/src/traits/wf.rs | 6 +- 33 files changed, 186 insertions(+), 246 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index 27933536166..2120d3e61f7 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -2059,15 +2059,12 @@ pub(crate) fn best_blame_constraint( // We currently do not store the `DefId` in the `ConstraintCategory` // for performances reasons. The error reporting code used by NLL only // uses the span, so this doesn't cause any problems at the moment. - Some(ObligationCauseCode::BindingObligation( - CRATE_DEF_ID.to_def_id(), - predicate_span, - )) + Some(ObligationCauseCode::Where(CRATE_DEF_ID.to_def_id(), predicate_span)) } else { None } }) - .unwrap_or_else(|| ObligationCauseCode::MiscObligation); + .unwrap_or_else(|| ObligationCauseCode::Misc); // Classify each of the constraints along the path. let mut categorized_path: Vec> = path diff --git a/compiler/rustc_const_eval/src/transform/check_consts/check.rs b/compiler/rustc_const_eval/src/transform/check_consts/check.rs index f9786acfc6c..adbeacecc0c 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/check.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/check.rs @@ -738,7 +738,7 @@ fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location let cause = ObligationCause::new( terminator.source_info.span, self.body.source.def_id().expect_local(), - ObligationCauseCode::ItemObligation(callee), + ObligationCauseCode::MiscItem(callee), ); let normalized_predicates = ocx.normalize(&cause, param_env, predicates); ocx.register_obligations(traits::predicates_for_generics( diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index 55bf06c1ec8..97b60d137e6 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -175,7 +175,7 @@ fn compare_method_predicate_entailment<'tcx>( let cause = ObligationCause::new( impl_m_span, impl_m_def_id, - ObligationCauseCode::CompareImplItemObligation { + ObligationCauseCode::CompareImplItem { impl_item_def_id: impl_m_def_id, trait_item_def_id: trait_m.def_id, kind: impl_m.kind, @@ -236,7 +236,7 @@ fn compare_method_predicate_entailment<'tcx>( let cause = ObligationCause::new( span, impl_m_def_id, - ObligationCauseCode::CompareImplItemObligation { + ObligationCauseCode::CompareImplItem { impl_item_def_id: impl_m_def_id, trait_item_def_id: trait_m.def_id, kind: impl_m.kind, @@ -464,7 +464,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>( let cause = ObligationCause::new( return_span, impl_m_def_id, - ObligationCauseCode::CompareImplItemObligation { + ObligationCauseCode::CompareImplItem { impl_item_def_id: impl_m_def_id, trait_item_def_id: trait_m.def_id, kind: impl_m.kind, @@ -819,7 +819,7 @@ fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { ObligationCause::new( self.span, self.body_id, - ObligationCauseCode::BindingObligation(proj.def_id, pred_span), + ObligationCauseCode::Where(proj.def_id, pred_span), ), self.param_env, pred, @@ -1752,7 +1752,7 @@ fn compare_const_predicate_entailment<'tcx>( let impl_ty = tcx.type_of(impl_ct_def_id).instantiate_identity(); let trait_ty = tcx.type_of(trait_ct.def_id).instantiate(tcx, trait_to_impl_args); - let code = ObligationCauseCode::CompareImplItemObligation { + let code = ObligationCauseCode::CompareImplItem { impl_item_def_id: impl_ct_def_id, trait_item_def_id: trait_ct.def_id, kind: impl_ct.kind, @@ -1924,7 +1924,7 @@ fn compare_type_predicate_entailment<'tcx>( let cause = ObligationCause::new( span, impl_ty_def_id, - ObligationCauseCode::CompareImplItemObligation { + ObligationCauseCode::CompareImplItem { impl_item_def_id: impl_ty.def_id.expect_local(), trait_item_def_id: trait_ty.def_id, kind: impl_ty.kind, @@ -2012,9 +2012,9 @@ pub(super) fn check_type_bounds<'tcx>( ); let mk_cause = |span: Span| { let code = if span.is_dummy() { - ObligationCauseCode::ItemObligation(trait_ty.def_id) + ObligationCauseCode::MiscItem(trait_ty.def_id) } else { - ObligationCauseCode::BindingObligation(trait_ty.def_id, span) + ObligationCauseCode::Where(trait_ty.def_id, span) }; ObligationCause::new(impl_ty_span, impl_ty_def_id, code) }; @@ -2251,8 +2251,7 @@ fn try_report_async_mismatch<'tcx>( }; for error in errors { - if let ObligationCauseCode::BindingObligation(def_id, _) = - *error.root_obligation.cause.code() + if let ObligationCauseCode::Where(def_id, _) = *error.root_obligation.cause.code() && def_id == async_future_def_id && let Some(proj) = error.root_obligation.predicate.to_opt_poly_projection_pred() && let Some(proj) = proj.no_bound_vars() diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 124de69140e..49d4b8ecfe7 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -1161,7 +1161,7 @@ fn check_type_defn<'tcx>( let cause = traits::ObligationCause::new( tcx.def_span(discr_def_id), wfcx.body_def_id, - ObligationCauseCode::MiscObligation, + ObligationCauseCode::Misc, ); wfcx.register_obligation(traits::Obligation::new( tcx, @@ -1550,7 +1550,7 @@ fn visit_const(&mut self, c: ty::Const<'tcx>) -> Self::Result { let cause = traits::ObligationCause::new( sp, wfcx.body_def_id, - ObligationCauseCode::ItemObligation(def_id.to_def_id()), + ObligationCauseCode::MiscItem(def_id.to_def_id()), ); traits::Obligation::new(tcx, cause, wfcx.param_env, pred) }); diff --git a/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs b/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs index be1be1a1354..9486e241f98 100644 --- a/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs +++ b/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs @@ -212,7 +212,7 @@ fn get_impl_args( traits::ObligationCause::new( impl1_span, impl1_def_id, - traits::ObligationCauseCode::BindingObligation(impl2_node.def_id(), span), + traits::ObligationCauseCode::Where(impl2_node.def_id(), span), ) }, ); diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 00371f21277..970f25983de 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -3083,7 +3083,7 @@ fn find_and_report_unsatisfied_index_impl( polarity: ty::PredicatePolarity::Positive, }), |derived| { - ObligationCauseCode::ImplDerivedObligation(Box::new( + ObligationCauseCode::ImplDerived(Box::new( traits::ImplDerivedObligationCause { derived, impl_or_alias_def_id: impl_def_id, @@ -3352,11 +3352,7 @@ fn check_offset_of( let field_ty = self.field_ty(expr.span, field, args); // FIXME: DSTs with static alignment should be allowed - self.require_type_is_sized( - field_ty, - expr.span, - ObligationCauseCode::MiscObligation, - ); + self.require_type_is_sized(field_ty, expr.span, ObligationCauseCode::Misc); if field.vis.is_accessible_from(sub_def_scope, self.tcx) { self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None); @@ -3384,11 +3380,7 @@ fn check_offset_of( let field_ty = self.field_ty(expr.span, field, args); // FIXME: DSTs with static alignment should be allowed - self.require_type_is_sized( - field_ty, - expr.span, - ObligationCauseCode::MiscObligation, - ); + self.require_type_is_sized(field_ty, expr.span, ObligationCauseCode::Misc); if field.vis.is_accessible_from(def_scope, self.tcx) { self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None); @@ -3409,11 +3401,7 @@ fn check_offset_of( && field.name == sym::integer(index) { for ty in tys.iter().take(index + 1) { - self.require_type_is_sized( - ty, - expr.span, - ObligationCauseCode::MiscObligation, - ); + self.require_type_is_sized(ty, expr.span, ObligationCauseCode::Misc); } if let Some(&field_ty) = tys.get(index) { field_indices.push((FIRST_VARIANT, index.into())); diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 887c0c9f4f0..0ca7d496d8b 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -1413,9 +1413,9 @@ pub(crate) fn add_required_obligations_for_hir( ) { self.add_required_obligations_with_code(span, def_id, args, |idx, span| { if span.is_dummy() { - ObligationCauseCode::ExprItemObligation(def_id, hir_id, idx) + ObligationCauseCode::MiscItemInExpr(def_id, hir_id, idx) } else { - ObligationCauseCode::ExprBindingObligation(def_id, span, hir_id, idx) + ObligationCauseCode::WhereInExpr(def_id, span, hir_id, idx) } }) } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs index 56745c1245b..5e136bb6afd 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs @@ -14,8 +14,8 @@ pub fn adjust_fulfillment_error_for_expr_obligation( &self, error: &mut traits::FulfillmentError<'tcx>, ) -> bool { - let (ObligationCauseCode::ExprItemObligation(def_id, hir_id, idx) - | ObligationCauseCode::ExprBindingObligation(def_id, _, hir_id, idx)) = + let (ObligationCauseCode::MiscItemInExpr(def_id, hir_id, idx) + | ObligationCauseCode::WhereInExpr(def_id, _, hir_id, idx)) = *error.obligation.cause.code().peel_derives() else { return false; @@ -167,7 +167,7 @@ pub fn adjust_fulfillment_error_for_expr_obligation( // the method's turbofish segments but still use `FunctionArgumentObligation` // elsewhere. Hopefully this doesn't break something. error.obligation.cause.map_code(|parent_code| { - ObligationCauseCode::FunctionArgumentObligation { + ObligationCauseCode::FunctionArg { arg_hir_id: receiver.hir_id, call_hir_id: hir_id, parent_code, @@ -456,12 +456,10 @@ fn blame_specific_arg_if_possible( self.blame_specific_expr_if_possible(error, arg_expr) } - error.obligation.cause.map_code(|parent_code| { - ObligationCauseCode::FunctionArgumentObligation { - arg_hir_id: arg.hir_id, - call_hir_id, - parent_code, - } + error.obligation.cause.map_code(|parent_code| ObligationCauseCode::FunctionArg { + arg_hir_id: arg.hir_id, + call_hir_id, + parent_code, }); return true; } else if args_referencing_param.len() > 0 { @@ -514,12 +512,12 @@ fn blame_specific_expr_if_possible_for_obligation_cause_code( expr: &'tcx hir::Expr<'tcx>, ) -> Result<&'tcx hir::Expr<'tcx>, &'tcx hir::Expr<'tcx>> { match obligation_cause_code { - traits::ObligationCauseCode::ExprBindingObligation(_, _, _, _) => { + traits::ObligationCauseCode::WhereInExpr(_, _, _, _) => { // This is the "root"; we assume that the `expr` is already pointing here. // Therefore, we return `Ok` so that this `expr` can be refined further. Ok(expr) } - traits::ObligationCauseCode::ImplDerivedObligation(impl_derived) => self + traits::ObligationCauseCode::ImplDerived(impl_derived) => self .blame_specific_expr_if_possible_for_derived_predicate_obligation( impl_derived, expr, diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 73bc5fd7d50..3e05f47646c 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -206,7 +206,7 @@ pub(in super::super) fn check_argument_types( self.register_wf_obligation( fn_input_ty.into(), arg_expr.span, - ObligationCauseCode::MiscObligation, + ObligationCauseCode::Misc, ); } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs index 85fa814f270..afba812a8e7 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs @@ -154,7 +154,7 @@ pub fn cause(&self, span: Span, code: ObligationCauseCode<'tcx>) -> ObligationCa } pub fn misc(&self, span: Span) -> ObligationCause<'tcx> { - self.cause(span, ObligationCauseCode::MiscObligation) + self.cause(span, ObligationCauseCode::Misc) } pub fn sess(&self) -> &Session { diff --git a/compiler/rustc_hir_typeck/src/method/confirm.rs b/compiler/rustc_hir_typeck/src/method/confirm.rs index f844083b57c..1d697b25641 100644 --- a/compiler/rustc_hir_typeck/src/method/confirm.rs +++ b/compiler/rustc_hir_typeck/src/method/confirm.rs @@ -565,14 +565,9 @@ fn add_obligations( for obligation in traits::predicates_for_generics( |idx, span| { let code = if span.is_dummy() { - ObligationCauseCode::ExprItemObligation(def_id, self.call_expr.hir_id, idx) + ObligationCauseCode::MiscItemInExpr(def_id, self.call_expr.hir_id, idx) } else { - ObligationCauseCode::ExprBindingObligation( - def_id, - span, - self.call_expr.hir_id, - idx, - ) + ObligationCauseCode::WhereInExpr(def_id, span, self.call_expr.hir_id, idx) }; traits::ObligationCause::new(self.span, self.body_id, code) }, diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index 163fcbf8cbb..021200d9d53 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -1402,13 +1402,13 @@ fn consider_probe( ocx.register_obligations(traits::predicates_for_generics( |idx, span| { let code = if span.is_dummy() { - ObligationCauseCode::ExprItemObligation( + ObligationCauseCode::MiscItemInExpr( impl_def_id, self.scope_expr_id, idx, ) } else { - ObligationCauseCode::ExprBindingObligation( + ObligationCauseCode::WhereInExpr( impl_def_id, span, self.scope_expr_id, diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 804413abb73..31677525d2a 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -146,7 +146,7 @@ fn is_iterator_predicate(predicate: ty::Predicate<'_>, tcx: TyCtxt<'_>) -> bool return false; }; let trait_ref = ty::TraitRef::new(self.tcx, into_iterator_trait, [ty]); - let cause = ObligationCause::new(span, self.body_id, ObligationCauseCode::MiscObligation); + let cause = ObligationCause::new(span, self.body_id, ObligationCauseCode::Misc); let obligation = Obligation::new(self.tcx, cause, self.param_env, trait_ref); if !self.predicate_must_hold_modulo_regions(&obligation) { return false; @@ -826,12 +826,12 @@ pub fn report_no_match_method_error( // Extract the predicate span and parent def id of the cause, // if we have one. let (item_def_id, cause_span) = match cause.as_ref().map(|cause| cause.code()) { - Some(ObligationCauseCode::ImplDerivedObligation(data)) => { + Some(ObligationCauseCode::ImplDerived(data)) => { (data.impl_or_alias_def_id, data.span) } Some( - ObligationCauseCode::ExprBindingObligation(def_id, span, _, _) - | ObligationCauseCode::BindingObligation(def_id, span), + ObligationCauseCode::WhereInExpr(def_id, span, _, _) + | ObligationCauseCode::Where(def_id, span), ) => (*def_id, *span), _ => continue, }; diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 56f8d4d34f6..5c74420af92 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -883,9 +883,8 @@ fn note_error_origin( err.help("...or use `match` instead of `let...else`"); } _ => { - if let ObligationCauseCode::BindingObligation(_, span) - | ObligationCauseCode::ExprBindingObligation(_, span, ..) = - cause.code().peel_derives() + if let ObligationCauseCode::Where(_, span) + | ObligationCauseCode::WhereInExpr(_, span, ..) = cause.code().peel_derives() && let TypeError::RegionsPlaceholderMismatch = terr { err.span_note(*span, "the lifetime requirement is introduced here"); @@ -2776,7 +2775,7 @@ fn as_failure_code(&self, terr: TypeError<'tcx>) -> FailureCode { match self.code() { ObligationCauseCode::IfExpressionWithNoElse => FailureCode::Error0317, ObligationCauseCode::MainFunctionType => FailureCode::Error0580, - ObligationCauseCode::CompareImplItemObligation { .. } + ObligationCauseCode::CompareImplItem { .. } | ObligationCauseCode::MatchExpressionArm(_) | ObligationCauseCode::IfExpression { .. } | ObligationCauseCode::LetElse @@ -2806,15 +2805,15 @@ fn as_failure_code_diag( subdiags: Vec, ) -> ObligationCauseFailureCode { match self.code() { - ObligationCauseCode::CompareImplItemObligation { kind: ty::AssocKind::Fn, .. } => { + ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn, .. } => { ObligationCauseFailureCode::MethodCompat { span, subdiags } } - ObligationCauseCode::CompareImplItemObligation { - kind: ty::AssocKind::Type, .. - } => ObligationCauseFailureCode::TypeCompat { span, subdiags }, - ObligationCauseCode::CompareImplItemObligation { - kind: ty::AssocKind::Const, .. - } => ObligationCauseFailureCode::ConstCompat { span, subdiags }, + ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type, .. } => { + ObligationCauseFailureCode::TypeCompat { span, subdiags } + } + ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Const, .. } => { + ObligationCauseFailureCode::ConstCompat { span, subdiags } + } ObligationCauseCode::BlockTailExpression(.., hir::MatchSource::TryDesugar(_)) => { ObligationCauseFailureCode::TryCompat { span, subdiags } } @@ -2870,15 +2869,15 @@ fn as_failure_code_diag( fn as_requirement_str(&self) -> &'static str { match self.code() { - ObligationCauseCode::CompareImplItemObligation { kind: ty::AssocKind::Fn, .. } => { + ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn, .. } => { "method type is compatible with trait" } - ObligationCauseCode::CompareImplItemObligation { - kind: ty::AssocKind::Type, .. - } => "associated type is compatible with trait", - ObligationCauseCode::CompareImplItemObligation { - kind: ty::AssocKind::Const, .. - } => "const is compatible with trait", + ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type, .. } => { + "associated type is compatible with trait" + } + ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Const, .. } => { + "const is compatible with trait" + } ObligationCauseCode::MainFunctionType => "`main` function has the correct type", ObligationCauseCode::StartFunctionType => "`#[start]` function has the correct type", ObligationCauseCode::LangFunctionType(_) => "lang item function has the correct type", @@ -2895,15 +2894,11 @@ fn as_requirement_str(&self) -> &'static str { impl IntoDiagArg for ObligationCauseAsDiagArg<'_> { fn into_diag_arg(self) -> rustc_errors::DiagArgValue { let kind = match self.0.code() { - ObligationCauseCode::CompareImplItemObligation { kind: ty::AssocKind::Fn, .. } => { - "method_compat" + ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn, .. } => "method_compat", + ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type, .. } => "type_compat", + ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Const, .. } => { + "const_compat" } - ObligationCauseCode::CompareImplItemObligation { - kind: ty::AssocKind::Type, .. - } => "type_compat", - ObligationCauseCode::CompareImplItemObligation { - kind: ty::AssocKind::Const, .. - } => "const_compat", ObligationCauseCode::MainFunctionType => "fn_main_correct_type", ObligationCauseCode::StartFunctionType => "fn_start_correct_type", ObligationCauseCode::LangFunctionType(_) => "fn_lang_correct_type", diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mismatched_static_lifetime.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mismatched_static_lifetime.rs index b93b8dc03f8..26a9e695929 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mismatched_static_lifetime.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mismatched_static_lifetime.rs @@ -32,14 +32,14 @@ pub(super) fn try_report_mismatched_static_lifetime(&self) -> Option &*parent_code, + ObligationCauseCode::FunctionArg { parent_code, .. } => &*parent_code, code => code, }; let ObligationCauseCode::MatchImpl(parent, impl_def_id) = code else { return None; }; - let (ObligationCauseCode::BindingObligation(_, binding_span) - | ObligationCauseCode::ExprBindingObligation(_, binding_span, ..)) = *parent.code() + let (ObligationCauseCode::Where(_, binding_span) + | ObligationCauseCode::WhereInExpr(_, binding_span, ..)) = *parent.code() else { return None; }; diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs index 01e75d59f4d..c9dc38cb98f 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs @@ -240,8 +240,8 @@ fn report_trait_placeholder_mismatch( let span = cause.span(); let (leading_ellipsis, satisfy_span, where_span, dup_span, def_id) = - if let ObligationCauseCode::ItemObligation(def_id) - | ObligationCauseCode::ExprItemObligation(def_id, ..) = *cause.code() + if let ObligationCauseCode::MiscItem(def_id) + | ObligationCauseCode::MiscItemInExpr(def_id, ..) = *cause.code() { ( true, diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs index 0bbabefaf95..c6176c5d3d8 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs @@ -214,8 +214,8 @@ pub(super) fn try_report_static_impl_trait(&self) -> Option { _ => cause.code(), } && let ( - &ObligationCauseCode::ItemObligation(item_def_id) - | &ObligationCauseCode::ExprItemObligation(item_def_id, ..), + &ObligationCauseCode::MiscItem(item_def_id) + | &ObligationCauseCode::MiscItemInExpr(item_def_id, ..), None, ) = (code, override_error_code) { diff --git a/compiler/rustc_infer/src/infer/error_reporting/note.rs b/compiler/rustc_infer/src/infer/error_reporting/note.rs index 3ae1165d2a4..8a65f9622d2 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/note.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/note.rs @@ -357,13 +357,12 @@ pub(super) fn report_placeholder_failure( infer::Subtype(box ref trace) if matches!( &trace.cause.code().peel_derives(), - ObligationCauseCode::BindingObligation(..) - | ObligationCauseCode::ExprBindingObligation(..) + ObligationCauseCode::Where(..) | ObligationCauseCode::WhereInExpr(..) ) => { // Hack to get around the borrow checker because trace.cause has an `Rc`. - if let ObligationCauseCode::BindingObligation(_, span) - | ObligationCauseCode::ExprBindingObligation(_, span, ..) = + if let ObligationCauseCode::Where(_, span) + | ObligationCauseCode::WhereInExpr(_, span, ..) = &trace.cause.code().peel_derives() { let span = *span; diff --git a/compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs b/compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs index 6a42f9b42c3..c24ad1fa1e7 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs @@ -629,8 +629,7 @@ fn expected_projection( | hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }), ) ); - let impl_comparison = - matches!(cause_code, ObligationCauseCode::CompareImplItemObligation { .. }); + let impl_comparison = matches!(cause_code, ObligationCauseCode::CompareImplItem { .. }); let assoc = tcx.associated_item(proj_ty.def_id); if impl_comparison { // We do not want to suggest calling functions when the reason of the diff --git a/compiler/rustc_infer/src/infer/error_reporting/suggest.rs b/compiler/rustc_infer/src/infer/error_reporting/suggest.rs index bf470bb1e3f..75479decebc 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/suggest.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/suggest.rs @@ -167,7 +167,7 @@ pub(super) fn suggest_await_on_expect_found( exp_span, exp_found.expected, exp_found.found, ); - if let ObligationCauseCode::CompareImplItemObligation { .. } = cause.code() { + if let ObligationCauseCode::CompareImplItem { .. } = cause.code() { return; } diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index 02c1b567740..ce82296a8aa 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -1876,7 +1876,7 @@ pub fn from_obligation_cause(cause: &traits::ObligationCause<'tcx>, default: SubregionOrigin::ReferenceOutlivesReferent(ref_type, cause.span) } - traits::ObligationCauseCode::CompareImplItemObligation { + traits::ObligationCauseCode::CompareImplItem { impl_item_def_id, trait_item_def_id, kind: _, diff --git a/compiler/rustc_infer/src/infer/outlives/obligations.rs b/compiler/rustc_infer/src/infer/outlives/obligations.rs index fe323982ec0..21abd90916a 100644 --- a/compiler/rustc_infer/src/infer/outlives/obligations.rs +++ b/compiler/rustc_infer/src/infer/outlives/obligations.rs @@ -103,8 +103,8 @@ pub fn register_region_obligation_with_cause( cause.span, sup_type, match cause.code().peel_derives() { - ObligationCauseCode::BindingObligation(_, span) - | ObligationCauseCode::ExprBindingObligation(_, span, ..) => Some(*span), + ObligationCauseCode::Where(_, span) + | ObligationCauseCode::WhereInExpr(_, span, ..) => Some(*span), _ => None, }, ) diff --git a/compiler/rustc_infer/src/traits/util.rs b/compiler/rustc_infer/src/traits/util.rs index 11e0d8fac3c..1104df570a8 100644 --- a/compiler/rustc_infer/src/traits/util.rs +++ b/compiler/rustc_infer/src/traits/util.rs @@ -129,14 +129,12 @@ fn child_with_derived_cause( index: usize, ) -> Self { let cause = self.cause.clone().derived_cause(parent_trait_pred, |derived| { - ObligationCauseCode::ImplDerivedObligation(Box::new( - traits::ImplDerivedObligationCause { - derived, - impl_or_alias_def_id: parent_trait_pred.def_id(), - impl_def_predicate_index: Some(index), - span, - }, - )) + ObligationCauseCode::ImplDerived(Box::new(traits::ImplDerivedObligationCause { + derived, + impl_or_alias_def_id: parent_trait_pred.def_id(), + impl_def_predicate_index: Some(index), + span, + })) }); Obligation { cause, diff --git a/compiler/rustc_lint/src/for_loops_over_fallibles.rs b/compiler/rustc_lint/src/for_loops_over_fallibles.rs index 1f9a3297844..a6876d8aae7 100644 --- a/compiler/rustc_lint/src/for_loops_over_fallibles.rs +++ b/compiler/rustc_lint/src/for_loops_over_fallibles.rs @@ -150,11 +150,8 @@ fn suggest_question_mark<'tcx>( let ocx = ObligationCtxt::new(&infcx); let body_def_id = cx.tcx.hir().body_owner_def_id(body_id); - let cause = ObligationCause::new( - span, - body_def_id, - rustc_infer::traits::ObligationCauseCode::MiscObligation, - ); + let cause = + ObligationCause::new(span, body_def_id, rustc_infer::traits::ObligationCauseCode::Misc); ocx.register_bound( cause, diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs index 50b85d895ad..c38e984af73 100644 --- a/compiler/rustc_middle/src/traits/mod.rs +++ b/compiler/rustc_middle/src/traits/mod.rs @@ -127,7 +127,7 @@ pub fn new( } pub fn misc(span: Span, body_id: LocalDefId) -> ObligationCause<'tcx> { - ObligationCause::new(span, body_id, ObligationCauseCode::MiscObligation) + ObligationCause::new(span, body_id, ObligationCauseCode::Misc) } #[inline(always)] @@ -207,7 +207,7 @@ pub struct UnifyReceiverContext<'tcx> { #[derive(Clone, PartialEq, Eq, Default, HashStable)] #[derive(TypeVisitable, TypeFoldable, TyEncodable, TyDecodable)] pub struct InternedObligationCauseCode<'tcx> { - /// `None` for `ObligationCauseCode::MiscObligation` (a common case, occurs ~60% of + /// `None` for `ObligationCauseCode::Misc` (a common case, occurs ~60% of /// the time). `Some` otherwise. code: Option>>, } @@ -223,11 +223,7 @@ impl<'tcx> ObligationCauseCode<'tcx> { #[inline(always)] fn into(self) -> InternedObligationCauseCode<'tcx> { InternedObligationCauseCode { - code: if let ObligationCauseCode::MiscObligation = self { - None - } else { - Some(Lrc::new(self)) - }, + code: if let ObligationCauseCode::Misc = self { None } else { Some(Lrc::new(self)) }, } } } @@ -236,7 +232,7 @@ impl<'tcx> std::ops::Deref for InternedObligationCauseCode<'tcx> { type Target = ObligationCauseCode<'tcx>; fn deref(&self) -> &Self::Target { - self.code.as_deref().unwrap_or(&ObligationCauseCode::MiscObligation) + self.code.as_deref().unwrap_or(&ObligationCauseCode::Misc) } } @@ -244,7 +240,7 @@ fn deref(&self) -> &Self::Target { #[derive(TypeVisitable, TypeFoldable)] pub enum ObligationCauseCode<'tcx> { /// Not well classified or should be obvious from the span. - MiscObligation, + Misc, /// A slice or array is WF only if `T: Sized`. SliceOrArrayElem, @@ -254,20 +250,20 @@ pub enum ObligationCauseCode<'tcx> { /// Must satisfy all of the where-clause predicates of the /// given item. - ItemObligation(DefId), + MiscItem(DefId), - /// Like `ItemObligation`, but carries the span of the + /// Like `MiscItem`, but carries the span of the /// predicate when it can be identified. - BindingObligation(DefId, Span), + Where(DefId, Span), /// Like `ItemObligation`, but carries the `HirId` of the /// expression that caused the obligation, and the `usize` /// indicates exactly which predicate it is in the list of /// instantiated predicates. - ExprItemObligation(DefId, HirId, usize), + MiscItemInExpr(DefId, HirId, usize), /// Combines `ExprItemObligation` and `BindingObligation`. - ExprBindingObligation(DefId, Span, HirId, usize), + WhereInExpr(DefId, Span, HirId, usize), /// A type like `&'a T` is WF only if `T: 'a`. ReferenceOutlivesReferent(Ty<'tcx>), @@ -331,14 +327,14 @@ pub enum ObligationCauseCode<'tcx> { /// Derived obligation (i.e. theoretical `where` clause) on a built-in /// implementation like `Copy` or `Sized`. - BuiltinDerivedObligation(DerivedObligationCause<'tcx>), + BuiltinDerived(DerivedObligationCause<'tcx>), /// Derived obligation (i.e. `where` clause) on an user-provided impl /// or a trait alias. - ImplDerivedObligation(Box>), + ImplDerived(Box>), /// Derived obligation for WF goals. - WellFormedDerivedObligation(DerivedObligationCause<'tcx>), + WellFormedDerived(DerivedObligationCause<'tcx>), FunctionArgumentObligation { /// The node of the relevant argument in the function call. @@ -430,8 +426,8 @@ pub enum ObligationCauseCode<'tcx> { /// then it will be used to perform HIR-based wf checking /// after an error occurs, in order to generate a more precise error span. /// This is purely for diagnostic purposes - it is always - /// correct to use `MiscObligation` instead, or to specify - /// `WellFormed(None)` + /// correct to use `Misc` instead, or to specify + /// `WellFormed(None)`. WellFormed(Option), /// From `match_impl`. The cause for us having to match an impl, and the DefId we are matching against. @@ -538,14 +534,11 @@ pub fn peel_derives_with_predicate(&self) -> (&Self, Option Option<(&Self, Option>)> { match self { - ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } => { - Some((parent_code, None)) - } - ObligationCauseCode::BuiltinDerivedObligation(derived) - | ObligationCauseCode::WellFormedDerivedObligation(derived) - | ObligationCauseCode::ImplDerivedObligation(box ImplDerivedObligationCause { - derived, - .. + ObligationCauseCode::FunctionArg { parent_code, .. } => Some((parent_code, None)), + ObligationCauseCode::BuiltinDerived(derived) + | ObligationCauseCode::WellFormedDerived(derived) + | ObligationCauseCode::ImplDerived(box ImplDerivedObligationCause { + derived, .. }) => Some((&derived.parent_code, Some(derived.parent_trait_pred))), _ => None, } diff --git a/compiler/rustc_trait_selection/src/solve/fulfill.rs b/compiler/rustc_trait_selection/src/solve/fulfill.rs index 893a6428a5e..410890e6680 100644 --- a/compiler/rustc_trait_selection/src/solve/fulfill.rs +++ b/compiler/rustc_trait_selection/src/solve/fulfill.rs @@ -425,14 +425,12 @@ fn derive_cause<'tcx>( tcx.predicates_of(impl_def_id).instantiate_identity(tcx).iter().nth(idx) { cause = cause.derived_cause(parent_trait_pred, |derived| { - ObligationCauseCode::ImplDerivedObligation(Box::new( - traits::ImplDerivedObligationCause { - derived, - impl_or_alias_def_id: impl_def_id, - impl_def_predicate_index: Some(idx), - span, - }, - )) + ObligationCauseCode::ImplDerived(Box::new(traits::ImplDerivedObligationCause { + derived, + impl_or_alias_def_id: impl_def_id, + impl_def_predicate_index: Some(idx), + span, + })) }) } } diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs index 31f9ac1541b..c46cee36cf7 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs @@ -127,9 +127,9 @@ fn on_unimplemented_note( flags.push((sym::ItemContext, enclosure)); match obligation.cause.code() { - ObligationCauseCode::BuiltinDerivedObligation(..) - | ObligationCauseCode::ImplDerivedObligation(..) - | ObligationCauseCode::WellFormedDerivedObligation(..) => {} + ObligationCauseCode::BuiltinDerived(..) + | ObligationCauseCode::ImplDerived(..) + | ObligationCauseCode::WellFormedDerived(..) => {} _ => { // this is a "direct", user-specified, rather than derived, // obligation. diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index a687ec0a706..86615475607 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -455,8 +455,7 @@ fn suggest_dereferences( trait_pred: ty::PolyTraitPredicate<'tcx>, ) -> bool { let mut code = obligation.cause.code(); - if let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, call_hir_id, .. } = - code + if let ObligationCauseCode::FunctionArg { arg_hir_id, call_hir_id, .. } = code && let Some(typeck_results) = &self.typeck_results && let hir::Node::Expr(expr) = self.tcx.hir_node(*arg_hir_id) && let Some(arg_ty) = typeck_results.expr_ty_adjusted_opt(expr) @@ -846,7 +845,7 @@ fn suggest_fn_call( .collect::>() .join(", "); - if matches!(obligation.cause.code(), ObligationCauseCode::FunctionArgumentObligation { .. }) + if matches!(obligation.cause.code(), ObligationCauseCode::FunctionArg { .. }) && obligation.cause.span.can_be_used_for_suggestions() { // When the obligation error has been ensured to have been caused by @@ -980,8 +979,7 @@ fn suggest_add_clone_to_arg( }; let ty::Ref(_, inner_ty, hir::Mutability::Not) = ty.kind() else { return false }; let ty::Param(param) = inner_ty.kind() else { return false }; - let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = - obligation.cause.code() + let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code() else { return false; }; @@ -1204,9 +1202,8 @@ fn suggest_add_reference_to_arg( let span = obligation.cause.span; let code = match obligation.cause.code() { - ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } => parent_code, - c @ ObligationCauseCode::ItemObligation(_) - | c @ ObligationCauseCode::ExprItemObligation(..) => c, + ObligationCauseCode::FunctionArg { parent_code, .. } => parent_code, + c @ ObligationCauseCode::MiscItem(_) | c @ ObligationCauseCode::MiscItemInExpr(..) => c, c if matches!( span.ctxt().outer_expn_data().kind, ExpnKind::Desugaring(DesugaringKind::ForLoop) @@ -1262,8 +1259,8 @@ fn suggest_add_reference_to_arg( let mut_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_mut_ref); let (ref_inner_ty_satisfies_pred, ref_inner_ty_mut) = - if let ObligationCauseCode::ItemObligation(_) - | ObligationCauseCode::ExprItemObligation(..) = obligation.cause.code() + if let ObligationCauseCode::MiscItem(_) | ObligationCauseCode::MiscItemInExpr(..) = + obligation.cause.code() && let ty::Ref(_, ty, mutability) = old_pred.self_ty().skip_binder().kind() { ( @@ -1401,12 +1398,12 @@ fn suggest_add_reference_to_arg( return false; }; - if let ObligationCauseCode::ImplDerivedObligation(cause) = &*code { + if let ObligationCauseCode::ImplDerived(cause) = &*code { try_borrowing(cause.derived.parent_trait_pred, &[]) - } else if let ObligationCauseCode::BindingObligation(_, _) - | ObligationCauseCode::ItemObligation(_) - | ObligationCauseCode::ExprItemObligation(..) - | ObligationCauseCode::ExprBindingObligation(..) = code + } else if let ObligationCauseCode::Where(_, _) + | ObligationCauseCode::MiscItem(_) + | ObligationCauseCode::MiscItemInExpr(..) + | ObligationCauseCode::WhereInExpr(..) = code { try_borrowing(poly_trait_pred, &never_suggest_borrow) } else { @@ -1644,10 +1641,8 @@ fn suggest_change_mut( err: &mut Diag<'_>, trait_pred: ty::PolyTraitPredicate<'tcx>, ) { - let points_at_arg = matches!( - obligation.cause.code(), - ObligationCauseCode::FunctionArgumentObligation { .. }, - ); + let points_at_arg = + matches!(obligation.cause.code(), ObligationCauseCode::FunctionArg { .. },); let span = obligation.cause.span; if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) { @@ -1953,7 +1948,7 @@ fn note_conflicting_fn_args( found: Ty<'tcx>, param_env: ty::ParamEnv<'tcx>, ) { - let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = cause else { + let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = cause else { return; }; let ty::FnPtr(expected) = expected.kind() else { @@ -2107,7 +2102,7 @@ fn note_conflicting_closure_bounds( // First, look for an `ExprBindingObligation`, which means we can get // the uninstantiated predicate list of the called function. And check // that the predicate that we failed to satisfy is a `Fn`-like trait. - if let ObligationCauseCode::ExprBindingObligation(def_id, _, _, idx) = cause + if let ObligationCauseCode::WhereInExpr(def_id, _, _, idx) = cause && let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx) && let Some(pred) = predicates.predicates.get(*idx) && let ty::ClauseKind::Trait(trait_pred) = pred.kind().skip_binder() @@ -2261,10 +2256,10 @@ fn maybe_note_obligation_cause_for_async_await( while let Some(code) = next_code { debug!(?code); match code { - ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } => { + ObligationCauseCode::FunctionArg { parent_code, .. } => { next_code = Some(parent_code); } - ObligationCauseCode::ImplDerivedObligation(cause) => { + ObligationCauseCode::ImplDerived(cause) => { let ty = cause.derived.parent_trait_pred.skip_binder().self_ty(); debug!( parent_trait_ref = ?cause.derived.parent_trait_pred, @@ -2293,8 +2288,8 @@ fn maybe_note_obligation_cause_for_async_await( next_code = Some(&cause.derived.parent_code); } - ObligationCauseCode::WellFormedDerivedObligation(derived_obligation) - | ObligationCauseCode::BuiltinDerivedObligation(derived_obligation) => { + ObligationCauseCode::WellFormedDerived(derived_obligation) + | ObligationCauseCode::BuiltinDerived(derived_obligation) => { let ty = derived_obligation.parent_trait_pred.skip_binder().self_ty(); debug!( parent_trait_ref = ?derived_obligation.parent_trait_pred, @@ -2719,7 +2714,7 @@ fn note_obligation_cause_code( | ObligationCauseCode::MethodReceiver | ObligationCauseCode::ReturnNoExpression | ObligationCauseCode::UnifyReceiver(..) - | ObligationCauseCode::MiscObligation + | ObligationCauseCode::Misc | ObligationCauseCode::WellFormed(..) | ObligationCauseCode::MatchImpl(..) | ObligationCauseCode::ReturnValue(_) @@ -2748,13 +2743,12 @@ fn note_obligation_cause_code( ObligationCauseCode::TupleElem => { err.note("only the last element of a tuple may have a dynamically sized type"); } - ObligationCauseCode::ItemObligation(_) - | ObligationCauseCode::ExprItemObligation(..) => { + ObligationCauseCode::MiscItem(_) | ObligationCauseCode::MiscItemInExpr(..) => { // We hold the `DefId` of the item introducing the obligation, but displaying it // doesn't add user usable information. It always point at an associated item. } - ObligationCauseCode::BindingObligation(item_def_id, span) - | ObligationCauseCode::ExprBindingObligation(item_def_id, span, ..) => { + ObligationCauseCode::Where(item_def_id, span) + | ObligationCauseCode::WhereInExpr(item_def_id, span, ..) => { let item_name = tcx.def_path_str(item_def_id); let short_item_name = with_forced_trimmed_paths!(tcx.def_path_str(item_def_id)); let mut multispan = MultiSpan::from(span); @@ -3175,7 +3169,7 @@ fn note_obligation_cause_code( ObligationCauseCode::SharedStatic => { err.note("shared static variables must have a type that implements `Sync`"); } - ObligationCauseCode::BuiltinDerivedObligation(ref data) => { + ObligationCauseCode::BuiltinDerived(ref data) => { let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred); let ty = parent_trait_ref.skip_binder().self_ty(); if parent_trait_ref.references_error() { @@ -3190,8 +3184,7 @@ fn note_obligation_cause_code( let is_upvar_tys_infer_tuple = if !matches!(ty.kind(), ty::Tuple(..)) { false } else { - if let ObligationCauseCode::BuiltinDerivedObligation(data) = &*data.parent_code - { + if let ObligationCauseCode::BuiltinDerived(data) = &*data.parent_code { let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred); let nested_ty = parent_trait_ref.skip_binder().self_ty(); @@ -3297,7 +3290,7 @@ fn note_obligation_cause_code( }); } } - ObligationCauseCode::ImplDerivedObligation(ref data) => { + ObligationCauseCode::ImplDerived(ref data) => { let mut parent_trait_pred = self.resolve_vars_if_possible(data.derived.parent_trait_pred); let parent_def_id = parent_trait_pred.def_id(); @@ -3367,9 +3360,7 @@ fn note_obligation_cause_code( if is_auto_trait { // We don't want to point at the ADT saying "required because it appears within // the type `X`", like we would otherwise do in test `supertrait-auto-trait.rs`. - while let ObligationCauseCode::BuiltinDerivedObligation(derived) = - &*data.parent_code - { + while let ObligationCauseCode::BuiltinDerived(derived) = &*data.parent_code { let child_trait_ref = self.resolve_vars_if_possible(derived.parent_trait_pred); let child_def_id = child_trait_ref.def_id(); @@ -3381,7 +3372,7 @@ fn note_obligation_cause_code( parent_trait_pred = child_trait_ref; } } - while let ObligationCauseCode::ImplDerivedObligation(child) = &*data.parent_code { + while let ObligationCauseCode::ImplDerived(child) = &*data.parent_code { // Skip redundant recursive obligation notes. See `ui/issue-20413.rs`. let child_trait_pred = self.resolve_vars_if_possible(child.derived.parent_trait_pred); @@ -3422,7 +3413,7 @@ fn note_obligation_cause_code( ) }); } - ObligationCauseCode::WellFormedDerivedObligation(ref data) => { + ObligationCauseCode::WellFormedDerived(ref data) => { let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred); let parent_predicate = parent_trait_ref; // #74711: avoid a stack overflow @@ -3458,11 +3449,8 @@ fn note_obligation_cause_code( format!("required by a bound on the type alias `{}`", tcx.item_name(def_id)), ); } - ObligationCauseCode::FunctionArgumentObligation { - arg_hir_id, - call_hir_id, - ref parent_code, - .. + ObligationCauseCode::FunctionArg { + arg_hir_id, call_hir_id, ref parent_code, .. } => { self.note_function_argument_obligation( body_id, @@ -3485,7 +3473,7 @@ fn note_obligation_cause_code( ) }); } - ObligationCauseCode::CompareImplItemObligation { trait_item_def_id, kind, .. } => { + ObligationCauseCode::CompareImplItem { trait_item_def_id, kind, .. } => { let item_name = tcx.item_name(trait_item_def_id); let msg = format!( "the requirement `{predicate}` appears on the `impl`'s {kind} \ @@ -3694,7 +3682,7 @@ fn suggest_dereferencing_index( err: &mut Diag<'_>, trait_pred: ty::PolyTraitPredicate<'tcx>, ) { - if let ObligationCauseCode::ImplDerivedObligation(_) = obligation.cause.code() + if let ObligationCauseCode::ImplDerived(_) = obligation.cause.code() && self .tcx .is_diagnostic_item(sym::SliceIndex, trait_pred.skip_binder().trait_ref.def_id) @@ -3811,7 +3799,7 @@ fn note_function_argument_obligation( // to an associated type (as seen from `trait_pred`) in the predicate. Like in // trait_pred `S: Sum<::Item>` and predicate `i32: Sum<&()>` let mut type_diffs = vec![]; - if let ObligationCauseCode::ExprBindingObligation(def_id, _, _, idx) = parent_code + if let ObligationCauseCode::WhereInExpr(def_id, _, _, idx) = parent_code && let Some(node_args) = typeck_results.node_args_opt(call_hir_id) && let where_clauses = self.tcx.predicates_of(def_id).instantiate(self.tcx, node_args) @@ -4320,8 +4308,8 @@ fn suggest_convert_to_slice( ) { // We can only suggest the slice coersion for function and binary operation arguments, // since the suggestion would make no sense in turbofish or call - let (ObligationCauseCode::BinOp { .. } - | ObligationCauseCode::FunctionArgumentObligation { .. }) = obligation.cause.code() + let (ObligationCauseCode::BinOp { .. } | ObligationCauseCode::FunctionArg { .. }) = + obligation.cause.code() else { return; }; @@ -4407,7 +4395,7 @@ fn explain_hrtb_projection( return; } - if let ObligationCauseCode::FunctionArgumentObligation { + if let ObligationCauseCode::FunctionArg { call_hir_id, arg_hir_id, parent_code: _, diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs index da2e4ab35d2..e85a2b50bf4 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs @@ -391,7 +391,7 @@ fn report_selection_error( } } - if let ObligationCauseCode::CompareImplItemObligation { + if let ObligationCauseCode::CompareImplItem { impl_item_def_id, trait_item_def_id, kind: _, @@ -1017,7 +1017,7 @@ fn emit_specialized_closure_kind_error( if let Some((_, Some(parent))) = obligation.cause.code().parent() { // If we have a derived obligation, then the parent will be a `AsyncFn*` goal. trait_ref = parent.to_poly_trait_ref(); - } else if let &ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = + } else if let &ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code() && let Some(typeck_results) = &self.typeck_results && let ty::Closure(closure_def_id, _) | ty::CoroutineClosure(closure_def_id, _) = @@ -1104,8 +1104,7 @@ fn fn_arg_obligation( &self, obligation: &PredicateObligation<'tcx>, ) -> Result<(), ErrorGuaranteed> { - if let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = - obligation.cause.code() + if let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code() && let Node::Expr(arg) = self.tcx.hir_node(*arg_hir_id) && let arg = arg.peel_borrows() && let hir::ExprKind::Path(hir::QPath::Resolved( @@ -1534,10 +1533,10 @@ fn report_fulfillment_error(&self, error: &FulfillmentError<'tcx>) -> ErrorGuara *err, ); let code = error.obligation.cause.code().peel_derives().peel_match_impls(); - if let ObligationCauseCode::BindingObligation(..) - | ObligationCauseCode::ItemObligation(..) - | ObligationCauseCode::ExprBindingObligation(..) - | ObligationCauseCode::ExprItemObligation(..) = code + if let ObligationCauseCode::Where(..) + | ObligationCauseCode::MiscItem(..) + | ObligationCauseCode::WhereInExpr(..) + | ObligationCauseCode::MiscItemInExpr(..) = code { self.note_obligation_cause_code( error.obligation.cause.body_id, @@ -1611,10 +1610,10 @@ fn report_projection_error( let is_normalized_term_expected = !matches!( obligation.cause.code().peel_derives(), - ObligationCauseCode::ItemObligation(_) - | ObligationCauseCode::BindingObligation(_, _) - | ObligationCauseCode::ExprItemObligation(..) - | ObligationCauseCode::ExprBindingObligation(..) + ObligationCauseCode::MiscItem(_) + | ObligationCauseCode::Where(_, _) + | ObligationCauseCode::MiscItemInExpr(..) + | ObligationCauseCode::WhereInExpr(..) | ObligationCauseCode::Coercion { .. } ); @@ -2211,7 +2210,7 @@ fn get_parent_trait_ref( code: &ObligationCauseCode<'tcx>, ) -> Option<(Ty<'tcx>, Option)> { match code { - ObligationCauseCode::BuiltinDerivedObligation(data) => { + ObligationCauseCode::BuiltinDerived(data) => { let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred); match self.get_parent_trait_ref(&data.parent_code) { Some(t) => Some(t), @@ -2223,7 +2222,7 @@ fn get_parent_trait_ref( } } } - ObligationCauseCode::FunctionArgumentObligation { parent_code, .. } => { + ObligationCauseCode::FunctionArg { parent_code, .. } => { self.get_parent_trait_ref(parent_code) } _ => None, @@ -2446,8 +2445,8 @@ fn maybe_report_ambiguity(&self, obligation: &PredicateObligation<'tcx>) -> Erro } } - if let ObligationCauseCode::ItemObligation(def_id) - | ObligationCauseCode::ExprItemObligation(def_id, ..) = *obligation.cause.code() + if let ObligationCauseCode::MiscItem(def_id) + | ObligationCauseCode::MiscItemInExpr(def_id, ..) = *obligation.cause.code() { self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id()); } @@ -2882,8 +2881,8 @@ fn suggest_unsized_bound_if_applicable( else { return; }; - let (ObligationCauseCode::BindingObligation(item_def_id, span) - | ObligationCauseCode::ExprBindingObligation(item_def_id, span, ..)) = + let (ObligationCauseCode::Where(item_def_id, span) + | ObligationCauseCode::WhereInExpr(item_def_id, span, ..)) = *obligation.cause.code().peel_derives() else { return; @@ -3003,7 +3002,7 @@ fn is_recursive_obligation( obligated_types: &mut Vec>, cause_code: &ObligationCauseCode<'tcx>, ) -> bool { - if let ObligationCauseCode::BuiltinDerivedObligation(ref data) = cause_code { + if let ObligationCauseCode::BuiltinDerived(ref data) = cause_code { let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred); let self_ty = parent_trait_ref.skip_binder().self_ty(); if obligated_types.iter().any(|ot| ot == &self_ty) { @@ -3180,8 +3179,7 @@ fn add_tuple_trait_message( ObligationCauseCode::RustCall => { err.primary_message("functions with the \"rust-call\" ABI must take a single non-self tuple argument"); } - ObligationCauseCode::BindingObligation(def_id, _) - | ObligationCauseCode::ItemObligation(def_id) + ObligationCauseCode::Where(def_id, _) | ObligationCauseCode::MiscItem(def_id) if self.tcx.is_fn_trait(*def_id) => { err.code(E0059); @@ -3570,7 +3568,7 @@ fn report_not_const_evaluatable_error( match self.tcx.sess.source_map().span_to_snippet(const_span) { Ok(snippet) => { let code = format!("[(); {snippet}{cast}]:"); - let def_id = if let ObligationCauseCode::CompareImplItemObligation { + let def_id = if let ObligationCauseCode::CompareImplItem { trait_item_def_id, .. } = obligation.cause.code() diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 0ef007e30f1..5cffd1fe8ee 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -574,9 +574,9 @@ pub fn normalize_inherent_projection<'a, 'b, 'tcx>( // diagnostics which is not ideal. // Consider creating separate cause codes for this specific situation. if span.is_dummy() { - ObligationCauseCode::ItemObligation(alias_ty.def_id) + ObligationCauseCode::MiscItem(alias_ty.def_id) } else { - ObligationCauseCode::BindingObligation(alias_ty.def_id, span) + ObligationCauseCode::Where(alias_ty.def_id, span) }, ); @@ -2114,7 +2114,7 @@ fn assoc_ty_own_obligations<'cx, 'tcx>( let nested_cause = if matches!( obligation.cause.code(), - ObligationCauseCode::CompareImplItemObligation { .. } + ObligationCauseCode::CompareImplItem { .. } | ObligationCauseCode::CheckAssociatedTypeBounds { .. } | ObligationCauseCode::AscribeUserTypeProvePredicate(..) ) { @@ -2123,13 +2123,13 @@ fn assoc_ty_own_obligations<'cx, 'tcx>( ObligationCause::new( obligation.cause.span, obligation.cause.body_id, - ObligationCauseCode::ItemObligation(obligation.predicate.def_id), + ObligationCauseCode::MiscItem(obligation.predicate.def_id), ) } else { ObligationCause::new( obligation.cause.span, obligation.cause.body_id, - ObligationCauseCode::BindingObligation(obligation.predicate.def_id, span), + ObligationCauseCode::Where(obligation.predicate.def_id, span), ) }; nested.push(Obligation::with_depth( diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 3746e56b7c9..5e06ef1c2da 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -1396,7 +1396,7 @@ fn confirm_const_destruct_candidate( debug!(?args, "impl args"); let cause = obligation.derived_cause(|derived| { - ObligationCauseCode::ImplDerivedObligation(Box::new(ImplDerivedObligationCause { + ObligationCauseCode::ImplDerived(Box::new(ImplDerivedObligationCause { derived, impl_or_alias_def_id: impl_def_id, impl_def_predicate_index: None, diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index f9246d22bfe..b95f784cd9b 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -2771,14 +2771,12 @@ fn impl_or_trait_obligations( cause.clone() } else { cause.clone().derived_cause(parent_trait_pred, |derived| { - ObligationCauseCode::ImplDerivedObligation(Box::new( - ImplDerivedObligationCause { - derived, - impl_or_alias_def_id: def_id, - impl_def_predicate_index: Some(index), - span, - }, - )) + ObligationCauseCode::ImplDerived(Box::new(ImplDerivedObligationCause { + derived, + impl_or_alias_def_id: def_id, + impl_def_predicate_index: Some(index), + span, + })) }) }; let clause = normalize_with_depth_to( diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index 44c70f2be00..73c1b2d24db 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -566,9 +566,9 @@ fn nominal_obligations( iter::zip(predicates, origins.into_iter().rev()) .map(|((pred, span), origin_def_id)| { let code = if span.is_dummy() { - ObligationCauseCode::ItemObligation(origin_def_id) + ObligationCauseCode::MiscItem(origin_def_id) } else { - ObligationCauseCode::BindingObligation(origin_def_id, span) + ObligationCauseCode::Where(origin_def_id, span) }; let cause = self.cause(code); traits::Obligation::with_depth( @@ -683,7 +683,7 @@ fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result { } ty::Pat(subty, _) => { - self.require_sized(subty, ObligationCauseCode::MiscObligation); + self.require_sized(subty, ObligationCauseCode::Misc); } ty::Tuple(tys) => { From 9108294a6c37c36ce561eade9d1c321a5d75bddd Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 9 May 2024 20:25:11 -0400 Subject: [PATCH 4/5] More rename fallout --- .../rustc_borrowck/src/region_infer/mod.rs | 2 +- .../src/check/compare_impl_item.rs | 6 +-- .../src/impl_wf_check/min_specialization.rs | 2 +- compiler/rustc_hir_typeck/src/expr.rs | 14 +++---- .../rustc_hir_typeck/src/fn_ctxt/_impl.rs | 2 +- .../src/fn_ctxt/adjust_fulfillment_errors.rs | 6 +-- .../rustc_hir_typeck/src/fn_ctxt/checks.rs | 5 +-- .../rustc_hir_typeck/src/method/confirm.rs | 2 +- compiler/rustc_hir_typeck/src/method/probe.rs | 2 +- .../rustc_hir_typeck/src/method/suggest.rs | 4 +- .../src/infer/error_reporting/mod.rs | 13 ++++--- .../mismatched_static_lifetime.rs | 4 +- .../trait_impl_difference.rs | 5 ++- .../src/infer/error_reporting/note.rs | 9 +++-- .../src/infer/outlives/obligations.rs | 4 +- compiler/rustc_infer/src/traits/mod.rs | 2 +- compiler/rustc_infer/src/traits/util.rs | 2 +- compiler/rustc_middle/src/traits/mod.rs | 37 ++++++++++--------- .../src/solve/fulfill.rs | 5 +-- .../src/traits/error_reporting/suggestions.rs | 18 ++++----- .../error_reporting/type_err_ctxt_ext.rs | 14 +++---- .../src/traits/project.rs | 4 +- .../src/traits/select/confirmation.rs | 12 +++--- .../src/traits/select/mod.rs | 6 +-- .../rustc_trait_selection/src/traits/wf.rs | 4 +- 25 files changed, 93 insertions(+), 91 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index 2120d3e61f7..e61c1ee98cd 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -2059,7 +2059,7 @@ pub(crate) fn best_blame_constraint( // We currently do not store the `DefId` in the `ConstraintCategory` // for performances reasons. The error reporting code used by NLL only // uses the span, so this doesn't cause any problems at the moment. - Some(ObligationCauseCode::Where(CRATE_DEF_ID.to_def_id(), predicate_span)) + Some(ObligationCauseCode::SpannedItem(CRATE_DEF_ID.to_def_id(), predicate_span)) } else { None } diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index 97b60d137e6..00f223f2ca3 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -819,7 +819,7 @@ fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { ObligationCause::new( self.span, self.body_id, - ObligationCauseCode::Where(proj.def_id, pred_span), + ObligationCauseCode::SpannedItem(proj.def_id, pred_span), ), self.param_env, pred, @@ -2014,7 +2014,7 @@ pub(super) fn check_type_bounds<'tcx>( let code = if span.is_dummy() { ObligationCauseCode::MiscItem(trait_ty.def_id) } else { - ObligationCauseCode::Where(trait_ty.def_id, span) + ObligationCauseCode::SpannedItem(trait_ty.def_id, span) }; ObligationCause::new(impl_ty_span, impl_ty_def_id, code) }; @@ -2251,7 +2251,7 @@ fn try_report_async_mismatch<'tcx>( }; for error in errors { - if let ObligationCauseCode::Where(def_id, _) = *error.root_obligation.cause.code() + if let ObligationCauseCode::SpannedItem(def_id, _) = *error.root_obligation.cause.code() && def_id == async_future_def_id && let Some(proj) = error.root_obligation.predicate.to_opt_poly_projection_pred() && let Some(proj) = proj.no_bound_vars() diff --git a/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs b/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs index 9486e241f98..77dbb23fcb6 100644 --- a/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs +++ b/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs @@ -212,7 +212,7 @@ fn get_impl_args( traits::ObligationCause::new( impl1_span, impl1_def_id, - traits::ObligationCauseCode::Where(impl2_node.def_id(), span), + traits::ObligationCauseCode::SpannedItem(impl2_node.def_id(), span), ) }, ); diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 970f25983de..cdf17f3a113 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -3083,14 +3083,12 @@ fn find_and_report_unsatisfied_index_impl( polarity: ty::PredicatePolarity::Positive, }), |derived| { - ObligationCauseCode::ImplDerived(Box::new( - traits::ImplDerivedObligationCause { - derived, - impl_or_alias_def_id: impl_def_id, - impl_def_predicate_index: Some(idx), - span, - }, - )) + ObligationCauseCode::ImplDerived(Box::new(traits::ImplDerivedCause { + derived, + impl_or_alias_def_id: impl_def_id, + impl_def_predicate_index: Some(idx), + span, + })) }, ) }, diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 0ca7d496d8b..8e9b12cb606 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -1415,7 +1415,7 @@ pub(crate) fn add_required_obligations_for_hir( if span.is_dummy() { ObligationCauseCode::MiscItemInExpr(def_id, hir_id, idx) } else { - ObligationCauseCode::WhereInExpr(def_id, span, hir_id, idx) + ObligationCauseCode::SpannedItemInExpr(def_id, span, hir_id, idx) } }) } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs index 5e136bb6afd..d1336d58eed 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs @@ -15,7 +15,7 @@ pub fn adjust_fulfillment_error_for_expr_obligation( error: &mut traits::FulfillmentError<'tcx>, ) -> bool { let (ObligationCauseCode::MiscItemInExpr(def_id, hir_id, idx) - | ObligationCauseCode::WhereInExpr(def_id, _, hir_id, idx)) = + | ObligationCauseCode::SpannedItemInExpr(def_id, _, hir_id, idx)) = *error.obligation.cause.code().peel_derives() else { return false; @@ -512,7 +512,7 @@ fn blame_specific_expr_if_possible_for_obligation_cause_code( expr: &'tcx hir::Expr<'tcx>, ) -> Result<&'tcx hir::Expr<'tcx>, &'tcx hir::Expr<'tcx>> { match obligation_cause_code { - traits::ObligationCauseCode::WhereInExpr(_, _, _, _) => { + traits::ObligationCauseCode::SpannedItemInExpr(_, _, _, _) => { // This is the "root"; we assume that the `expr` is already pointing here. // Therefore, we return `Ok` so that this `expr` can be refined further. Ok(expr) @@ -555,7 +555,7 @@ fn blame_specific_expr_if_possible_for_obligation_cause_code( /// only a partial success - but it cannot be refined even further. fn blame_specific_expr_if_possible_for_derived_predicate_obligation( &self, - obligation: &traits::ImplDerivedObligationCause<'tcx>, + obligation: &traits::ImplDerivedCause<'tcx>, expr: &'tcx hir::Expr<'tcx>, ) -> Result<&'tcx hir::Expr<'tcx>, &'tcx hir::Expr<'tcx>> { // First, we attempt to refine the `expr` for our span using the parent obligation. diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 3e05f47646c..f82aeb63734 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -33,7 +33,6 @@ use rustc_infer::infer::error_reporting::{FailureCode, ObligationCauseExt}; use rustc_infer::infer::TypeTrace; use rustc_infer::infer::{DefineOpaqueTypes, InferOk}; -use rustc_middle::traits::ObligationCauseCode::ExprBindingObligation; use rustc_middle::ty::adjustment::AllowTwoPhase; use rustc_middle::ty::visit::TypeVisitableExt; use rustc_middle::ty::{self, IsSuggestable, Ty, TyCtxt}; @@ -2014,7 +2013,7 @@ pub(super) fn collect_unused_stmts_for_coerce_return_ty( for (span, code) in errors_causecode { self.dcx().try_steal_modify_and_emit_err(span, StashKey::MaybeForgetReturn, |err| { if let Some(fn_sig) = self.body_fn_sig() - && let ExprBindingObligation(_, _, binding_hir_id, ..) = code + && let ObligationCauseCode::SpannedItemInExpr(_, _, binding_hir_id, ..) = code && !fn_sig.output().is_unit() { let mut block_num = 0; @@ -2103,7 +2102,7 @@ pub(super) fn adjust_fulfillment_errors_for_expr_obligation( // // This is because due to normalization, we often register duplicate // obligations with misc obligations that are basically impossible to - // line back up with a useful ExprBindingObligation. + // line back up with a useful SpannedItemInExpr. for error in not_adjusted { for (span, predicate, cause) in &remap_cause { if *predicate == error.obligation.predicate diff --git a/compiler/rustc_hir_typeck/src/method/confirm.rs b/compiler/rustc_hir_typeck/src/method/confirm.rs index 1d697b25641..7cb60c792ba 100644 --- a/compiler/rustc_hir_typeck/src/method/confirm.rs +++ b/compiler/rustc_hir_typeck/src/method/confirm.rs @@ -567,7 +567,7 @@ fn add_obligations( let code = if span.is_dummy() { ObligationCauseCode::MiscItemInExpr(def_id, self.call_expr.hir_id, idx) } else { - ObligationCauseCode::WhereInExpr(def_id, span, self.call_expr.hir_id, idx) + ObligationCauseCode::SpannedItemInExpr(def_id, span, self.call_expr.hir_id, idx) }; traits::ObligationCause::new(self.span, self.body_id, code) }, diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index 021200d9d53..8d5696d3e5c 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -1408,7 +1408,7 @@ fn consider_probe( idx, ) } else { - ObligationCauseCode::WhereInExpr( + ObligationCauseCode::SpannedItemInExpr( impl_def_id, span, self.scope_expr_id, diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 31677525d2a..aa814d2b457 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -830,8 +830,8 @@ pub fn report_no_match_method_error( (data.impl_or_alias_def_id, data.span) } Some( - ObligationCauseCode::WhereInExpr(def_id, span, _, _) - | ObligationCauseCode::Where(def_id, span), + ObligationCauseCode::SpannedItemInExpr(def_id, span, _, _) + | ObligationCauseCode::SpannedItem(def_id, span), ) => (*def_id, *span), _ => continue, }; diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 5c74420af92..8354dfe3bf3 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -883,8 +883,9 @@ fn note_error_origin( err.help("...or use `match` instead of `let...else`"); } _ => { - if let ObligationCauseCode::Where(_, span) - | ObligationCauseCode::WhereInExpr(_, span, ..) = cause.code().peel_derives() + if let ObligationCauseCode::SpannedItem(_, span) + | ObligationCauseCode::SpannedItemInExpr(_, span, ..) = + cause.code().peel_derives() && let TypeError::RegionsPlaceholderMismatch = terr { err.span_note(*span, "the lifetime requirement is introduced here"); @@ -2011,7 +2012,6 @@ pub fn type_error_additional_suggestions( trace: &TypeTrace<'tcx>, terr: TypeError<'tcx>, ) -> Vec { - use crate::traits::ObligationCauseCode::{BlockTailExpression, MatchExpressionArm}; let mut suggestions = Vec::new(); let span = trace.cause.span(); let values = self.resolve_vars_if_possible(trace.values); @@ -2078,8 +2078,11 @@ pub fn type_error_additional_suggestions( } } let code = trace.cause.code(); - if let &(MatchExpressionArm(box MatchExpressionArmCause { source, .. }) - | BlockTailExpression(.., source)) = code + if let &(ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause { + source, + .. + }) + | ObligationCauseCode::BlockTailExpression(.., source)) = code && let hir::MatchSource::TryDesugar(_) = source && let Some((expected_ty, found_ty, _)) = self.values_str(trace.values) { diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mismatched_static_lifetime.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mismatched_static_lifetime.rs index 26a9e695929..30116b43297 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mismatched_static_lifetime.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mismatched_static_lifetime.rs @@ -38,8 +38,8 @@ pub(super) fn try_report_mismatched_static_lifetime(&self) -> Option Option { // Hack to get around the borrow checker because trace.cause has an `Rc`. - if let ObligationCauseCode::Where(_, span) - | ObligationCauseCode::WhereInExpr(_, span, ..) = + if let ObligationCauseCode::SpannedItem(_, span) + | ObligationCauseCode::SpannedItemInExpr(_, span, ..) = &trace.cause.code().peel_derives() { let span = *span; @@ -370,7 +371,7 @@ pub(super) fn report_placeholder_failure( .with_span_note(span, "the lifetime requirement is introduced here") } else { unreachable!( - "control flow ensures we have a `BindingObligation` or `ExprBindingObligation` here..." + "control flow ensures we have a `BindingObligation` or `SpannedItemInExpr` here..." ) } } diff --git a/compiler/rustc_infer/src/infer/outlives/obligations.rs b/compiler/rustc_infer/src/infer/outlives/obligations.rs index 21abd90916a..58eeec9956d 100644 --- a/compiler/rustc_infer/src/infer/outlives/obligations.rs +++ b/compiler/rustc_infer/src/infer/outlives/obligations.rs @@ -103,8 +103,8 @@ pub fn register_region_obligation_with_cause( cause.span, sup_type, match cause.code().peel_derives() { - ObligationCauseCode::Where(_, span) - | ObligationCauseCode::WhereInExpr(_, span, ..) => Some(*span), + ObligationCauseCode::SpannedItem(_, span) + | ObligationCauseCode::SpannedItemInExpr(_, span, ..) => Some(*span), _ => None, }, ) diff --git a/compiler/rustc_infer/src/traits/mod.rs b/compiler/rustc_infer/src/traits/mod.rs index ce6b3e8f487..85510cf2dcc 100644 --- a/compiler/rustc_infer/src/traits/mod.rs +++ b/compiler/rustc_infer/src/traits/mod.rs @@ -105,7 +105,7 @@ pub fn flip_polarity(&self, tcx: TyCtxt<'tcx>) -> Option PolyTraitObligation<'tcx> { pub fn derived_cause( &self, - variant: impl FnOnce(DerivedObligationCause<'tcx>) -> ObligationCauseCode<'tcx>, + variant: impl FnOnce(DerivedCause<'tcx>) -> ObligationCauseCode<'tcx>, ) -> ObligationCause<'tcx> { self.cause.clone().derived_cause(self.predicate, variant) } diff --git a/compiler/rustc_infer/src/traits/util.rs b/compiler/rustc_infer/src/traits/util.rs index 1104df570a8..d8d84b777e0 100644 --- a/compiler/rustc_infer/src/traits/util.rs +++ b/compiler/rustc_infer/src/traits/util.rs @@ -129,7 +129,7 @@ fn child_with_derived_cause( index: usize, ) -> Self { let cause = self.cause.clone().derived_cause(parent_trait_pred, |derived| { - ObligationCauseCode::ImplDerived(Box::new(traits::ImplDerivedObligationCause { + ObligationCauseCode::ImplDerived(Box::new(traits::ImplDerivedCause { derived, impl_or_alias_def_id: parent_trait_pred.def_id(), impl_def_predicate_index: Some(index), diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs index c38e984af73..5e5f28a54e8 100644 --- a/compiler/rustc_middle/src/traits/mod.rs +++ b/compiler/rustc_middle/src/traits/mod.rs @@ -165,7 +165,7 @@ pub fn map_code( pub fn derived_cause( mut self, parent_trait_pred: ty::PolyTraitPredicate<'tcx>, - variant: impl FnOnce(DerivedObligationCause<'tcx>) -> ObligationCauseCode<'tcx>, + variant: impl FnOnce(DerivedCause<'tcx>) -> ObligationCauseCode<'tcx>, ) -> ObligationCause<'tcx> { /*! * Creates a cause for obligations that are derived from @@ -180,8 +180,7 @@ pub fn derived_cause( // NOTE(flaper87): As of now, it keeps track of the whole error // chain. Ideally, we should have a way to configure this either // by using -Z verbose-internals or just a CLI argument. - self.code = - variant(DerivedObligationCause { parent_trait_pred, parent_code: self.code }).into(); + self.code = variant(DerivedCause { parent_trait_pred, parent_code: self.code }).into(); self } @@ -254,16 +253,16 @@ pub enum ObligationCauseCode<'tcx> { /// Like `MiscItem`, but carries the span of the /// predicate when it can be identified. - Where(DefId, Span), + SpannedItem(DefId, Span), - /// Like `ItemObligation`, but carries the `HirId` of the + /// Like `MiscItem`, but carries the `HirId` of the /// expression that caused the obligation, and the `usize` /// indicates exactly which predicate it is in the list of /// instantiated predicates. MiscItemInExpr(DefId, HirId, usize), - /// Combines `ExprItemObligation` and `BindingObligation`. - WhereInExpr(DefId, Span, HirId, usize), + /// Combines `SpannedItem` and `MiscItemInExpr`. + SpannedItemInExpr(DefId, Span, HirId, usize), /// A type like `&'a T` is WF only if `T: 'a`. ReferenceOutlivesReferent(Ty<'tcx>), @@ -327,16 +326,18 @@ pub enum ObligationCauseCode<'tcx> { /// Derived obligation (i.e. theoretical `where` clause) on a built-in /// implementation like `Copy` or `Sized`. - BuiltinDerived(DerivedObligationCause<'tcx>), + BuiltinDerived(DerivedCause<'tcx>), /// Derived obligation (i.e. `where` clause) on an user-provided impl /// or a trait alias. - ImplDerived(Box>), + ImplDerived(Box>), /// Derived obligation for WF goals. - WellFormedDerived(DerivedObligationCause<'tcx>), + WellFormedDerived(DerivedCause<'tcx>), - FunctionArgumentObligation { + /// Derived obligation refined to point at a specific argument in + /// a call or method expression. + FunctionArg { /// The node of the relevant argument in the function call. arg_hir_id: HirId, /// The node of the function call. @@ -347,7 +348,7 @@ pub enum ObligationCauseCode<'tcx> { /// Error derived when checking an impl item is compatible with /// its corresponding trait item's definition - CompareImplItemObligation { + CompareImplItem { impl_item_def_id: LocalDefId, trait_item_def_id: DefId, kind: ty::AssocKind, @@ -495,8 +496,8 @@ pub enum WellFormedLoc { #[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)] #[derive(TypeVisitable, TypeFoldable)] -pub struct ImplDerivedObligationCause<'tcx> { - pub derived: DerivedObligationCause<'tcx>, +pub struct ImplDerivedCause<'tcx> { + pub derived: DerivedCause<'tcx>, /// The `DefId` of the `impl` that gave rise to the `derived` obligation. /// If the `derived` obligation arose from a trait alias, which conceptually has a synthetic impl, /// then this will be the `DefId` of that trait alias. Care should therefore be taken to handle @@ -537,9 +538,9 @@ pub fn parent(&self) -> Option<(&Self, Option>)> { ObligationCauseCode::FunctionArg { parent_code, .. } => Some((parent_code, None)), ObligationCauseCode::BuiltinDerived(derived) | ObligationCauseCode::WellFormedDerived(derived) - | ObligationCauseCode::ImplDerived(box ImplDerivedObligationCause { - derived, .. - }) => Some((&derived.parent_code, Some(derived.parent_trait_pred))), + | ObligationCauseCode::ImplDerived(box ImplDerivedCause { derived, .. }) => { + Some((&derived.parent_code, Some(derived.parent_trait_pred))) + } _ => None, } } @@ -592,7 +593,7 @@ pub struct IfExpressionCause<'tcx> { #[derive(Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)] #[derive(TypeVisitable, TypeFoldable)] -pub struct DerivedObligationCause<'tcx> { +pub struct DerivedCause<'tcx> { /// The trait predicate of the parent obligation that led to the /// current obligation. Note that only trait obligations lead to /// derived obligations, so we just store the trait predicate here diff --git a/compiler/rustc_trait_selection/src/solve/fulfill.rs b/compiler/rustc_trait_selection/src/solve/fulfill.rs index 410890e6680..4cc041fca8c 100644 --- a/compiler/rustc_trait_selection/src/solve/fulfill.rs +++ b/compiler/rustc_trait_selection/src/solve/fulfill.rs @@ -425,7 +425,7 @@ fn derive_cause<'tcx>( tcx.predicates_of(impl_def_id).instantiate_identity(tcx).iter().nth(idx) { cause = cause.derived_cause(parent_trait_pred, |derived| { - ObligationCauseCode::ImplDerived(Box::new(traits::ImplDerivedObligationCause { + ObligationCauseCode::ImplDerived(Box::new(traits::ImplDerivedCause { derived, impl_or_alias_def_id: impl_def_id, impl_def_predicate_index: Some(idx), @@ -435,8 +435,7 @@ fn derive_cause<'tcx>( } } ProbeKind::TraitCandidate { source: CandidateSource::BuiltinImpl(..), result: _ } => { - cause = cause - .derived_cause(parent_trait_pred, ObligationCauseCode::BuiltinDerivedObligation); + cause = cause.derived_cause(parent_trait_pred, ObligationCauseCode::BuiltinDerived); } _ => {} }; diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 86615475607..9839f4768e2 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -7,7 +7,7 @@ use crate::errors; use crate::infer::InferCtxt; -use crate::traits::{ImplDerivedObligationCause, NormalizeExt, ObligationCtxt}; +use crate::traits::{ImplDerivedCause, NormalizeExt, ObligationCtxt}; use hir::def::CtorOf; use rustc_data_structures::fx::FxHashSet; @@ -1400,10 +1400,10 @@ fn suggest_add_reference_to_arg( if let ObligationCauseCode::ImplDerived(cause) = &*code { try_borrowing(cause.derived.parent_trait_pred, &[]) - } else if let ObligationCauseCode::Where(_, _) + } else if let ObligationCauseCode::SpannedItem(_, _) | ObligationCauseCode::MiscItem(_) | ObligationCauseCode::MiscItemInExpr(..) - | ObligationCauseCode::WhereInExpr(..) = code + | ObligationCauseCode::SpannedItemInExpr(..) = code { try_borrowing(poly_trait_pred, &never_suggest_borrow) } else { @@ -2099,10 +2099,10 @@ fn note_conflicting_closure_bounds( cause: &ObligationCauseCode<'tcx>, err: &mut Diag<'tcx>, ) { - // First, look for an `ExprBindingObligation`, which means we can get + // First, look for an `SpannedItemInExpr`, which means we can get // the uninstantiated predicate list of the called function. And check // that the predicate that we failed to satisfy is a `Fn`-like trait. - if let ObligationCauseCode::WhereInExpr(def_id, _, _, idx) = cause + if let ObligationCauseCode::SpannedItemInExpr(def_id, _, _, idx) = cause && let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx) && let Some(pred) = predicates.predicates.get(*idx) && let ty::ClauseKind::Trait(trait_pred) = pred.kind().skip_binder() @@ -2747,8 +2747,8 @@ fn note_obligation_cause_code( // We hold the `DefId` of the item introducing the obligation, but displaying it // doesn't add user usable information. It always point at an associated item. } - ObligationCauseCode::Where(item_def_id, span) - | ObligationCauseCode::WhereInExpr(item_def_id, span, ..) => { + ObligationCauseCode::SpannedItem(item_def_id, span) + | ObligationCauseCode::SpannedItemInExpr(item_def_id, span, ..) => { let item_name = tcx.def_path_str(item_def_id); let short_item_name = with_forced_trimmed_paths!(tcx.def_path_str(item_def_id)); let mut multispan = MultiSpan::from(span); @@ -3799,7 +3799,7 @@ fn note_function_argument_obligation( // to an associated type (as seen from `trait_pred`) in the predicate. Like in // trait_pred `S: Sum<::Item>` and predicate `i32: Sum<&()>` let mut type_diffs = vec![]; - if let ObligationCauseCode::WhereInExpr(def_id, _, _, idx) = parent_code + if let ObligationCauseCode::SpannedItemInExpr(def_id, _, _, idx) = parent_code && let Some(node_args) = typeck_results.node_args_opt(call_hir_id) && let where_clauses = self.tcx.predicates_of(def_id).instantiate(self.tcx, node_args) @@ -4955,7 +4955,7 @@ fn point_at_assoc_type_restriction( trait_name: &str, predicate: ty::Predicate<'_>, generics: &hir::Generics<'_>, - data: &ImplDerivedObligationCause<'_>, + data: &ImplDerivedCause<'_>, ) { let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder() else { return; diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs index e85a2b50bf4..f3a4e933164 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs @@ -1533,9 +1533,9 @@ fn report_fulfillment_error(&self, error: &FulfillmentError<'tcx>) -> ErrorGuara *err, ); let code = error.obligation.cause.code().peel_derives().peel_match_impls(); - if let ObligationCauseCode::Where(..) + if let ObligationCauseCode::SpannedItem(..) | ObligationCauseCode::MiscItem(..) - | ObligationCauseCode::WhereInExpr(..) + | ObligationCauseCode::SpannedItemInExpr(..) | ObligationCauseCode::MiscItemInExpr(..) = code { self.note_obligation_cause_code( @@ -1611,9 +1611,9 @@ fn report_projection_error( let is_normalized_term_expected = !matches!( obligation.cause.code().peel_derives(), ObligationCauseCode::MiscItem(_) - | ObligationCauseCode::Where(_, _) + | ObligationCauseCode::SpannedItem(_, _) | ObligationCauseCode::MiscItemInExpr(..) - | ObligationCauseCode::WhereInExpr(..) + | ObligationCauseCode::SpannedItemInExpr(..) | ObligationCauseCode::Coercion { .. } ); @@ -2881,8 +2881,8 @@ fn suggest_unsized_bound_if_applicable( else { return; }; - let (ObligationCauseCode::Where(item_def_id, span) - | ObligationCauseCode::WhereInExpr(item_def_id, span, ..)) = + let (ObligationCauseCode::SpannedItem(item_def_id, span) + | ObligationCauseCode::SpannedItemInExpr(item_def_id, span, ..)) = *obligation.cause.code().peel_derives() else { return; @@ -3179,7 +3179,7 @@ fn add_tuple_trait_message( ObligationCauseCode::RustCall => { err.primary_message("functions with the \"rust-call\" ABI must take a single non-self tuple argument"); } - ObligationCauseCode::Where(def_id, _) | ObligationCauseCode::MiscItem(def_id) + ObligationCauseCode::SpannedItem(def_id, _) | ObligationCauseCode::MiscItem(def_id) if self.tcx.is_fn_trait(*def_id) => { err.code(E0059); diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 5cffd1fe8ee..e99ecff44f9 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -576,7 +576,7 @@ pub fn normalize_inherent_projection<'a, 'b, 'tcx>( if span.is_dummy() { ObligationCauseCode::MiscItem(alias_ty.def_id) } else { - ObligationCauseCode::Where(alias_ty.def_id, span) + ObligationCauseCode::SpannedItem(alias_ty.def_id, span) }, ); @@ -2129,7 +2129,7 @@ fn assoc_ty_own_obligations<'cx, 'tcx>( ObligationCause::new( obligation.cause.span, obligation.cause.body_id, - ObligationCauseCode::Where(obligation.predicate.def_id, span), + ObligationCauseCode::SpannedItem(obligation.predicate.def_id, span), ) }; nested.push(Obligation::with_depth( diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 5e06ef1c2da..0b91dd72e3b 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -26,7 +26,7 @@ VtblSegment, }; use crate::traits::{ - ImplDerivedObligationCause, ImplSource, ImplSourceUserDefinedData, Normalized, Obligation, + ImplDerivedCause, ImplSource, ImplSourceUserDefinedData, Normalized, Obligation, ObligationCause, PolyTraitObligation, PredicateObligation, Selection, SelectionError, SignatureMismatch, TraitNotObjectSafe, TraitObligation, Unimplemented, }; @@ -275,7 +275,7 @@ fn confirm_builtin_candidate( bug!("obligation {:?} had matched a builtin impl but now doesn't", obligation); }; - let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerivedObligation); + let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived); self.collect_predicates_for_types( obligation.param_env, cause, @@ -435,7 +435,7 @@ fn vtable_auto_impl( ) -> Vec> { debug!(?nested, "vtable_auto_impl"); ensure_sufficient_stack(|| { - let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerivedObligation); + let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived); let poly_trait_ref = obligation.predicate.to_poly_trait_ref(); let trait_ref = self.infcx.enter_forall_and_leak_universe(poly_trait_ref); @@ -723,7 +723,7 @@ fn confirm_fn_pointer_candidate( let mut nested = self.equate_trait_refs(obligation.with(tcx, placeholder_predicate), trait_ref)?; - let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerivedObligation); + let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived); // Confirm the `type Output: Sized;` bound that is present on `FnOnce` let output_ty = self.infcx.enter_forall_and_leak_universe(sig.output()); @@ -1381,7 +1381,7 @@ fn confirm_const_destruct_candidate( let self_ty = obligation.self_ty().map_bound(|ty| self.infcx.shallow_resolve(ty)); let mut nested = vec![]; - let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerivedObligation); + let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived); // If we have a custom `impl const Drop`, then // first check it like a regular impl candidate. @@ -1396,7 +1396,7 @@ fn confirm_const_destruct_candidate( debug!(?args, "impl args"); let cause = obligation.derived_cause(|derived| { - ObligationCauseCode::ImplDerived(Box::new(ImplDerivedObligationCause { + ObligationCauseCode::ImplDerived(Box::new(ImplDerivedCause { derived, impl_or_alias_def_id: impl_def_id, impl_def_predicate_index: None, diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index b95f784cd9b..50353a1fd2f 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -13,8 +13,8 @@ use super::util::closure_trait_ref_and_return_type; use super::wf; use super::{ - ImplDerivedObligationCause, Normalized, Obligation, ObligationCause, ObligationCauseCode, - Overflow, PolyTraitObligation, PredicateObligation, Selection, SelectionError, SelectionResult, + ImplDerivedCause, Normalized, Obligation, ObligationCause, ObligationCauseCode, Overflow, + PolyTraitObligation, PredicateObligation, Selection, SelectionError, SelectionResult, TraitQueryMode, }; @@ -2771,7 +2771,7 @@ fn impl_or_trait_obligations( cause.clone() } else { cause.clone().derived_cause(parent_trait_pred, |derived| { - ObligationCauseCode::ImplDerived(Box::new(ImplDerivedObligationCause { + ObligationCauseCode::ImplDerived(Box::new(ImplDerivedCause { derived, impl_or_alias_def_id: def_id, impl_def_predicate_index: Some(index), diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index 73c1b2d24db..d4eb3af3b64 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -382,7 +382,7 @@ fn compute_trait_pred(&mut self, trait_pred: ty::TraitPredicate<'tcx>, elaborate if let Some(parent_trait_pred) = predicate.to_opt_poly_trait_pred() { cause = cause.derived_cause( parent_trait_pred, - traits::ObligationCauseCode::WellFormedDerivedObligation, + traits::ObligationCauseCode::WellFormedDerived, ); } extend_cause_with_original_assoc_item_obligation(tcx, item, &mut cause, predicate); @@ -568,7 +568,7 @@ fn nominal_obligations( let code = if span.is_dummy() { ObligationCauseCode::MiscItem(origin_def_id) } else { - ObligationCauseCode::Where(origin_def_id, span) + ObligationCauseCode::SpannedItem(origin_def_id, span) }; let cause = self.cause(code); traits::Obligation::with_depth( From 6f77bfe8b63b2e1bba71a0eb4973ccd17fe8885c Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 9 May 2024 21:18:48 -0400 Subject: [PATCH 5/5] Name tweaks --- .../rustc_borrowck/src/region_infer/mod.rs | 5 +++- .../src/transform/check_consts/check.rs | 2 +- .../src/check/compare_impl_item.rs | 9 ++++--- .../rustc_hir_analysis/src/check/wfcheck.rs | 2 +- .../src/impl_wf_check/min_specialization.rs | 2 +- .../rustc_hir_typeck/src/fn_ctxt/_impl.rs | 4 +-- .../src/fn_ctxt/adjust_fulfillment_errors.rs | 6 ++--- .../rustc_hir_typeck/src/fn_ctxt/checks.rs | 5 ++-- .../rustc_hir_typeck/src/method/confirm.rs | 9 +++++-- compiler/rustc_hir_typeck/src/method/probe.rs | 4 +-- .../rustc_hir_typeck/src/method/suggest.rs | 4 +-- .../src/infer/error_reporting/mod.rs | 4 +-- .../mismatched_static_lifetime.rs | 4 +-- .../nice_region_error/placeholder_error.rs | 4 +-- .../nice_region_error/static_impl_trait.rs | 4 +-- .../src/infer/error_reporting/note.rs | 10 +++---- .../src/infer/outlives/obligations.rs | 4 +-- compiler/rustc_middle/src/traits/mod.rs | 14 +++++----- .../src/traits/error_reporting/suggestions.rs | 27 ++++++++++--------- .../error_reporting/type_err_ctxt_ext.rs | 27 ++++++++++--------- .../src/traits/project.rs | 8 +++--- .../rustc_trait_selection/src/traits/wf.rs | 4 +-- 22 files changed, 87 insertions(+), 75 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index e61c1ee98cd..e58723c2ec9 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -2059,7 +2059,10 @@ pub(crate) fn best_blame_constraint( // We currently do not store the `DefId` in the `ConstraintCategory` // for performances reasons. The error reporting code used by NLL only // uses the span, so this doesn't cause any problems at the moment. - Some(ObligationCauseCode::SpannedItem(CRATE_DEF_ID.to_def_id(), predicate_span)) + Some(ObligationCauseCode::SpannedWhereClause( + CRATE_DEF_ID.to_def_id(), + predicate_span, + )) } else { None } diff --git a/compiler/rustc_const_eval/src/transform/check_consts/check.rs b/compiler/rustc_const_eval/src/transform/check_consts/check.rs index adbeacecc0c..d43dc467c0f 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/check.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/check.rs @@ -738,7 +738,7 @@ fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location let cause = ObligationCause::new( terminator.source_info.span, self.body.source.def_id().expect_local(), - ObligationCauseCode::MiscItem(callee), + ObligationCauseCode::WhereClause(callee), ); let normalized_predicates = ocx.normalize(&cause, param_env, predicates); ocx.register_obligations(traits::predicates_for_generics( diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index 00f223f2ca3..b2b82670d8b 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -819,7 +819,7 @@ fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { ObligationCause::new( self.span, self.body_id, - ObligationCauseCode::SpannedItem(proj.def_id, pred_span), + ObligationCauseCode::SpannedWhereClause(proj.def_id, pred_span), ), self.param_env, pred, @@ -2012,9 +2012,9 @@ pub(super) fn check_type_bounds<'tcx>( ); let mk_cause = |span: Span| { let code = if span.is_dummy() { - ObligationCauseCode::MiscItem(trait_ty.def_id) + ObligationCauseCode::WhereClause(trait_ty.def_id) } else { - ObligationCauseCode::SpannedItem(trait_ty.def_id, span) + ObligationCauseCode::SpannedWhereClause(trait_ty.def_id, span) }; ObligationCause::new(impl_ty_span, impl_ty_def_id, code) }; @@ -2251,7 +2251,8 @@ fn try_report_async_mismatch<'tcx>( }; for error in errors { - if let ObligationCauseCode::SpannedItem(def_id, _) = *error.root_obligation.cause.code() + if let ObligationCauseCode::SpannedWhereClause(def_id, _) = + *error.root_obligation.cause.code() && def_id == async_future_def_id && let Some(proj) = error.root_obligation.predicate.to_opt_poly_projection_pred() && let Some(proj) = proj.no_bound_vars() diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 49d4b8ecfe7..0578317f914 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -1550,7 +1550,7 @@ fn visit_const(&mut self, c: ty::Const<'tcx>) -> Self::Result { let cause = traits::ObligationCause::new( sp, wfcx.body_def_id, - ObligationCauseCode::MiscItem(def_id.to_def_id()), + ObligationCauseCode::WhereClause(def_id.to_def_id()), ); traits::Obligation::new(tcx, cause, wfcx.param_env, pred) }); diff --git a/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs b/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs index 77dbb23fcb6..de697b04ebf 100644 --- a/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs +++ b/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs @@ -212,7 +212,7 @@ fn get_impl_args( traits::ObligationCause::new( impl1_span, impl1_def_id, - traits::ObligationCauseCode::SpannedItem(impl2_node.def_id(), span), + traits::ObligationCauseCode::SpannedWhereClause(impl2_node.def_id(), span), ) }, ); diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 8e9b12cb606..c214634832b 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -1413,9 +1413,9 @@ pub(crate) fn add_required_obligations_for_hir( ) { self.add_required_obligations_with_code(span, def_id, args, |idx, span| { if span.is_dummy() { - ObligationCauseCode::MiscItemInExpr(def_id, hir_id, idx) + ObligationCauseCode::WhereClauseInExpr(def_id, hir_id, idx) } else { - ObligationCauseCode::SpannedItemInExpr(def_id, span, hir_id, idx) + ObligationCauseCode::SpannedWhereClauseInExpr(def_id, span, hir_id, idx) } }) } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs index d1336d58eed..26996397659 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs @@ -14,8 +14,8 @@ pub fn adjust_fulfillment_error_for_expr_obligation( &self, error: &mut traits::FulfillmentError<'tcx>, ) -> bool { - let (ObligationCauseCode::MiscItemInExpr(def_id, hir_id, idx) - | ObligationCauseCode::SpannedItemInExpr(def_id, _, hir_id, idx)) = + let (ObligationCauseCode::WhereClauseInExpr(def_id, hir_id, idx) + | ObligationCauseCode::SpannedWhereClauseInExpr(def_id, _, hir_id, idx)) = *error.obligation.cause.code().peel_derives() else { return false; @@ -512,7 +512,7 @@ fn blame_specific_expr_if_possible_for_obligation_cause_code( expr: &'tcx hir::Expr<'tcx>, ) -> Result<&'tcx hir::Expr<'tcx>, &'tcx hir::Expr<'tcx>> { match obligation_cause_code { - traits::ObligationCauseCode::SpannedItemInExpr(_, _, _, _) => { + traits::ObligationCauseCode::SpannedWhereClauseInExpr(_, _, _, _) => { // This is the "root"; we assume that the `expr` is already pointing here. // Therefore, we return `Ok` so that this `expr` can be refined further. Ok(expr) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index f82aeb63734..4bf82355b62 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -2013,7 +2013,8 @@ pub(super) fn collect_unused_stmts_for_coerce_return_ty( for (span, code) in errors_causecode { self.dcx().try_steal_modify_and_emit_err(span, StashKey::MaybeForgetReturn, |err| { if let Some(fn_sig) = self.body_fn_sig() - && let ObligationCauseCode::SpannedItemInExpr(_, _, binding_hir_id, ..) = code + && let ObligationCauseCode::SpannedWhereClauseInExpr(_, _, binding_hir_id, ..) = + code && !fn_sig.output().is_unit() { let mut block_num = 0; @@ -2102,7 +2103,7 @@ pub(super) fn adjust_fulfillment_errors_for_expr_obligation( // // This is because due to normalization, we often register duplicate // obligations with misc obligations that are basically impossible to - // line back up with a useful SpannedItemInExpr. + // line back up with a useful SpannedWhereClauseInExpr. for error in not_adjusted { for (span, predicate, cause) in &remap_cause { if *predicate == error.obligation.predicate diff --git a/compiler/rustc_hir_typeck/src/method/confirm.rs b/compiler/rustc_hir_typeck/src/method/confirm.rs index 7cb60c792ba..56c296fc1c0 100644 --- a/compiler/rustc_hir_typeck/src/method/confirm.rs +++ b/compiler/rustc_hir_typeck/src/method/confirm.rs @@ -565,9 +565,14 @@ fn add_obligations( for obligation in traits::predicates_for_generics( |idx, span| { let code = if span.is_dummy() { - ObligationCauseCode::MiscItemInExpr(def_id, self.call_expr.hir_id, idx) + ObligationCauseCode::WhereClauseInExpr(def_id, self.call_expr.hir_id, idx) } else { - ObligationCauseCode::SpannedItemInExpr(def_id, span, self.call_expr.hir_id, idx) + ObligationCauseCode::SpannedWhereClauseInExpr( + def_id, + span, + self.call_expr.hir_id, + idx, + ) }; traits::ObligationCause::new(self.span, self.body_id, code) }, diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index 8d5696d3e5c..434bd957498 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -1402,13 +1402,13 @@ fn consider_probe( ocx.register_obligations(traits::predicates_for_generics( |idx, span| { let code = if span.is_dummy() { - ObligationCauseCode::MiscItemInExpr( + ObligationCauseCode::WhereClauseInExpr( impl_def_id, self.scope_expr_id, idx, ) } else { - ObligationCauseCode::SpannedItemInExpr( + ObligationCauseCode::SpannedWhereClauseInExpr( impl_def_id, span, self.scope_expr_id, diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index aa814d2b457..42429812d9f 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -830,8 +830,8 @@ pub fn report_no_match_method_error( (data.impl_or_alias_def_id, data.span) } Some( - ObligationCauseCode::SpannedItemInExpr(def_id, span, _, _) - | ObligationCauseCode::SpannedItem(def_id, span), + ObligationCauseCode::SpannedWhereClauseInExpr(def_id, span, _, _) + | ObligationCauseCode::SpannedWhereClause(def_id, span), ) => (*def_id, *span), _ => continue, }; diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 8354dfe3bf3..89cdc3e45a5 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -883,8 +883,8 @@ fn note_error_origin( err.help("...or use `match` instead of `let...else`"); } _ => { - if let ObligationCauseCode::SpannedItem(_, span) - | ObligationCauseCode::SpannedItemInExpr(_, span, ..) = + if let ObligationCauseCode::SpannedWhereClause(_, span) + | ObligationCauseCode::SpannedWhereClauseInExpr(_, span, ..) = cause.code().peel_derives() && let TypeError::RegionsPlaceholderMismatch = terr { diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mismatched_static_lifetime.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mismatched_static_lifetime.rs index 30116b43297..b6c38739e9a 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mismatched_static_lifetime.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mismatched_static_lifetime.rs @@ -38,8 +38,8 @@ pub(super) fn try_report_mismatched_static_lifetime(&self) -> Option Option { _ => cause.code(), } && let ( - &ObligationCauseCode::MiscItem(item_def_id) - | &ObligationCauseCode::MiscItemInExpr(item_def_id, ..), + &ObligationCauseCode::WhereClause(item_def_id) + | &ObligationCauseCode::WhereClauseInExpr(item_def_id, ..), None, ) = (code, override_error_code) { diff --git a/compiler/rustc_infer/src/infer/error_reporting/note.rs b/compiler/rustc_infer/src/infer/error_reporting/note.rs index b1d978867cb..ecbe65fe926 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/note.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/note.rs @@ -357,13 +357,13 @@ pub(super) fn report_placeholder_failure( infer::Subtype(box ref trace) if matches!( &trace.cause.code().peel_derives(), - ObligationCauseCode::SpannedItem(..) - | ObligationCauseCode::SpannedItemInExpr(..) + ObligationCauseCode::SpannedWhereClause(..) + | ObligationCauseCode::SpannedWhereClauseInExpr(..) ) => { // Hack to get around the borrow checker because trace.cause has an `Rc`. - if let ObligationCauseCode::SpannedItem(_, span) - | ObligationCauseCode::SpannedItemInExpr(_, span, ..) = + if let ObligationCauseCode::SpannedWhereClause(_, span) + | ObligationCauseCode::SpannedWhereClauseInExpr(_, span, ..) = &trace.cause.code().peel_derives() { let span = *span; @@ -371,7 +371,7 @@ pub(super) fn report_placeholder_failure( .with_span_note(span, "the lifetime requirement is introduced here") } else { unreachable!( - "control flow ensures we have a `BindingObligation` or `SpannedItemInExpr` here..." + "control flow ensures we have a `BindingObligation` or `SpannedWhereClauseInExpr` here..." ) } } diff --git a/compiler/rustc_infer/src/infer/outlives/obligations.rs b/compiler/rustc_infer/src/infer/outlives/obligations.rs index 58eeec9956d..94f8a2664f9 100644 --- a/compiler/rustc_infer/src/infer/outlives/obligations.rs +++ b/compiler/rustc_infer/src/infer/outlives/obligations.rs @@ -103,8 +103,8 @@ pub fn register_region_obligation_with_cause( cause.span, sup_type, match cause.code().peel_derives() { - ObligationCauseCode::SpannedItem(_, span) - | ObligationCauseCode::SpannedItemInExpr(_, span, ..) => Some(*span), + ObligationCauseCode::SpannedWhereClause(_, span) + | ObligationCauseCode::SpannedWhereClauseInExpr(_, span, ..) => Some(*span), _ => None, }, ) diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs index 5e5f28a54e8..6c33a29ea81 100644 --- a/compiler/rustc_middle/src/traits/mod.rs +++ b/compiler/rustc_middle/src/traits/mod.rs @@ -249,20 +249,20 @@ pub enum ObligationCauseCode<'tcx> { /// Must satisfy all of the where-clause predicates of the /// given item. - MiscItem(DefId), + WhereClause(DefId), - /// Like `MiscItem`, but carries the span of the + /// Like `WhereClause`, but carries the span of the /// predicate when it can be identified. - SpannedItem(DefId, Span), + SpannedWhereClause(DefId, Span), - /// Like `MiscItem`, but carries the `HirId` of the + /// Like `WhereClause`, but carries the `HirId` of the /// expression that caused the obligation, and the `usize` /// indicates exactly which predicate it is in the list of /// instantiated predicates. - MiscItemInExpr(DefId, HirId, usize), + WhereClauseInExpr(DefId, HirId, usize), - /// Combines `SpannedItem` and `MiscItemInExpr`. - SpannedItemInExpr(DefId, Span, HirId, usize), + /// Combines `SpannedWhereClause` and `WhereClauseInExpr`. + SpannedWhereClauseInExpr(DefId, Span, HirId, usize), /// A type like `&'a T` is WF only if `T: 'a`. ReferenceOutlivesReferent(Ty<'tcx>), diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 9839f4768e2..3defc03e7f0 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1203,7 +1203,8 @@ fn suggest_add_reference_to_arg( let code = match obligation.cause.code() { ObligationCauseCode::FunctionArg { parent_code, .. } => parent_code, - c @ ObligationCauseCode::MiscItem(_) | c @ ObligationCauseCode::MiscItemInExpr(..) => c, + c @ ObligationCauseCode::WhereClause(_) + | c @ ObligationCauseCode::WhereClauseInExpr(..) => c, c if matches!( span.ctxt().outer_expn_data().kind, ExpnKind::Desugaring(DesugaringKind::ForLoop) @@ -1259,8 +1260,8 @@ fn suggest_add_reference_to_arg( let mut_ref_self_ty_satisfies_pred = mk_result(trait_pred_and_mut_ref); let (ref_inner_ty_satisfies_pred, ref_inner_ty_mut) = - if let ObligationCauseCode::MiscItem(_) | ObligationCauseCode::MiscItemInExpr(..) = - obligation.cause.code() + if let ObligationCauseCode::WhereClause(_) + | ObligationCauseCode::WhereClauseInExpr(..) = obligation.cause.code() && let ty::Ref(_, ty, mutability) = old_pred.self_ty().skip_binder().kind() { ( @@ -1400,10 +1401,10 @@ fn suggest_add_reference_to_arg( if let ObligationCauseCode::ImplDerived(cause) = &*code { try_borrowing(cause.derived.parent_trait_pred, &[]) - } else if let ObligationCauseCode::SpannedItem(_, _) - | ObligationCauseCode::MiscItem(_) - | ObligationCauseCode::MiscItemInExpr(..) - | ObligationCauseCode::SpannedItemInExpr(..) = code + } else if let ObligationCauseCode::SpannedWhereClause(_, _) + | ObligationCauseCode::WhereClause(_) + | ObligationCauseCode::WhereClauseInExpr(..) + | ObligationCauseCode::SpannedWhereClauseInExpr(..) = code { try_borrowing(poly_trait_pred, &never_suggest_borrow) } else { @@ -2099,10 +2100,10 @@ fn note_conflicting_closure_bounds( cause: &ObligationCauseCode<'tcx>, err: &mut Diag<'tcx>, ) { - // First, look for an `SpannedItemInExpr`, which means we can get + // First, look for an `SpannedWhereClauseInExpr`, which means we can get // the uninstantiated predicate list of the called function. And check // that the predicate that we failed to satisfy is a `Fn`-like trait. - if let ObligationCauseCode::SpannedItemInExpr(def_id, _, _, idx) = cause + if let ObligationCauseCode::SpannedWhereClauseInExpr(def_id, _, _, idx) = cause && let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx) && let Some(pred) = predicates.predicates.get(*idx) && let ty::ClauseKind::Trait(trait_pred) = pred.kind().skip_binder() @@ -2743,12 +2744,12 @@ fn note_obligation_cause_code( ObligationCauseCode::TupleElem => { err.note("only the last element of a tuple may have a dynamically sized type"); } - ObligationCauseCode::MiscItem(_) | ObligationCauseCode::MiscItemInExpr(..) => { + ObligationCauseCode::WhereClause(_) | ObligationCauseCode::WhereClauseInExpr(..) => { // We hold the `DefId` of the item introducing the obligation, but displaying it // doesn't add user usable information. It always point at an associated item. } - ObligationCauseCode::SpannedItem(item_def_id, span) - | ObligationCauseCode::SpannedItemInExpr(item_def_id, span, ..) => { + ObligationCauseCode::SpannedWhereClause(item_def_id, span) + | ObligationCauseCode::SpannedWhereClauseInExpr(item_def_id, span, ..) => { let item_name = tcx.def_path_str(item_def_id); let short_item_name = with_forced_trimmed_paths!(tcx.def_path_str(item_def_id)); let mut multispan = MultiSpan::from(span); @@ -3799,7 +3800,7 @@ fn note_function_argument_obligation( // to an associated type (as seen from `trait_pred`) in the predicate. Like in // trait_pred `S: Sum<::Item>` and predicate `i32: Sum<&()>` let mut type_diffs = vec![]; - if let ObligationCauseCode::SpannedItemInExpr(def_id, _, _, idx) = parent_code + if let ObligationCauseCode::SpannedWhereClauseInExpr(def_id, _, _, idx) = parent_code && let Some(node_args) = typeck_results.node_args_opt(call_hir_id) && let where_clauses = self.tcx.predicates_of(def_id).instantiate(self.tcx, node_args) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs index f3a4e933164..de8ec17b113 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs @@ -1533,10 +1533,10 @@ fn report_fulfillment_error(&self, error: &FulfillmentError<'tcx>) -> ErrorGuara *err, ); let code = error.obligation.cause.code().peel_derives().peel_match_impls(); - if let ObligationCauseCode::SpannedItem(..) - | ObligationCauseCode::MiscItem(..) - | ObligationCauseCode::SpannedItemInExpr(..) - | ObligationCauseCode::MiscItemInExpr(..) = code + if let ObligationCauseCode::SpannedWhereClause(..) + | ObligationCauseCode::WhereClause(..) + | ObligationCauseCode::SpannedWhereClauseInExpr(..) + | ObligationCauseCode::WhereClauseInExpr(..) = code { self.note_obligation_cause_code( error.obligation.cause.body_id, @@ -1610,10 +1610,10 @@ fn report_projection_error( let is_normalized_term_expected = !matches!( obligation.cause.code().peel_derives(), - ObligationCauseCode::MiscItem(_) - | ObligationCauseCode::SpannedItem(_, _) - | ObligationCauseCode::MiscItemInExpr(..) - | ObligationCauseCode::SpannedItemInExpr(..) + ObligationCauseCode::WhereClause(_) + | ObligationCauseCode::SpannedWhereClause(_, _) + | ObligationCauseCode::WhereClauseInExpr(..) + | ObligationCauseCode::SpannedWhereClauseInExpr(..) | ObligationCauseCode::Coercion { .. } ); @@ -2445,8 +2445,8 @@ fn maybe_report_ambiguity(&self, obligation: &PredicateObligation<'tcx>) -> Erro } } - if let ObligationCauseCode::MiscItem(def_id) - | ObligationCauseCode::MiscItemInExpr(def_id, ..) = *obligation.cause.code() + if let ObligationCauseCode::WhereClause(def_id) + | ObligationCauseCode::WhereClauseInExpr(def_id, ..) = *obligation.cause.code() { self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id()); } @@ -2881,8 +2881,8 @@ fn suggest_unsized_bound_if_applicable( else { return; }; - let (ObligationCauseCode::SpannedItem(item_def_id, span) - | ObligationCauseCode::SpannedItemInExpr(item_def_id, span, ..)) = + let (ObligationCauseCode::SpannedWhereClause(item_def_id, span) + | ObligationCauseCode::SpannedWhereClauseInExpr(item_def_id, span, ..)) = *obligation.cause.code().peel_derives() else { return; @@ -3179,7 +3179,8 @@ fn add_tuple_trait_message( ObligationCauseCode::RustCall => { err.primary_message("functions with the \"rust-call\" ABI must take a single non-self tuple argument"); } - ObligationCauseCode::SpannedItem(def_id, _) | ObligationCauseCode::MiscItem(def_id) + ObligationCauseCode::SpannedWhereClause(def_id, _) + | ObligationCauseCode::WhereClause(def_id) if self.tcx.is_fn_trait(*def_id) => { err.code(E0059); diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index e99ecff44f9..0aafc1a2efe 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -574,9 +574,9 @@ pub fn normalize_inherent_projection<'a, 'b, 'tcx>( // diagnostics which is not ideal. // Consider creating separate cause codes for this specific situation. if span.is_dummy() { - ObligationCauseCode::MiscItem(alias_ty.def_id) + ObligationCauseCode::WhereClause(alias_ty.def_id) } else { - ObligationCauseCode::SpannedItem(alias_ty.def_id, span) + ObligationCauseCode::SpannedWhereClause(alias_ty.def_id, span) }, ); @@ -2123,13 +2123,13 @@ fn assoc_ty_own_obligations<'cx, 'tcx>( ObligationCause::new( obligation.cause.span, obligation.cause.body_id, - ObligationCauseCode::MiscItem(obligation.predicate.def_id), + ObligationCauseCode::WhereClause(obligation.predicate.def_id), ) } else { ObligationCause::new( obligation.cause.span, obligation.cause.body_id, - ObligationCauseCode::SpannedItem(obligation.predicate.def_id, span), + ObligationCauseCode::SpannedWhereClause(obligation.predicate.def_id, span), ) }; nested.push(Obligation::with_depth( diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index d4eb3af3b64..ebcda61f2ad 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -566,9 +566,9 @@ fn nominal_obligations( iter::zip(predicates, origins.into_iter().rev()) .map(|((pred, span), origin_def_id)| { let code = if span.is_dummy() { - ObligationCauseCode::MiscItem(origin_def_id) + ObligationCauseCode::WhereClause(origin_def_id) } else { - ObligationCauseCode::SpannedItem(origin_def_id, span) + ObligationCauseCode::SpannedWhereClause(origin_def_id, span) }; let cause = self.cause(code); traits::Obligation::with_depth(