From 1b8c7784e58de489331dd8957a889916a0dcbee3 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Wed, 26 Apr 2023 11:48:17 +0000 Subject: [PATCH] Add new `ToPredicate` impls and `TraitRef` methods to remove some `ty::Binber::dummy` calls --- .../src/transform/check_consts/qualifs.rs | 9 ++------- compiler/rustc_hir_analysis/src/bounds.rs | 2 +- compiler/rustc_middle/src/ty/mod.rs | 18 ++++++++++++++++++ compiler/rustc_middle/src/ty/sty.rs | 12 ++++++++++++ .../rustc_trait_selection/src/traits/mod.rs | 2 +- .../src/traits/object_safety.rs | 11 ++++------- .../src/traits/project.rs | 12 +++++------- .../src/traits/select/confirmation.rs | 7 +------ compiler/rustc_ty_utils/src/ty.rs | 5 ++--- 9 files changed, 46 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs b/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs index 60d4e6ece48..1da20579021 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs @@ -157,13 +157,8 @@ impl Qualif for NeedsNonConstDrop { cx.tcx, ObligationCause::dummy_with_span(cx.body.span), cx.param_env, - ty::Binder::dummy(ty::TraitRef::from_lang_item( - cx.tcx, - LangItem::Destruct, - cx.body.span, - [ty], - )) - .with_constness(ty::BoundConstness::ConstIfConst), + ty::TraitRef::from_lang_item(cx.tcx, LangItem::Destruct, cx.body.span, [ty]) + .with_constness(ty::BoundConstness::ConstIfConst), ); let infcx = cx.tcx.infer_ctxt().build(); diff --git a/compiler/rustc_hir_analysis/src/bounds.rs b/compiler/rustc_hir_analysis/src/bounds.rs index a751639b56f..f25eaa78504 100644 --- a/compiler/rustc_hir_analysis/src/bounds.rs +++ b/compiler/rustc_hir_analysis/src/bounds.rs @@ -57,7 +57,7 @@ impl<'tcx> Bounds<'tcx> { pub fn push_sized(&mut self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) { let sized_def_id = tcx.require_lang_item(LangItem::Sized, Some(span)); - let trait_ref = ty::Binder::dummy(ty::TraitRef::new(tcx, sized_def_id, [ty])); + let trait_ref = ty::TraitRef::new(tcx, sized_def_id, [ty]); // Preferable to put this obligation first, since we report better errors for sized ambiguity. self.predicates.insert(0, (trait_ref.without_const().to_predicate(tcx), span)); } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 17647d13f99..62a22708210 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1207,6 +1207,18 @@ impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for Binder<'tcx, TraitRef } } +impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for TraitRef<'tcx> { + fn to_predicate(self, tcx: TyCtxt<'tcx>) -> PolyTraitPredicate<'tcx> { + ty::Binder::dummy(self).to_predicate(tcx) + } +} + +impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for TraitPredicate<'tcx> { + fn to_predicate(self, _tcx: TyCtxt<'tcx>) -> PolyTraitPredicate<'tcx> { + ty::Binder::dummy(self) + } +} + impl<'tcx> ToPredicate<'tcx> for PolyTraitPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { self.map_bound(|p| PredicateKind::Clause(Clause::Trait(p))).to_predicate(tcx) @@ -1231,6 +1243,12 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> { } } +impl<'tcx> ToPredicate<'tcx> for TraitPredicate<'tcx> { + fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { + PredicateKind::Clause(Clause::Trait(self)).to_predicate(tcx) + } +} + impl<'tcx> Predicate<'tcx> { pub fn to_opt_poly_trait_pred(self) -> Option> { let predicate = self.kind(); diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 32809783c29..1849a66fa8f 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -871,6 +871,18 @@ impl<'tcx> TraitRef<'tcx> { ) } + /// Converts this trait ref to a trait predicate with a given `constness` and a positive polarity. + #[inline] + pub fn with_constness(self, constness: ty::BoundConstness) -> ty::TraitPredicate<'tcx> { + ty::TraitPredicate { trait_ref: self, constness, polarity: ty::ImplPolarity::Positive } + } + + /// Converts this trait ref to a trait predicate without `const` and a positive polarity. + #[inline] + pub fn without_const(self) -> ty::TraitPredicate<'tcx> { + self.with_constness(ty::BoundConstness::NotConst) + } + #[inline] pub fn self_ty(&self) -> Ty<'tcx> { self.substs.type_at(0) diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index edea13854ff..af1c253c3ad 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -127,7 +127,7 @@ pub fn type_known_to_meet_bound_modulo_regions<'tcx>( ty: Ty<'tcx>, def_id: DefId, ) -> bool { - let trait_ref = ty::Binder::dummy(ty::TraitRef::new(infcx.tcx, def_id, [ty])); + let trait_ref = ty::TraitRef::new(infcx.tcx, def_id, [ty]); pred_known_to_hold_modulo_regions(infcx, param_env, trait_ref.without_const()) } diff --git a/compiler/rustc_trait_selection/src/traits/object_safety.rs b/compiler/rustc_trait_selection/src/traits/object_safety.rs index aae88199167..3f526e70522 100644 --- a/compiler/rustc_trait_selection/src/traits/object_safety.rs +++ b/compiler/rustc_trait_selection/src/traits/object_safety.rs @@ -769,13 +769,10 @@ fn receiver_is_dispatchable<'tcx>( let param_env = tcx.param_env(method.def_id); // Self: Unsize - let unsize_predicate = ty::Binder::dummy(ty::TraitRef::new( - tcx, - unsize_did, - [tcx.types.self_param, unsized_self_ty], - )) - .without_const() - .to_predicate(tcx); + let unsize_predicate = + ty::TraitRef::new(tcx, unsize_did, [tcx.types.self_param, unsized_self_ty]) + .without_const() + .to_predicate(tcx); // U: Trait let trait_predicate = { diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 68e3600a5e4..f46bf36ea21 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -1319,7 +1319,7 @@ fn assemble_candidate_for_impl_trait_in_trait<'cx, 'tcx>( let trait_substs = obligation.predicate.substs.truncate_to(tcx, tcx.generics_of(trait_def_id)); // FIXME(named-returns): Binders - let trait_predicate = ty::Binder::dummy(ty::TraitRef::new(tcx, trait_def_id, trait_substs)); + let trait_predicate = ty::TraitRef::new(tcx, trait_def_id, trait_substs); let _ = selcx.infcx.commit_if_ok(|_| { match selcx.select(&obligation.with(tcx, trait_predicate)) { @@ -1682,10 +1682,8 @@ fn assemble_candidates_from_impls<'cx, 'tcx>( if selcx.infcx.predicate_must_hold_modulo_regions( &obligation.with( selcx.tcx(), - ty::Binder::dummy( - ty::TraitRef::from_lang_item(selcx.tcx(), LangItem::Sized, obligation.cause.span(),[self_ty]), - ) - .without_const(), + ty::TraitRef::from_lang_item(selcx.tcx(), LangItem::Sized, obligation.cause.span(),[self_ty]) + .without_const(), ), ) => { @@ -1948,12 +1946,12 @@ fn confirm_builtin_candidate<'cx, 'tcx>( ) }); if check_is_sized { - let sized_predicate = ty::Binder::dummy(ty::TraitRef::from_lang_item( + let sized_predicate = ty::TraitRef::from_lang_item( tcx, LangItem::Sized, obligation.cause.span(), [self_ty], - )) + ) .without_const(); obligations.push(obligation.with(tcx, sized_predicate)); } diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 089f680654e..422285d9474 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -1049,12 +1049,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { ); // We can only make objects from sized types. - let tr = ty::Binder::dummy(ty::TraitRef::from_lang_item( - tcx, - LangItem::Sized, - cause.span, - [source], - )); + let tr = ty::TraitRef::from_lang_item(tcx, LangItem::Sized, cause.span, [source]); nested.push(predicate_to_obligation(tr.without_const().to_predicate(tcx))); // If the type is `Foo + 'a`, ensure that the type diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index 6d6eb40f252..78efcce572d 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -62,9 +62,8 @@ fn sized_constraint_for_ty<'tcx>( // it on the impl. let Some(sized_trait) = tcx.lang_items().sized_trait() else { return vec![ty] }; - let sized_predicate = ty::Binder::dummy(ty::TraitRef::new(tcx, sized_trait, [ty])) - .without_const() - .to_predicate(tcx); + let sized_predicate = + ty::TraitRef::new(tcx, sized_trait, [ty]).without_const().to_predicate(tcx); let predicates = tcx.predicates_of(adtdef.did()).predicates; if predicates.iter().any(|(p, _)| *p == sized_predicate) { vec![] } else { vec![ty] } }