Skip to content

Commit 33d6d79

Browse files
committed
Non-parametric dropck; instead trust an unsafe attribute (RFC 1238).
Implement cannot-assume-parametricity (CAP) from RFC 1238, and add the UGEH attribute. ---- Note that we check for the attribute attached to the dtor method, not the Drop impl. (This is just to match the specification of RFC and the tests; I am not wedded to this approach.)
1 parent dccb222 commit 33d6d79

File tree

4 files changed

+87
-25
lines changed

4 files changed

+87
-25
lines changed

src/doc/reference.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,20 @@ macro scope.
19291929
- `simd` - on certain tuple structs, derive the arithmetic operators, which
19301930
lower to the target's SIMD instructions, if any; the `simd` feature gate
19311931
is necessary to use this attribute.
1932+
- `unsafe_destructor_blind_to_params` - on `Drop::drop` method, asserts that the
1933+
destructor code (and all potential specializations of that code) will
1934+
never attempt to read from nor write to any references with lifetimes
1935+
that come in via generic parameters. This is a constraint we cannot
1936+
currently express via the type system, and therefore we rely on the
1937+
programmer to assert that it holds. Adding this to a Drop impl causes
1938+
the associated destructor to be considered "uninteresting" by the
1939+
Drop-Check rule, and thus it can help sidestep data ordering
1940+
constraints that would otherwise be introduced by the Drop-Check
1941+
rule. Such sidestepping of the constraints, if done incorrectly, can
1942+
lead to undefined behavior (in the form of reading or writing to data
1943+
outside of its dynamic extent), and thus this attribute has the word
1944+
"unsafe" in its name. To use this, the
1945+
`unsafe_destructor_blind_to_params` feature gate must be enabled.
19321946
- `unsafe_no_drop_flag` - on structs, remove the flag that prevents
19331947
destructors from being run twice. Destructors might be run multiple times on
19341948
the same object with this attribute. To use this, the `unsafe_no_drop_flag` feature

src/librustc/middle/ty/util.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,16 @@ impl<'tcx> ty::ctxt<'tcx> {
578578
});
579579
let generics = adt.type_scheme(self).generics;
580580

581+
// RFC 1238: if the destructor method is tagged with the
582+
// attribute `unsafe_destructor_blind_to_params`, then the
583+
// compiler is being instructed to *assume* that the
584+
// destructor will not access borrowed data via a type
585+
// parameter, even if such data is otherwise reachable.
586+
if self.has_attr(dtor_method, "unsafe_destructor_blind_to_params") {
587+
debug!("typ: {:?} assumed blind and thus is dtorck-safe", adt);
588+
return false;
589+
}
590+
581591
// In `impl<'a> Drop ...`, we automatically assume
582592
// `'a` is meaningful and thus represents a bound
583593
// through which we could reach borrowed data.
@@ -592,6 +602,14 @@ impl<'tcx> ty::ctxt<'tcx> {
592602
return true;
593603
}
594604

605+
// RFC 1238: *any* type parameter at all makes this a dtor of
606+
// interest (i.e. cannot-assume-parametricity from RFC 1238.)
607+
if generics.has_type_params(subst::TypeSpace) {
608+
debug!("typ: {:?} has interesting dtor due to type params",
609+
adt);
610+
return true;
611+
}
612+
595613
let mut seen_items = Vec::new();
596614
let mut items_to_inspect = vec![impl_did];
597615
while let Some(item_def_id) = items_to_inspect.pop() {

src/librustc_typeck/check/dropck.rs

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -217,26 +217,52 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
217217
///
218218
/// ----
219219
///
220-
/// The Drop Check Rule is the following:
220+
/// The simplified (*) Drop Check Rule is the following:
221221
///
222222
/// Let `v` be some value (either temporary or named) and 'a be some
223223
/// lifetime (scope). If the type of `v` owns data of type `D`, where
224224
///
225-
/// * (1.) `D` has a lifetime- or type-parametric Drop implementation, and
226-
/// * (2.) the structure of `D` can reach a reference of type `&'a _`, and
227-
/// * (3.) either:
228-
/// * (A.) the Drop impl for `D` instantiates `D` at 'a directly,
229-
/// i.e. `D<'a>`, or,
230-
/// * (B.) the Drop impl for `D` has some type parameter with a
231-
/// trait bound `T` where `T` is a trait that has at least
232-
/// one method,
225+
/// * (1.) `D` has a lifetime- or type-parametric Drop implementation,
226+
/// (where that `Drop` implementation does not opt-out of
227+
/// this check via the `unsafe_destructor_blind_to_params`
228+
/// attribute), and
229+
/// * (2.) the structure of `D` can reach a reference of type `&'a _`,
233230
///
234231
/// then 'a must strictly outlive the scope of v.
235232
///
236233
/// ----
237234
///
238235
/// This function is meant to by applied to the type for every
239236
/// expression in the program.
237+
///
238+
/// ----
239+
///
240+
/// (*) The qualifier "simplified" is attached to the above
241+
/// definition of the Drop Check Rule, because it is a simplification
242+
/// of the original Drop Check rule, which attempted to prove that
243+
/// some `Drop` implementations could not possibly access data even if
244+
/// it was technically reachable, due to parametricity.
245+
///
246+
/// However, (1.) parametricity on its own turned out to be a
247+
/// necessary but insufficient condition, and (2.) future changes to
248+
/// the language are expected to make it impossible to ensure that a
249+
/// `Drop` implementation is actually parametric with respect to any
250+
/// particular type parameter. (In particular, impl specialization is
251+
/// expected to break the needed parametricity property beyond
252+
/// repair.)
253+
///
254+
/// Therefore we have scaled back Drop-Check to a more conservative
255+
/// rule that does not attempt to deduce whether a `Drop`
256+
/// implementation could not possible access data of a given lifetime;
257+
/// instead Drop-Check now simply assumes that if a destructor has
258+
/// access (direct or indirect) to a lifetime parameter, then that
259+
/// lifetime must be forced to outlive that destructor's dynamic
260+
/// extent. We then provide the `unsafe_destructor_blind_to_params`
261+
/// attribute as a way for destructor implementations to opt-out of
262+
/// this conservative assumption (and thus assume the obligation of
263+
/// ensuring that they do not access data nor invoke methods of
264+
/// values that have been previously dropped).
265+
///
240266
pub fn check_safety_of_destructor_if_necessary<'a, 'tcx>(rcx: &mut Rcx<'a, 'tcx>,
241267
typ: ty::Ty<'tcx>,
242268
span: Span,
@@ -356,30 +382,25 @@ fn iterate_over_potentially_unsafe_regions_in_type<'a, 'b, 'tcx>(
356382
// borrowed data reachable via `typ` must outlive the parent
357383
// of `scope`. This is handled below.
358384
//
359-
// However, there is an important special case: by
360-
// parametricity, any generic type parameters have *no* trait
361-
// bounds in the Drop impl can not be used in any way (apart
362-
// from being dropped), and thus we can treat data borrowed
363-
// via such type parameters remains unreachable.
385+
// However, there is an important special case: for any Drop
386+
// impl that is tagged as "blind" to their parameters,
387+
// we assume that data borrowed via such type parameters
388+
// remains unreachable via that Drop impl.
389+
//
390+
// For example, consider:
391+
//
392+
// ```rust
393+
// #[unsafe_destructor_blind_to_params]
394+
// impl<T> Drop for Vec<T> { ... }
395+
// ```
364396
//
365-
// For example, consider `impl<T> Drop for Vec<T> { ... }`,
366397
// which does have to be able to drop instances of `T`, but
367398
// otherwise cannot read data from `T`.
368399
//
369400
// Of course, for the type expression passed in for any such
370401
// unbounded type parameter `T`, we must resume the recursive
371402
// analysis on `T` (since it would be ignored by
372403
// type_must_outlive).
373-
//
374-
// FIXME (pnkfelix): Long term, we could be smart and actually
375-
// feed which generic parameters can be ignored *into* `fn
376-
// type_must_outlive` (or some generalization thereof). But
377-
// for the short term, it probably covers most cases of
378-
// interest to just special case Drop impls where: (1.) there
379-
// are no generic lifetime parameters and (2.) *all* generic
380-
// type parameters are unbounded. If both conditions hold, we
381-
// simply skip the `type_must_outlive` call entirely (but
382-
// resume the recursive checking of the type-substructure).
383404
if has_dtor_of_interest(tcx, ty) {
384405
debug!("iterate_over_potentially_unsafe_regions_in_type \
385406
{}ty: {} - is a dtorck type!",

src/libsyntax/feature_gate.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Option<u32>, Status
136136
// switch to Accepted; see RFC 320)
137137
("unsafe_no_drop_flag", "1.0.0", None, Active),
138138

139+
// Allows using the unsafe_destructor_blind_to_params attribute
140+
// (Needs an RFC link)
141+
("unsafe_destructor_blind_to_params", "1.3.0", Some(28498), Active),
142+
139143
// Allows the use of custom attributes; RFC 572
140144
("custom_attribute", "1.0.0", None, Active),
141145

@@ -339,6 +343,11 @@ pub const KNOWN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeGat
339343
("unsafe_no_drop_flag", Whitelisted, Gated("unsafe_no_drop_flag",
340344
"unsafe_no_drop_flag has unstable semantics \
341345
and may be removed in the future")),
346+
("unsafe_destructor_blind_to_params",
347+
Normal,
348+
Gated("unsafe_destructor_blind_to_params",
349+
"unsafe_destructor_blind_to_params has unstable semantics \
350+
and may be removed in the future")),
342351
("unwind", Whitelisted, Gated("unwind_attributes", "#[unwind] is experimental")),
343352

344353
// used in resolve

0 commit comments

Comments
 (0)