Skip to content

Commit 45abb02

Browse files
authored
Rollup merge of rust-lang#52782 - pnkfelix:issue-45696-dangly-paths-for-box, r=eddyb
[NLL] Dangly paths for box Special-case `Box` in `rustc_mir::borrow_check`. Since we know dropping a box will not access any `&mut` or `&` references, it is safe to model its destructor as only touching the contents *owned* by the box. ---- There are three main things going on here: 1. The first main thing, this PR is fixing a bug in NLL where `rustc` previously would issue a diagnostic error in a case like this: ```rust fn foo(x: Box<&mut i32>) -> &mut i32 { &mut **x } ``` such code was accepted by the AST-borrowck in the past, but NLL was rejecting it with the following message ([playground](https://play.rust-lang.org/?gist=13c5560f73bfb16d6dab3ceaad44c0f8&version=nightly&mode=release&edition=2015)) ``` error[E0597]: `**x` does not live long enough --> src/main.rs:3:40 | 3 | fn foo(x: Box<&mut i32>) -> &mut i32 { &mut **x } | ^^^^^^^^ - `**x` dropped here while still borrowed | | | borrowed value does not live long enough | note: borrowed value must be valid for the anonymous lifetime rust-lang#1 defined on the function body at 3:1... --> src/main.rs:3:1 | 3 | fn foo(x: Box<&mut i32>) -> &mut i32 { &mut **x } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to previous error ``` 2. The second main thing: The reason such code was previously rejected was because NLL (MIR-borrowck) incorporates a fix for issue rust-lang#31567, where it models a destructor's execution as potentially accessing any borrows held by the thing being destructed. The tests with `Scribble` model this, showing that the compiler now catches such unsoundness. However, that fix for issue rust-lang#31567 is too strong, in that NLL (MIR-borrowck) includes `Box` as one of the types with a destructor that potentially accesses any borrows held by the box. This thus was the cause of the main remaining discrepancy between AST-borrowck and MIR-borrowck, as documented in issue rust-lang#45696, specifically in [the last example of this comment](rust-lang#45696 (comment)), which I have adapted into the `fn foo` shown above. We did close issue rust-lang#45696 back in December of 2017, but AFAICT that example was not fixed by PR rust-lang#46268. (And we did not include a test, etc etc.) This PR fixes that case, by trying to model the so-called `DerefPure` semantics of `Box<T>` when we traverse the type of the input to `visit_terminator_drop`. 3. The third main thing is that during a review of the first draft of this PR, @matthewjasper pointed out that the new traversal of `Box<T>` could cause the compiler to infinite loop. I have adjusted the PR to avoid this (by tracking what types we have previously seen), and added a much needed test of this somewhat odd scenario. (Its an odd scenario because the particular case only arises for things like `struct A(Box<A>);`, something which cannot be constructed in practice.) Fix rust-lang#45696.
2 parents 5c54bd9 + c02c00b commit 45abb02

9 files changed

+586
-9
lines changed

src/librustc_mir/borrow_check/mod.rs

+146-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc::mir::{ClearCrossCrate, Local, Location, Mir, Mutability, Operand, Pla
2222
use rustc::mir::{Field, Projection, ProjectionElem, Rvalue, Statement, StatementKind};
2323
use rustc::mir::{Terminator, TerminatorKind};
2424
use rustc::ty::query::Providers;
25-
use rustc::ty::{self, ParamEnv, TyCtxt};
25+
use rustc::ty::{self, ParamEnv, TyCtxt, Ty};
2626

2727
use rustc_errors::{Diagnostic, DiagnosticBuilder, Level};
2828
use rustc_data_structures::graph::dominators::Dominators;
@@ -598,7 +598,12 @@ impl<'cx, 'gcx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx
598598
// that is useful later.
599599
let drop_place_ty = gcx.lift(&drop_place_ty).unwrap();
600600

601-
self.visit_terminator_drop(loc, term, flow_state, drop_place, drop_place_ty, span);
601+
debug!("visit_terminator_drop \
602+
loc: {:?} term: {:?} drop_place: {:?} drop_place_ty: {:?} span: {:?}",
603+
loc, term, drop_place, drop_place_ty, span);
604+
605+
self.visit_terminator_drop(
606+
loc, term, flow_state, drop_place, drop_place_ty, span, SeenTy(None));
602607
}
603608
TerminatorKind::DropAndReplace {
604609
location: ref drop_place,
@@ -832,6 +837,35 @@ impl InitializationRequiringAction {
832837
}
833838
}
834839

840+
/// A simple linked-list threaded up the stack of recursive calls in `visit_terminator_drop`.
841+
#[derive(Copy, Clone, Debug)]
842+
struct SeenTy<'a, 'gcx: 'a>(Option<(Ty<'gcx>, &'a SeenTy<'a, 'gcx>)>);
843+
844+
impl<'a, 'gcx> SeenTy<'a, 'gcx> {
845+
/// Return a new list with `ty` prepended to the front of `self`.
846+
fn cons(&'a self, ty: Ty<'gcx>) -> Self {
847+
SeenTy(Some((ty, self)))
848+
}
849+
850+
/// True if and only if `ty` occurs on the linked list `self`.
851+
fn have_seen(self, ty: Ty) -> bool {
852+
let mut this = self.0;
853+
loop {
854+
match this {
855+
None => return false,
856+
Some((seen_ty, recur)) => {
857+
if seen_ty == ty {
858+
return true;
859+
} else {
860+
this = recur.0;
861+
continue;
862+
}
863+
}
864+
}
865+
}
866+
}
867+
}
868+
835869
impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
836870
/// Invokes `access_place` as appropriate for dropping the value
837871
/// at `drop_place`. Note that the *actual* `Drop` in the MIR is
@@ -847,14 +881,57 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
847881
drop_place: &Place<'tcx>,
848882
erased_drop_place_ty: ty::Ty<'gcx>,
849883
span: Span,
884+
prev_seen: SeenTy<'_, 'gcx>,
850885
) {
886+
if prev_seen.have_seen(erased_drop_place_ty) {
887+
// if we have directly seen the input ty `T`, then we must
888+
// have had some *direct* ownership loop between `T` and
889+
// some directly-owned (as in, actually traversed by
890+
// recursive calls below) part that is also of type `T`.
891+
//
892+
// Note: in *all* such cases, the data in question cannot
893+
// be constructed (nor destructed) in finite time/space.
894+
//
895+
// Proper examples, some of which are statically rejected:
896+
//
897+
// * `struct A { field: A, ... }`:
898+
// statically rejected as infinite size
899+
//
900+
// * `type B = (B, ...);`:
901+
// statically rejected as cyclic
902+
//
903+
// * `struct C { field: Box<C>, ... }`
904+
// * `struct D { field: Box<(D, D)>, ... }`:
905+
// *accepted*, though impossible to construct
906+
//
907+
// Here is *NOT* an example:
908+
// * `struct Z { field: Option<Box<Z>>, ... }`:
909+
// Here, the type is both representable in finite space (due to the boxed indirection)
910+
// and constructable in finite time (since the recursion can bottom out with `None`).
911+
// This is an obvious instance of something the compiler must accept.
912+
//
913+
// Since some of the above impossible cases like `C` and
914+
// `D` are accepted by the compiler, we must take care not
915+
// to infinite-loop while processing them. But since such
916+
// cases cannot actually arise, it is sound for us to just
917+
// skip them during drop. If the developer uses unsafe
918+
// code to construct them, they should not be surprised by
919+
// weird drop behavior in their resulting code.
920+
debug!("visit_terminator_drop previously seen \
921+
erased_drop_place_ty: {:?} on prev_seen: {:?}; returning early.",
922+
erased_drop_place_ty, prev_seen);
923+
return;
924+
}
925+
851926
let gcx = self.tcx.global_tcx();
852927
let drop_field = |mir: &mut MirBorrowckCtxt<'cx, 'gcx, 'tcx>,
853928
(index, field): (usize, ty::Ty<'gcx>)| {
854929
let field_ty = gcx.normalize_erasing_regions(mir.param_env, field);
855930
let place = drop_place.clone().field(Field::new(index), field_ty);
856931

857-
mir.visit_terminator_drop(loc, term, flow_state, &place, field_ty, span);
932+
debug!("visit_terminator_drop drop_field place: {:?} field_ty: {:?}", place, field_ty);
933+
let seen = prev_seen.cons(erased_drop_place_ty);
934+
mir.visit_terminator_drop(loc, term, flow_state, &place, field_ty, span, seen);
858935
};
859936

860937
match erased_drop_place_ty.sty {
@@ -899,20 +976,84 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
899976
.enumerate()
900977
.for_each(|field| drop_field(self, field));
901978
}
979+
980+
// #45696: special-case Box<T> by treating its dtor as
981+
// only deep *across owned content*. Namely, we know
982+
// dropping a box does not touch data behind any
983+
// references it holds; if we were to instead fall into
984+
// the base case below, we would have a Deep Write due to
985+
// the box being `needs_drop`, and that Deep Write would
986+
// touch `&mut` data in the box.
987+
ty::TyAdt(def, _) if def.is_box() => {
988+
// When/if we add a `&own T` type, this action would
989+
// be like running the destructor of the `&own T`.
990+
// (And the owner of backing storage referenced by the
991+
// `&own T` would be responsible for deallocating that
992+
// backing storage.)
993+
994+
// we model dropping any content owned by the box by
995+
// recurring on box contents. This catches cases like
996+
// `Box<Box<ScribbleWhenDropped<&mut T>>>`, while
997+
// still restricting Write to *owned* content.
998+
let ty = erased_drop_place_ty.boxed_ty();
999+
let deref_place = drop_place.clone().deref();
1000+
debug!("visit_terminator_drop drop-box-content deref_place: {:?} ty: {:?}",
1001+
deref_place, ty);
1002+
let seen = prev_seen.cons(erased_drop_place_ty);
1003+
self.visit_terminator_drop(
1004+
loc, term, flow_state, &deref_place, ty, span, seen);
1005+
}
1006+
9021007
_ => {
9031008
// We have now refined the type of the value being
9041009
// dropped (potentially) to just the type of a
9051010
// subfield; so check whether that field's type still
906-
// "needs drop". If so, we assume that the destructor
907-
// may access any data it likes (i.e., a Deep Write).
1011+
// "needs drop".
9081012
if erased_drop_place_ty.needs_drop(gcx, self.param_env) {
1013+
// If so, we assume that the destructor may access
1014+
// any data it likes (i.e., a Deep Write).
9091015
self.access_place(
9101016
ContextKind::Drop.new(loc),
9111017
(drop_place, span),
9121018
(Deep, Write(WriteKind::StorageDeadOrDrop)),
9131019
LocalMutationIsAllowed::Yes,
9141020
flow_state,
9151021
);
1022+
} else {
1023+
// If there is no destructor, we still include a
1024+
// *shallow* write. This essentially ensures that
1025+
// borrows of the memory directly at `drop_place`
1026+
// cannot continue to be borrowed across the drop.
1027+
//
1028+
// If we were to use a Deep Write here, then any
1029+
// `&mut T` that is reachable from `drop_place`
1030+
// would get invalidated; fixing that is the
1031+
// essence of resolving issue #45696.
1032+
//
1033+
// * Note: In the compiler today, doing a Deep
1034+
// Write here would not actually break
1035+
// anything beyond #45696; for example it does not
1036+
// break this example:
1037+
//
1038+
// ```rust
1039+
// fn reborrow(x: &mut i32) -> &mut i32 { &mut *x }
1040+
// ```
1041+
//
1042+
// Why? Because we do not schedule/emit
1043+
// `Drop(x)` in the MIR unless `x` needs drop in
1044+
// the first place.
1045+
//
1046+
// FIXME: Its possible this logic actually should
1047+
// be attached to the `StorageDead` statement
1048+
// rather than the `Drop`. See discussion on PR
1049+
// #52782.
1050+
self.access_place(
1051+
ContextKind::Drop.new(loc),
1052+
(drop_place, span),
1053+
(Shallow(None), Write(WriteKind::StorageDeadOrDrop)),
1054+
LocalMutationIsAllowed::Yes,
1055+
flow_state,
1056+
);
9161057
}
9171058
}
9181059
}

src/test/ui/generator/dropck.nll.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ LL | }
99
| |
1010
| `*cell` dropped here while still borrowed
1111
| borrow later used here, when `gen` is dropped
12-
|
13-
= note: values in a scope are dropped in the opposite order they are defined
1412

1513
error[E0597]: `ref_` does not live long enough
1614
--> $DIR/dropck.rs:22:11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// rust-lang/rust#45696: This test is checking that we can return
12+
// mutable borrows owned by boxes even when the boxes are dropped.
13+
//
14+
// We will explicitly test AST-borrowck, NLL, and migration modes;
15+
// thus we will also skip the automated compare-mode=nll.
16+
17+
// revisions: ast nll migrate
18+
// ignore-compare-mode-nll
19+
20+
#![cfg_attr(nll, feature(nll))]
21+
//[migrate]compile-flags: -Z borrowck=migrate -Z two-phase-borrows
22+
23+
// run-pass
24+
25+
// This function shows quite directly what is going on: We have a
26+
// reborrow of contents within the box.
27+
fn return_borrow_from_dropped_box_1(x: Box<&mut u32>) -> &mut u32 { &mut **x }
28+
29+
// This function is the way you'll probably see this in practice (the
30+
// reborrow is now implicit).
31+
fn return_borrow_from_dropped_box_2(x: Box<&mut u32>) -> &mut u32 { *x }
32+
33+
// For the remaining tests we just add some fields or other
34+
// indirection to ensure that the compiler isn't just special-casing
35+
// the above `Box<&mut T>` as the only type that would work.
36+
37+
// Here we add a tuple of indirection between the box and the
38+
// reference.
39+
type BoxedTup<'a, 'b> = Box<(&'a mut u32, &'b mut u32)>;
40+
41+
fn return_borrow_of_field_from_dropped_box_1<'a>(x: BoxedTup<'a, '_>) -> &'a mut u32 {
42+
&mut *x.0
43+
}
44+
45+
fn return_borrow_of_field_from_dropped_box_2<'a>(x: BoxedTup<'a, '_>) -> &'a mut u32 {
46+
x.0
47+
}
48+
49+
fn return_borrow_from_dropped_tupled_box_1<'a>(x: (BoxedTup<'a, '_>, &mut u32)) -> &'a mut u32 {
50+
&mut *(x.0).0
51+
}
52+
53+
fn return_borrow_from_dropped_tupled_box_2<'a>(x: (BoxedTup<'a, '_>, &mut u32)) -> &'a mut u32 {
54+
(x.0).0
55+
}
56+
57+
fn basic_tests() {
58+
let mut x = 2;
59+
let mut y = 3;
60+
let mut z = 4;
61+
*return_borrow_from_dropped_box_1(Box::new(&mut x)) += 10;
62+
assert_eq!((x, y, z), (12, 3, 4));
63+
*return_borrow_from_dropped_box_2(Box::new(&mut x)) += 10;
64+
assert_eq!((x, y, z), (22, 3, 4));
65+
*return_borrow_of_field_from_dropped_box_1(Box::new((&mut x, &mut y))) += 10;
66+
assert_eq!((x, y, z), (32, 3, 4));
67+
*return_borrow_of_field_from_dropped_box_2(Box::new((&mut x, &mut y))) += 10;
68+
assert_eq!((x, y, z), (42, 3, 4));
69+
*return_borrow_from_dropped_tupled_box_1((Box::new((&mut x, &mut y)), &mut z)) += 10;
70+
assert_eq!((x, y, z), (52, 3, 4));
71+
*return_borrow_from_dropped_tupled_box_2((Box::new((&mut x, &mut y)), &mut z)) += 10;
72+
assert_eq!((x, y, z), (62, 3, 4));
73+
}
74+
75+
// These scribbling tests have been transcribed from
76+
// issue-45696-scribble-on-boxed-borrow.rs
77+
//
78+
// In the context of that file, these tests are meant to show cases
79+
// that should be *accepted* by the compiler, so here we are actually
80+
// checking that the code we get when they are compiled matches our
81+
// expectations.
82+
83+
struct Scribble<'a>(&'a mut u32);
84+
85+
impl<'a> Drop for Scribble<'a> { fn drop(&mut self) { *self.0 = 42; } }
86+
87+
// this is okay, in both AST-borrowck and NLL: The `Scribble` here *has*
88+
// to strictly outlive `'a`
89+
fn borrowed_scribble<'a>(s: &'a mut Scribble) -> &'a mut u32 {
90+
&mut *s.0
91+
}
92+
93+
// this, by analogy to previous case, is also okay.
94+
fn boxed_borrowed_scribble<'a>(s: Box<&'a mut Scribble>) -> &'a mut u32 {
95+
&mut *(*s).0
96+
}
97+
98+
// this, by analogy to previous case, is also okay.
99+
fn boxed_boxed_borrowed_scribble<'a>(s: Box<Box<&'a mut Scribble>>) -> &'a mut u32 {
100+
&mut *(**s).0
101+
}
102+
103+
fn scribbling_tests() {
104+
let mut x = 1;
105+
{
106+
let mut long_lived = Scribble(&mut x);
107+
*borrowed_scribble(&mut long_lived) += 10;
108+
assert_eq!(*long_lived.0, 11);
109+
// (Scribble dtor runs here, after `&mut`-borrow above ends)
110+
}
111+
assert_eq!(x, 42);
112+
x = 1;
113+
{
114+
let mut long_lived = Scribble(&mut x);
115+
*boxed_borrowed_scribble(Box::new(&mut long_lived)) += 10;
116+
assert_eq!(*long_lived.0, 11);
117+
// (Scribble dtor runs here, after `&mut`-borrow above ends)
118+
}
119+
assert_eq!(x, 42);
120+
x = 1;
121+
{
122+
let mut long_lived = Scribble(&mut x);
123+
*boxed_boxed_borrowed_scribble(Box::new(Box::new(&mut long_lived))) += 10;
124+
assert_eq!(*long_lived.0, 11);
125+
// (Scribble dtor runs here, after `&mut`-borrow above ends)
126+
}
127+
assert_eq!(x, 42);
128+
}
129+
130+
fn main() {
131+
basic_tests();
132+
scribbling_tests();
133+
}

0 commit comments

Comments
 (0)