diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index 1e75902884f..76fd59577f2 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -964,8 +964,8 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), res = res.and(enter_wf_checking_ctxt(tcx, def_id, |wfcx| { let ty = tcx.type_of(def_id).instantiate_identity(); let ty_span = tcx.ty_span(def_id); - let ty = wfcx.deeply_normalize(ty_span, Some(WellFormedLoc::Ty(def_id)), ty); - wfcx.register_wf_obligation(ty_span, Some(WellFormedLoc::Ty(def_id)), ty.into()); + let ty = wfcx.deeply_normalize(ty_span, WellFormedLoc::Ty(def_id), ty); + wfcx.register_wf_obligation(ty_span, WellFormedLoc::Ty(def_id), ty.into()); wfcx.register_bound( traits::ObligationCause::new( ty_span, @@ -993,12 +993,8 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), let span = tcx.def_span(def_id); if tcx.type_alias_is_checked(def_id) { res = res.and(enter_wf_checking_ctxt(tcx, def_id, |wfcx| { - let item_ty = wfcx.deeply_normalize(span, Some(WellFormedLoc::Ty(def_id)), ty); - wfcx.register_wf_obligation( - span, - Some(WellFormedLoc::Ty(def_id)), - item_ty.into(), - ); + let item_ty = wfcx.deeply_normalize(span, WellFormedLoc::Ty(def_id), ty); + wfcx.register_wf_obligation(span, WellFormedLoc::Ty(def_id), item_ty.into()); check_where_clauses(wfcx, def_id); Ok(()) })); diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 043e3fad050..86f386381b9 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -68,12 +68,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> { // Convenience function to normalize during wfcheck. This performs // `ObligationCtxt::normalize`, but provides a nice `ObligationCauseCode`. - fn normalize( - &self, - span: Span, - loc: Option, - value: Unnormalized<'tcx, T>, - ) -> T + fn normalize(&self, span: Span, loc: WellFormedLoc, value: Unnormalized<'tcx, T>) -> T where T: TypeFoldable>, { @@ -96,7 +91,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> { pub(super) fn deeply_normalize( &self, span: Span, - loc: Option, + loc: WellFormedLoc, value: Unnormalized<'tcx, T>, ) -> T where @@ -122,7 +117,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> { pub(super) fn register_wf_obligation( &self, span: Span, - loc: Option, + loc: WellFormedLoc, term: ty::Term<'tcx>, ) { let cause = traits::ObligationCause::new( @@ -802,7 +797,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &ty::GenericParamDef) -> Result<(), Er if tcx.features().const_param_ty_unchecked() { enter_wf_checking_ctxt(tcx, tcx.local_parent(def_id), |wfcx| { - wfcx.register_wf_obligation(span, None, ty.into()); + wfcx.register_wf_obligation(span, WellFormedLoc::None, ty.into()); Ok(()) }) } else if tcx.features().adt_const_params() || tcx.features().min_adt_const_params() { @@ -916,7 +911,7 @@ pub(crate) fn check_associated_item( tcx: TyCtxt<'_>, def_id: LocalDefId, ) -> Result<(), ErrorGuaranteed> { - let loc = Some(WellFormedLoc::Ty(def_id)); + let loc = WellFormedLoc::Ty(def_id); enter_wf_checking_ctxt(tcx, def_id, |wfcx| { let item = tcx.associated_item(def_id); @@ -936,7 +931,7 @@ pub(crate) fn check_associated_item( match item.kind { ty::AssocKind::Const { .. } => { let ty = tcx.type_of(def_id).instantiate_identity(); - let ty = wfcx.deeply_normalize(span, Some(WellFormedLoc::Ty(def_id)), ty); + let ty = wfcx.deeply_normalize(span, WellFormedLoc::Ty(def_id), ty); wfcx.register_wf_obligation(span, loc, ty.into()); if item.defaultness(tcx).has_value() { @@ -964,7 +959,7 @@ pub(crate) fn check_associated_item( } if item.defaultness(tcx).has_value() { let ty = tcx.type_of(def_id).instantiate_identity(); - let ty = wfcx.deeply_normalize(span, Some(WellFormedLoc::Ty(def_id)), ty); + let ty = wfcx.deeply_normalize(span, WellFormedLoc::Ty(def_id), ty); wfcx.register_wf_obligation(span, loc, ty.into()); } Ok(()) @@ -1013,10 +1008,10 @@ pub(crate) fn check_type_defn<'tcx>( let span = tcx.ty_span(field_id); let ty = wfcx.deeply_normalize( span, - None, + WellFormedLoc::None, tcx.type_of(field.did).instantiate_identity(), ); - wfcx.register_wf_obligation(span, Some(WellFormedLoc::Ty(field_id)), ty.into()); + wfcx.register_wf_obligation(span, WellFormedLoc::Ty(field_id), ty.into()); if matches!(ty.kind(), ty::Adt(def, _) if def.repr().scalable()) && !matches!(adt_def.repr().scalable, Some(ScalableElt::Container)) @@ -1051,7 +1046,11 @@ pub(crate) fn check_type_defn<'tcx>( { let last = idx == variant.fields.len() - 1; let span = tcx.ty_span(field.did.expect_local()); - let ty = wfcx.normalize(span, None, tcx.type_of(field.did).instantiate_identity()); + let ty = wfcx.normalize( + span, + WellFormedLoc::None, + tcx.type_of(field.did).instantiate_identity(), + ); wfcx.register_bound( traits::ObligationCause::new( span, @@ -1215,7 +1214,7 @@ pub(crate) fn check_static_item<'tcx>( } let span = tcx.ty_span(item_id); - let loc = Some(WellFormedLoc::Ty(item_id)); + let loc = WellFormedLoc::Ty(item_id); let item_ty = wfcx.deeply_normalize(span, loc, Unnormalized::new_wip(ty)); let is_foreign_item = tcx.is_foreign_item(item_id); @@ -1231,7 +1230,7 @@ pub(crate) fn check_static_item<'tcx>( }; let forbid_unsized = !(is_foreign_item && is_structurally_foreign_item()); - wfcx.register_wf_obligation(span, Some(WellFormedLoc::Ty(item_id)), item_ty.into()); + wfcx.register_wf_obligation(span, WellFormedLoc::Ty(item_id), item_ty.into()); if forbid_unsized { let span = tcx.def_span(item_id); wfcx.register_bound( @@ -1300,12 +1299,16 @@ pub(super) fn check_const_item<'tcx>( if let Some(direct_rhs) = tcx.const_of_item(def_id) { let raw_ct = direct_rhs.instantiate_identity(); - let norm_ct = wfcx.deeply_normalize(span, Some(WellFormedLoc::Ty(def_id)), raw_ct); - wfcx.register_wf_obligation(span, Some(WellFormedLoc::Ty(def_id)), norm_ct.into()); + let norm_ct = wfcx.deeply_normalize(span, WellFormedLoc::Ty(def_id), raw_ct); + wfcx.register_wf_obligation(span, WellFormedLoc::Ty(def_id), norm_ct.into()); wfcx.register_obligation(Obligation::new( tcx, - ObligationCause::new(span, def_id, ObligationCauseCode::WellFormed(None)), + ObligationCause::new( + span, + def_id, + ObligationCauseCode::WellFormed(WellFormedLoc::Ty(def_id)), + ), wfcx.param_env, ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(norm_ct, item_ty)), )); @@ -1330,7 +1333,7 @@ fn check_impl<'tcx>( let trait_span = of_trait.trait_ref.path.span; let trait_ref = wfcx.deeply_normalize( trait_span, - Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)), + WellFormedLoc::Ty(item.hir_id().expect_owner().def_id), trait_ref, ); let trait_pred = @@ -1367,7 +1370,7 @@ fn check_impl<'tcx>( { let bound = wfcx.normalize( item.span, - Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)), + WellFormedLoc::Ty(item.hir_id().expect_owner().def_id), bound, ); wfcx.register_obligation(Obligation::new( @@ -1375,7 +1378,9 @@ fn check_impl<'tcx>( ObligationCause::new( impl_.self_ty.span, wfcx.body_def_id, - ObligationCauseCode::WellFormed(None), + ObligationCauseCode::WellFormed(WellFormedLoc::HirId( + of_trait.trait_ref.hir_ref_id, + )), ), wfcx.param_env, bound.to_host_effect_clause(tcx, ty::BoundConstness::Maybe), @@ -1390,12 +1395,12 @@ fn check_impl<'tcx>( let self_ty = tcx.type_of(item.owner_id).instantiate_identity().skip_norm_wip(); let self_ty = wfcx.deeply_normalize( item.span, - Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)), + WellFormedLoc::Ty(item.hir_id().expect_owner().def_id), Unnormalized::new_wip(self_ty), ); wfcx.register_wf_obligation( impl_.self_ty.span, - Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)), + WellFormedLoc::Ty(item.hir_id().expect_owner().def_id), self_ty.into(), ); } @@ -1437,7 +1442,8 @@ pub(super) fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, def_id: wfcx.register_wf_obligation( tcx.def_span(param.def_id), matches!(param.kind, GenericParamDefKind::Type { .. }) - .then(|| WellFormedLoc::Ty(param.def_id.expect_local())), + .then(|| WellFormedLoc::Ty(param.def_id.expect_local())) + .unwrap_or(WellFormedLoc::None), default.as_term().unwrap(), ); } else { @@ -1466,7 +1472,7 @@ pub(super) fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, def_id: let cause = traits::ObligationCause::new( tcx.def_span(param.def_id), wfcx.body_def_id, - ObligationCauseCode::WellFormed(None), + ObligationCauseCode::WellFormed(WellFormedLoc::Ty(def_id)), ); wfcx.register_obligation(Obligation::new( tcx, @@ -1560,7 +1566,7 @@ pub(super) fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, def_id: // Note the subtle difference from how we handle `gen_clauses` // below: there, we are not trying to prove those clauses // to be *true* but merely *well-formed*. - let clause = wfcx.normalize(sp, None, clause); + let clause = wfcx.normalize(sp, WellFormedLoc::None, clause); let cause = traits::ObligationCause::new( sp, wfcx.body_def_id, @@ -1636,12 +1642,12 @@ fn check_fn_or_method<'tcx>( tcx.mk_type_list_from_iter(sig.inputs_and_output.iter().enumerate().map(|(idx, ty)| { wfcx.deeply_normalize( arg_span(idx), - Some(WellFormedLoc::Param { + WellFormedLoc::Param { function: def_id, // Note that the `param_idx` of the output type is // one greater than the index of the last input type. param_idx: idx, - }), + }, Unnormalized::new_wip(ty), ) })); @@ -1649,7 +1655,7 @@ fn check_fn_or_method<'tcx>( for (idx, ty) in sig.inputs_and_output.iter().enumerate() { wfcx.register_wf_obligation( arg_span(idx), - Some(WellFormedLoc::Param { function: def_id, param_idx: idx }), + WellFormedLoc::Param { function: def_id, param_idx: idx }, ty.into(), ); } @@ -1726,7 +1732,7 @@ fn check_method_receiver<'tcx>( } let span = fn_sig.decl.inputs[0].span; - let loc = Some(WellFormedLoc::Param { function: method.def_id.expect_local(), param_idx: 0 }); + let loc = WellFormedLoc::Param { function: method.def_id.expect_local(), param_idx: 0 }; let sig = tcx.fn_sig(method.def_id).instantiate_identity().skip_norm_wip(); let sig = tcx.liberate_late_bound_regions(method.def_id, sig); @@ -2319,7 +2325,8 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> { // Match the existing behavior. if clause.is_global() && !clause.has_type_flags(TypeFlags::HAS_BINDER_VARS) { - let clause = self.normalize(span, None, Unnormalized::new_wip(clause)); + let clause = + self.normalize(span, WellFormedLoc::None, Unnormalized::new_wip(clause)); // only use the span of the predicate clause (#90869) let hir_node = tcx.hir_node_by_def_id(self.body_def_id); diff --git a/compiler/rustc_hir_analysis/src/hir_wf_check.rs b/compiler/rustc_hir_analysis/src/hir_wf_check.rs index 9b05789a28f..9806b7b8739 100644 --- a/compiler/rustc_hir_analysis/src/hir_wf_check.rs +++ b/compiler/rustc_hir_analysis/src/hir_wf_check.rs @@ -19,7 +19,8 @@ pub(super) fn diagnostic_hir_wf_check<'tcx>( ) -> Option> { let def_id = match loc { WellFormedLoc::Ty(def_id) => def_id, - WellFormedLoc::Param { function, param_idx: _ } => function, + WellFormedLoc::Param { function, .. } => function, + WellFormedLoc::HirId(_) | WellFormedLoc::None => return None, }; let hir_id = tcx.local_def_id_to_hir_id(def_id); @@ -85,7 +86,7 @@ pub(super) fn diagnostic_hir_wf_check<'tcx>( let cause = traits::ObligationCause::new( ty.span, self.def_id, - traits::ObligationCauseCode::WellFormed(None), + traits::ObligationCauseCode::WellFormed(WellFormedLoc::HirId(ty.hir_id)), ); ocx.register_obligation(traits::Obligation::new( @@ -191,7 +192,7 @@ pub(super) fn diagnostic_hir_wf_check<'tcx>( } ref node => bug!("Unexpected node {:?}", node), }, - WellFormedLoc::Param { function: _, param_idx } => { + WellFormedLoc::Param { param_idx, .. } => { let fn_decl = tcx.hir_fn_decl_by_hir_id(hir_id).unwrap(); // Get return type if param_idx as usize == fn_decl.inputs.len() { @@ -204,6 +205,7 @@ pub(super) fn diagnostic_hir_wf_check<'tcx>( vec![&fn_decl.inputs[param_idx as usize]] } } + WellFormedLoc::HirId(_) | WellFormedLoc::None => return None, }; for ty in tys { visitor.visit_ty_unambig(ty); diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index 025c64896ed..5ddbb3c502c 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -11,7 +11,7 @@ use rustc_hir::def_id::DefId; use rustc_hir::{self as hir, HirId}; use rustc_hir_analysis::autoderef::Autoderef; use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes}; -use rustc_infer::traits::{Obligation, ObligationCause, ObligationCauseCode}; +use rustc_infer::traits::{Obligation, ObligationCause, ObligationCauseCode, WellFormedLoc}; use rustc_middle::ty::adjustment::{ Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, }; @@ -177,7 +177,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.register_wf_obligation( output.into(), call_expr.span, - ObligationCauseCode::WellFormed(None), + ObligationCauseCode::WellFormed(WellFormedLoc::HirId(call_expr.hir_id)), ); output @@ -728,7 +728,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // now we have to unify it with signature input in order to resolve // all inference variables. After that we update input signature for // adjustments search for mapped arguments. - let cause = self.cause(call_expr.span, ObligationCauseCode::Misc); + let cause = self.cause( + call_expr.span, + ObligationCauseCode::WellFormed(WellFormedLoc::HirId(call_expr.hir_id)), + ); if self .at(&cause, self.param_env) .sup(DefineOpaqueTypes::Yes, formal_input_tys[0], adjusted_arg_type) diff --git a/compiler/rustc_hir_typeck/src/check.rs b/compiler/rustc_hir_typeck/src/check.rs index b1b7aa57270..8061e522f43 100644 --- a/compiler/rustc_hir_typeck/src/check.rs +++ b/compiler/rustc_hir_typeck/src/check.rs @@ -72,10 +72,10 @@ pub(super) fn check_fn<'a, 'tcx>( fcx.register_wf_obligation( param_ty.into(), param.span, - ObligationCauseCode::WellFormed(Some(WellFormedLoc::Param { + ObligationCauseCode::WellFormed(WellFormedLoc::Param { function: fn_def_id, param_idx: idx, - })), + }), ); } diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 9f0d00d5274..ed39719b500 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -25,6 +25,7 @@ use rustc_hir::{ExprKind, HirId, QPath, is_range_literal}; use rustc_hir_analysis::diagnostics::{NoFieldOnType, NoVariantNamed}; use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer as _; use rustc_infer::infer::{self, DefineOpaqueTypes, InferOk, RegionVariableOrigin}; +use rustc_infer::traits::WellFormedLoc; use rustc_infer::traits::query::NoSolution; use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase}; use rustc_middle::ty::consts::ConstExt; @@ -699,14 +700,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.require_type_is_sized_deferred( output, call_expr_and_args.map_or(expr.span, |(e, _)| e.span), - ObligationCauseCode::SizedCallReturnType, + ObligationCauseCode::SizedCallReturnType(expr.hir_id), ); } // We always require that the type provided as the value for // a type parameter outlives the moment of instantiation. let args = self.typeck_results.borrow().node_args(expr.hir_id); - self.add_wf_bounds(args, expr.span); + self.add_wf_bounds(args, expr.span, expr.hir_id); ty } @@ -1772,7 +1773,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.deferred_repeat_expr_checks.borrow_mut().push((element, element_ty, count)); let ty = Ty::new_array_with_const_len(tcx, t, count); - self.register_wf_obligation(ty.into(), expr.span, ObligationCauseCode::WellFormed(None)); + self.register_wf_obligation( + ty.into(), + expr.span, + ObligationCauseCode::WellFormed(WellFormedLoc::HirId(expr.hir_id)), + ); ty } @@ -1944,7 +1949,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.register_wf_obligation( field_type.into(), field.expr.span, - ObligationCauseCode::WellFormed(None), + ObligationCauseCode::WellFormed(WellFormedLoc::HirId(field.expr.hir_id)), ); // Make sure to give a type to the field even if there's @@ -3895,7 +3900,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.require_type_is_sized( field_ty, expr.span, - ObligationCauseCode::Misc, + ObligationCauseCode::WellFormed(WellFormedLoc::None), ); } @@ -3924,7 +3929,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.require_type_is_sized( field_ty, expr.span, - ObligationCauseCode::Misc, + ObligationCauseCode::WellFormed(WellFormedLoc::HirId( + expr.hir_id, + )), ); } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 1ad6225a8c4..b0f940dd7a2 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -22,7 +22,7 @@ use rustc_hir_analysis::hir_ty_lowering::{ }; use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse}; use rustc_infer::infer::{DefineOpaqueTypes, InferResult}; -use rustc_infer::traits::TraitErrors; +use rustc_infer::traits::{TraitErrors, WellFormedLoc}; use rustc_lint_defs::builtin::{SELF_CONSTRUCTOR_FROM_OUTER_ITEM, UNREACHABLE_CODE}; use rustc_middle::ty::adjustment::{ Adjust, Adjustment, AutoBorrow, AutoBorrowMutability, DerefAdjustKind, @@ -543,7 +543,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { pub(crate) fn lower_ty(&self, hir_ty: &hir::Ty<'_>) -> LoweredTy<'tcx> { let ty = self.lowerer().lower_ty(hir_ty); - self.register_wf_obligation(ty.into(), hir_ty.span, ObligationCauseCode::WellFormed(None)); + self.register_wf_obligation( + ty.into(), + hir_ty.span, + ObligationCauseCode::WellFormed(WellFormedLoc::HirId(hir_ty.hir_id)), + ); LoweredTy::from_raw(self, hir_ty.span, ty) } @@ -608,7 +612,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.register_wf_obligation( ct.into(), self.tcx.hir_span(const_arg.hir_id), - ObligationCauseCode::WellFormed(None), + ObligationCauseCode::WellFormed(WellFormedLoc::HirId(const_arg.hir_id)), ); ct } @@ -664,9 +668,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } /// Registers obligations that all `args` are well-formed. - pub(crate) fn add_wf_bounds(&self, args: GenericArgsRef<'tcx>, span: Span) { + pub(crate) fn add_wf_bounds(&self, args: GenericArgsRef<'tcx>, span: Span, hir_id: HirId) { for term in args.iter().filter_map(ty::GenericArg::as_term) { - self.register_wf_obligation(term, span, ObligationCauseCode::WellFormed(None)); + self.register_wf_obligation( + term, + span, + ObligationCauseCode::WellFormed(WellFormedLoc::HirId(hir_id)), + ); } } @@ -810,7 +818,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.register_wf_obligation( ty.raw.into(), qself.span, - ObligationCauseCode::WellFormed(None), + ObligationCauseCode::WellFormed(WellFormedLoc::HirId(hir_id)), ); self.select_obligations_where_possible(|_| {}); diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 937182c2030..8ecd7838a0e 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -19,6 +19,7 @@ use rustc_index::IndexVec; use rustc_infer::infer::{ BoundRegionConversionTime, DefineOpaqueTypes, InferOk, TypeTrace, relate, }; +use rustc_infer::traits::WellFormedLoc; use rustc_middle::ty::adjustment::AllowTwoPhase; use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::print::with_forced_trimmed_paths; @@ -230,7 +231,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.register_wf_obligation( fn_input_ty.into(), arg_expr.span, - ObligationCauseCode::WellFormed(None), + ObligationCauseCode::WellFormed(WellFormedLoc::HirId(arg_expr.hir_id)), ); self.check_place_expr_if_unsized(fn_input_ty, arg_expr); @@ -1197,7 +1198,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.register_wf_obligation( decl_ty.into(), decl.pat.span, - ObligationCauseCode::WellFormed(None), + ObligationCauseCode::WellFormed(WellFormedLoc::HirId(decl.pat.hir_id)), ); } self.overwrite_local_ty_if_err(decl.hir_id, decl.pat, pat_ty); diff --git a/compiler/rustc_hir_typeck/src/lib.rs b/compiler/rustc_hir_typeck/src/lib.rs index 5efb2775c77..2945a6b3b51 100644 --- a/compiler/rustc_hir_typeck/src/lib.rs +++ b/compiler/rustc_hir_typeck/src/lib.rs @@ -193,7 +193,7 @@ fn typeck_with_inspect<'tcx>( let expected_type = fcx.normalize(body.value.span, Unnormalized::new_wip(expected_type)); - let wf_code = ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(def_id))); + let wf_code = ObligationCauseCode::WellFormed(WellFormedLoc::Ty(def_id)); fcx.register_wf_obligation(expected_type.into(), body.value.span, wf_code); if let hir::Node::AnonConst(_) = node { diff --git a/compiler/rustc_hir_typeck/src/method/confirm.rs b/compiler/rustc_hir_typeck/src/method/confirm.rs index 4518e419aea..721e9a38fc5 100644 --- a/compiler/rustc_hir_typeck/src/method/confirm.rs +++ b/compiler/rustc_hir_typeck/src/method/confirm.rs @@ -13,6 +13,7 @@ use rustc_hir_analysis::hir_ty_lowering::{ use rustc_infer::infer::{ BoundRegionConversionTime, DefineOpaqueTypes, InferOk, RegionVariableOrigin, }; +use rustc_infer::traits::WellFormedLoc; use rustc_lint_defs::builtin::{ AMBIGUOUS_GLOB_IMPORTED_TRAITS, RESOLVING_TO_ITEMS_SHADOWING_SUPERTRAIT_ITEMS, }; @@ -648,7 +649,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> { // this is a projection from a trait reference, so we have to // make sure that the trait reference inputs are well-formed. - self.add_wf_bounds(all_args, self.call_expr.span); + self.add_wf_bounds(all_args, self.call_expr.span, self.call_expr.hir_id); // the function type must also be well-formed (this is not // implied by the args being well-formed because of inherent @@ -657,7 +658,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> { self.register_wf_obligation( ty.into(), self.span, - ObligationCauseCode::WellFormed(None), + ObligationCauseCode::WellFormed(WellFormedLoc::HirId(self.call_expr.hir_id)), ); } } diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs index 493f6989f34..7c4c2d5aed1 100644 --- a/compiler/rustc_middle/src/traits/mod.rs +++ b/compiler/rustc_middle/src/traits/mod.rs @@ -238,7 +238,7 @@ pub enum ObligationCauseCode<'tcx> { /// Return type must be `Sized`. SizedReturnType, /// Return type of a call expression must be `Sized`. - SizedCallReturnType, + SizedCallReturnType(HirId), /// Yield type must be `Sized`. SizedYieldType, /// Inline asm operand type must be `Sized`. @@ -384,7 +384,7 @@ pub enum ObligationCauseCode<'tcx> { /// This is purely for diagnostic purposes - it is always /// correct to use `Misc` instead, or to specify /// `WellFormed(None)`. - WellFormed(Option), + WellFormed(WellFormedLoc), /// From `match_impl`. The cause for us having to match an impl, and the DefId we are matching /// against. @@ -461,6 +461,8 @@ pub enum WellFormedLoc { /// being the last 'parameter' param_idx: usize, }, + HirId(HirId), + None, } impl<'tcx> ObligationCauseCode<'tcx> { diff --git a/compiler/rustc_passes/src/layout_test.rs b/compiler/rustc_passes/src/layout_test.rs index 961d39ec8e5..31490a49b69 100644 --- a/compiler/rustc_passes/src/layout_test.rs +++ b/compiler/rustc_passes/src/layout_test.rs @@ -41,7 +41,7 @@ pub fn ensure_wf<'tcx>( traits::ObligationCause::new( span, def_id, - traits::ObligationCauseCode::WellFormed(Some(traits::WellFormedLoc::Ty(def_id))), + traits::ObligationCauseCode::WellFormed(traits::WellFormedLoc::Ty(def_id)), ), param_env, pred, diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs index 3b21c5e5f19..30ca4082262 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs @@ -74,7 +74,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { SelectionError::Unimplemented => { // If this obligation was generated as a result of well-formedness checking, see if we // can get a better error message by performing HIR-based well-formedness checking. - if let ObligationCauseCode::WellFormed(Some(wf_loc)) = + if let ObligationCauseCode::WellFormed(wf_loc) = root_obligation.cause.code().peel_derives() && !obligation.predicate.has_non_region_infer() { diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs index 76755eb481d..a2aae724228 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -4325,7 +4325,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { err.help("unsized fn params are gated as an unstable feature"); } } - ObligationCauseCode::SizedReturnType | ObligationCauseCode::SizedCallReturnType => { + ObligationCauseCode::SizedReturnType | ObligationCauseCode::SizedCallReturnType(_) => { err.note("the return type of a function must have a statically known size"); } ObligationCauseCode::SizedYieldType => { diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index ba463184a41..41e6797e71d 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -7,7 +7,9 @@ use std::iter; use rustc_attr_ir::lang_items::LangItem; use rustc_hir as hir; -use rustc_infer::traits::{ObligationCauseCode, PredicateObligation, PredicateObligations}; +use rustc_infer::traits::{ + ObligationCauseCode, PredicateObligation, PredicateObligations, WellFormedLoc, +}; use rustc_middle::ty::{ self, DelayedSet, GenericArgsRef, PredicateProxy, Term, TermKind, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, @@ -352,7 +354,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { return self.out; } - let cause = self.cause(ObligationCauseCode::WellFormed(None)); + let cause = self.cause(ObligationCauseCode::WellFormed(WellFormedLoc::None)); let param_env = self.param_env; let mut obligations = PredicateObligations::with_capacity(self.out.len()); for mut obligation in self.out { @@ -519,7 +521,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { &mut traits::SelectionContext::new(self.infcx), self.param_env, data, - self.cause(ObligationCauseCode::WellFormed(None)), + self.cause(ObligationCauseCode::WellFormed(WellFormedLoc::None)), self.recursion_depth, &mut self.out, ); @@ -532,7 +534,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { fn add_wf_preds_for_projection_args(&mut self, args: GenericArgsRef<'tcx>) { let tcx = self.tcx(); - let cause = self.cause(ObligationCauseCode::WellFormed(None)); + let cause = self.cause(ObligationCauseCode::WellFormed(WellFormedLoc::None)); let param_env = self.param_env; let depth = self.recursion_depth; @@ -976,7 +978,7 @@ impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { let principal_def_id = principal.skip_binder().def_id; self.out.push(traits::Obligation::with_depth( tcx, - self.cause(ObligationCauseCode::WellFormed(None)), + self.cause(ObligationCauseCode::WellFormed(WellFormedLoc::None)), self.recursion_depth, self.param_env, ty::Binder::dummy(ty::PredicateKind::DynCompatible(principal_def_id)), @@ -1044,7 +1046,7 @@ impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { if let Some(pred_binder) = pred_binder { self.out.push(traits::Obligation::with_depth( tcx, - self.cause(ObligationCauseCode::WellFormed(None)), + self.cause(ObligationCauseCode::WellFormed(WellFormedLoc::None)), self.recursion_depth, self.param_env, pred_binder, @@ -1067,7 +1069,7 @@ impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { // See also the comment on `fn obligations`, describing cycle // prevention, which happens before this can be reached. ty::Infer(_) => { - let cause = self.cause(ObligationCauseCode::WellFormed(None)); + let cause = self.cause(ObligationCauseCode::WellFormed(WellFormedLoc::None)); self.out.push(traits::Obligation::with_depth( tcx, cause, @@ -1094,7 +1096,8 @@ impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { let predicate = ty::Binder::dummy(ty::PredicateKind::Clause( ty::ClauseKind::ConstEvaluatable(c), )); - let cause = self.cause(ObligationCauseCode::WellFormed(None)); + let cause = + self.cause(ObligationCauseCode::WellFormed(WellFormedLoc::None)); self.out.push(traits::Obligation::with_depth( tcx, cause, @@ -1128,7 +1131,7 @@ impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { } } ty::ConstKind::Infer(_) => { - let cause = self.cause(ObligationCauseCode::WellFormed(None)); + let cause = self.cause(ObligationCauseCode::WellFormed(WellFormedLoc::None)); self.out.push(traits::Obligation::with_depth( tcx, @@ -1151,7 +1154,7 @@ impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { let predicate = ty::Binder::dummy(ty::PredicateKind::Clause( ty::ClauseKind::ConstEvaluatable(c), )); - let cause = self.cause(ObligationCauseCode::WellFormed(None)); + let cause = self.cause(ObligationCauseCode::WellFormed(WellFormedLoc::None)); self.out.push(traits::Obligation::with_depth( tcx, cause, @@ -1174,7 +1177,8 @@ impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { ty::Adt(adt_def, args) => { let adt_val = val.destructure_adt_const(); let variant_def = adt_def.variant(adt_val.variant); - let cause = self.cause(ObligationCauseCode::WellFormed(None)); + let cause = + self.cause(ObligationCauseCode::WellFormed(WellFormedLoc::None)); self.out.extend(variant_def.fields.iter().zip(adt_val.fields).map( |(field_def, &field_val)| { let field_ty = tcx @@ -1196,7 +1200,8 @@ impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { } ty::Tuple(field_tys) => { let field_vals = val.to_branch(); - let cause = self.cause(ObligationCauseCode::WellFormed(None)); + let cause = + self.cause(ObligationCauseCode::WellFormed(WellFormedLoc::None)); self.out.extend(field_tys.iter().zip(field_vals).map( |(field_ty, &field_val)| { let predicate = ty::PredicateKind::Clause( @@ -1214,7 +1219,8 @@ impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { } ty::Array(elem_ty, _len) => { let elem_vals = val.to_branch(); - let cause = self.cause(ObligationCauseCode::WellFormed(None)); + let cause = + self.cause(ObligationCauseCode::WellFormed(WellFormedLoc::None)); self.out.extend(elem_vals.iter().map(|&elem_val| { let predicate = ty::PredicateKind::Clause(