Skip to content

Commit 01af1ac

Browse files
committed
hir_analysis: add {Meta,Pointee}Sized bounds
Opting-out of `Sized` with `?Sized` is now equivalent to adding a `MetaSized` bound, and adding a `MetaSized` or `PointeeSized` bound is equivalent to removing the default `Sized` bound - this commit implements this change in `rustc_hir_analysis::hir_ty_lowering`. `MetaSized` is also added as a supertrait of all traits, as this is necessary to preserve backwards compatibility. Unfortunately, non-global where clauses being preferred over item bounds (where `PointeeSized` bounds would be proven) - which can result in errors when a `PointeeSized` supertrait/bound/predicate is added to some items. Rather than `PointeeSized` being a bound on everything, it can be the absence of a bound on everything, as `?Sized` was.
1 parent 6a24ff2 commit 01af1ac

File tree

15 files changed

+667
-161
lines changed

15 files changed

+667
-161
lines changed

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ fn associated_type_bounds<'tcx>(
4545
| PredicateFilter::SelfOnly
4646
| PredicateFilter::SelfTraitThatDefines(_)
4747
| PredicateFilter::SelfAndAssociatedTypeBounds => {
48-
icx.lowerer().add_sized_bound(&mut bounds, item_ty, hir_bounds, None, span);
48+
icx.lowerer().adjust_sizedness_params_and_assoc_types(
49+
&mut bounds,
50+
item_ty,
51+
hir_bounds,
52+
None,
53+
span,
54+
);
4955
}
5056
// `ConstIfConst` is only interested in `~const` bounds.
5157
PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
@@ -335,7 +341,13 @@ fn opaque_type_bounds<'tcx>(
335341
| PredicateFilter::SelfTraitThatDefines(_)
336342
| PredicateFilter::SelfAndAssociatedTypeBounds => {
337343
// Associated types are implicitly sized unless a `?Sized` bound is found
338-
icx.lowerer().add_sized_bound(&mut bounds, item_ty, hir_bounds, None, span);
344+
icx.lowerer().adjust_sizedness_params_and_assoc_types(
345+
&mut bounds,
346+
item_ty,
347+
hir_bounds,
348+
None,
349+
span,
350+
);
339351
}
340352
//`ConstIfConst` is only interested in `~const` bounds.
341353
PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,11 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
162162
.map(|t| ty::Binder::dummy(t.instantiate_identity()));
163163
}
164164
}
165-
166-
ItemKind::Trait(_, _, _, self_bounds, ..) | ItemKind::TraitAlias(_, self_bounds) => {
167-
is_trait = Some(self_bounds);
165+
ItemKind::Trait(is_auto, _, _, self_bounds, ..) => {
166+
is_trait = Some((is_auto, self_bounds));
167+
}
168+
ItemKind::TraitAlias(_, self_bounds) => {
169+
is_trait = Some((IsAuto::No, self_bounds));
168170
}
169171
_ => {}
170172
}
@@ -176,7 +178,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
176178
// and the explicit where-clauses, but to get the full set of predicates
177179
// on a trait we must also consider the bounds that follow the trait's name,
178180
// like `trait Foo: A + B + C`.
179-
if let Some(self_bounds) = is_trait {
181+
if let Some((is_auto, self_bounds)) = is_trait {
180182
let mut bounds = Vec::new();
181183
icx.lowerer().lower_bounds(
182184
tcx.types.self_param,
@@ -185,6 +187,18 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
185187
ty::List::empty(),
186188
PredicateFilter::All,
187189
);
190+
191+
// Don't add default supertraits to auto traits as it is not permitted to explicitly
192+
// relax it in the source code.
193+
if is_auto == IsAuto::No {
194+
icx.lowerer().adjust_sizedness_supertraits(
195+
&mut bounds,
196+
tcx.types.self_param,
197+
self_bounds,
198+
def_id,
199+
);
200+
}
201+
188202
predicates.extend(bounds);
189203
}
190204

@@ -210,7 +224,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
210224
let param_ty = icx.lowerer().lower_ty_param(param.hir_id);
211225
let mut bounds = Vec::new();
212226
// Params are implicitly sized unless a `?Sized` bound is found
213-
icx.lowerer().add_sized_bound(
227+
icx.lowerer().adjust_sizedness_params_and_assoc_types(
214228
&mut bounds,
215229
param_ty,
216230
&[],
@@ -268,6 +282,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
268282
bound_vars,
269283
PredicateFilter::All,
270284
);
285+
icx.lowerer().adjust_sizedness_predicates(&mut bounds, bound_pred.bounded_ty.span);
271286
predicates.extend(bounds);
272287
}
273288

@@ -624,6 +639,7 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
624639
let self_param_ty = tcx.types.self_param;
625640
let mut bounds = Vec::new();
626641
icx.lowerer().lower_bounds(self_param_ty, superbounds, &mut bounds, ty::List::empty(), filter);
642+
icx.lowerer().adjust_sizedness_predicates(&mut bounds, item.span);
627643

628644
let where_bounds_that_match =
629645
icx.probe_ty_param_bounds_in_generics(generics, item.owner_id.def_id, filter);
@@ -931,6 +947,7 @@ impl<'tcx> ItemCtxt<'tcx> {
931947
bound_vars,
932948
filter,
933949
);
950+
self.lowerer().adjust_sizedness_predicates(&mut bounds, predicate.bounded_ty.span);
934951
}
935952

936953
bounds
@@ -1010,6 +1027,7 @@ pub(super) fn const_conditions<'tcx>(
10101027
bound_vars,
10111028
PredicateFilter::ConstIfConst,
10121029
);
1030+
icx.lowerer().adjust_sizedness_predicates(&mut bounds, bound_pred.bounded_ty.span);
10131031
}
10141032
_ => {}
10151033
}
@@ -1030,6 +1048,7 @@ pub(super) fn const_conditions<'tcx>(
10301048
ty::List::empty(),
10311049
PredicateFilter::ConstIfConst,
10321050
);
1051+
icx.lowerer().adjust_sizedness_predicates(&mut bounds, DUMMY_SP);
10331052
}
10341053

10351054
ty::ConstConditions {

0 commit comments

Comments
 (0)