Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 37354eb

Browse files
committedDec 2, 2020
Revert "Auto merge of #79209 - spastorino:trait-inheritance-self, r=nikomatsakis"
This reverts commit 349b3b3, reversing changes made to b776d1c.
1 parent b7ebc6b commit 37354eb

28 files changed

+151
-498
lines changed
 

‎compiler/rustc_infer/src/traits/util.rs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use smallvec::smallvec;
22

33
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
4-
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
4+
use rustc_data_structures::fx::FxHashSet;
55
use rustc_middle::ty::outlives::Component;
66
use rustc_middle::ty::{self, ToPredicate, TyCtxt, WithConstness};
7-
use rustc_span::symbol::Ident;
87

98
pub fn anonymize_predicate<'tcx>(
109
tcx: TyCtxt<'tcx>,
@@ -288,37 +287,6 @@ pub fn transitive_bounds<'tcx>(
288287
elaborate_trait_refs(tcx, bounds).filter_to_traits()
289288
}
290289

291-
/// A specialized variant of `elaborate_trait_refs` that only elaborates trait references that may
292-
/// define the given associated type `assoc_name`. It uses the
293-
/// `super_predicates_that_define_assoc_type` query to avoid enumerating super-predicates that
294-
/// aren't related to `assoc_item`. This is used when resolving types like `Self::Item` or
295-
/// `T::Item` and helps to avoid cycle errors (see e.g. #35237).
296-
pub fn transitive_bounds_that_define_assoc_type<'tcx>(
297-
tcx: TyCtxt<'tcx>,
298-
bounds: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
299-
assoc_name: Ident,
300-
) -> FxIndexSet<ty::PolyTraitRef<'tcx>> {
301-
let mut stack: Vec<_> = bounds.collect();
302-
let mut trait_refs = FxIndexSet::default();
303-
304-
while let Some(trait_ref) = stack.pop() {
305-
if trait_refs.insert(trait_ref) {
306-
let super_predicates =
307-
tcx.super_predicates_that_define_assoc_type((trait_ref.def_id(), Some(assoc_name)));
308-
for (super_predicate, _) in super_predicates.predicates {
309-
let bound_predicate = super_predicate.bound_atom();
310-
let subst_predicate = super_predicate
311-
.subst_supertrait(tcx, &bound_predicate.rebind(trait_ref.skip_binder()));
312-
if let Some(binder) = subst_predicate.to_opt_poly_trait_ref() {
313-
stack.push(binder.value);
314-
}
315-
}
316-
}
317-
}
318-
319-
trait_refs
320-
}
321-
322290
///////////////////////////////////////////////////////////////////////////
323291
// Other
324292
///////////////////////////////////////////////////////////////////////////

‎compiler/rustc_middle/src/query/mod.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -433,23 +433,12 @@ rustc_queries! {
433433
/// full predicates are available (note that supertraits have
434434
/// additional acyclicity requirements).
435435
query super_predicates_of(key: DefId) -> ty::GenericPredicates<'tcx> {
436-
desc { |tcx| "computing the super predicates of `{}`", tcx.def_path_str(key) }
437-
}
438-
439-
/// The `Option<Ident>` is the name of an associated type. If it is `None`, then this query
440-
/// returns the full set of predicates. If `Some<Ident>`, then the query returns only the
441-
/// subset of super-predicates that reference traits that define the given associated type.
442-
/// This is used to avoid cycles in resolving types like `T::Item`.
443-
query super_predicates_that_define_assoc_type(key: (DefId, Option<rustc_span::symbol::Ident>)) -> ty::GenericPredicates<'tcx> {
444-
desc { |tcx| "computing the super traits of `{}`{}",
445-
tcx.def_path_str(key.0),
446-
if let Some(assoc_name) = key.1 { format!(" with associated type name `{}`", assoc_name) } else { "".to_string() },
447-
}
436+
desc { |tcx| "computing the supertraits of `{}`", tcx.def_path_str(key) }
448437
}
449438

450439
/// To avoid cycles within the predicates of a single item we compute
451440
/// per-type-parameter predicates for resolving `T::AssocTy`.
452-
query type_param_predicates(key: (DefId, LocalDefId, rustc_span::symbol::Ident)) -> ty::GenericPredicates<'tcx> {
441+
query type_param_predicates(key: (DefId, LocalDefId)) -> ty::GenericPredicates<'tcx> {
453442
desc { |tcx| "computing the bounds for type parameter `{}`", {
454443
let id = tcx.hir().local_def_id_to_hir_id(key.1);
455444
tcx.hir().ty_param_name(id)

‎compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames};
5151
use rustc_session::lint::{Level, Lint};
5252
use rustc_session::Session;
5353
use rustc_span::source_map::MultiSpan;
54-
use rustc_span::symbol::{kw, sym, Ident, Symbol};
54+
use rustc_span::symbol::{kw, sym, Symbol};
5555
use rustc_span::{Span, DUMMY_SP};
5656
use rustc_target::abi::{Layout, TargetDataLayout, VariantIdx};
5757
use rustc_target::spec::abi;
@@ -2085,42 +2085,6 @@ impl<'tcx> TyCtxt<'tcx> {
20852085
self.mk_fn_ptr(sig.map_bound(|sig| ty::FnSig { unsafety: hir::Unsafety::Unsafe, ..sig }))
20862086
}
20872087

2088-
/// Given the def_id of a Trait `trait_def_id` and the name of an associated item `assoc_name`
2089-
/// returns true if the `trait_def_id` defines an associated item of name `assoc_name`.
2090-
pub fn trait_may_define_assoc_type(self, trait_def_id: DefId, assoc_name: Ident) -> bool {
2091-
self.super_traits_of(trait_def_id).any(|trait_did| {
2092-
self.associated_items(trait_did)
2093-
.find_by_name_and_kind(self, assoc_name, ty::AssocKind::Type, trait_did)
2094-
.is_some()
2095-
})
2096-
}
2097-
2098-
/// Computes the def-ids of the transitive super-traits of `trait_def_id`. This (intentionally)
2099-
/// does not compute the full elaborated super-predicates but just the set of def-ids. It is used
2100-
/// to identify which traits may define a given associated type to help avoid cycle errors.
2101-
/// Returns a `DefId` iterator.
2102-
fn super_traits_of(self, trait_def_id: DefId) -> impl Iterator<Item = DefId> + 'tcx {
2103-
let mut set = FxHashSet::default();
2104-
let mut stack = vec![trait_def_id];
2105-
2106-
set.insert(trait_def_id);
2107-
2108-
iter::from_fn(move || -> Option<DefId> {
2109-
let trait_did = stack.pop()?;
2110-
let generic_predicates = self.super_predicates_of(trait_did);
2111-
2112-
for (predicate, _) in generic_predicates.predicates {
2113-
if let ty::PredicateAtom::Trait(data, _) = predicate.skip_binders() {
2114-
if set.insert(data.def_id()) {
2115-
stack.push(data.def_id());
2116-
}
2117-
}
2118-
}
2119-
2120-
Some(trait_did)
2121-
})
2122-
}
2123-
21242088
/// Given a closure signature, returns an equivalent fn signature. Detuples
21252089
/// and so forth -- so e.g., if we have a sig with `Fn<(u32, i32)>` then
21262090
/// you would get a `fn(u32, i32)`.

‎compiler/rustc_middle/src/ty/query/keys.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::ty::subst::{GenericArg, SubstsRef};
77
use crate::ty::{self, Ty, TyCtxt};
88
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
99
use rustc_query_system::query::DefaultCacheSelector;
10-
use rustc_span::symbol::{Ident, Symbol};
10+
use rustc_span::symbol::Symbol;
1111
use rustc_span::{Span, DUMMY_SP};
1212

1313
/// The `Key` trait controls what types can legally be used as the key
@@ -149,28 +149,6 @@ impl Key for (LocalDefId, DefId) {
149149
}
150150
}
151151

152-
impl Key for (DefId, Option<Ident>) {
153-
type CacheSelector = DefaultCacheSelector;
154-
155-
fn query_crate(&self) -> CrateNum {
156-
self.0.krate
157-
}
158-
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
159-
tcx.def_span(self.0)
160-
}
161-
}
162-
163-
impl Key for (DefId, LocalDefId, Ident) {
164-
type CacheSelector = DefaultCacheSelector;
165-
166-
fn query_crate(&self) -> CrateNum {
167-
self.0.krate
168-
}
169-
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
170-
self.1.default_span(tcx)
171-
}
172-
}
173-
174152
impl Key for (CrateNum, DefId) {
175153
type CacheSelector = DefaultCacheSelector;
176154

‎compiler/rustc_trait_selection/src/traits/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ pub use self::util::{
6565
get_vtable_index_of_object_method, impl_item_is_final, predicate_for_trait_def, upcast_choices,
6666
};
6767
pub use self::util::{
68-
supertrait_def_ids, supertraits, transitive_bounds, transitive_bounds_that_define_assoc_type,
69-
SupertraitDefIds, Supertraits,
68+
supertrait_def_ids, supertraits, transitive_bounds, SupertraitDefIds, Supertraits,
7069
};
7170

7271
pub use self::chalk_fulfill::FulfillmentContext as ChalkFulfillmentContext;

‎compiler/rustc_typeck/src/astconv/mod.rs

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ pub trait AstConv<'tcx> {
4949

5050
fn default_constness_for_trait_bounds(&self) -> Constness;
5151

52-
/// Returns predicates in scope of the form `X: Foo<T>`, where `X`
53-
/// is a type parameter `X` with the given id `def_id` and T
54-
/// matches `assoc_name`. This is a subset of the full set of
55-
/// predicates.
52+
/// Returns predicates in scope of the form `X: Foo`, where `X` is
53+
/// a type parameter `X` with the given id `def_id`. This is a
54+
/// subset of the full set of predicates.
5655
///
5756
/// This is used for one specific purpose: resolving "short-hand"
5857
/// associated type references like `T::Item`. In principle, we
@@ -61,12 +60,7 @@ pub trait AstConv<'tcx> {
6160
/// but this can lead to cycle errors. The problem is that we have
6261
/// to do this resolution *in order to create the predicates in
6362
/// the first place*. Hence, we have this "special pass".
64-
fn get_type_parameter_bounds(
65-
&self,
66-
span: Span,
67-
def_id: DefId,
68-
assoc_name: Ident,
69-
) -> ty::GenericPredicates<'tcx>;
63+
fn get_type_parameter_bounds(&self, span: Span, def_id: DefId) -> ty::GenericPredicates<'tcx>;
7064

7165
/// Returns the lifetime to use when a lifetime is omitted (and not elided).
7266
fn re_infer(&self, param: Option<&ty::GenericParamDef>, span: Span)
@@ -768,7 +762,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
768762
}
769763

770764
// Returns `true` if a bounds list includes `?Sized`.
771-
pub fn is_unsized(&self, ast_bounds: &[&hir::GenericBound<'_>], span: Span) -> bool {
765+
pub fn is_unsized(&self, ast_bounds: &[hir::GenericBound<'_>], span: Span) -> bool {
772766
let tcx = self.tcx();
773767

774768
// Try to find an unbound in bounds.
@@ -826,7 +820,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
826820
fn add_bounds(
827821
&self,
828822
param_ty: Ty<'tcx>,
829-
ast_bounds: &[&hir::GenericBound<'_>],
823+
ast_bounds: &[hir::GenericBound<'_>],
830824
bounds: &mut Bounds<'tcx>,
831825
) {
832826
let constness = self.default_constness_for_trait_bounds();
@@ -841,7 +835,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
841835
hir::GenericBound::Trait(_, hir::TraitBoundModifier::Maybe) => {}
842836
hir::GenericBound::LangItemTrait(lang_item, span, hir_id, args) => self
843837
.instantiate_lang_item_trait_ref(
844-
*lang_item, *span, *hir_id, args, param_ty, bounds,
838+
lang_item, span, hir_id, args, param_ty, bounds,
845839
),
846840
hir::GenericBound::Outlives(ref l) => {
847841
bounds.region_bounds.push((self.ast_region_to_region(l, None), l.span))
@@ -872,42 +866,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
872866
ast_bounds: &[hir::GenericBound<'_>],
873867
sized_by_default: SizedByDefault,
874868
span: Span,
875-
) -> Bounds<'tcx> {
876-
let ast_bounds: Vec<_> = ast_bounds.iter().collect();
877-
self.compute_bounds_inner(param_ty, &ast_bounds, sized_by_default, span)
878-
}
879-
880-
/// Convert the bounds in `ast_bounds` that refer to traits which define an associated type
881-
/// named `assoc_name` into ty::Bounds. Ignore the rest.
882-
pub fn compute_bounds_that_match_assoc_type(
883-
&self,
884-
param_ty: Ty<'tcx>,
885-
ast_bounds: &[hir::GenericBound<'_>],
886-
sized_by_default: SizedByDefault,
887-
span: Span,
888-
assoc_name: Ident,
889-
) -> Bounds<'tcx> {
890-
let mut result = Vec::new();
891-
892-
for ast_bound in ast_bounds {
893-
if let Some(trait_ref) = ast_bound.trait_ref() {
894-
if let Some(trait_did) = trait_ref.trait_def_id() {
895-
if self.tcx().trait_may_define_assoc_type(trait_did, assoc_name) {
896-
result.push(ast_bound);
897-
}
898-
}
899-
}
900-
}
901-
902-
self.compute_bounds_inner(param_ty, &result, sized_by_default, span)
903-
}
904-
905-
fn compute_bounds_inner(
906-
&self,
907-
param_ty: Ty<'tcx>,
908-
ast_bounds: &[&hir::GenericBound<'_>],
909-
sized_by_default: SizedByDefault,
910-
span: Span,
911869
) -> Bounds<'tcx> {
912870
let mut bounds = Bounds::default();
913871

@@ -1077,8 +1035,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10771035
// Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty`
10781036
// parameter to have a skipped binder.
10791037
let param_ty = tcx.mk_projection(assoc_ty.def_id, candidate.skip_binder().substs);
1080-
let ast_bounds: Vec<_> = ast_bounds.iter().collect();
1081-
self.add_bounds(param_ty, &ast_bounds, bounds);
1038+
self.add_bounds(param_ty, ast_bounds, bounds);
10821039
}
10831040
}
10841041
Ok(())
@@ -1395,24 +1352,21 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
13951352
ty_param_def_id, assoc_name, span,
13961353
);
13971354

1398-
let predicates = &self
1399-
.get_type_parameter_bounds(span, ty_param_def_id.to_def_id(), assoc_name)
1400-
.predicates;
1355+
let predicates =
1356+
&self.get_type_parameter_bounds(span, ty_param_def_id.to_def_id()).predicates;
14011357

14021358
debug!("find_bound_for_assoc_item: predicates={:#?}", predicates);
14031359

14041360
let param_hir_id = tcx.hir().local_def_id_to_hir_id(ty_param_def_id);
14051361
let param_name = tcx.hir().ty_param_name(param_hir_id);
14061362
self.one_bound_for_assoc_type(
14071363
|| {
1408-
traits::transitive_bounds_that_define_assoc_type(
1364+
traits::transitive_bounds(
14091365
tcx,
14101366
predicates.iter().filter_map(|(p, _)| {
14111367
p.to_opt_poly_trait_ref().map(|trait_ref| trait_ref.value)
14121368
}),
1413-
assoc_name,
14141369
)
1415-
.into_iter()
14161370
},
14171371
|| param_name.to_string(),
14181372
assoc_name,

‎compiler/rustc_typeck/src/check/fn_ctxt/mod.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use rustc_middle::ty::fold::TypeFoldable;
2020
use rustc_middle::ty::subst::GenericArgKind;
2121
use rustc_middle::ty::{self, Const, Ty, TyCtxt};
2222
use rustc_session::Session;
23-
use rustc_span::symbol::Ident;
2423
use rustc_span::{self, Span};
2524
use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode};
2625

@@ -184,12 +183,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
184183
}
185184
}
186185

187-
fn get_type_parameter_bounds(
188-
&self,
189-
_: Span,
190-
def_id: DefId,
191-
_: Ident,
192-
) -> ty::GenericPredicates<'tcx> {
186+
fn get_type_parameter_bounds(&self, _: Span, def_id: DefId) -> ty::GenericPredicates<'tcx> {
193187
let tcx = self.tcx;
194188
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
195189
let item_id = tcx.hir().ty_param_owner(hir_id);

‎compiler/rustc_typeck/src/collect.rs

Lines changed: 52 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// ignore-tidy-filelength
21
//! "Collection" is the process of determining the type and other external
32
//! details of each item in Rust. Collection is specifically concerned
43
//! with *inter-procedural* things -- for example, for a function
@@ -80,7 +79,6 @@ pub fn provide(providers: &mut Providers) {
8079
projection_ty_from_predicates,
8180
explicit_predicates_of,
8281
super_predicates_of,
83-
super_predicates_that_define_assoc_type,
8482
trait_explicit_predicates_and_bounds,
8583
type_param_predicates,
8684
trait_def,
@@ -312,17 +310,8 @@ impl AstConv<'tcx> for ItemCtxt<'tcx> {
312310
}
313311
}
314312

315-
fn get_type_parameter_bounds(
316-
&self,
317-
span: Span,
318-
def_id: DefId,
319-
assoc_name: Ident,
320-
) -> ty::GenericPredicates<'tcx> {
321-
self.tcx.at(span).type_param_predicates((
322-
self.item_def_id,
323-
def_id.expect_local(),
324-
assoc_name,
325-
))
313+
fn get_type_parameter_bounds(&self, span: Span, def_id: DefId) -> ty::GenericPredicates<'tcx> {
314+
self.tcx.at(span).type_param_predicates((self.item_def_id, def_id.expect_local()))
326315
}
327316

328317
fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> {
@@ -503,7 +492,7 @@ fn get_new_lifetime_name<'tcx>(
503492
/// `X: Foo` where `X` is the type parameter `def_id`.
504493
fn type_param_predicates(
505494
tcx: TyCtxt<'_>,
506-
(item_def_id, def_id, assoc_name): (DefId, LocalDefId, Ident),
495+
(item_def_id, def_id): (DefId, LocalDefId),
507496
) -> ty::GenericPredicates<'_> {
508497
use rustc_hir::*;
509498

@@ -528,7 +517,7 @@ fn type_param_predicates(
528517
let mut result = parent
529518
.map(|parent| {
530519
let icx = ItemCtxt::new(tcx, parent);
531-
icx.get_type_parameter_bounds(DUMMY_SP, def_id.to_def_id(), assoc_name)
520+
icx.get_type_parameter_bounds(DUMMY_SP, def_id.to_def_id())
532521
})
533522
.unwrap_or_default();
534523
let mut extend = None;
@@ -571,18 +560,12 @@ fn type_param_predicates(
571560

572561
let icx = ItemCtxt::new(tcx, item_def_id);
573562
let extra_predicates = extend.into_iter().chain(
574-
icx.type_parameter_bounds_in_generics(
575-
ast_generics,
576-
param_id,
577-
ty,
578-
OnlySelfBounds(true),
579-
Some(assoc_name),
580-
)
581-
.into_iter()
582-
.filter(|(predicate, _)| match predicate.skip_binders() {
583-
ty::PredicateAtom::Trait(data, _) => data.self_ty().is_param(index),
584-
_ => false,
585-
}),
563+
icx.type_parameter_bounds_in_generics(ast_generics, param_id, ty, OnlySelfBounds(true))
564+
.into_iter()
565+
.filter(|(predicate, _)| match predicate.skip_binders() {
566+
ty::PredicateAtom::Trait(data, _) => data.self_ty().is_param(index),
567+
_ => false,
568+
}),
586569
);
587570
result.predicates =
588571
tcx.arena.alloc_from_iter(result.predicates.iter().copied().chain(extra_predicates));
@@ -600,7 +583,6 @@ impl ItemCtxt<'tcx> {
600583
param_id: hir::HirId,
601584
ty: Ty<'tcx>,
602585
only_self_bounds: OnlySelfBounds,
603-
assoc_name: Option<Ident>,
604586
) -> Vec<(ty::Predicate<'tcx>, Span)> {
605587
let constness = self.default_constness_for_trait_bounds();
606588
let from_ty_params = ast_generics
@@ -611,10 +593,6 @@ impl ItemCtxt<'tcx> {
611593
_ => None,
612594
})
613595
.flat_map(|bounds| bounds.iter())
614-
.filter(|b| match assoc_name {
615-
Some(assoc_name) => self.bound_defines_assoc_item(b, assoc_name),
616-
None => true,
617-
})
618596
.flat_map(|b| predicates_from_bound(self, ty, b, constness));
619597

620598
let from_where_clauses = ast_generics
@@ -633,34 +611,12 @@ impl ItemCtxt<'tcx> {
633611
} else {
634612
None
635613
};
636-
bp.bounds
637-
.iter()
638-
.filter(|b| match assoc_name {
639-
Some(assoc_name) => self.bound_defines_assoc_item(b, assoc_name),
640-
None => true,
641-
})
642-
.filter_map(move |b| bt.map(|bt| (bt, b)))
614+
bp.bounds.iter().filter_map(move |b| bt.map(|bt| (bt, b)))
643615
})
644616
.flat_map(|(bt, b)| predicates_from_bound(self, bt, b, constness));
645617

646618
from_ty_params.chain(from_where_clauses).collect()
647619
}
648-
649-
fn bound_defines_assoc_item(&self, b: &hir::GenericBound<'_>, assoc_name: Ident) -> bool {
650-
debug!("bound_defines_assoc_item(b={:?}, assoc_name={:?})", b, assoc_name);
651-
652-
match b {
653-
hir::GenericBound::Trait(poly_trait_ref, _) => {
654-
let trait_ref = &poly_trait_ref.trait_ref;
655-
if let Some(trait_did) = trait_ref.trait_def_id() {
656-
self.tcx.trait_may_define_assoc_type(trait_did, assoc_name)
657-
} else {
658-
false
659-
}
660-
}
661-
_ => false,
662-
}
663-
}
664620
}
665621

666622
/// Tests whether this is the AST for a reference to the type
@@ -1029,90 +985,54 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::AdtDef {
1029985
/// the transitive super-predicates are converted.
1030986
fn super_predicates_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> ty::GenericPredicates<'_> {
1031987
debug!("super_predicates(trait_def_id={:?})", trait_def_id);
1032-
tcx.super_predicates_that_define_assoc_type((trait_def_id, None))
1033-
}
1034-
1035-
/// Ensures that the super-predicates of the trait with a `DefId`
1036-
/// of `trait_def_id` are converted and stored. This also ensures that
1037-
/// the transitive super-predicates are converted.
1038-
fn super_predicates_that_define_assoc_type(
1039-
tcx: TyCtxt<'_>,
1040-
(trait_def_id, assoc_name): (DefId, Option<Ident>),
1041-
) -> ty::GenericPredicates<'_> {
1042-
debug!(
1043-
"super_predicates_that_define_assoc_type(trait_def_id={:?}, assoc_name={:?})",
1044-
trait_def_id, assoc_name
1045-
);
1046-
if trait_def_id.is_local() {
1047-
debug!("super_predicates_that_define_assoc_type: local trait_def_id={:?}", trait_def_id);
1048-
let trait_hir_id = tcx.hir().local_def_id_to_hir_id(trait_def_id.expect_local());
1049-
1050-
let item = match tcx.hir().get(trait_hir_id) {
1051-
Node::Item(item) => item,
1052-
_ => bug!("trait_node_id {} is not an item", trait_hir_id),
1053-
};
988+
let trait_hir_id = tcx.hir().local_def_id_to_hir_id(trait_def_id.expect_local());
1054989

1055-
let (generics, bounds) = match item.kind {
1056-
hir::ItemKind::Trait(.., ref generics, ref supertraits, _) => (generics, supertraits),
1057-
hir::ItemKind::TraitAlias(ref generics, ref supertraits) => (generics, supertraits),
1058-
_ => span_bug!(item.span, "super_predicates invoked on non-trait"),
1059-
};
1060-
1061-
let icx = ItemCtxt::new(tcx, trait_def_id);
990+
let item = match tcx.hir().get(trait_hir_id) {
991+
Node::Item(item) => item,
992+
_ => bug!("trait_node_id {} is not an item", trait_hir_id),
993+
};
1062994

1063-
// Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`.
1064-
let self_param_ty = tcx.types.self_param;
1065-
let superbounds1 = if let Some(assoc_name) = assoc_name {
1066-
AstConv::compute_bounds_that_match_assoc_type(
1067-
&icx,
1068-
self_param_ty,
1069-
&bounds,
1070-
SizedByDefault::No,
1071-
item.span,
1072-
assoc_name,
1073-
)
1074-
} else {
1075-
AstConv::compute_bounds(&icx, self_param_ty, &bounds, SizedByDefault::No, item.span)
1076-
};
995+
let (generics, bounds) = match item.kind {
996+
hir::ItemKind::Trait(.., ref generics, ref supertraits, _) => (generics, supertraits),
997+
hir::ItemKind::TraitAlias(ref generics, ref supertraits) => (generics, supertraits),
998+
_ => span_bug!(item.span, "super_predicates invoked on non-trait"),
999+
};
10771000

1078-
let superbounds1 = superbounds1.predicates(tcx, self_param_ty);
1001+
let icx = ItemCtxt::new(tcx, trait_def_id);
1002+
1003+
// Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`.
1004+
let self_param_ty = tcx.types.self_param;
1005+
let superbounds1 =
1006+
AstConv::compute_bounds(&icx, self_param_ty, bounds, SizedByDefault::No, item.span);
1007+
1008+
let superbounds1 = superbounds1.predicates(tcx, self_param_ty);
1009+
1010+
// Convert any explicit superbounds in the where-clause,
1011+
// e.g., `trait Foo where Self: Bar`.
1012+
// In the case of trait aliases, however, we include all bounds in the where-clause,
1013+
// so e.g., `trait Foo = where u32: PartialEq<Self>` would include `u32: PartialEq<Self>`
1014+
// as one of its "superpredicates".
1015+
let is_trait_alias = tcx.is_trait_alias(trait_def_id);
1016+
let superbounds2 = icx.type_parameter_bounds_in_generics(
1017+
generics,
1018+
item.hir_id,
1019+
self_param_ty,
1020+
OnlySelfBounds(!is_trait_alias),
1021+
);
10791022

1080-
// Convert any explicit superbounds in the where-clause,
1081-
// e.g., `trait Foo where Self: Bar`.
1082-
// In the case of trait aliases, however, we include all bounds in the where-clause,
1083-
// so e.g., `trait Foo = where u32: PartialEq<Self>` would include `u32: PartialEq<Self>`
1084-
// as one of its "superpredicates".
1085-
let is_trait_alias = tcx.is_trait_alias(trait_def_id);
1086-
let superbounds2 = icx.type_parameter_bounds_in_generics(
1087-
generics,
1088-
item.hir_id,
1089-
self_param_ty,
1090-
OnlySelfBounds(!is_trait_alias),
1091-
assoc_name,
1092-
);
1023+
// Combine the two lists to form the complete set of superbounds:
1024+
let superbounds = &*tcx.arena.alloc_from_iter(superbounds1.into_iter().chain(superbounds2));
10931025

1094-
// Combine the two lists to form the complete set of superbounds:
1095-
let superbounds = &*tcx.arena.alloc_from_iter(superbounds1.into_iter().chain(superbounds2));
1096-
1097-
// Now require that immediate supertraits are converted,
1098-
// which will, in turn, reach indirect supertraits.
1099-
if assoc_name.is_none() {
1100-
// FIXME: move this into the `super_predicates_of` query
1101-
for &(pred, span) in superbounds {
1102-
debug!("superbound: {:?}", pred);
1103-
if let ty::PredicateAtom::Trait(bound, _) = pred.skip_binders() {
1104-
tcx.at(span).super_predicates_of(bound.def_id());
1105-
}
1106-
}
1026+
// Now require that immediate supertraits are converted,
1027+
// which will, in turn, reach indirect supertraits.
1028+
for &(pred, span) in superbounds {
1029+
debug!("superbound: {:?}", pred);
1030+
if let ty::PredicateAtom::Trait(bound, _) = pred.skip_binders() {
1031+
tcx.at(span).super_predicates_of(bound.def_id());
11071032
}
1108-
1109-
ty::GenericPredicates { parent: None, predicates: superbounds }
1110-
} else {
1111-
// if `assoc_name` is None, then the query should've been redirected to an
1112-
// external provider
1113-
assert!(assoc_name.is_some());
1114-
tcx.super_predicates_of(trait_def_id)
11151033
}
1034+
1035+
ty::GenericPredicates { parent: None, predicates: superbounds }
11161036
}
11171037

11181038
fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef {

‎compiler/rustc_typeck/src/collect/item_bounds.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn associated_type_bounds<'tcx>(
2828
let bounds = AstConv::compute_bounds(
2929
&ItemCtxt::new(tcx, assoc_item_def_id),
3030
item_ty,
31-
&bounds,
31+
bounds,
3232
SizedByDefault::Yes,
3333
span,
3434
);
@@ -68,7 +68,7 @@ fn opaque_type_bounds<'tcx>(
6868
let bounds = AstConv::compute_bounds(
6969
&ItemCtxt::new(tcx, opaque_def_id),
7070
item_ty,
71-
&bounds,
71+
bounds,
7272
SizedByDefault::Yes,
7373
span,
7474
)

‎src/test/incremental/cyclic-trait-hierarchy.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
// revisions: rpass1 cfail2
44

55
#[cfg(rpass1)]
6-
pub trait T2 {}
6+
pub trait T2 { }
77
#[cfg(cfail2)]
8-
pub trait T2: T1 {}
9-
//[cfail2]~^ ERROR cycle detected when computing the super predicates of `T2`
8+
pub trait T2: T1 { }
9+
//[cfail2]~^ ERROR cycle detected when computing the supertraits of `T2`
1010

11-
pub trait T1: T2 {}
11+
pub trait T1: T2 { }
1212

13-
fn main() {}
13+
fn main() { }

‎src/test/ui/associated-type-bounds/ambiguous-associated-type2.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎src/test/ui/associated-type-bounds/ambiguous-associated-type2.stderr

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎src/test/ui/associated-type-bounds/associated-item-through-where-clause.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎src/test/ui/associated-type-bounds/handle-predicates-that-can-define-assoc-type.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎src/test/ui/associated-type-bounds/missing-trait-bound-for-assoc-fails.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎src/test/ui/associated-type-bounds/missing-trait-bound-for-assoc-fails.stderr

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎src/test/ui/associated-type-bounds/super-trait-referencing-self.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎src/test/ui/associated-type-bounds/super-trait-referencing.rs

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎src/test/ui/associated-type-bounds/super-trait-where-referencing-self.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Example cycle where a bound on `T` uses a shorthand for `T`. This
2+
// creates a cycle because we have to know the bounds on `T` to figure
3+
// out what trait defines `Item`, but we can't know the bounds on `T`
4+
// without knowing how to handle `T::Item`.
5+
//
6+
// Note that in the future cases like this could perhaps become legal,
7+
// if we got more fine-grained about our cycle detection or changed
8+
// how we handle `T::Item` resolution.
9+
10+
use std::ops::Add;
11+
12+
// Preamble.
13+
trait Trait { type Item; }
14+
15+
struct A<T>
16+
where T : Trait,
17+
T : Add<T::Item>
18+
//~^ ERROR cycle detected
19+
{
20+
data: T
21+
}
22+
23+
fn main() {
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0391]: cycle detected when computing the bounds for type parameter `T`
2+
--> $DIR/cycle-projection-based-on-where-clause.rs:17:19
3+
|
4+
LL | T : Add<T::Item>
5+
| ^^^^^^^
6+
|
7+
= note: ...which again requires computing the bounds for type parameter `T`, completing the cycle
8+
note: cycle used when computing explicit predicates of `A`
9+
--> $DIR/cycle-projection-based-on-where-clause.rs:17:19
10+
|
11+
LL | T : Add<T::Item>
12+
| ^^^^^^^
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0391`.

‎src/test/ui/cycle-trait/cycle-trait-supertrait-direct.stderr

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
error[E0391]: cycle detected when computing the super predicates of `Chromosome`
2-
--> $DIR/cycle-trait-supertrait-direct.rs:3:1
3-
|
4-
LL | trait Chromosome: Chromosome {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
note: ...which requires computing the super traits of `Chromosome`...
1+
error[E0391]: cycle detected when computing the supertraits of `Chromosome`
82
--> $DIR/cycle-trait-supertrait-direct.rs:3:19
93
|
104
LL | trait Chromosome: Chromosome {
115
| ^^^^^^^^^^
12-
= note: ...which again requires computing the super predicates of `Chromosome`, completing the cycle
6+
|
7+
= note: ...which again requires computing the supertraits of `Chromosome`, completing the cycle
138
note: cycle used when collecting item types in top-level module
149
--> $DIR/cycle-trait-supertrait-direct.rs:3:1
1510
|

‎src/test/ui/cycle-trait/cycle-trait-supertrait-indirect.stderr

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
1-
error[E0391]: cycle detected when computing the super predicates of `B`
2-
--> $DIR/cycle-trait-supertrait-indirect.rs:7:1
3-
|
4-
LL | trait B: C {
5-
| ^^^^^^^^^^
6-
|
7-
note: ...which requires computing the super traits of `B`...
1+
error[E0391]: cycle detected when computing the supertraits of `B`
82
--> $DIR/cycle-trait-supertrait-indirect.rs:7:10
93
|
104
LL | trait B: C {
115
| ^
12-
note: ...which requires computing the super predicates of `C`...
13-
--> $DIR/cycle-trait-supertrait-indirect.rs:11:1
146
|
15-
LL | trait C: B { }
16-
| ^^^^^^^^^^
17-
note: ...which requires computing the super traits of `C`...
7+
note: ...which requires computing the supertraits of `C`...
188
--> $DIR/cycle-trait-supertrait-indirect.rs:11:10
199
|
2010
LL | trait C: B { }
2111
| ^
22-
= note: ...which again requires computing the super predicates of `B`, completing the cycle
23-
note: cycle used when computing the super traits of `A`
12+
= note: ...which again requires computing the supertraits of `B`, completing the cycle
13+
note: cycle used when computing the supertraits of `A`
2414
--> $DIR/cycle-trait-supertrait-indirect.rs:4:10
2515
|
2616
LL | trait A: B {

‎src/test/ui/issues/issue-12511.stderr

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1-
error[E0391]: cycle detected when computing the super predicates of `T1`
2-
--> $DIR/issue-12511.rs:1:1
3-
|
4-
LL | trait T1 : T2 {
5-
| ^^^^^^^^^^^^^
6-
|
7-
note: ...which requires computing the super traits of `T1`...
1+
error[E0391]: cycle detected when computing the supertraits of `T1`
82
--> $DIR/issue-12511.rs:1:12
93
|
104
LL | trait T1 : T2 {
115
| ^^
12-
note: ...which requires computing the super predicates of `T2`...
13-
--> $DIR/issue-12511.rs:5:1
146
|
15-
LL | trait T2 : T1 {
16-
| ^^^^^^^^^^^^^
17-
note: ...which requires computing the super traits of `T2`...
7+
note: ...which requires computing the supertraits of `T2`...
188
--> $DIR/issue-12511.rs:5:12
199
|
2010
LL | trait T2 : T1 {
2111
| ^^
22-
= note: ...which again requires computing the super predicates of `T1`, completing the cycle
12+
= note: ...which again requires computing the supertraits of `T1`, completing the cycle
2313
note: cycle used when collecting item types in top-level module
2414
--> $DIR/issue-12511.rs:1:1
2515
|

‎src/test/ui/issues/issue-20772.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
error[E0391]: cycle detected when computing the super traits of `T` with associated type name `Item`
1+
error[E0391]: cycle detected when computing the supertraits of `T`
22
--> $DIR/issue-20772.rs:1:1
33
|
44
LL | / trait T : Iterator<Item=Self::Item>
55
LL | |
66
LL | | {}
77
| |__^
88
|
9-
= note: ...which again requires computing the super traits of `T` with associated type name `Item`, completing the cycle
10-
note: cycle used when computing the super traits of `T`
9+
= note: ...which again requires computing the supertraits of `T`, completing the cycle
10+
note: cycle used when collecting item types in top-level module
1111
--> $DIR/issue-20772.rs:1:1
1212
|
1313
LL | / trait T : Iterator<Item=Self::Item>

‎src/test/ui/issues/issue-20825.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0391]: cycle detected when computing the super traits of `Processor` with associated type name `Input`
1+
error[E0391]: cycle detected when computing the supertraits of `Processor`
22
--> $DIR/issue-20825.rs:5:1
33
|
44
LL | pub trait Processor: Subscriber<Input = Self::Input> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: ...which again requires computing the super traits of `Processor` with associated type name `Input`, completing the cycle
8-
note: cycle used when computing the super traits of `Processor`
7+
= note: ...which again requires computing the supertraits of `Processor`, completing the cycle
8+
note: cycle used when collecting item types in top-level module
99
--> $DIR/issue-20825.rs:5:1
1010
|
1111
LL | pub trait Processor: Subscriber<Input = Self::Input> {

‎src/test/ui/issues/issue-22673.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// check-pass
2-
3-
trait Expr: PartialEq<Self::Item> {
1+
trait Expr : PartialEq<Self::Item> {
2+
//~^ ERROR: cycle detected
43
type Item;
54
}
65

‎src/test/ui/issues/issue-22673.stderr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0391]: cycle detected when computing the supertraits of `Expr`
2+
--> $DIR/issue-22673.rs:1:1
3+
|
4+
LL | trait Expr : PartialEq<Self::Item> {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: ...which again requires computing the supertraits of `Expr`, completing the cycle
8+
note: cycle used when collecting item types in top-level module
9+
--> $DIR/issue-22673.rs:1:1
10+
|
11+
LL | trait Expr : PartialEq<Self::Item> {
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0391`.

0 commit comments

Comments
 (0)
Please sign in to comment.