Skip to content

Commit 86ab2b6

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 f0b84b8 commit 86ab2b6

File tree

20 files changed

+798
-207
lines changed

20 files changed

+798
-207
lines changed

compiler/rustc_hir_analysis/src/check/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub use check::{check_abi, check_abi_fn_ptr, check_custom_abi};
7676
use rustc_abi::{ExternAbi, VariantIdx};
7777
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
7878
use rustc_errors::{Diag, ErrorGuaranteed, pluralize, struct_span_code_err};
79+
use rustc_hir::LangItem;
7980
use rustc_hir::def_id::{DefId, LocalDefId};
8081
use rustc_hir::intravisit::Visitor;
8182
use rustc_index::bit_set::DenseBitSet;
@@ -331,7 +332,7 @@ fn bounds_from_generic_predicates<'tcx>(
331332
ty::ClauseKind::Trait(trait_predicate) => {
332333
let entry = types.entry(trait_predicate.self_ty()).or_default();
333334
let def_id = trait_predicate.def_id();
334-
if !tcx.is_default_trait(def_id) {
335+
if !tcx.is_default_trait(def_id) && !tcx.is_lang_item(def_id, LangItem::Sized) {
335336
// Do not add that restriction to the list if it is a positive requirement.
336337
entry.push(trait_predicate.def_id());
337338
}

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ fn associated_type_bounds<'tcx>(
4444
| PredicateFilter::SelfOnly
4545
| PredicateFilter::SelfTraitThatDefines(_)
4646
| PredicateFilter::SelfAndAssociatedTypeBounds => {
47+
icx.lowerer().add_sizedness_bounds(
48+
&mut bounds,
49+
item_ty,
50+
hir_bounds,
51+
None,
52+
None,
53+
span,
54+
);
4755
icx.lowerer().add_default_traits(&mut bounds, item_ty, hir_bounds, None, span);
4856
}
4957
// `ConstIfConst` is only interested in `~const` bounds.
@@ -333,6 +341,14 @@ fn opaque_type_bounds<'tcx>(
333341
| PredicateFilter::SelfOnly
334342
| PredicateFilter::SelfTraitThatDefines(_)
335343
| PredicateFilter::SelfAndAssociatedTypeBounds => {
344+
icx.lowerer().add_sizedness_bounds(
345+
&mut bounds,
346+
item_ty,
347+
hir_bounds,
348+
None,
349+
None,
350+
span,
351+
);
336352
icx.lowerer().add_default_traits(&mut bounds, item_ty, hir_bounds, None, span);
337353
}
338354
//`ConstIfConst` is only interested in `~const` bounds.

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
162162
.map(|t| ty::Binder::dummy(t.instantiate_identity()));
163163
}
164164
}
165-
166165
ItemKind::Trait(_, _, _, _, self_bounds, ..)
167166
| ItemKind::TraitAlias(_, _, self_bounds) => {
168167
is_trait = Some((self_bounds, item.span));
@@ -183,21 +182,29 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
183182
// and the explicit where-clauses, but to get the full set of predicates
184183
// on a trait we must also consider the bounds that follow the trait's name,
185184
// like `trait Foo: A + B + C`.
186-
if let Some(self_bounds) = is_trait {
185+
if let Some((self_bounds, span)) = is_trait {
187186
let mut bounds = Vec::new();
188187
icx.lowerer().lower_bounds(
189188
tcx.types.self_param,
190-
self_bounds.0,
189+
self_bounds,
191190
&mut bounds,
192191
ty::List::empty(),
193192
PredicateFilter::All,
194193
);
194+
icx.lowerer().add_sizedness_bounds(
195+
&mut bounds,
196+
tcx.types.self_param,
197+
self_bounds,
198+
None,
199+
Some(def_id),
200+
span,
201+
);
195202
icx.lowerer().add_default_super_traits(
196203
def_id,
197204
&mut bounds,
198-
self_bounds.0,
205+
self_bounds,
199206
hir_generics,
200-
self_bounds.1,
207+
span,
201208
);
202209
predicates.extend(bounds);
203210
}
@@ -224,6 +231,14 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
224231
let param_ty = icx.lowerer().lower_ty_param(param.hir_id);
225232
let mut bounds = Vec::new();
226233
// Implicit bounds are added to type params unless a `?Trait` bound is found
234+
icx.lowerer().add_sizedness_bounds(
235+
&mut bounds,
236+
param_ty,
237+
&[],
238+
Some((param.def_id, hir_generics.predicates)),
239+
None,
240+
param.span,
241+
);
227242
icx.lowerer().add_default_traits(
228243
&mut bounds,
229244
param_ty,

0 commit comments

Comments
 (0)