-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
base: master
Are you sure you want to change the base?
Sized Hierarchy: Part I #137944
Conversation
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
73f1b4f
to
7f509ab
Compare
This comment has been minimized.
This comment has been minimized.
2537bfb
to
b31fd85
Compare
cc @rust-lang/lang |
Does this perhaps fix #127336 by rejecting it? |
This comment was marked as resolved.
This comment was marked as resolved.
b31fd85
to
2beed43
Compare
It doesn't currently. |
2beed43
to
839b844
Compare
Undrafting now that CI passes |
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. |
There was a problem hiding this 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?
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
9540d13
to
7e8918d
Compare
Rebased and addressed feedback.
I don't think so, but I'm not as familiar with what type of stuff that would be as I ought to be. |
There was a problem hiding this 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
compiler/rustc_ty_utils/src/ty.rs
Outdated
/// 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>( |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See previous comment
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>(); |
There was a problem hiding this comment.
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.
library/core/src/ops/deref.rs
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
7e8918d
to
60dec26
Compare
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 |
I'll assign this to @oli-obk having already done a review rather than add a new reviewer entirely. |
@bors try cratertime |
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)
☀️ Try build successful - checks-actions |
@craterbot check |
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
There was a problem hiding this 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 provingSized
. I expect these to no longer cover the original issue if we now add aMetaSized
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
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 |
There was a problem hiding this comment.
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; | ||
} |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
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; | ||
} |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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()) |
There was a problem hiding this comment.
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
? 🤔
This patch implements the non-const parts of rust-lang/rfcs#3729. It introduces two new traits to the standard library,
MetaSized
andPointeeSized
. 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 behindcfg
s as this would make implementation unfeasible, there would simply be too manycfg
s required to add the necessary bounds everywhere. So, likeSized
, 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 asMetaSized
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 seesMetaSized
) unless thesized_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:
PointeeSized
is a different name from the RFC just to make it more obvious that it is different fromstd::ptr::Pointee
but all the names are yet to be bikeshed anyway.Fixes #79409.
r? @ghost (I'll discuss this with relevant teams to find a reviewer)