-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Stabilize derive(CoercePointee)
#133820
base: master
Are you sure you want to change the base?
Stabilize derive(CoercePointee)
#133820
Conversation
8baca16
to
7865ddb
Compare
This comment has been minimized.
This comment has been minimized.
derive(CoercePointee)
7865ddb
to
208e5bb
Compare
@rustbot ready
|
This comment has been minimized.
This comment has been minimized.
208e5bb
to
d9f6c00
Compare
@rustbot label +F-derive_smart_pointer |
#[rustc_builtin_macro(CoercePointee, attributes(pointee))] | ||
#[allow_internal_unstable(dispatch_from_dyn, coerce_unsized, unsize)] | ||
#[unstable(feature = "derive_coerce_pointee", issue = "123430")] | ||
#[cfg(not(bootstrap))] |
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.
Question:
Do we need to condition on bootstrap
? No standard library item depends on this macro yet.
The RFC for this feature said that it depends on the arbitrary self types feature, but it is not a full blocker to stabilizing this feature. There are two things that the macro enables: The first thing is coercions from The second thing is making traits such as this one object safe: trait MyTrait {
fn func(self: MySmartPtr<Self>);
} Without arbitrary self types, it's not legal to declare this trait because So if we stabilize the macro now, then we gain the ability to make coercions and call trait methods via deref, but we don't gain the ability to declare traits that take the smart pointer without a deref coercion. We would gain that ability once arbitrary self types are stabilized. |
@rfcbot fcp merge Thanks @dingxiangfei2009. We talked about this in our triage call today. Let's ship it. |
Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
@compiler-errors my preferred way to manage that is not unchecking my box but filing a concern |
Yeah, at this point @lcnr has already begun to do a good job at characterizing the types-level surface of what is being stabilized here, and funny enough that they found an interesting quirk in the process :D |
In practice I don't think this is exploitable, regardless I don't think we should stabilize the ability for semi-user-written impls or trait bounds to be written involving this trait until the builtin checks are brought in line with edit: thanks to @steffahn for pointing this out @rfcbot concern |
The @rfcbot concern |
I've moved some of the concerns out to separate issues where people can assign themselves and |
☔ The latest upstream changes (presumably #135286) made this pull request unmergeable. Please resolve the merge conflicts. |
I dont know who the "owners" of arbitrary self types are but I would like for them to sign off on this to some degree as this does stabilize additional method receivers and I can't speak to the status of the implementation of that feature. @rfcbot concern arbitrary self types impl status From a types POV though I've spent a while thinking over the traits involved and feel somewhat convinced that |
cc @adetaylor from the tracking issues of arbitrary self types it seems like you've been rather with arbitrary self types? see the above comment |
Yep, I've been the one driving arbitrary self types. I'm in the early stages of drafting a stabilization report for it - hopefully I'll have written that up in about a week, but I've got a few experiments I need to run first, so it might be a wee bit longer. I'll read through here as well in the next few days and add any comments/questions I've got. |
This feature doesn't have any effect on what method receivers are possible. Adding |
Ah okay, I did not realise that dispatch from dyn was completely irrelevant to this stabilization given that it had been previously talked about and calling trait methods on these smart pointers had been thrown around in code examples (and the description of the PR explicitly showing that DispatchFromDyn had been derived) |
@rfcbot resolved concern arbitrary self types impl status |
This is about object safety, so doesn't that mean that |
@RalfJung Current dyn-compatibility definitions also require all (non- This means that without
(according to the reference) So custom smart pointers are never relevant. (The type |
I mean, arbitrary self types are relevant because you cannot use the trait MyTrait {
fn foo(&mut self);
}
fn foo(arc: MySmartPtr<dyn MyTrait>) {
arc.foo();
}
foo(MySmartPtr::new(MyStruct::new())); |
I'm not the right reviewer for this (just cleaning up my review queue, this is waiting on FCP anyway). |
@rustbot ping |
What is being proposed for stabilization
This stabilization report concerns the macro specified by RFC3621, now called
CoercePointee
.This macro enables the derivation of trait implementation of
Unsize
,CoerceUnsize
andDispatchFromDyn
traits, both of which are unstable at the moment, under a well-defined and structured schema, so that the users who would like to allow their custom type to admit unsizing coercion and use ofdyn
DST in one of its generic type parameter in a safe way. The greatest use case of this macro is to allow users to equip their types with this behaviour without dependence onalloc
crate, or whenalloc
is not available and a custom implementation of pointer-like data structure in the likes ofRc
andArc
is highly desirable. The most prominent example is thekernel
crate by Rust-for-Linux, which would greatly benefit from this macro due to reduction of boilerplate, and reduce the project's dependence on unstable features behindUnsize
,CoerceUnsize
andDispatchFromDyn
.In a nutshell,
derive(CoercePointee)
derives the implementation ofCoerceUnsize
andDispatchFromDyn
traits on the targetstruct
definition with all the necessary trait bounds. It identifies one generic type variable in the generic list of the targetstruct
, that is either uniquely annotated with the#[pointee]
attribute or is the only type variable among the generics. This identified generic, which is called source type in this document, will be treated as the target of unsizing operation anddyn
trait object dispatch. Correspondingly, the resultant type after the unsizing coercion will be called target type in this document. In case additional trait bounds applies on the source type, the said trait bounds will be migrated to both the source type and the target type in the trait implementation.Out of necessity of unsizing coercion, the macro requires the
struct
to adopt therepr(transparent)
layout. The only data field in thestruct
definition, which is required by this layout, should also implementCoerceUnsize
andDispatchFromDyn
at the moment, or at least conform to requirement of the unsizing coercion and trait object dispatching protocol.As an example, here is how the generated code would look like after expanding the macro. The following is a user source that invokes this macro.
This is the expansion result. The source type
T
after the unsizing coercion is assumed to be__S
and the trait bounds requested by the original definition are migrated for__S
as well throughtout.Future interaction
This stabilization of this macro does not require the stabilization of any of the unstable traits mentioned. This macro aims at stabilising a subset of features thereof, which has widely proliferated in the ecosystem, without blocking further development of DSTs and unsizing coercion. In case of change in the design of these language features, as long as basic provision of the coercion operation remains the same, the macro should be updated to use the new facility inside its implementation when necessary, to preserve only the semantics and capabilities that this macro exposes users to.
Tracking:
derive(CoercePointee)
#123430cc @rust-lang/lang