Skip to content

Commit d43da6f

Browse files
committed
trait_sel: {Meta,Pointee}Sized on Sized types
Introduce the `MetaSized` and `PointeeSized` traits as supertraits of `Sized` and initially implement it on everything that currently implements `Sized` to isolate any changes that simply adding the traits introduces.
1 parent d9ca9bd commit d43da6f

File tree

15 files changed

+253
-1
lines changed

15 files changed

+253
-1
lines changed

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ declare_features! (
237237
(internal, profiler_runtime, "1.18.0", None),
238238
/// Allows using `rustc_*` attributes (RFC 572).
239239
(internal, rustc_attrs, "1.0.0", None),
240+
/// Introduces a hierarchy of `Sized` traits (RFC 3729).
241+
(unstable, sized_hierarchy, "CURRENT_RUSTC_VERSION", None),
240242
/// Allows using the `#[stable]` and `#[unstable]` attributes.
241243
(internal, staged_api, "1.0.0", None),
242244
/// Added for testing unstable lints; perma-unstable.

compiler/rustc_hir/src/lang_items.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ pub fn extract(attrs: &[impl AttributeExt]) -> Option<(Symbol, Span)> {
165165
language_item_table! {
166166
// Variant name, Name, Getter method name, Target Generic requirements;
167167
Sized, sym::sized, sized_trait, Target::Trait, GenericRequirement::Exact(0);
168+
MetaSized, sym::meta_sized, meta_sized_trait, Target::Trait, GenericRequirement::Exact(0);
169+
PointeeSized, sym::pointee_sized, pointee_sized_trait, Target::Trait, GenericRequirement::Exact(0);
168170
Unsize, sym::unsize, unsize_trait, Target::Trait, GenericRequirement::Minimum(1);
169171
/// Trait injected by `#[derive(PartialEq)]`, (i.e. "Partial EQ").
170172
StructuralPeq, sym::structural_peq, structural_peq_trait, Target::Trait, GenericRequirement::None;

compiler/rustc_middle/src/ty/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,9 @@ bidirectional_lang_item_map! {
774774
FutureOutput,
775775
Iterator,
776776
Metadata,
777+
MetaSized,
777778
Option,
779+
PointeeSized,
778780
PointeeTrait,
779781
Poll,
780782
Sized,

compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,24 @@ where
212212
goal: Goal<I, Self>,
213213
) -> Result<Candidate<I>, NoSolution>;
214214

215+
/// A type is `MetaSized` if its tail component is `MetaSized`.
216+
///
217+
/// These components are given by built-in rules from
218+
/// [`structural_traits::instantiate_constituent_tys_for_sized_trait`].
219+
fn consider_builtin_meta_sized_candidate(
220+
ecx: &mut EvalCtxt<'_, D>,
221+
goal: Goal<I, Self>,
222+
) -> Result<Candidate<I>, NoSolution>;
223+
224+
/// A type is `PointeeSized` if its tail component is `PointeeSized`.
225+
///
226+
/// These components are given by built-in rules from
227+
/// [`structural_traits::instantiate_constituent_tys_for_sized_trait`].
228+
fn consider_builtin_pointee_sized_candidate(
229+
ecx: &mut EvalCtxt<'_, D>,
230+
goal: Goal<I, Self>,
231+
) -> Result<Candidate<I>, NoSolution>;
232+
215233
/// A type is `Copy` or `Clone` if its components are `Copy` or `Clone`.
216234
///
217235
/// These components are given by built-in rules from
@@ -467,6 +485,12 @@ where
467485
} else {
468486
match cx.as_lang_item(trait_def_id) {
469487
Some(TraitSolverLangItem::Sized) => G::consider_builtin_sized_candidate(self, goal),
488+
Some(TraitSolverLangItem::MetaSized) => {
489+
G::consider_builtin_meta_sized_candidate(self, goal)
490+
}
491+
Some(TraitSolverLangItem::PointeeSized) => {
492+
G::consider_builtin_pointee_sized_candidate(self, goal)
493+
}
470494
Some(TraitSolverLangItem::Copy | TraitSolverLangItem::Clone) => {
471495
G::consider_builtin_copy_clone_candidate(self, goal)
472496
}

compiler/rustc_next_trait_solver/src/solve/effect_goals.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,20 @@ where
205205
unreachable!("Sized is never const")
206206
}
207207

208+
fn consider_builtin_meta_sized_candidate(
209+
_ecx: &mut EvalCtxt<'_, D>,
210+
_goal: Goal<I, Self>,
211+
) -> Result<Candidate<I>, NoSolution> {
212+
unreachable!("MetaSized is never const")
213+
}
214+
215+
fn consider_builtin_pointee_sized_candidate(
216+
_ecx: &mut EvalCtxt<'_, D>,
217+
_goal: Goal<I, Self>,
218+
) -> Result<Candidate<I>, NoSolution> {
219+
unreachable!("PointeeSized is never const")
220+
}
221+
208222
fn consider_builtin_copy_clone_candidate(
209223
_ecx: &mut EvalCtxt<'_, D>,
210224
_goal: Goal<I, Self>,

compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,20 @@ where
420420
panic!("`Sized` does not have an associated type: {:?}", goal);
421421
}
422422

423+
fn consider_builtin_meta_sized_candidate(
424+
_ecx: &mut EvalCtxt<'_, D>,
425+
goal: Goal<I, Self>,
426+
) -> Result<Candidate<I>, NoSolution> {
427+
panic!("`MetaSized` does not have an associated type: {:?}", goal);
428+
}
429+
430+
fn consider_builtin_pointee_sized_candidate(
431+
_ecx: &mut EvalCtxt<'_, D>,
432+
goal: Goal<I, Self>,
433+
) -> Result<Candidate<I>, NoSolution> {
434+
panic!("`PointeeSized` does not have an associated type: {:?}", goal);
435+
}
436+
423437
fn consider_builtin_copy_clone_candidate(
424438
_ecx: &mut EvalCtxt<'_, D>,
425439
goal: Goal<I, Self>,

compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,36 @@ where
260260
)
261261
}
262262

263+
fn consider_builtin_meta_sized_candidate(
264+
ecx: &mut EvalCtxt<'_, D>,
265+
goal: Goal<I, Self>,
266+
) -> Result<Candidate<I>, NoSolution> {
267+
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
268+
return Err(NoSolution);
269+
}
270+
271+
ecx.probe_and_evaluate_goal_for_constituent_tys(
272+
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
273+
goal,
274+
structural_traits::instantiate_constituent_tys_for_sized_trait,
275+
)
276+
}
277+
278+
fn consider_builtin_pointee_sized_candidate(
279+
ecx: &mut EvalCtxt<'_, D>,
280+
goal: Goal<I, Self>,
281+
) -> Result<Candidate<I>, NoSolution> {
282+
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
283+
return Err(NoSolution);
284+
}
285+
286+
ecx.probe_and_evaluate_goal_for_constituent_tys(
287+
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
288+
goal,
289+
structural_traits::instantiate_constituent_tys_for_sized_trait,
290+
)
291+
}
292+
263293
fn consider_builtin_copy_clone_candidate(
264294
ecx: &mut EvalCtxt<'_, D>,
265295
goal: Goal<I, Self>,

compiler/rustc_span/src/symbol.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,7 @@ symbols! {
13551355
memtag,
13561356
message,
13571357
meta,
1358+
meta_sized,
13581359
metadata_type,
13591360
min_const_fn,
13601361
min_const_generics,
@@ -1613,6 +1614,7 @@ symbols! {
16131614
plugin_registrar,
16141615
plugins,
16151616
pointee,
1617+
pointee_sized,
16161618
pointee_trait,
16171619
pointer,
16181620
pointer_like,
@@ -2016,6 +2018,7 @@ symbols! {
20162018
size_of,
20172019
size_of_val,
20182020
sized,
2021+
sized_hierarchy,
20192022
skip,
20202023
slice,
20212024
slice_from_raw_parts,

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
8989
Some(LangItem::Sized) => {
9090
self.assemble_builtin_sized_candidate(obligation, &mut candidates);
9191
}
92+
Some(LangItem::MetaSized) => {
93+
self.assemble_builtin_sized_candidate(obligation, &mut candidates);
94+
}
95+
Some(LangItem::PointeeSized) => {
96+
self.assemble_builtin_sized_candidate(obligation, &mut candidates);
97+
}
9298
Some(LangItem::Unsize) => {
9399
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
94100
}

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
252252
let trait_def = obligation.predicate.def_id();
253253
let conditions = match tcx.as_lang_item(trait_def) {
254254
Some(LangItem::Sized) => self.sized_conditions(obligation),
255+
Some(LangItem::MetaSized) => self.sized_conditions(obligation),
256+
Some(LangItem::PointeeSized) => self.sized_conditions(obligation),
255257
Some(LangItem::Copy | LangItem::Clone) => self.copy_clone_conditions(obligation),
256258
Some(LangItem::FusedIterator) => self.fused_iterator_conditions(obligation),
257259
other => bug!("unexpected builtin trait {trait_def:?} ({other:?})"),

0 commit comments

Comments
 (0)