Skip to content

Commit 8ea71f2

Browse files
Do not consider associated type bounds for super_predicates_that_define_assoc_type
1 parent 0bcfff4 commit 8ea71f2

File tree

5 files changed

+94
-16
lines changed

5 files changed

+94
-16
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ use std::slice;
5656
#[derive(Debug)]
5757
pub struct PathSeg(pub DefId, pub usize);
5858

59+
#[derive(Copy, Clone, Debug)]
60+
pub struct OnlySelfBounds(pub bool);
61+
5962
pub trait AstConv<'tcx> {
6063
fn tcx(&self) -> TyCtxt<'tcx>;
6164

@@ -670,6 +673,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
670673
args: &GenericArgs<'_>,
671674
infer_args: bool,
672675
self_ty: Ty<'tcx>,
676+
only_self_bounds: OnlySelfBounds,
673677
) -> GenericArgCountResult {
674678
let (substs, arg_count) = self.create_substs_for_ast_path(
675679
trait_ref_span,
@@ -706,6 +710,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
706710
&mut dup_bindings,
707711
binding_span.unwrap_or(binding.span),
708712
constness,
713+
only_self_bounds,
709714
);
710715
// Okay to ignore `Err` because of `ErrorGuaranteed` (see above).
711716
}
@@ -741,6 +746,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
741746
self_ty: Ty<'tcx>,
742747
bounds: &mut Bounds<'tcx>,
743748
speculative: bool,
749+
only_self_bounds: OnlySelfBounds,
744750
) -> GenericArgCountResult {
745751
let hir_id = trait_ref.hir_ref_id;
746752
let binding_span = None;
@@ -766,6 +772,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
766772
args,
767773
infer_args,
768774
self_ty,
775+
only_self_bounds,
769776
)
770777
}
771778

@@ -777,6 +784,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
777784
args: &GenericArgs<'_>,
778785
self_ty: Ty<'tcx>,
779786
bounds: &mut Bounds<'tcx>,
787+
only_self_bounds: OnlySelfBounds,
780788
) {
781789
let binding_span = Some(span);
782790
let constness = ty::BoundConstness::NotConst;
@@ -799,6 +807,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
799807
args,
800808
infer_args,
801809
self_ty,
810+
only_self_bounds,
802811
);
803812
}
804813

@@ -947,6 +956,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
947956
ast_bounds: I,
948957
bounds: &mut Bounds<'tcx>,
949958
bound_vars: &'tcx ty::List<ty::BoundVariableKind>,
959+
only_self_bounds: OnlySelfBounds,
950960
) {
951961
for ast_bound in ast_bounds {
952962
match ast_bound {
@@ -964,11 +974,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
964974
param_ty,
965975
bounds,
966976
false,
977+
only_self_bounds,
967978
);
968979
}
969980
&hir::GenericBound::LangItemTrait(lang_item, span, hir_id, args) => {
970981
self.instantiate_lang_item_trait_ref(
971-
lang_item, span, hir_id, args, param_ty, bounds,
982+
lang_item,
983+
span,
984+
hir_id,
985+
args,
986+
param_ty,
987+
bounds,
988+
only_self_bounds,
972989
);
973990
}
974991
hir::GenericBound::Outlives(lifetime) => {
@@ -1006,9 +1023,16 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10061023
&self,
10071024
param_ty: Ty<'tcx>,
10081025
ast_bounds: &[hir::GenericBound<'_>],
1026+
only_self_bounds: OnlySelfBounds,
10091027
) -> Bounds<'tcx> {
10101028
let mut bounds = Bounds::default();
1011-
self.add_bounds(param_ty, ast_bounds.iter(), &mut bounds, ty::List::empty());
1029+
self.add_bounds(
1030+
param_ty,
1031+
ast_bounds.iter(),
1032+
&mut bounds,
1033+
ty::List::empty(),
1034+
only_self_bounds,
1035+
);
10121036
debug!(?bounds);
10131037

10141038
bounds
@@ -1034,7 +1058,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10341058
}
10351059

10361060
let mut bounds = Bounds::default();
1037-
self.add_bounds(param_ty, result.iter(), &mut bounds, ty::List::empty());
1061+
self.add_bounds(
1062+
param_ty,
1063+
result.iter(),
1064+
&mut bounds,
1065+
ty::List::empty(),
1066+
OnlySelfBounds(true),
1067+
);
10381068
debug!(?bounds);
10391069

10401070
bounds
@@ -1057,6 +1087,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10571087
dup_bindings: &mut FxHashMap<DefId, Span>,
10581088
path_span: Span,
10591089
constness: ty::BoundConstness,
1090+
only_self_bounds: OnlySelfBounds,
10601091
) -> Result<(), ErrorGuaranteed> {
10611092
// Given something like `U: SomeTrait<T = X>`, we want to produce a
10621093
// predicate like `<U as SomeTrait>::T = X`. This is somewhat
@@ -1356,8 +1387,20 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
13561387
//
13571388
// Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty`
13581389
// parameter to have a skipped binder.
1359-
let param_ty = tcx.mk_alias(ty::Projection, projection_ty.skip_binder());
1360-
self.add_bounds(param_ty, ast_bounds.iter(), bounds, projection_ty.bound_vars());
1390+
//
1391+
// NOTE: If `only_self_bounds` is true, do NOT expand this associated
1392+
// type bound into a trait predicate, since we only want to add predicates
1393+
// for the `Self` type.
1394+
if !only_self_bounds.0 {
1395+
let param_ty = tcx.mk_alias(ty::Projection, projection_ty.skip_binder());
1396+
self.add_bounds(
1397+
param_ty,
1398+
ast_bounds.iter(),
1399+
bounds,
1400+
projection_ty.bound_vars(),
1401+
only_self_bounds,
1402+
);
1403+
}
13611404
}
13621405
}
13631406
Ok(())
@@ -1398,6 +1441,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
13981441
dummy_self,
13991442
&mut bounds,
14001443
false,
1444+
// FIXME: This should be `true`, but we don't really handle
1445+
// associated type bounds or type aliases in objects in a way
1446+
// that makes this meaningful, I think.
1447+
OnlySelfBounds(false),
14011448
) {
14021449
potential_assoc_types.extend(cur_potential_assoc_types);
14031450
}

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::ItemCtxt;
2-
use crate::astconv::AstConv;
2+
use crate::astconv::{AstConv, OnlySelfBounds};
33
use rustc_hir as hir;
44
use rustc_infer::traits::util;
55
use rustc_middle::ty::subst::InternalSubsts;
@@ -26,7 +26,7 @@ fn associated_type_bounds<'tcx>(
2626
);
2727

2828
let icx = ItemCtxt::new(tcx, assoc_item_def_id);
29-
let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds);
29+
let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds, OnlySelfBounds(false));
3030
// Associated types are implicitly sized unless a `?Sized` bound is found
3131
icx.astconv().add_implicitly_sized(&mut bounds, item_ty, ast_bounds, None, span);
3232

@@ -67,7 +67,7 @@ fn opaque_type_bounds<'tcx>(
6767
) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
6868
ty::print::with_no_queries!({
6969
let icx = ItemCtxt::new(tcx, opaque_def_id);
70-
let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds);
70+
let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds, OnlySelfBounds(false));
7171
// Opaque types are implicitly sized unless a `?Sized` bound is found
7272
icx.astconv().add_implicitly_sized(&mut bounds, item_ty, ast_bounds, None, span);
7373
debug!(?bounds);

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::astconv::AstConv;
1+
use crate::astconv::{AstConv, OnlySelfBounds};
22
use crate::bounds::Bounds;
33
use crate::collect::ItemCtxt;
44
use crate::constrained_generic_params as cgp;
@@ -14,9 +14,6 @@ use rustc_middle::ty::{GenericPredicates, ToPredicate};
1414
use rustc_span::symbol::{sym, Ident};
1515
use rustc_span::{Span, DUMMY_SP};
1616

17-
#[derive(Debug)]
18-
struct OnlySelfBounds(bool);
19-
2017
/// Returns a list of all type predicates (explicit and implicit) for the definition with
2118
/// ID `def_id`. This includes all predicates returned by `predicates_defined_on`, plus
2219
/// `Self: Trait` predicates for traits.
@@ -225,7 +222,13 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
225222
}
226223

227224
let mut bounds = Bounds::default();
228-
icx.astconv().add_bounds(ty, bound_pred.bounds.iter(), &mut bounds, bound_vars);
225+
icx.astconv().add_bounds(
226+
ty,
227+
bound_pred.bounds.iter(),
228+
&mut bounds,
229+
bound_vars,
230+
OnlySelfBounds(false),
231+
);
229232
predicates.extend(bounds.predicates());
230233
}
231234

@@ -608,7 +611,7 @@ pub(super) fn implied_predicates_with_filter(
608611
let (superbounds, where_bounds_that_match) = match filter {
609612
PredicateFilter::All => (
610613
// Convert the bounds that follow the colon (or equal in trait aliases)
611-
icx.astconv().compute_bounds(self_param_ty, bounds),
614+
icx.astconv().compute_bounds(self_param_ty, bounds, OnlySelfBounds(false)),
612615
// Also include all where clause bounds
613616
icx.type_parameter_bounds_in_generics(
614617
generics,
@@ -620,7 +623,7 @@ pub(super) fn implied_predicates_with_filter(
620623
),
621624
PredicateFilter::SelfOnly => (
622625
// Convert the bounds that follow the colon (or equal in trait aliases)
623-
icx.astconv().compute_bounds(self_param_ty, bounds),
626+
icx.astconv().compute_bounds(self_param_ty, bounds, OnlySelfBounds(true)),
624627
// Include where clause bounds for `Self`
625628
icx.type_parameter_bounds_in_generics(
626629
generics,
@@ -798,6 +801,7 @@ impl<'tcx> ItemCtxt<'tcx> {
798801
}),
799802
&mut bounds,
800803
bound_vars,
804+
only_self_bounds,
801805
);
802806
}
803807

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode,
116116

117117
use std::ops::Not;
118118

119-
use astconv::AstConv;
119+
use astconv::{AstConv, OnlySelfBounds};
120120
use bounds::Bounds;
121121

122122
fluent_messages! { "../messages.ftl" }
@@ -531,6 +531,7 @@ pub fn hir_trait_to_predicates<'tcx>(
531531
self_ty,
532532
&mut bounds,
533533
true,
534+
OnlySelfBounds(false),
534535
);
535536

536537
bounds
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// check-pass
2+
3+
// Make sure that we don't look into associated type bounds when looking for
4+
// supertraits that define an associated type. Fixes #76593.
5+
6+
#![feature(associated_type_bounds)]
7+
8+
trait Load: Sized {
9+
type Blob;
10+
}
11+
12+
trait Primitive: Load<Blob = Self> {}
13+
14+
trait BlobPtr: Primitive {}
15+
16+
trait CleanPtr: Load<Blob: BlobPtr> {
17+
fn to_blob(&self) -> Self::Blob;
18+
}
19+
20+
impl Load for () {
21+
type Blob = Self;
22+
}
23+
impl Primitive for () {}
24+
impl BlobPtr for () {}
25+
26+
fn main() {}

0 commit comments

Comments
 (0)