-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
stop adding dropck outlives requirements for [T; 0]
#110288
Comments
Team member @lcnr has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns:
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! See this document for info about what commands tagged team members can give me. |
How (if at all) does this interact with const generics? What if the length is unknown but could be zero? Seems surprising that behavior depends so heavily on whether the compiler can "figure out" the length. |
This happens after type inference, so we should only fail to evaluate for generic constants. In this case they are always considered to be potentially non-zero.
yes, I considered to propose changing the behavior for arrays to always require drop glue, even for I am a bit confused by why exactly this errors, but the following snippet would fail if we remove the special case for zero length arrays in const fn zero_sized<T>() -> &'static [T; 0] {
&[]
} error[E0493]: destructor of `[T; 0]` cannot be evaluated at compile-time
--> src/main.rs:2:6
|
2 | &[]
| ^^ the destructor for this type cannot be evaluated in constant functions
3 | }
| - value is dropped here opened #110322 if you want to take a deeper look at this. Afaict we never actually drop @rfcbot concern we could instead stop specialcasing |
What probably happens is that fn zero_sized<T>() -> &'static [T; 0] {
&[]
} |
no, this still works 😅 apparently promotion does work. Had the same idea at first when seeing the test failures |
Hm, maybe promotion has its own knowledge that |
Promotion for arrays (unlike any other type) even supports mutable references, i.e. |
[T; 0]
adding outlives requirements to dropck[T; 0]
[T; 0]
[T; 0]
I'm not convinced yet that we want to ditch that option. Clearly there's something going on that we don't understand -- if this still works fn zero_sized<T>() -> &'static [T; 0] {
&[]
} then this should also still work const fn zero_sized<T>() -> &'static [T; 0] {
&[]
} This looks like an issue specific to promotion, not something related to dropck in general. |
Note that this also works fn foo() -> &'static Option<Vec<i32>> { &None } so "promotion of types that need dropping, if we know that the value doesn't need dropping", is already a thing. This works even with |
If a local is defined by an assignment from Rvalue::Aggregate, its value needs pub const fn f() {
// OK:
let _a: [Option<String>; 2] = [None, None];
let _b: &'static [Option<String>; 2] = &[None, None];
} Additionally, in const checking (but not in promotion which works differently use std::cell::Cell;
pub const fn g() {
// OK:
let _a: Option<Cell<String>> = None;
// Error:
let _b: Option<Cell<String>> = None;
&_b;
} Note that |
@rfcbot concern rushed-fcp I personally don't feel comfortable making non-gated changes here right now. As I noted on zulip, I'm not really satisfied with the answer regarding const generics. And, it seems like there is still deeper discussion to be had (glancing at the above comments). @lcnr I would feel more comfortable canceling FCP here in favor of more discussion and gated experimentation. Ultimately, I might be swayed with a very detailed and well thought out writeup of the problems here and intended solution, though. |
rushed-fcp seems fair, how about rust-lang/rfcs#3414 ? |
hmm, I originally assumed to this be fairly straightforward. Will attempt to provide a full summary of the issue and the proposed solution: the issueImplicitly dropping variables during borrowck is different from explicitly calling Computing this is currently done in two steps:
For how can we fix thisI believe there to only be two valid options change
|
if we do opt to apply |
@tmiasko oh I see... with interior mutability the value might change before it is dropped so the value-based analysis gives up. That makes sense. @lcnr thanks for the writeup!
Yes that is probably correct, so this pretty much defeats my argument.
This is fair. My remaining concern here is consistency: the reasoning above applies not just to dropping. To name an example, Of course we could set a goal to make 0 special for more cases (Copy, Clone, Default, ...); then the question is -- how realistic is it to achieve that goal?
Well, it would force us to add such a special case to the trait system before we can do the move. Currently we don't have such a special case, right? And it is unclear whether specialization will be powerful enough for this. |
We already have that special case for The specialcase also exists in the wild, e.g. in serde: https://doc.rust-lang.org/nightly/std/primitive.array.html#impl-Default-for-%5BT;+0%5D Using some compiler hack and marker traits it is already possible right now, allowing this behavior on stable is probably also a longterm goal. |
Ah, fair, I didn't know that. |
[T; 0]
[T; 0]
will discuss this as part of rust-lang/types-team#92 |
@rfcbot resolve rushed-fcp While I'm not ready to check my box yet, this was obviously discussed, so resolving concern |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, 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. This will be merged soon. |
Rollup merge of rust-lang#128438 - Bryanskiy:empty-array-dropck, r=lcnr Add special-case for [T, 0] in dropck_outlives implements/fixes rust-lang#110288. r? `@lcnr`
implemented in #128438, we still need to update the documentation however, cc @Bryanskiy |
This comment was marked as spam.
This comment was marked as spam.
fix dropck documentation for `[T;0]` special-case fixes rust-lang#110288. r? `@lcnr`
Rollup merge of rust-lang#128497 - Bryanskiy:fix-dropck-doc, r=lcnr fix dropck documentation for `[T;0]` special-case fixes rust-lang#110288. r? ``@lcnr``
fix dropck documentation for `[T;0]` special-case fixes rust-lang/rust#110288. r? ``@lcnr``
[T; 0]
currently doesn't need drop glue by itself but still adds liveness requirements forT
when used in a more complex value which already needs drop.This behavior is identitical to
PhantomData
, which also does not need drop glue by itself but adds liveness requirements when used in values who do. I do not propose to change anything aboutPhantomData
with this issue.Example
playground
This behavior is confusing. I propose that we don't add any dropck outlives requirements for arrays of length zero. This allows strictly more code to compile, it may however result in unsoundness if people rely on this behavior to guarantee errors.
The text was updated successfully, but these errors were encountered: