Skip to content

Commit e5d9716

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 67c2147 commit e5d9716

File tree

16 files changed

+267
-5
lines changed

16 files changed

+267
-5
lines changed

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ declare_features! (
234234
(internal, profiler_runtime, "1.18.0", None),
235235
/// Allows using `rustc_*` attributes (RFC 572).
236236
(internal, rustc_attrs, "1.0.0", None),
237+
/// Introduces a hierarchy of `Sized` traits (RFC 3729).
238+
(unstable, sized_hierarchy, "CURRENT_RUSTC_VERSION", None),
237239
/// Allows using the `#[stable]` and `#[unstable]` attributes.
238240
(internal, staged_api, "1.0.0", None),
239241
/// 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::metasized, metasized_trait, Target::Trait, GenericRequirement::Exact(0);
169+
PointeeSized, sym::pointeesized, pointeesized_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/traits/select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub enum SelectionCandidate<'tcx> {
9898
/// A builtin implementation for some specific traits, used in cases
9999
/// where we cannot rely an ordinary library implementations.
100100
///
101-
/// The most notable examples are `sized`, `Copy` and `Clone`. This is also
101+
/// The most notable examples are `Sized`, `Copy` and `Clone`. This is also
102102
/// used for the `DiscriminantKind` and `Pointee` trait, both of which have
103103
/// an associated type.
104104
BuiltinCandidate {

compiler/rustc_middle/src/ty/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,9 @@ bidirectional_lang_item_map! {
715715
FutureOutput,
716716
Iterator,
717717
Metadata,
718+
MetaSized,
718719
Option,
720+
PointeeSized,
719721
PointeeTrait,
720722
Poll,
721723
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
@@ -155,6 +155,24 @@ where
155155
goal: Goal<I, Self>,
156156
) -> Result<Candidate<I>, NoSolution>;
157157

158+
/// A type is `MetaSized` if its tail component is `MetaSized`.
159+
///
160+
/// These components are given by built-in rules from
161+
/// [`structural_traits::instantiate_constituent_tys_for_sized_trait`].
162+
fn consider_builtin_metasized_candidate(
163+
ecx: &mut EvalCtxt<'_, D>,
164+
goal: Goal<I, Self>,
165+
) -> Result<Candidate<I>, NoSolution>;
166+
167+
/// A type is `PointeeSized` if its tail component is `PointeeSized`.
168+
///
169+
/// These components are given by built-in rules from
170+
/// [`structural_traits::instantiate_constituent_tys_for_sized_trait`].
171+
fn consider_builtin_pointeesized_candidate(
172+
ecx: &mut EvalCtxt<'_, D>,
173+
goal: Goal<I, Self>,
174+
) -> Result<Candidate<I>, NoSolution>;
175+
158176
/// A type is `Copy` or `Clone` if its components are `Copy` or `Clone`.
159177
///
160178
/// These components are given by built-in rules from
@@ -397,6 +415,12 @@ where
397415
} else {
398416
match cx.as_lang_item(trait_def_id) {
399417
Some(TraitSolverLangItem::Sized) => G::consider_builtin_sized_candidate(self, goal),
418+
Some(TraitSolverLangItem::MetaSized) => {
419+
G::consider_builtin_metasized_candidate(self, goal)
420+
}
421+
Some(TraitSolverLangItem::PointeeSized) => {
422+
G::consider_builtin_pointeesized_candidate(self, goal)
423+
}
400424
Some(TraitSolverLangItem::Copy | TraitSolverLangItem::Clone) => {
401425
G::consider_builtin_copy_clone_candidate(self, goal)
402426
}

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_metasized_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_pointeesized_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
@@ -319,6 +319,20 @@ where
319319
panic!("`Sized` does not have an associated type: {:?}", goal);
320320
}
321321

322+
fn consider_builtin_metasized_candidate(
323+
_ecx: &mut EvalCtxt<'_, D>,
324+
goal: Goal<I, Self>,
325+
) -> Result<Candidate<I>, NoSolution> {
326+
panic!("`MetaSized` does not have an associated type: {:?}", goal);
327+
}
328+
329+
fn consider_builtin_pointeesized_candidate(
330+
_ecx: &mut EvalCtxt<'_, D>,
331+
goal: Goal<I, Self>,
332+
) -> Result<Candidate<I>, NoSolution> {
333+
panic!("`PointeeSized` does not have an associated type: {:?}", goal);
334+
}
335+
322336
fn consider_builtin_copy_clone_candidate(
323337
_ecx: &mut EvalCtxt<'_, D>,
324338
goal: Goal<I, Self>,

compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_type_ir::visit::TypeVisitableExt as _;
99
use rustc_type_ir::{
1010
self as ty, Interner, Movability, TraitPredicate, TypingMode, Upcast as _, elaborate,
1111
};
12-
use tracing::{instrument, trace};
12+
use tracing::{debug, instrument, trace};
1313

1414
use crate::delegate::SolverDelegate;
1515
use crate::solve::assembly::structural_traits::{self, AsyncCallableRelevantTypes};
@@ -232,6 +232,36 @@ where
232232
)
233233
}
234234

235+
fn consider_builtin_metasized_candidate(
236+
ecx: &mut EvalCtxt<'_, D>,
237+
goal: Goal<I, Self>,
238+
) -> Result<Candidate<I>, NoSolution> {
239+
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
240+
return Err(NoSolution);
241+
}
242+
243+
ecx.probe_and_evaluate_goal_for_constituent_tys(
244+
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
245+
goal,
246+
structural_traits::instantiate_constituent_tys_for_sized_trait,
247+
)
248+
}
249+
250+
fn consider_builtin_pointeesized_candidate(
251+
ecx: &mut EvalCtxt<'_, D>,
252+
goal: Goal<I, Self>,
253+
) -> Result<Candidate<I>, NoSolution> {
254+
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
255+
return Err(NoSolution);
256+
}
257+
258+
ecx.probe_and_evaluate_goal_for_constituent_tys(
259+
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
260+
goal,
261+
structural_traits::instantiate_constituent_tys_for_sized_trait,
262+
)
263+
}
264+
235265
fn consider_builtin_copy_clone_candidate(
236266
ecx: &mut EvalCtxt<'_, D>,
237267
goal: Goal<I, Self>,
@@ -1378,6 +1408,7 @@ where
13781408
goal: Goal<I, TraitPredicate<I>>,
13791409
) -> Result<(CanonicalResponse<I>, Option<TraitGoalProvenVia>), NoSolution> {
13801410
let candidates = self.assemble_and_evaluate_candidates(goal);
1411+
debug!(?candidates);
13811412
self.merge_trait_candidates(goal, candidates)
13821413
}
13831414
}

compiler/rustc_span/src/symbol.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,7 @@ symbols! {
12881288
message,
12891289
meta,
12901290
metadata_type,
1291+
metasized,
12911292
min_align_of,
12921293
min_align_of_val,
12931294
min_const_fn,
@@ -1535,6 +1536,7 @@ symbols! {
15351536
plugins,
15361537
pointee,
15371538
pointee_trait,
1539+
pointeesized,
15381540
pointer,
15391541
pointer_like,
15401542
poll,
@@ -1929,6 +1931,7 @@ symbols! {
19291931
size_of,
19301932
size_of_val,
19311933
sized,
1934+
sized_hierarchy,
19321935
skip,
19331936
slice,
19341937
slice_from_raw_parts,

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,18 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
8888
} else if tcx.is_lang_item(def_id, LangItem::Sized) {
8989
// Sized is never implementable by end-users, it is
9090
// always automatically computed.
91-
let sized_conditions = self.sized_conditions(obligation);
92-
self.assemble_builtin_bound_candidates(sized_conditions, &mut candidates);
91+
let conditions = self.sized_conditions(obligation);
92+
self.assemble_builtin_bound_candidates(conditions, &mut candidates);
93+
} else if tcx.is_lang_item(def_id, LangItem::MetaSized) {
94+
// MetaSized is never implementable by end-users, it is
95+
// always automatically computed.
96+
let conditions = self.sized_conditions(obligation);
97+
self.assemble_builtin_bound_candidates(conditions, &mut candidates);
98+
} else if tcx.is_lang_item(def_id, LangItem::PointeeSized) {
99+
// PointeeSized is never implementable by end-users, it is
100+
// always automatically computed.
101+
let conditions = self.sized_conditions(obligation);
102+
self.assemble_builtin_bound_candidates(conditions, &mut candidates);
93103
} else if tcx.is_lang_item(def_id, LangItem::Unsize) {
94104
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
95105
} else if tcx.is_lang_item(def_id, LangItem::Destruct) {

0 commit comments

Comments
 (0)