Skip to content

Commit 79f6f11

Browse files
committed
Remove some unnecessary trait_ref calls
1 parent 9526c0c commit 79f6f11

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

compiler/rustc_privacy/src/lib.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ trait DefIdVisitor<'tcx> {
7777
fn visit_trait(&mut self, trait_ref: TraitRef<'tcx>) -> ControlFlow<Self::BreakTy> {
7878
self.skeleton().visit_trait(trait_ref)
7979
}
80+
fn visit_projection_ty(
81+
&mut self,
82+
projection: ty::ProjectionTy<'tcx>,
83+
) -> ControlFlow<Self::BreakTy> {
84+
self.skeleton().visit_projection_ty(projection)
85+
}
8086
fn visit_predicates(
8187
&mut self,
8288
predicates: ty::GenericPredicates<'tcx>,
@@ -101,14 +107,28 @@ where
101107
if self.def_id_visitor.shallow() { ControlFlow::CONTINUE } else { substs.visit_with(self) }
102108
}
103109

110+
fn visit_projection_ty(
111+
&mut self,
112+
projection: ty::ProjectionTy<'tcx>,
113+
) -> ControlFlow<V::BreakTy> {
114+
let (trait_ref, assoc_substs) =
115+
projection.trait_ref_and_own_substs(self.def_id_visitor.tcx());
116+
self.visit_trait(trait_ref)?;
117+
if self.def_id_visitor.shallow() {
118+
ControlFlow::CONTINUE
119+
} else {
120+
assoc_substs.iter().try_for_each(|subst| subst.visit_with(self))
121+
}
122+
}
123+
104124
fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow<V::BreakTy> {
105125
match predicate.kind().skip_binder() {
106126
ty::PredicateKind::Trait(ty::TraitPredicate { trait_ref }, _) => {
107127
self.visit_trait(trait_ref)
108128
}
109129
ty::PredicateKind::Projection(ty::ProjectionPredicate { projection_ty, ty }) => {
110130
ty.visit_with(self)?;
111-
self.visit_trait(projection_ty.trait_ref(self.def_id_visitor.tcx()))
131+
self.visit_projection_ty(projection_ty)
112132
}
113133
ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, _region)) => {
114134
ty.visit_with(self)
@@ -197,7 +217,7 @@ where
197217
return ControlFlow::CONTINUE;
198218
}
199219
// This will also visit substs if necessary, so we don't need to recurse.
200-
return self.visit_trait(proj.trait_ref(tcx));
220+
return self.visit_projection_ty(proj);
201221
}
202222
ty::Dynamic(predicates, ..) => {
203223
// All traits in the list are considered the "primary" part of the type
@@ -1204,10 +1224,9 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
12041224
}
12051225

12061226
for (poly_predicate, _) in bounds.projection_bounds {
1207-
let tcx = self.tcx;
12081227
if self.visit(poly_predicate.skip_binder().ty).is_break()
12091228
|| self
1210-
.visit_trait(poly_predicate.skip_binder().projection_ty.trait_ref(tcx))
1229+
.visit_projection_ty(poly_predicate.skip_binder().projection_ty)
12111230
.is_break()
12121231
{
12131232
return;

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_errors::ErrorReported;
66
use rustc_infer::traits::{TraitEngine, TraitEngineExt as _, TraitObligation};
77
use rustc_middle::mir::interpret::ErrorHandled;
88
use rustc_middle::ty::error::ExpectedFound;
9+
use rustc_middle::ty::subst::SubstsRef;
910
use rustc_middle::ty::ToPredicate;
1011
use rustc_middle::ty::{self, Binder, Const, Ty, TypeFoldable};
1112
use std::marker::PhantomData;
@@ -633,9 +634,9 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
633634
// only reason we can fail to make progress on
634635
// trait selection is because we don't have enough
635636
// information about the types in the trait.
636-
*stalled_on = trait_ref_infer_vars(
637+
*stalled_on = substs_infer_vars(
637638
self.selcx,
638-
trait_obligation.predicate.map_bound(|pred| pred.trait_ref),
639+
trait_obligation.predicate.map_bound(|pred| pred.trait_ref.substs),
639640
);
640641

641642
debug!(
@@ -663,9 +664,9 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
663664
match project::poly_project_and_unify_type(self.selcx, &project_obligation) {
664665
Ok(Ok(Some(os))) => ProcessResult::Changed(mk_pending(os)),
665666
Ok(Ok(None)) => {
666-
*stalled_on = trait_ref_infer_vars(
667+
*stalled_on = substs_infer_vars(
667668
self.selcx,
668-
project_obligation.predicate.to_poly_trait_ref(tcx),
669+
project_obligation.predicate.map_bound(|pred| pred.projection_ty.substs),
669670
);
670671
ProcessResult::Unchanged
671672
}
@@ -678,16 +679,15 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
678679
}
679680
}
680681

681-
/// Returns the set of inference variables contained in a trait ref.
682-
fn trait_ref_infer_vars<'a, 'tcx>(
682+
/// Returns the set of inference variables contained in `substs`.
683+
fn substs_infer_vars<'a, 'tcx>(
683684
selcx: &mut SelectionContext<'a, 'tcx>,
684-
trait_ref: ty::PolyTraitRef<'tcx>,
685+
substs: ty::Binder<SubstsRef<'tcx>>,
685686
) -> Vec<TyOrConstInferVar<'tcx>> {
686687
selcx
687688
.infcx()
688-
.resolve_vars_if_possible(trait_ref)
689-
.skip_binder()
690-
.substs
689+
.resolve_vars_if_possible(substs)
690+
.skip_binder() // ok because this check doesn't care about regions
691691
.iter()
692692
// FIXME(eddyb) try using `skip_current_subtree` to skip everything that
693693
// doesn't contain inference variables, not just the outermost level.

compiler/rustc_trait_selection/src/traits/object_safety.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,7 @@ fn predicate_references_self(
292292
//
293293
// This is ALT2 in issue #56288, see that for discussion of the
294294
// possible alternatives.
295-
if data.projection_ty.trait_ref(tcx).substs[1..].iter().any(has_self_ty) {
296-
Some(sp)
297-
} else {
298-
None
299-
}
295+
if data.projection_ty.substs[1..].iter().any(has_self_ty) { Some(sp) } else { None }
300296
}
301297
ty::PredicateKind::WellFormed(..)
302298
| ty::PredicateKind::ObjectSafe(..)

compiler/rustc_typeck/src/constrained_generic_params.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub fn setup_constraining_predicates<'tcx>(
198198
// `<<T as Bar>::Baz as Iterator>::Output = <U as Iterator>::Output`
199199
// Then the projection only applies if `T` is known, but it still
200200
// does not determine `U`.
201-
let inputs = parameters_for(&projection.projection_ty.trait_ref(tcx), true);
201+
let inputs = parameters_for(&projection.projection_ty, true);
202202
let relies_only_on_inputs = inputs.iter().all(|p| input_parameters.contains(&p));
203203
if !relies_only_on_inputs {
204204
continue;

0 commit comments

Comments
 (0)