Skip to content

Commit f435972

Browse files
committed
Auto merge of #142712 - davidtwco:sized-hierarchy-missing-default-bounds, r=lcnr
hir_analysis: add missing sizedness bounds Depends on #144064 Default sizedness bounds were not being added to `explicit_super_predicates_of` and `explicit_implied_predicates_of` which meant that a trait bound added to a associated type projection would be missing the implied predicate of the default sizedness supertrait of that trait. An unexpected consequence of this change was that the check for multiple principals was now finding an additional `MetaSized` principal when eagerly expanding trait aliases - which is fixed by skipping `MetaSized` when elaborating trait aliases in lowering `dyn TraitAlias`.
2 parents 2aaa62b + 82a4049 commit f435972

File tree

13 files changed

+215
-194
lines changed

13 files changed

+215
-194
lines changed

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use tracing::{debug, instrument};
1212

1313
use super::ItemCtxt;
1414
use super::predicates_of::assert_only_contains_predicates_from;
15-
use crate::hir_ty_lowering::{HirTyLowerer, OverlappingAsssocItemConstraints, PredicateFilter};
15+
use crate::hir_ty_lowering::{
16+
HirTyLowerer, ImpliedBoundsContext, OverlappingAsssocItemConstraints, PredicateFilter,
17+
};
1618

1719
/// For associated types we include both bounds written on the type
1820
/// (`type X: Trait`) and predicates from the trait: `where Self::X: Trait`.
@@ -52,15 +54,20 @@ fn associated_type_bounds<'tcx>(
5254
| PredicateFilter::SelfTraitThatDefines(_)
5355
| PredicateFilter::SelfAndAssociatedTypeBounds => {
5456
// Implicit bounds are added to associated types unless a `?Trait` bound is found.
55-
icx.lowerer().add_sizedness_bounds(
57+
icx.lowerer().add_implicit_sizedness_bounds(
58+
&mut bounds,
59+
item_ty,
60+
hir_bounds,
61+
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
62+
span,
63+
);
64+
icx.lowerer().add_default_traits(
5665
&mut bounds,
5766
item_ty,
5867
hir_bounds,
59-
None,
60-
None,
68+
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
6169
span,
6270
);
63-
icx.lowerer().add_default_traits(&mut bounds, item_ty, hir_bounds, None, span);
6471

6572
// Also collect `where Self::Assoc: Trait` from the parent trait's where clauses.
6673
let trait_def_id = tcx.local_parent(assoc_item_def_id);
@@ -372,15 +379,20 @@ fn opaque_type_bounds<'tcx>(
372379
| PredicateFilter::SelfOnly
373380
| PredicateFilter::SelfTraitThatDefines(_)
374381
| PredicateFilter::SelfAndAssociatedTypeBounds => {
375-
icx.lowerer().add_sizedness_bounds(
382+
icx.lowerer().add_implicit_sizedness_bounds(
383+
&mut bounds,
384+
item_ty,
385+
hir_bounds,
386+
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
387+
span,
388+
);
389+
icx.lowerer().add_default_traits(
376390
&mut bounds,
377391
item_ty,
378392
hir_bounds,
379-
None,
380-
None,
393+
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
381394
span,
382395
);
383-
icx.lowerer().add_default_traits(&mut bounds, item_ty, hir_bounds, None, span);
384396
}
385397
//`ConstIfConst` is only interested in `[const]` bounds.
386398
PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use crate::collect::ItemCtxt;
1919
use crate::constrained_generic_params as cgp;
2020
use crate::delegation::inherit_predicates_for_delegation_item;
2121
use crate::hir_ty_lowering::{
22-
HirTyLowerer, OverlappingAsssocItemConstraints, PredicateFilter, RegionInferReason,
22+
HirTyLowerer, ImpliedBoundsContext, OverlappingAsssocItemConstraints, PredicateFilter,
23+
RegionInferReason,
2324
};
2425

2526
/// Returns a list of all type predicates (explicit and implicit) for the definition with
@@ -189,19 +190,18 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
189190
PredicateFilter::All,
190191
OverlappingAsssocItemConstraints::Allowed,
191192
);
192-
icx.lowerer().add_sizedness_bounds(
193+
icx.lowerer().add_implicit_sizedness_bounds(
193194
&mut bounds,
194195
tcx.types.self_param,
195196
self_bounds,
196-
None,
197-
Some(def_id),
197+
ImpliedBoundsContext::TraitDef(def_id),
198198
span,
199199
);
200-
icx.lowerer().add_default_super_traits(
201-
def_id,
200+
icx.lowerer().add_default_traits(
202201
&mut bounds,
202+
tcx.types.self_param,
203203
self_bounds,
204-
hir_generics,
204+
ImpliedBoundsContext::TraitDef(def_id),
205205
span,
206206
);
207207
predicates.extend(bounds);
@@ -229,19 +229,18 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
229229
let param_ty = icx.lowerer().lower_ty_param(param.hir_id);
230230
let mut bounds = Vec::new();
231231
// Implicit bounds are added to type params unless a `?Trait` bound is found
232-
icx.lowerer().add_sizedness_bounds(
232+
icx.lowerer().add_implicit_sizedness_bounds(
233233
&mut bounds,
234234
param_ty,
235235
&[],
236-
Some((param.def_id, hir_generics.predicates)),
237-
None,
236+
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
238237
param.span,
239238
);
240239
icx.lowerer().add_default_traits(
241240
&mut bounds,
242241
param_ty,
243242
&[],
244-
Some((param.def_id, hir_generics.predicates)),
243+
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
245244
param.span,
246245
);
247246
trace!(?bounds);
@@ -676,11 +675,18 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
676675
| PredicateFilter::SelfOnly
677676
| PredicateFilter::SelfTraitThatDefines(_)
678677
| PredicateFilter::SelfAndAssociatedTypeBounds => {
679-
icx.lowerer().add_default_super_traits(
680-
trait_def_id,
678+
icx.lowerer().add_implicit_sizedness_bounds(
679+
&mut bounds,
680+
self_param_ty,
681+
superbounds,
682+
ImpliedBoundsContext::TraitDef(trait_def_id),
683+
item.span,
684+
);
685+
icx.lowerer().add_default_traits(
681686
&mut bounds,
687+
self_param_ty,
682688
superbounds,
683-
generics,
689+
ImpliedBoundsContext::TraitDef(trait_def_id),
684690
item.span,
685691
);
686692
}

0 commit comments

Comments
 (0)