Description
The "dropck eyepatch" (#[may_dangle]) allows a Drop impl to declare that it won't access the data behind certain generic parameters during drop. This relaxes the borrow checker's requirements.
unsafe impl<#[may_dangle] T> Drop for Vec<T> {
fn drop(&mut self) { /* deallocates memory but doesn't read T values */ }
}
Without #[may_dangle], the borrow checker would require that T outlives the Vec<T> value. With it, T is allowed to dangle.
Mentoring notes
Background: Read the Nomicon section on dropck and RFC 1327.
Where to start:
- Add
#[may_dangle] as an attribute on type parameters in Drop impls (grammar change)
- In the drop-glue checking logic (depends on "detect implicit use" above), skip the outlives requirement for parameters marked
#[may_dangle]
Prerequisite: "Detect implicit use at scope exit" must be implemented first.
Description
The "dropck eyepatch" (
#[may_dangle]) allows aDropimpl to declare that it won't access the data behind certain generic parameters during drop. This relaxes the borrow checker's requirements.Without
#[may_dangle], the borrow checker would require thatToutlives theVec<T>value. With it,Tis allowed to dangle.Mentoring notes
Background: Read the Nomicon section on dropck and RFC 1327.
Where to start:
#[may_dangle]as an attribute on type parameters inDropimpls (grammar change)#[may_dangle]Prerequisite: "Detect implicit use at scope exit" must be implemented first.