You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// We drop `_x` here even though `reference` is no longer live.
113
115
//
114
-
// This is accepted as `'s` is marked as `#[only_dropped]` in the
116
+
// This is accepted as `T` is marked as `#[may_dangle(can_drop)]` in the
115
117
// `Drop` impl of `MyType`.
116
118
}
117
119
```
118
120
119
-
The ability to differentiate between `#[fully_unused]` and `#[only_dropped]` is significant
120
-
for type parameters:
121
+
`Drop` impls for collections tend to require `#[may_dangle(droppable)]`:
121
122
122
123
```rust
123
124
pubstructBTreeMap<K, V> {
@@ -134,7 +135,7 @@ unsafe impl<#[only_dropped] K, #[only_dropped] V> Drop for BTreeMap<K, V> {
134
135
}
135
136
```
136
137
137
-
A type where `#[fully_unused]` would be useful is a `Weak` pointer for a variant of `Rc`
138
+
A type where `#[may_dangle(must_not_use)]` would be useful is a `Weak` pointer for a variant of `Rc`
138
139
where the value is dropped when the last `Rc` goes out of scope. Dropping a `Weak` pointer
139
140
would never access `T` in this case.
140
141
@@ -151,43 +152,47 @@ When implicitly dropping a variable of type `T`, liveness requirements are compu
151
152
- If `T` does not have any drop glue, do not add any requirements.
152
153
- If `T` is a trait object, `T` has to be live.
153
154
- If `T` has an explicit `Drop` impl, require all generic argument to be live, unless
154
-
- they are marked with `#[fully_unused]`, in which case they are ignored,
155
-
- or they are marked with `#[only_dropped]`, in which case recurse into the generic argument.
155
+
- they marked with `#[may_dangle]`:
156
+
- arguments for lifetime parameters marked `#[may_dangle]` and type parameters
157
+
marked `#[may_dangle(must_not_use)]` are ignored,
158
+
- we recurse into arguments for type parameters marked `#[may_dangle(droppable)]`.
156
159
- Regardless of whether `T` implements `Drop`, recurse into all types *owned* by `T`:
157
160
- references, raw pointers, function pointers, function items and scalars do not own
158
161
anything. They can be trivially dropped.
159
162
- tuples and arrays consider their element types to be owned.
160
163
- all fields (of all variants) of ADTs are considered owned. We consider all variants
161
-
for enums. The only exception here is `ManuallyDrop<U>` which is not considered to own `U`. `PhantomData<U>` does not have any fields and therefore also does not consider
164
+
for enums. The only exception here is `ManuallyDrop<U>` which is not considered to own `U`.
165
+
`PhantomData<U>` does not have any fields and therefore also does not consider
162
166
`U` to be owned.
163
167
- closures and generators own their captured upvars.
164
168
165
169
Checking drop impls may error for generic parameters which are known to be incorrectly marked:
166
-
-`#[fully_unused]` parameters which are recursively owned
167
-
-`#[only_dropped]` parameters which are required to be live by a recursively owned type
170
+
-`#[may_dangle(must_not_use)]` parameters which are recursively owned
171
+
-`#[may_dangle(droppable)]` parameters which are required to be live by a recursively owned type
168
172
169
173
This cannot catch all misuses, as the parameters can be incorrectly used by the `Drop` impl itself.
170
174
We therefore require the impl to be marked as `unsafe`.
171
175
172
176
## How this differs from the status quo
173
177
174
-
Instead of `#[fully_unused]` and `#[only_dropped]`,there is only the `#[may_dangle]` attribute which
175
-
skips the generic parameter. This is equivalent to the behavior of `#[fully_unused]` and relies on the recursion
176
-
into types owned by `T` to figure out the correct constraints.
178
+
Right now there is only the `#[may_dangle]` attribute which skips the generic parameter.
179
+
This is equivalent to the behavior of `#[may_dangle(must_not_use)]` and relies on the recursion
180
+
into types owned by `T` to figure out the correct constraints. This is now explicitly annotated
181
+
using `#[may_dangle(droppable)]`.
177
182
178
183
`PhantomData<U>` currently considers `U` to be owned while not having drop glue itself. This means
179
184
that `(PhantomData<PrintOnDrop<'s>>, String)` requires `'s` to be live while
180
185
`(PhantomData<PrintOnDrop<'s>>, u32)` does not. This is required for get the
181
-
behavior of `#[only_dropped]` for parameters otherwise not owned by adding `PhantomData` as a field.
182
-
One can easily forget this, which caused the [unsound](https://github.com/rust-lang/rust/issues/76367)
186
+
behavior of `#[may_dangle(droppable)]` for parameters otherwise not owned by adding `PhantomData`
187
+
as a field. One can easily forget this, which caused the
0 commit comments