Skip to content
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

StableDeref trait into core #125048

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dingxiangfei2009
Copy link
Contributor

@dingxiangfei2009 dingxiangfei2009 commented May 12, 2024

cc @Darksonn @wedsonaf @ojeda

This is a draft PR to introduce a StableDeref trait as a possible answer to the unsoundness problem raised in the RFC draft rust-lang/rfcs#3621.

We can follow up discussion in Zulip stream.

@rustbot
Copy link
Collaborator

rustbot commented May 12, 2024

r? @m-ou-se

rustbot has assigned @m-ou-se.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added 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. labels May 12, 2024
@rust-log-analyzer

This comment has been minimized.

#[unstable(feature = "stable_deref_trait", issue = "123430")]
/// # Safety
///
/// Any two calls to `deref` must return the same value at the same address unless
Copy link
Member

Choose a reason for hiding this comment

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

"same value at the same address" here is pretty vague, and it's unclear to me without reading the RFC thread what exactly that entails and how it compares to the stable deref trait crate. notably, the preconditions on the crate are currently not satisfied by Box because of the special strict aliasing rules imposed by it.

Copy link
Contributor

@Darksonn Darksonn May 12, 2024

Choose a reason for hiding this comment

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

The meaning of "same value" is that the concrete type must not change. I copied the explanation in below. Was it unclear?

Here, "same value" means that if deref returns a trait object, then the actual type behind that trait object must not change. Additionally, when you unsize coerce from Self to Unsized, then if you call deref on Unsized and get a trait object, then the underlying type of that trait object must be <Self as Deref>::Target.

Analogous requirements apply to other unsized types. E.g., if deref returns [T], then the length must not change. In other words, the underlying type must not change from [T; N] to [T; M].

The motivation for this requirement is that with trait objects, you could otherwise first return one struct, and then later return some wrapper struct that wraps the original struct using #[repr(transparent)].

@@ -309,3 +309,25 @@ impl<T: ?Sized> Receiver for &T {}

#[unstable(feature = "receiver_trait", issue = "none")]
impl<T: ?Sized> Receiver for &mut T {}

#[lang = "stable_deref"]
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be made into a lang item until it's actually used by the compiler. I feel like having unused lang items is not desirable.

Copy link
Contributor

Choose a reason for hiding this comment

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

is there any chance of us somehow managing to check and error in that case?

Copy link
Contributor

Choose a reason for hiding this comment

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

thinks okay the moment this left my hands the answer came to me as "probably not usefully" (it would only be one more formality-tier listing somewhere and someone will just add it to that).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the use of it is rather imminent. Ideally with #123472 this would go into the trait bounds that #[derive(SmartPointer)] generates.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I came to my right sense! I have dropped the lang term here.

@rust-log-analyzer

This comment has been minimized.

@dingxiangfei2009 dingxiangfei2009 force-pushed the stable-deref branch 2 times, most recently from 9a5dbc0 to 1795d68 Compare May 13, 2024 18:38
@dingxiangfei2009 dingxiangfei2009 marked this pull request as ready for review May 13, 2024 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants