Skip to content

Commit 3d9d36b

Browse files
author
Alexander Regueiro
committed
Got rid of concept of 'principal trait' in more places in codebase.
1 parent 8f9ca24 commit 3d9d36b

File tree

4 files changed

+47
-49
lines changed

4 files changed

+47
-49
lines changed

src/librustc_privacy/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,10 +1050,14 @@ impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
10501050
if !self.in_body {
10511051
// Avoid calling `hir_trait_to_predicates` in bodies, it will ICE.
10521052
// The traits' privacy in bodies is already checked as a part of trait object types.
1053-
let (principal, bounds) = rustc_typeck::hir_trait_to_predicates(self.tcx, trait_ref);
1054-
if self.visit_trait(*principal.skip_binder()) {
1055-
return;
1053+
let bounds = rustc_typeck::hir_trait_to_predicates(self.tcx, trait_ref);
1054+
1055+
for (trait_predicate, _) in bounds.trait_bounds {
1056+
if self.visit_trait(*trait_predicate.skip_binder()) {
1057+
return;
1058+
}
10561059
}
1060+
10571061
for (poly_predicate, _) in bounds.projection_bounds {
10581062
let tcx = self.tcx;
10591063
if self.visit(poly_predicate.skip_binder().ty)

src/librustc_typeck/astconv.rs

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -775,11 +775,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
775775
/// The given trait-ref must actually be a trait.
776776
pub(super) fn instantiate_poly_trait_ref_inner(&self,
777777
trait_ref: &hir::TraitRef,
778+
span: Span,
778779
self_ty: Ty<'tcx>,
779780
bounds: &mut Bounds<'tcx>,
780781
speculative: bool,
781-
) -> (ty::PolyTraitRef<'tcx>, Option<Vec<Span>>)
782-
{
782+
) -> Option<Vec<Span>> {
783783
let trait_def_id = trait_ref.trait_def_id();
784784

785785
debug!("instantiate_poly_trait_ref({:?}, def_id={:?})", trait_ref, trait_def_id);
@@ -794,6 +794,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
794794
);
795795
let poly_trait_ref = ty::Binder::bind(ty::TraitRef::new(trait_def_id, substs));
796796

797+
bounds.trait_bounds.push((poly_trait_ref, span));
798+
797799
let mut dup_bindings = FxHashMap::default();
798800
for binding in &assoc_bindings {
799801
// Specify type to assert that error was already reported in `Err` case.
@@ -811,7 +813,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
811813

812814
debug!("instantiate_poly_trait_ref({:?}, bounds={:?}) -> {:?}",
813815
trait_ref, bounds, poly_trait_ref);
814-
(poly_trait_ref, potential_assoc_types)
816+
potential_assoc_types
815817
}
816818

817819
/// Given a trait bound like `Debug`, applies that trait bound the given self-type to construct
@@ -836,10 +838,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
836838
pub fn instantiate_poly_trait_ref(&self,
837839
poly_trait_ref: &hir::PolyTraitRef,
838840
self_ty: Ty<'tcx>,
839-
bounds: &mut Bounds<'tcx>
840-
) -> (ty::PolyTraitRef<'tcx>, Option<Vec<Span>>)
841-
{
842-
self.instantiate_poly_trait_ref_inner(&poly_trait_ref.trait_ref, self_ty, bounds, false)
841+
bounds: &mut Bounds<'tcx>,
842+
) -> Option<Vec<Span>> {
843+
self.instantiate_poly_trait_ref_inner(
844+
&poly_trait_ref.trait_ref,
845+
poly_trait_ref.span,
846+
self_ty,
847+
bounds,
848+
false,
849+
)
843850
}
844851

845852
fn ast_path_to_mono_trait_ref(&self,
@@ -983,12 +990,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
983990
}
984991

985992
for bound in trait_bounds {
986-
let (poly_trait_ref, _) = self.instantiate_poly_trait_ref(
993+
let _ = self.instantiate_poly_trait_ref(
987994
bound,
988995
param_ty,
989996
bounds,
990997
);
991-
bounds.trait_bounds.push((poly_trait_ref, bound.span))
992998
}
993999

9941000
bounds.region_bounds.extend(region_bounds
@@ -1172,11 +1178,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11721178
// Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty`
11731179
// parameter to have a skipped binder.
11741180
let param_ty = tcx.mk_projection(assoc_ty.def_id, candidate.skip_binder().substs);
1175-
self.add_bounds(
1176-
param_ty,
1177-
ast_bounds,
1178-
bounds,
1179-
);
1181+
self.add_bounds(param_ty, ast_bounds, bounds);
11801182
}
11811183
}
11821184
Ok(())
@@ -1216,25 +1218,19 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12161218
let mut bounds = Bounds::default();
12171219
let mut potential_assoc_types = Vec::new();
12181220
let dummy_self = self.tcx().types.trait_object_dummy_self;
1219-
// FIXME: we want to avoid collecting into a `Vec` here, but simply cloning the iterator is
1220-
// not straightforward due to the borrow checker.
1221-
let bound_trait_refs: Vec<_> = trait_bounds
1222-
.iter()
1223-
.rev()
1224-
.map(|trait_bound| {
1225-
let (trait_ref, cur_potential_assoc_types) = self.instantiate_poly_trait_ref(
1226-
trait_bound,
1227-
dummy_self,
1228-
&mut bounds,
1229-
);
1230-
potential_assoc_types.extend(cur_potential_assoc_types.into_iter().flatten());
1231-
(trait_ref, trait_bound.span)
1232-
})
1233-
.collect();
1221+
for trait_bound in trait_bounds.iter().rev() {
1222+
let cur_potential_assoc_types = self.instantiate_poly_trait_ref(
1223+
trait_bound,
1224+
dummy_self,
1225+
&mut bounds,
1226+
);
1227+
potential_assoc_types.extend(cur_potential_assoc_types.into_iter().flatten());
1228+
}
12341229

12351230
// Expand trait aliases recursively and check that only one regular (non-auto) trait
12361231
// is used and no 'maybe' bounds are used.
1237-
let expanded_traits = traits::expand_trait_aliases(tcx, bound_trait_refs.iter().cloned());
1232+
let expanded_traits =
1233+
traits::expand_trait_aliases(tcx, bounds.trait_bounds.iter().cloned());
12381234
let (mut auto_traits, regular_traits): (Vec<_>, Vec<_>) =
12391235
expanded_traits.partition(|i| tcx.trait_is_auto(i.trait_ref().def_id()));
12401236
if regular_traits.len() > 1 {
@@ -1276,7 +1272,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12761272
// Use a `BTreeSet` to keep output in a more consistent order.
12771273
let mut associated_types = BTreeSet::default();
12781274

1279-
let regular_traits_refs = bound_trait_refs
1275+
let regular_traits_refs = bounds.trait_bounds
12801276
.into_iter()
12811277
.filter(|(trait_ref, _)| !tcx.trait_is_auto(trait_ref.def_id()))
12821278
.map(|(trait_ref, _)| trait_ref);

src/librustc_typeck/collect.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ use rustc::hir::{self, CodegenFnAttrFlags, CodegenFnAttrs, Unsafety};
4949

5050
use errors::{Applicability, DiagnosticId};
5151

52-
use std::iter;
53-
5452
struct OnlySelfBounds(bool);
5553

5654
///////////////////////////////////////////////////////////////////////////
@@ -2191,15 +2189,12 @@ fn explicit_predicates_of(
21912189
match bound {
21922190
&hir::GenericBound::Trait(ref poly_trait_ref, _) => {
21932191
let mut bounds = Bounds::default();
2194-
2195-
let (trait_ref, _) = AstConv::instantiate_poly_trait_ref(
2192+
let _ = AstConv::instantiate_poly_trait_ref(
21962193
&icx,
21972194
poly_trait_ref,
21982195
ty,
21992196
&mut bounds,
22002197
);
2201-
2202-
predicates.push((trait_ref.to_predicate(), poly_trait_ref.span));
22032198
predicates.extend(bounds.predicates(tcx, ty));
22042199
}
22052200

@@ -2301,10 +2296,13 @@ fn predicates_from_bound<'tcx>(
23012296
match *bound {
23022297
hir::GenericBound::Trait(ref tr, hir::TraitBoundModifier::None) => {
23032298
let mut bounds = Bounds::default();
2304-
let (pred, _) = astconv.instantiate_poly_trait_ref(tr, param_ty, &mut bounds);
2305-
iter::once((pred.to_predicate(), tr.span))
2306-
.chain(bounds.predicates(astconv.tcx(), param_ty))
2307-
.collect()
2299+
let _ = astconv.instantiate_poly_trait_ref(
2300+
tr,
2301+
param_ty,
2302+
&mut bounds,
2303+
false,
2304+
);
2305+
bounds.predicates(astconv.tcx(), param_ty)
23082306
}
23092307
hir::GenericBound::Outlives(ref lifetime) => {
23102308
let region = astconv.ast_region_to_region(lifetime, None);

src/librustc_typeck/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ use rustc::ty::subst::SubstsRef;
108108
use rustc::ty::{self, Ty, TyCtxt};
109109
use rustc::ty::query::Providers;
110110
use rustc::util;
111-
use syntax_pos::Span;
111+
use syntax_pos::{DUMMY_SP, Span};
112112
use util::common::time;
113113

114114
use std::iter;
@@ -375,19 +375,19 @@ pub fn hir_ty_to_ty<'tcx>(tcx: TyCtxt<'tcx>, hir_ty: &hir::Ty) -> Ty<'tcx> {
375375
pub fn hir_trait_to_predicates<'tcx>(
376376
tcx: TyCtxt<'tcx>,
377377
hir_trait: &hir::TraitRef,
378-
) -> (ty::PolyTraitRef<'tcx>, Bounds<'tcx>) {
378+
) -> Bounds<'tcx> {
379379
// In case there are any projections, etc., find the "environment"
380380
// def-ID that will be used to determine the traits/predicates in
381381
// scope. This is derived from the enclosing item-like thing.
382382
let env_hir_id = tcx.hir().get_parent_item(hir_trait.hir_ref_id);
383383
let env_def_id = tcx.hir().local_def_id(env_hir_id);
384384
let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id);
385385
let mut bounds = Bounds::default();
386-
let (principal, _) = AstConv::instantiate_poly_trait_ref_inner(
387-
&item_cx, hir_trait, tcx.types.err, &mut bounds, true
386+
let _ = AstConv::instantiate_poly_trait_ref_inner(
387+
&item_cx, hir_trait, DUMMY_SP, tcx.types.err, &mut bounds, true
388388
);
389389

390-
(principal, bounds)
390+
bounds
391391
}
392392

393393
__build_diagnostic_array! { librustc_typeck, DIAGNOSTICS }

0 commit comments

Comments
 (0)