diff options
| author | bors <bors@rust-lang.org> | 2024-05-10 22:24:53 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-05-10 22:24:53 +0000 |
| commit | 19dacee0d8820d42b34e21eae99ff41026413add (patch) | |
| tree | 94771814538001ac754403068de439ddc330f0f1 /compiler/rustc_trait_selection/src | |
| parent | 6e1d94708a0a4a35ca7e46c6cac98adf62fe800e (diff) | |
| parent | b55d8a3b4917698fd6d389f05afc423086f33d97 (diff) | |
| download | rust-19dacee0d8820d42b34e21eae99ff41026413add.tar.gz rust-19dacee0d8820d42b34e21eae99ff41026413add.zip | |
Auto merge of #124982 - compiler-errors:uplift-trait-ref, r=lcnr
Uplift `TraitRef` into `rustc_type_ir` Emotional rollercoaster r? lcnr
Diffstat (limited to 'compiler/rustc_trait_selection/src')
12 files changed, 75 insertions, 47 deletions
diff --git a/compiler/rustc_trait_selection/src/errors.rs b/compiler/rustc_trait_selection/src/errors.rs index 7228a9ba016..b442446f79b 100644 --- a/compiler/rustc_trait_selection/src/errors.rs +++ b/compiler/rustc_trait_selection/src/errors.rs @@ -4,7 +4,7 @@ use rustc_errors::{ SubdiagMessageOp, Subdiagnostic, }; use rustc_macros::{Diagnostic, Subdiagnostic}; -use rustc_middle::ty::{self, ClosureKind, PolyTraitRef, Ty}; +use rustc_middle::ty::{self, print::PrintTraitRefExt as _, ClosureKind, PolyTraitRef, Ty}; use rustc_span::{Span, Symbol}; #[derive(Diagnostic)] diff --git a/compiler/rustc_trait_selection/src/solve/normalizes_to/mod.rs b/compiler/rustc_trait_selection/src/solve/normalizes_to/mod.rs index f886c588650..454c7a5f00f 100644 --- a/compiler/rustc_trait_selection/src/solve/normalizes_to/mod.rs +++ b/compiler/rustc_trait_selection/src/solve/normalizes_to/mod.rs @@ -368,7 +368,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> { } }; let output_is_sized_pred = tupled_inputs_and_output.map_bound(|(_, output)| { - ty::TraitRef::from_lang_item(tcx, LangItem::Sized, DUMMY_SP, [output]) + ty::TraitRef::new(tcx, tcx.require_lang_item(LangItem::Sized, None), [output]) }); let pred = tupled_inputs_and_output @@ -414,7 +414,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> { )?; let output_is_sized_pred = tupled_inputs_and_output_and_coroutine.map_bound( |AsyncCallableRelevantTypes { output_coroutine_ty: output_ty, .. }| { - ty::TraitRef::from_lang_item(tcx, LangItem::Sized, DUMMY_SP, [output_ty]) + ty::TraitRef::new(tcx, tcx.require_lang_item(LangItem::Sized, None), [output_ty]) }, ); @@ -576,10 +576,9 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> { // and opaque types: If the `self_ty` is `Sized`, then the metadata is `()`. // FIXME(ptr_metadata): This impl overlaps with the other impls and shouldn't // exist. Instead, `Pointee<Metadata = ()>` should be a supertrait of `Sized`. - let sized_predicate = ty::TraitRef::from_lang_item( + let sized_predicate = ty::TraitRef::new( tcx, - LangItem::Sized, - DUMMY_SP, + tcx.require_lang_item(LangItem::Sized, None), [ty::GenericArg::from(goal.predicate.self_ty())], ); // FIXME(-Znext-solver=coinductive): Should this be `GoalSource::ImplWhereBound`? diff --git a/compiler/rustc_trait_selection/src/solve/trait_goals.rs b/compiler/rustc_trait_selection/src/solve/trait_goals.rs index 0fde9dd4cd6..083cc0aa3c2 100644 --- a/compiler/rustc_trait_selection/src/solve/trait_goals.rs +++ b/compiler/rustc_trait_selection/src/solve/trait_goals.rs @@ -16,7 +16,7 @@ use rustc_middle::traits::{BuiltinImplSource, Reveal}; use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams}; use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt}; use rustc_middle::ty::{TraitPredicate, TypeVisitableExt}; -use rustc_span::{ErrorGuaranteed, DUMMY_SP}; +use rustc_span::ErrorGuaranteed; impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> { fn self_ty(self) -> Ty<'tcx> { @@ -307,7 +307,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> { } }; let output_is_sized_pred = tupled_inputs_and_output.map_bound(|(_, output)| { - ty::TraitRef::from_lang_item(tcx, LangItem::Sized, DUMMY_SP, [output]) + ty::TraitRef::new(tcx, tcx.require_lang_item(LangItem::Sized, None), [output]) }); let pred = tupled_inputs_and_output @@ -346,7 +346,11 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> { )?; let output_is_sized_pred = tupled_inputs_and_output_and_coroutine.map_bound( |AsyncCallableRelevantTypes { output_coroutine_ty, .. }| { - ty::TraitRef::from_lang_item(tcx, LangItem::Sized, DUMMY_SP, [output_coroutine_ty]) + ty::TraitRef::new( + tcx, + tcx.require_lang_item(LangItem::Sized, None), + [output_coroutine_ty], + ) }, ); 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 c46cee36cf7..040ce450aaf 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 @@ -10,6 +10,7 @@ use rustc_errors::{codes::*, struct_span_code_err, ErrorGuaranteed}; use rustc_hir as hir; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_macros::{extension, LintDiagnostic}; +use rustc_middle::ty::print::PrintTraitRefExt as _; use rustc_middle::ty::GenericArgsRef; use rustc_middle::ty::{self, GenericParamDefKind, TyCtxt}; use rustc_parse_format::{ParseMode, Parser, Piece, Position}; 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 3defc03e7f0..597effcbbf0 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -537,10 +537,12 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { self.tcx, obligation.cause.clone(), obligation.param_env, - ty::TraitRef::from_lang_item( + ty::TraitRef::new( self.tcx, - hir::LangItem::Sized, - obligation.cause.span, + self.tcx.require_lang_item( + hir::LangItem::Sized, + Some(obligation.cause.span), + ), [base_ty], ), ); @@ -4033,10 +4035,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { let node = tcx.hir_node_by_def_id(hir.get_parent_item(expr.hir_id).def_id); let pred = ty::Binder::dummy(ty::TraitPredicate { - trait_ref: ty::TraitRef::from_lang_item( + trait_ref: ty::TraitRef::new( tcx, - LangItem::Clone, - span, + tcx.require_lang_item(LangItem::Clone, Some(span)), [*ty], ), polarity: ty::PredicatePolarity::Positive, 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 de8ec17b113..cdde12af8ea 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 @@ -37,7 +37,9 @@ use rustc_middle::traits::SignatureMismatchData; use rustc_middle::ty::abstract_const::NotConstEvaluatable; use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::fold::{BottomUpFolder, TypeFolder, TypeSuperFoldable}; -use rustc_middle::ty::print::{with_forced_trimmed_paths, FmtPrinter, Print}; +use rustc_middle::ty::print::{ + with_forced_trimmed_paths, FmtPrinter, Print, PrintTraitRefExt as _, +}; use rustc_middle::ty::{ self, SubtypePredicate, ToPolyTraitRef, ToPredicate, TraitRef, Ty, TyCtxt, TypeFoldable, TypeVisitable, TypeVisitableExt, diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 0aafc1a2efe..1e78484f305 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -974,9 +974,12 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( // // NOTE: This should be kept in sync with the similar code in // `rustc_ty_utils::instance::resolve_associated_item()`. - let node_item = - specialization_graph::assoc_def(selcx.tcx(), impl_data.impl_def_id, obligation.predicate.def_id) - .map_err(|ErrorGuaranteed { .. }| ())?; + let node_item = specialization_graph::assoc_def( + selcx.tcx(), + impl_data.impl_def_id, + obligation.predicate.def_id, + ) + .map_err(|ErrorGuaranteed { .. }| ())?; if node_item.is_final() { // Non-specializable items are always projectable. @@ -1019,7 +1022,8 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( lang_items.async_fn_trait(), lang_items.async_fn_mut_trait(), lang_items.async_fn_once_trait(), - ].contains(&Some(trait_ref.def_id)) + ] + .contains(&Some(trait_ref.def_id)) { true } else if lang_items.async_fn_kind_helper() == Some(trait_ref.def_id) { @@ -1032,7 +1036,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( true } else { obligation.predicate.args.type_at(0).to_opt_closure_kind().is_some() - && obligation.predicate.args.type_at(1).to_opt_closure_kind().is_some() + && obligation.predicate.args.type_at(1).to_opt_closure_kind().is_some() } } else if lang_items.discriminant_kind_trait() == Some(trait_ref.def_id) { match self_ty.kind() { @@ -1159,12 +1163,20 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( // Otherwise, type parameters, opaques, and unnormalized projections have // unit metadata if they're known (e.g. by the param_env) to be sized. ty::Param(_) | ty::Alias(..) - if self_ty != tail || selcx.infcx.predicate_must_hold_modulo_regions( - &obligation.with( - selcx.tcx(), - ty::TraitRef::from_lang_item(selcx.tcx(), LangItem::Sized, obligation.cause.span(),[self_ty]), - ), - ) => + if self_ty != tail + || selcx.infcx.predicate_must_hold_modulo_regions( + &obligation.with( + selcx.tcx(), + ty::TraitRef::new( + selcx.tcx(), + selcx.tcx().require_lang_item( + LangItem::Sized, + Some(obligation.cause.span()), + ), + [self_ty], + ), + ), + ) => { true } @@ -1227,7 +1239,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( obligation.cause.span, format!("Cannot project an associated type from `{impl_source:?}`"), ); - return Err(()) + return Err(()); } }; @@ -1556,10 +1568,9 @@ fn confirm_builtin_candidate<'cx, 'tcx>( // and opaque types: If the `self_ty` is `Sized`, then the metadata is `()`. // FIXME(ptr_metadata): This impl overlaps with the other impls and shouldn't // exist. Instead, `Pointee<Metadata = ()>` should be a supertrait of `Sized`. - let sized_predicate = ty::TraitRef::from_lang_item( + let sized_predicate = ty::TraitRef::new( tcx, - LangItem::Sized, - obligation.cause.span(), + tcx.require_lang_item(LangItem::Sized, Some(obligation.cause.span())), [self_ty], ); obligations.push(obligation.with(tcx, sized_predicate)); diff --git a/compiler/rustc_trait_selection/src/traits/query/type_op/normalize.rs b/compiler/rustc_trait_selection/src/traits/query/type_op/normalize.rs index 279d96dec72..e9948bf1f71 100644 --- a/compiler/rustc_trait_selection/src/traits/query/type_op/normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/query/type_op/normalize.rs @@ -34,7 +34,9 @@ where } } -pub trait Normalizable<'tcx>: fmt::Debug + TypeFoldable<TyCtxt<'tcx>> + Lift<'tcx> + Copy { +pub trait Normalizable<'tcx>: + fmt::Debug + TypeFoldable<TyCtxt<'tcx>> + Lift<TyCtxt<'tcx>> + Copy +{ fn type_op_method( tcx: TyCtxt<'tcx>, canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, Normalize<Self>>>, diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 0b91dd72e3b..69d11b45e60 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -735,7 +735,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { output_ty, &mut nested, ); - let tr = ty::TraitRef::from_lang_item(self.tcx(), LangItem::Sized, cause.span, [output_ty]); + let tr = ty::TraitRef::new( + self.tcx(), + self.tcx().require_lang_item(LangItem::Sized, Some(cause.span)), + [output_ty], + ); nested.push(Obligation::new(self.infcx.tcx, cause, obligation.param_env, tr)); Ok(nested) @@ -1009,10 +1013,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } else { nested.push(obligation.with( self.tcx(), - ty::TraitRef::from_lang_item( + ty::TraitRef::new( self.tcx(), - LangItem::AsyncFnKindHelper, - obligation.cause.span, + self.tcx().require_lang_item( + LangItem::AsyncFnKindHelper, + Some(obligation.cause.span), + ), [kind_ty, Ty::from_closure_kind(self.tcx(), goal_kind)], ), )); @@ -1241,10 +1247,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { .collect(); // We can only make objects from sized types. - let tr = ty::TraitRef::from_lang_item( + let tr = ty::TraitRef::new( tcx, - LangItem::Sized, - obligation.cause.span, + tcx.require_lang_item(LangItem::Sized, Some(obligation.cause.span)), [source], ); nested.push(predicate_to_obligation(tr.to_predicate(tcx))); @@ -1474,10 +1479,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { cause.clone(), obligation.recursion_depth + 1, self_ty.rebind(ty::TraitPredicate { - trait_ref: ty::TraitRef::from_lang_item( + trait_ref: ty::TraitRef::new( self.tcx(), - LangItem::Destruct, - cause.span, + self.tcx().require_lang_item(LangItem::Destruct, Some(cause.span)), [nested_ty.into(), host_effect_param], ), polarity: ty::PredicatePolarity::Positive, @@ -1507,10 +1511,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { | ty::Infer(_) | ty::Placeholder(_) => { let predicate = self_ty.rebind(ty::TraitPredicate { - trait_ref: ty::TraitRef::from_lang_item( + trait_ref: ty::TraitRef::new( self.tcx(), - LangItem::Destruct, - cause.span, + self.tcx().require_lang_item(LangItem::Destruct, Some(cause.span)), [nested_ty.into(), host_effect_param], ), polarity: ty::PredicatePolarity::Positive, diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 50353a1fd2f..7aa2aabed7f 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -41,6 +41,7 @@ use rustc_middle::dep_graph::DepNodeIndex; use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::ty::_match::MatchAgainstFreshVars; use rustc_middle::ty::abstract_const::NotConstEvaluatable; +use rustc_middle::ty::print::PrintTraitRefExt as _; use rustc_middle::ty::relate::TypeRelation; use rustc_middle::ty::GenericArgsRef; use rustc_middle::ty::{self, PolyProjectionPredicate, ToPredicate}; diff --git a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs index 390e711a18d..fe3f66f3a3f 100644 --- a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs @@ -11,6 +11,7 @@ pub mod specialization_graph; use rustc_infer::infer::DefineOpaqueTypes; +use rustc_middle::ty::print::PrintTraitRefExt as _; use specialization_graph::GraphExt; use crate::errors::NegativePositiveConflict; diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index ebcda61f2ad..2c2b84d4a87 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -526,8 +526,11 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { fn require_sized(&mut self, subty: Ty<'tcx>, cause: traits::ObligationCauseCode<'tcx>) { if !subty.has_escaping_bound_vars() { let cause = self.cause(cause); - let trait_ref = - ty::TraitRef::from_lang_item(self.tcx(), LangItem::Sized, cause.span, [subty]); + let trait_ref = ty::TraitRef::new( + self.tcx(), + self.tcx().require_lang_item(LangItem::Sized, Some(cause.span)), + [subty], + ); self.out.push(traits::Obligation::with_depth( self.tcx(), cause, |
