Skip to content

Commit 1b8c778

Browse files
committed
Add new ToPredicate impls and TraitRef methods to remove some ty::Binber::dummy calls
1 parent 4f2532f commit 1b8c778

File tree

9 files changed

+46
-32
lines changed

9 files changed

+46
-32
lines changed

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,8 @@ impl Qualif for NeedsNonConstDrop {
157157
cx.tcx,
158158
ObligationCause::dummy_with_span(cx.body.span),
159159
cx.param_env,
160-
ty::Binder::dummy(ty::TraitRef::from_lang_item(
161-
cx.tcx,
162-
LangItem::Destruct,
163-
cx.body.span,
164-
[ty],
165-
))
166-
.with_constness(ty::BoundConstness::ConstIfConst),
160+
ty::TraitRef::from_lang_item(cx.tcx, LangItem::Destruct, cx.body.span, [ty])
161+
.with_constness(ty::BoundConstness::ConstIfConst),
167162
);
168163

169164
let infcx = cx.tcx.infer_ctxt().build();

compiler/rustc_hir_analysis/src/bounds.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'tcx> Bounds<'tcx> {
5757

5858
pub fn push_sized(&mut self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) {
5959
let sized_def_id = tcx.require_lang_item(LangItem::Sized, Some(span));
60-
let trait_ref = ty::Binder::dummy(ty::TraitRef::new(tcx, sized_def_id, [ty]));
60+
let trait_ref = ty::TraitRef::new(tcx, sized_def_id, [ty]);
6161
// Preferable to put this obligation first, since we report better errors for sized ambiguity.
6262
self.predicates.insert(0, (trait_ref.without_const().to_predicate(tcx), span));
6363
}

compiler/rustc_middle/src/ty/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,18 @@ impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for Binder<'tcx, TraitRef
12071207
}
12081208
}
12091209

1210+
impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for TraitRef<'tcx> {
1211+
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> PolyTraitPredicate<'tcx> {
1212+
ty::Binder::dummy(self).to_predicate(tcx)
1213+
}
1214+
}
1215+
1216+
impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for TraitPredicate<'tcx> {
1217+
fn to_predicate(self, _tcx: TyCtxt<'tcx>) -> PolyTraitPredicate<'tcx> {
1218+
ty::Binder::dummy(self)
1219+
}
1220+
}
1221+
12101222
impl<'tcx> ToPredicate<'tcx> for PolyTraitPredicate<'tcx> {
12111223
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
12121224
self.map_bound(|p| PredicateKind::Clause(Clause::Trait(p))).to_predicate(tcx)
@@ -1231,6 +1243,12 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
12311243
}
12321244
}
12331245

1246+
impl<'tcx> ToPredicate<'tcx> for TraitPredicate<'tcx> {
1247+
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1248+
PredicateKind::Clause(Clause::Trait(self)).to_predicate(tcx)
1249+
}
1250+
}
1251+
12341252
impl<'tcx> Predicate<'tcx> {
12351253
pub fn to_opt_poly_trait_pred(self) -> Option<PolyTraitPredicate<'tcx>> {
12361254
let predicate = self.kind();

compiler/rustc_middle/src/ty/sty.rs

+12
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,18 @@ impl<'tcx> TraitRef<'tcx> {
871871
)
872872
}
873873

874+
/// Converts this trait ref to a trait predicate with a given `constness` and a positive polarity.
875+
#[inline]
876+
pub fn with_constness(self, constness: ty::BoundConstness) -> ty::TraitPredicate<'tcx> {
877+
ty::TraitPredicate { trait_ref: self, constness, polarity: ty::ImplPolarity::Positive }
878+
}
879+
880+
/// Converts this trait ref to a trait predicate without `const` and a positive polarity.
881+
#[inline]
882+
pub fn without_const(self) -> ty::TraitPredicate<'tcx> {
883+
self.with_constness(ty::BoundConstness::NotConst)
884+
}
885+
874886
#[inline]
875887
pub fn self_ty(&self) -> Ty<'tcx> {
876888
self.substs.type_at(0)

compiler/rustc_trait_selection/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub fn type_known_to_meet_bound_modulo_regions<'tcx>(
127127
ty: Ty<'tcx>,
128128
def_id: DefId,
129129
) -> bool {
130-
let trait_ref = ty::Binder::dummy(ty::TraitRef::new(infcx.tcx, def_id, [ty]));
130+
let trait_ref = ty::TraitRef::new(infcx.tcx, def_id, [ty]);
131131
pred_known_to_hold_modulo_regions(infcx, param_env, trait_ref.without_const())
132132
}
133133

compiler/rustc_trait_selection/src/traits/object_safety.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -769,13 +769,10 @@ fn receiver_is_dispatchable<'tcx>(
769769
let param_env = tcx.param_env(method.def_id);
770770

771771
// Self: Unsize<U>
772-
let unsize_predicate = ty::Binder::dummy(ty::TraitRef::new(
773-
tcx,
774-
unsize_did,
775-
[tcx.types.self_param, unsized_self_ty],
776-
))
777-
.without_const()
778-
.to_predicate(tcx);
772+
let unsize_predicate =
773+
ty::TraitRef::new(tcx, unsize_did, [tcx.types.self_param, unsized_self_ty])
774+
.without_const()
775+
.to_predicate(tcx);
779776

780777
// U: Trait<Arg1, ..., ArgN>
781778
let trait_predicate = {

compiler/rustc_trait_selection/src/traits/project.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ fn assemble_candidate_for_impl_trait_in_trait<'cx, 'tcx>(
13191319
let trait_substs =
13201320
obligation.predicate.substs.truncate_to(tcx, tcx.generics_of(trait_def_id));
13211321
// FIXME(named-returns): Binders
1322-
let trait_predicate = ty::Binder::dummy(ty::TraitRef::new(tcx, trait_def_id, trait_substs));
1322+
let trait_predicate = ty::TraitRef::new(tcx, trait_def_id, trait_substs);
13231323

13241324
let _ = selcx.infcx.commit_if_ok(|_| {
13251325
match selcx.select(&obligation.with(tcx, trait_predicate)) {
@@ -1682,10 +1682,8 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
16821682
if selcx.infcx.predicate_must_hold_modulo_regions(
16831683
&obligation.with(
16841684
selcx.tcx(),
1685-
ty::Binder::dummy(
1686-
ty::TraitRef::from_lang_item(selcx.tcx(), LangItem::Sized, obligation.cause.span(),[self_ty]),
1687-
)
1688-
.without_const(),
1685+
ty::TraitRef::from_lang_item(selcx.tcx(), LangItem::Sized, obligation.cause.span(),[self_ty])
1686+
.without_const(),
16891687
),
16901688
) =>
16911689
{
@@ -1948,12 +1946,12 @@ fn confirm_builtin_candidate<'cx, 'tcx>(
19481946
)
19491947
});
19501948
if check_is_sized {
1951-
let sized_predicate = ty::Binder::dummy(ty::TraitRef::from_lang_item(
1949+
let sized_predicate = ty::TraitRef::from_lang_item(
19521950
tcx,
19531951
LangItem::Sized,
19541952
obligation.cause.span(),
19551953
[self_ty],
1956-
))
1954+
)
19571955
.without_const();
19581956
obligations.push(obligation.with(tcx, sized_predicate));
19591957
}

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1049,12 +1049,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10491049
);
10501050

10511051
// We can only make objects from sized types.
1052-
let tr = ty::Binder::dummy(ty::TraitRef::from_lang_item(
1053-
tcx,
1054-
LangItem::Sized,
1055-
cause.span,
1056-
[source],
1057-
));
1052+
let tr = ty::TraitRef::from_lang_item(tcx, LangItem::Sized, cause.span, [source]);
10581053
nested.push(predicate_to_obligation(tr.without_const().to_predicate(tcx)));
10591054

10601055
// If the type is `Foo + 'a`, ensure that the type

compiler/rustc_ty_utils/src/ty.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ fn sized_constraint_for_ty<'tcx>(
6262
// it on the impl.
6363

6464
let Some(sized_trait) = tcx.lang_items().sized_trait() else { return vec![ty] };
65-
let sized_predicate = ty::Binder::dummy(ty::TraitRef::new(tcx, sized_trait, [ty]))
66-
.without_const()
67-
.to_predicate(tcx);
65+
let sized_predicate =
66+
ty::TraitRef::new(tcx, sized_trait, [ty]).without_const().to_predicate(tcx);
6867
let predicates = tcx.predicates_of(adtdef.did()).predicates;
6968
if predicates.iter().any(|(p, _)| *p == sized_predicate) { vec![] } else { vec![ty] }
7069
}

0 commit comments

Comments
 (0)