Skip to content

Sized Hierarchy: Part I #137944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open

Conversation

davidtwco
Copy link
Member

@davidtwco davidtwco commented Mar 3, 2025

This patch implements the non-const parts of rust-lang/rfcs#3729. It introduces two new traits to the standard library, MetaSized and PointeeSized. See the RFC for the rationale behind these traits and to discuss whether this change makes sense in the abstract.

These traits are unstable (as is their constness), so users cannot refer to them without opting-in to feature(sized_hierarchy). These traits are not behind cfgs as this would make implementation unfeasible, there would simply be too many cfgs required to add the necessary bounds everywhere. So, like Sized, these traits are automatically implemented by the compiler.

RFC 3729 describes changes which are necessary to preserve backwards compatibility given the introduction of these traits, which are implemented and as follows:

  • ?Sized is rewritten as MetaSized
  • MetaSized is added as a default supertrait for all traits w/out an explicit sizedness supertrait already.

There are no edition migrations implemented in this, as these are primarily required for the constness parts of the RFC and prior to stabilisation of this (and so will come in follow-up PRs alongside the const parts). All diagnostic output should remain the same (showing ?Sized even if the compiler sees MetaSized) unless the sized_hierarchy feature is enabled.

Due to the use of unstable extern types in the standard library and rustc, some bounds in both projects have had to be relaxed already - this is unfortunate but unavoidable so that these extern types can continue to be used where they were before. Performing these relaxations in the standard library and rustc are desirable longer-term anyway, but some bounds are not as relaxed as they ideally would be due to the inability to relax Deref::Target (this will be investigated separately).

It is hoped that this is implemented such that it could be merged and these traits could exist "under the hood" without that being observable to the user (other than in any performance impact this has on the compiler, etc). Some details might leak through due to the standard library relaxations, but this has not been observed in test output.

Notes:

  • Any commits starting with "upstream:" can be ignored, as these correspond to other upstream PRs that this is based on which have yet to be merged.
  • This best reviewed commit-by-commit. I've attempted to make the implementation easy to follow and keep similar changes and test output updates together.
    • Each commit has a short description describing its purpose.
    • This patch is large but it's primarily in the test suite.
  • I've worked on the performance of this patch and a few optimisations are implemented so that the performance impact is neutral-to-minor.
  • PointeeSized is a different name from the RFC just to make it more obvious that it is different from std::ptr::Pointee but all the names are yet to be bikeshed anyway.
  • @nikomatsakis has confirmed that this can proceed as an experiment from the t-lang side

Fixes #79409.

r? @ghost (I'll discuss this with relevant teams to find a reviewer)

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-run-make Area: port run-make Makefiles to rmake.rs A-rustdoc-json Area: Rustdoc JSON backend A-rustdoc-search Area: Rustdoc's search feature PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Mar 3, 2025
@rust-log-analyzer

This comment has been minimized.

@davidtwco
Copy link
Member Author

davidtwco commented Mar 3, 2025

I can reproduce this locally but I have no idea why it would be related to this patch. Clippy needed adjusting.

@rustbot rustbot added the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label Mar 3, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@traviscross traviscross added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Mar 4, 2025
@traviscross
Copy link
Contributor

cc @rust-lang/lang

@fee1-dead fee1-dead self-assigned this Mar 4, 2025
@tmiasko
Copy link
Contributor

tmiasko commented Mar 4, 2025

Does this perhaps fix #127336 by rejecting it?

@bors

This comment was marked as resolved.

@davidtwco
Copy link
Member Author

Does this perhaps fix #127336 by rejecting it?

It doesn't currently.

@davidtwco
Copy link
Member Author

Undrafting now that CI passes

@davidtwco
Copy link
Member Author

Had an initial glance yesterday (not posting the review comment yet), might have more time to do a full review next week, but I might reroll depending on my IRL availability.

No worries, feel free to post what you have. @oli-obk said he'd try and review this week so don't worry about having to get through it all yourself. Thanks for reviewing so far.

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need to review bounds.rs

Is there any new stuff that people can rely on and get wrong in unsafe code that we should be checking in miri?

Comment on lines 13 to 15
impl<C: ?Sized> A for u8 { //~ ERROR: the type parameter `C` is not constrained
impl<C: PointeeSized> A for u8 { //~ ERROR: the type parameter `C` is not constrained
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you search and replace the test suite for ?Sized? I'm not sure what's best, but I would have assumed keeping most of the test suite on ?Sized and only changing what needs changing would be best.

Copy link
Member Author

@davidtwco davidtwco Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't, I only changed tests that failed and needed changing. In this test, for example, ?Sized would no longer be unconstrained, it would be MetaSized-constrained, and so the test would be doing something different (and failing as a consequence).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something is wrong then. C is unconstrained because it is not used in the impl, what bounds are on it are irrelevant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can check if this test still fails because it might just have been the state of the implementation at the time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've revisited these tests and they fail with some additional "type annotations needed" errors, which I expect is what I was trying to avoid, but these are likely just downstream of the unconstrained parameter error as there's now an unconstrained C: MetaSized instead of bare C. I've just blessed these instead in the commit which was changing them.

@davidtwco
Copy link
Member Author

Rebased and addressed feedback.

Is there any new stuff that people can rely on and get wrong in unsafe code that we should be checking in miri?

I don't think so, but I'm not as familiar with what type of stuff that would be as I ought to be.

Copy link
Member

@fee1-dead fee1-dead left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already got some comments stashed up, posting them now

Comment on lines 78 to 113
/// Calculates the `Sized` constraint.
/// Calculates the `Sized`, `MetaSized` or `PointeeSized` constraint.
///
/// In fact, there are only a few options for the types in the constraint:
/// - an obviously-unsized type
/// For `Sized`, there are only a few options for the types in the constraint:
/// - an metasized type (str, slices, trait objects, etc)
/// - an pointee-sized type (extern types)
/// - a type parameter or projection whose sizedness can't be known
///
/// For `MetaSized`, there are only a few options for the types in the constraint:
/// - an pointee-sized type (extern types)
/// - a type parameter or projection whose sizedness can't be known
///
/// For `PointeeSized`, there is only one option for the types in the constraint:
/// - a type parameter or projection whose sizedness can't be known
#[instrument(level = "debug", skip(tcx), ret)]
fn adt_sized_constraint<'tcx>(
fn adt_sizedness_constraint<'tcx>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, as above, this needs some revamp with how it is presented and named.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comment

Comment on lines +237 to +244
struct StructAllFieldsMetaSized { x: [u8], y: [u8] }
//~^ ERROR the size for values of type `[u8]` cannot be known at compilation time
needs_sized::<StructAllFieldsMetaSized>();
//~^ ERROR the size for values of type `[u8]` cannot be known at compilation time
needs_metasized::<StructAllFieldsMetaSized>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see a more thorough exploration of how allowing this would affect our semantic model. But that is not a blocker for this PR.

I feel like if T: MetaSized then &T should be well-formed. But that's not what's shown here. But StructAllFieldsMetaSized already isn't wf since only the trailing field is allowed to be !Sized right now, but still I think this is a point worth thinking about.

@@ -267,7 +269,8 @@ impl<T: ?Sized> const Deref for &mut T {
#[stable(feature = "rust1", since = "1.0.0")]
#[const_trait]
#[rustc_const_unstable(feature = "const_deref", issue = "88955")]
pub trait DerefMut: ~const Deref {
#[allow(multiple_supertrait_upcastable)] // FIXME(sized_hierarchy): remove this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These now needs to be removed in the "lint: don't consider sizedness in upcastable lint" commit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still necessary for bootstrap, but I'll add a cfg_attr(bootstrap, ..) in that commit

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 22, 2025
@fee1-dead
Copy link
Member

r? compiler

sorry about the reroll, but I currently have exams coming up and I expect to be back to full activity on May 22nd

@rustbot rustbot assigned SparrowLii and unassigned fee1-dead Apr 22, 2025
@davidtwco davidtwco assigned oli-obk and unassigned SparrowLii Apr 22, 2025
@davidtwco
Copy link
Member Author

I'll assign this to @oli-obk having already done a review rather than add a new reviewer entirely.

@davidtwco davidtwco added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 23, 2025
@oli-obk
Copy link
Contributor

oli-obk commented Apr 24, 2025

@bors try

cratertime

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 24, 2025
Sized Hierarchy: Part I

This patch implements the non-const parts of rust-lang/rfcs#3729. It introduces two new traits to the standard library, `MetaSized` and `PointeeSized`. See the RFC for the rationale behind these traits and to discuss whether this change makes sense in the abstract.

These traits are unstable (as is their constness), so users cannot refer to them without opting-in to `feature(sized_hierarchy)`. These traits are not behind `cfg`s as this would make implementation unfeasible, there would simply be too many `cfg`s required to add the necessary bounds everywhere. So, like `Sized`, these traits are automatically implemented by the compiler.

RFC 3729 describes changes which are necessary to preserve backwards compatibility given the introduction of these traits, which are implemented and as follows:

- `?Sized` is rewritten as `MetaSized`
- `MetaSized` is added as a default supertrait for all traits w/out an explicit sizedness supertrait already.

There are no edition migrations implemented in this,  as these are primarily required for the constness parts of the RFC and prior to stabilisation of this (and so will come in follow-up PRs alongside the const parts). All diagnostic output should remain the same (showing `?Sized` even if the compiler sees `MetaSized`) unless the `sized_hierarchy` feature is enabled.

Due to the use of unstable extern types in the standard library and rustc, some bounds in both projects have had to be relaxed already - this is unfortunate but unavoidable so that these extern types can continue to be used where they were before. Performing these relaxations in the standard library and rustc are desirable longer-term anyway, but some bounds are not as relaxed as they ideally would be due to the inability to relax `Deref::Target` (this will be investigated separately).

It is hoped that this is implemented such that it could be merged and these traits could exist "under the hood" without that being observable to the user (other than in any performance impact this has on the compiler, etc). Some details might leak through due to the standard library relaxations, but this has not been observed in test output.

**Notes:**

- Any commits starting with "upstream:" can be ignored, as these correspond to other upstream PRs that this is based on which have yet to be merged.
- This best reviewed commit-by-commit. I've attempted to make the implementation easy to follow and keep similar changes and test output updates together.
  - Each commit has a short description describing its purpose.
  - This patch is large but it's primarily in the test suite.
- I've worked on the performance of this patch and a few optimisations are implemented so that the performance impact is neutral-to-minor.
- `PointeeSized` is a different name from the RFC just to make it more obvious that it is different from `std::ptr::Pointee` but all the names are yet to be bikeshed anyway.
- `@nikomatsakis` has confirmed [that this can proceed as an experiment from the t-lang side](https://rust-lang.zulipchat.com/#narrow/channel/435869-project-goals/topic/SVE.20and.20SME.20on.20AArch64.20.28goals.23270.29/near/506196491)

Fixes rust-lang#79409.

r? `@ghost` (I'll discuss this with relevant teams to find a reviewer)
@bors
Copy link
Collaborator

bors commented Apr 24, 2025

⌛ Trying commit 60dec26 with merge 0b080f0...

@bors
Copy link
Collaborator

bors commented Apr 24, 2025

☀️ Try build successful - checks-actions
Build commit: 0b080f0 (0b080f04a8020716beb803629c1dfc0f9dc25d66)

@oli-obk
Copy link
Contributor

oli-obk commented Apr 24, 2025

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-137944 created and queued.
🤖 Automatically detected try build 0b080f0
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 24, 2025
Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only reviewed the type system changes and have some general thoughts:

  • I mentioned this in a review comment. Please move all the unrelated refactorings and style changes out of this PR and land them separately
  • a lot of type system tests currently use ?Sized to mean "no implicit bounds". For most tests, this is just to simplify debugging, but there are at least some tests, e.g. for cycle/overflow handling, where the issue only reproduces without proving Sized. I expect these to no longer cover the original issue if we now add a MetaSized bound. Please add a #![rustc_no_implicit_bounds] (or sth like it) crate attribute in a separate PR which can be used to avoid relying on ?Sized.
  • these sort of type system changes generally make me uncomfortable as it's really easy to miss subtle interactions and insufficiencies. Changing the approach later on tends to be a lot more difficult as it needs to be pretty much more powerful than the current impl to avoid introducing regressions. I am somewhat sorry that I am insisting on overwhelmingly theoretical issues. I deeply care about avoiding any user-facing hacks

.filter_map(|(pred, _)| pred.as_trait_clause());
.filter_map(|(pred, _)| pred.as_trait_clause())
.filter(|pred| {
!cx.tcx.is_lang_item(pred.def_id(), hir::LangItem::Sized)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd expect this lint to never trigger for Sized 🤔 if Sized is a super trait the trait cannot be used as a trait object, can it?

}

Err(NoSolution)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move these spurious cleanups/changes into a separate PR

Comment on lines +163 to +167
let expected_self_ty = ecx.resolve_vars_if_possible(goal.predicate.self_ty());
let found_self_ty =
ecx.resolve_vars_if_possible(trait_clause.self_ty().skip_binder());
goal.predicate.polarity == trait_clause.polarity()
&& expected_self_ty == found_self_ty
Copy link
Contributor

@lcnr lcnr Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

structurally equatingn (using ==) types does not handle alias types. Create a <sized_self_type>: MetaSized assumption instead and then recur.

With your impl, a <T as Id>::This: Sized where-clause does not imply T: MetaSized. Please add this as a test.

)
{
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is that necessary. I would expect the change in the trait solver itself to be sufficient.

} else if tcx.is_lang_item(trait_ref.def_id(), LangItem::PointeeSized) {
Some(SizedTraitKind::PointeeSized)
} else {
None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: instead of using an Option here, early return if the trait isn't a Sized trait

Comment on lines +539 to +547
let has_sized_in_param_env =
param_env.caller_bounds().iter().filter_map(|c| c.as_trait_clause()).any(|c| {
trait_ref.self_ty() == c.skip_binder().self_ty()
&& tcx.is_lang_item(c.def_id(), LangItem::Sized)
});
if has_sized_in_param_env {
debug!("fast path -- metasized paramenv");
return true;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is observable if you've got the following environment:

where
    MyType<'a, T>: Sized,
    MyType<'static, T>: Sized,

Actually proving MyType<'some_region, T>: [Meta]Sized would be ambiguous. This fast path does structural equality instead so it may succeed if 'some_region ended up as literally the same region var as 'a or 'static.

I am confident that this can cause MIR borrowck ICE similar to the issue in rust-lang/trait-system-refactor-initiative#27

We're able to prove MyType<'a, T>: Sized during HIR typeck. We then erase the region during writeback and replace it with a fresh nll var at the start of borrowck. At this point the fastpath no longer applies, we get ambiguity and an ICE.

What's the perf impact of this fast path?


let expected_self_ty = infcx.resolve_vars_if_possible(predicate.self_ty());
let found_self_ty = infcx.resolve_vars_if_possible(c.self_ty());
predicate.polarity() == c.polarity() && expected_self_ty == found_self_ty
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, structurally comparing types is insufficient if there are aliases/regions

This fast path means we fail to prove MyType<'_, T>: MetaSized if there's MyType<'a, T>: Sized in the env

@@ -568,6 +568,14 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
def_id: DefId,
args: GenericArgsRef<'tcx>,
) -> PredicateObligations<'tcx> {
// PERF: `Sized`'s predicates include `MetaSized`, but both are compiler implemented marker
// traits, so `MetaSized` will never be WF if `Sized` is WF and vice-versa. Determining
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// traits, so `MetaSized` will never be WF if `Sized` is WF and vice-versa. Determining
// traits, so `MetaSized` will always be WF if `Sized` is WF and vice-versa. Determining

@@ -132,12 +168,14 @@ impl<I: Interner, O: Elaboratable<I>> Elaborator<I, O> {
// Get predicates implied by the trait, or only super predicates if we only care about self predicates.
match self.mode {
Filter::All => self.extend_deduped(
Some(clause),
cx.explicit_implied_predicates_of(data.def_id())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not instead do this filter in implied_predicates_of and super_predicates_of? 🤔

@lcnr lcnr self-assigned this Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-run-make Area: port run-make Makefiles to rmake.rs A-rustdoc-json Area: Rustdoc JSON backend A-rustdoc-search Area: Rustdoc's search feature F-autodiff `#![feature(autodiff)]` perf-regression Performance regression. PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-crater Status: Waiting on a crater run to be completed. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE with unsizing an extern type