-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
RFC: add the Freeze trait to libcore/libstd #2944
Conversation
Isn't LLVM's name for turning uninit data into a fixed-but-arbitrary data value also called "freeze"? If so, perhaps we should use some other name. This is a bikeshed, but seems like a fair one. |
@Lokathor Totally fair. |
Yeah those are two totally different uses of the same word. :/ |
… emphasize that `UnsafeCell` is the reason a type is not `Freeze`
To get bikeshedding rolling, how about Additionally, I think libcore must provide a |
…. Users aren't able to get the same functionality as PhantomUnfrozen without sacrificing performance.
That would seem to imply that there are two kinds of
What are the use cases for |
The only use case for |
So, what if people just opted into Freeze instead using a normal Derive, like how it works with Copy |
I think most of the arguments for having For example: #[derive(Freeze)]
pub struct Bar<T>(*const T); Would not be |
That's just because the derivation system is overly simplistic and needs to be improved. The fundamental idea that trait impls are a public part of the API of a type, thus also being a stability hazard that designers should have to opt in to, stands. |
I also think the "shallow"/"direct" aspect in the RFC should be part of the name, or else a lot of people will misinterpret this trait name as being about deep immutability the first time they see it. And unless I'm missing a huge part of the motivation, nobody should be writing this trait name very often, so I'm inclined toward clear/descriptive names rather than short names. My current favorite would be I dislike |
I do not feel that this is a substantial enough problem to block adoption, and I don't think
We could reduce the number of kinds of
I like that too. |
EDIT:
Eh, that is the entire point, yes. EDIT2: Ah you are worried about future compatibility. Hm. I have to say a non-attribute-affecting |
This wouldn't work due to this impl (necessary since unsafe impl<T: ?Sized> Freeze for PhantomData<T> {} |
Interesting, I did not know this existed. |
Is it possible to use specialization to make |
Genuine question: is there a reason why Like, it technically already exists as a feature of the language. And we can technically already observe it in code through the restriction on const borrowing. For a relatively obscure feature, I don't see why its existence would require everyone to add |
I agree 💯. We shouldn't let semver constrain language features. Compatibility is important, but nothing prevents me as a author from making a breaking change in a minor version. Ultimately semver is a useful convention followed by authors, not a guarantee upheld by the compiler. |
Semver is a bit more than a convention considering how cargo is designed to reduce the number of versions used based upon it, and there's a very specific definition of breaking changes that could theoretically be applied algorithmically to Rust code. But, I agree in this case that we could make an exception. |
There is some desire to use I somehow feel like rust-lang/rust#49206 is related, because this is about observing the address at which some shared data lies, but I cannot put my finger onto it or make this precise. |
Approving on behalf of withoutboats, as per rust-lang/team#526. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to close, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. The RFC is now closed. |
We should reconsider stabilizing some sort of built-in |
The Think of it this way: we could have just disallowed |
I recently discovered? another use-case for Freeze that has nothing to do with shared data https://internals.rust-lang.org/t/interior-mutability-reflection/18198
Right now, I'm using |
They already do, there is more cases than the const fn one, that break when such a type is added eg: pub struct SomeType<'a> {
val: &'a i32,
// x: std::cell::Cell<&'a i32>, // Enable this for error.
}
pub fn assert_covariance<'x: 'y, 'y>(x: SomeType<'x>) -> SomeType<'y> {
// This function only compiles for covariant types.
x
} If the authors choose to add the uncommented line, the code stops compiling. I see a clear benefit here, and a dubious reason not to add something like this. |
That is because the variance between |
Well the variance here, changes as direct result of transitively containing UnsafeCell or not. Arguably that's the same effect for users. |
If the main concern is code breaking. I suggest this alternative design: (const?) fn has_direct_interior_mutability<T>() -> bool; If we add such a function to the standard library it would serve the purpose of allowing such optimisations, without building things that will stop compiling if users add a new member. With constant propagation and folding the optimiser ought to be able to generate efficient code depending on the types property. To motivate it, here is what a simple type wrapping a Of course a simple |
Invariance can arise without interior mutability. So it's really unrelated to this discussion.
It makes me very sad that |
It seems, I poorly explained my point. My understanding is, that the main reason this proposal was declined, was a worry that it would make it easier for library Authors to inadvertently break SemVer. I gave the example to demonstrate, that something very similar, already happens today on stable.
It's a tricky situation, here are the options I'm seeing:
A, is IMO a bad fit for Rust, a language that that markets itself as an efficiently way to exploit hardware resources. I tried B, and have not had good success, even the failed attempts I had, made the code significantly more complex. That's not a property that should be ignored, especially because unsafe code is being used. C, is really problematic too. It creates this two class 'society', where builtin Is using
And if I understood correctly, Orson Peters the author of pdqsort who is currently working on glidesort came across the same problem and came up with the same 'solution' of limiting certain optimisations to |
Workarounds, such as |
I meant to say that we are not making a guarantee that covariance has any relation with UnsafeCell either way. In fact I don't even know what you mean by this question. Covariance is a property of type constructors, not types. The following type has interior mutability but is covariant in its only agument: struct MyType<T>(T, Cell<i32>); |
Got it, thank you very much for taking the time to clarify and your example. |
It would be great to have this for zerocopy; we need to reason about this for things like google/zerocopy#8 and google/zerocopy#251. |
This is a closed PR, please stop posting here, it's really not useful. rust-lang/rust#60715 is a more appropriate place to comment. |
Rendered
There was some pre RFC discussion here.