Skip to content

Commit dc618dc

Browse files
committed
Taint borrowck results without running any borrowck if the MIR body was already tainted
1 parent 7f7bfe1 commit dc618dc

11 files changed

+40
-160
lines changed

compiler/rustc_borrowck/src/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,16 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
110110
let (input_body, promoted) = tcx.mir_promoted(def);
111111
debug!("run query mir_borrowck: {}", tcx.def_path_str(def));
112112

113-
if input_body.borrow().should_skip() {
114-
debug!("Skipping borrowck because of injected body");
113+
let input_body: &Body<'_> = &input_body.borrow();
114+
115+
if input_body.should_skip() || input_body.tainted_by_errors.is_some() {
116+
debug!("Skipping borrowck because of injected body or tainted body");
115117
// Let's make up a borrowck result! Fun times!
116118
let result = BorrowCheckResult {
117119
concrete_opaque_types: FxIndexMap::default(),
118120
closure_requirements: None,
119121
used_mut_upvars: SmallVec::new(),
120-
tainted_by_errors: None,
122+
tainted_by_errors: input_body.tainted_by_errors,
121123
};
122124
return tcx.arena.alloc(result);
123125
}
@@ -126,7 +128,6 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
126128

127129
let infcx =
128130
tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id)).build();
129-
let input_body: &Body<'_> = &input_body.borrow();
130131
let promoted: &IndexSlice<_, _> = &promoted.borrow();
131132
let opt_closure_req = do_mir_borrowck(&infcx, input_body, promoted, None).0;
132133
debug!("mir_borrowck done");

tests/ui/consts/promoted_const_call.stderr

+1-21
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,6 @@ LL | let _: &'static _ = &id(&Panic);
66
| |
77
| the destructor for this type cannot be evaluated in constants
88

9-
error[E0716]: temporary value dropped while borrowed
10-
--> $DIR/promoted_const_call.rs:11:26
11-
|
12-
LL | let _: &'static _ = &id(&Panic);
13-
| ---------- ^^^^^^^^^^ creates a temporary value which is freed while still in use
14-
| |
15-
| type annotation requires that borrow lasts for `'static`
16-
...
17-
LL | };
18-
| - temporary value is freed at the end of this statement
19-
20-
error[E0716]: temporary value dropped while borrowed
21-
--> $DIR/promoted_const_call.rs:11:30
22-
|
23-
LL | let _: &'static _ = &id(&Panic);
24-
| ---------- ^^^^^ - temporary value is freed at the end of this statement
25-
| | |
26-
| | creates a temporary value which is freed while still in use
27-
| type annotation requires that borrow lasts for `'static`
28-
299
error[E0716]: temporary value dropped while borrowed
3010
--> $DIR/promoted_const_call.rs:17:26
3111
|
@@ -68,7 +48,7 @@ LL | let _: &'static _ = &&(Panic, 0).1;
6848
LL | }
6949
| - temporary value is freed at the end of this statement
7050

71-
error: aborting due to 7 previous errors
51+
error: aborting due to 5 previous errors
7252

7353
Some errors have detailed explanations: E0493, E0716.
7454
For more information about an error, try `rustc --explain E0493`.

tests/ui/consts/promoted_const_call3.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ pub const fn id<T>(x: T) -> T { x }
22
pub const C: () = {
33
let _: &'static _ = &String::new();
44
//~^ ERROR: destructor of `String` cannot be evaluated at compile-time
5-
//~| ERROR: temporary value dropped while borrowed
5+
};
66

7+
pub const _: () = {
78
let _: &'static _ = &id(&String::new());
89
//~^ ERROR: destructor of `String` cannot be evaluated at compile-time
9-
//~| ERROR: temporary value dropped while borrowed
10-
//~| ERROR: temporary value dropped while borrowed
10+
};
1111

12+
pub const _: () = {
1213
let _: &'static _ = &std::mem::ManuallyDrop::new(String::new());
1314
//~^ ERROR: temporary value dropped while borrowed
1415
};
+12-43
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,22 @@
1-
error[E0493]: destructor of `String` cannot be evaluated at compile-time
2-
--> $DIR/promoted_const_call3.rs:7:30
3-
|
4-
LL | let _: &'static _ = &id(&String::new());
5-
| ^^^^^^^^^^^^^ - value is dropped here
6-
| |
7-
| the destructor for this type cannot be evaluated in constants
8-
91
error[E0493]: destructor of `String` cannot be evaluated at compile-time
102
--> $DIR/promoted_const_call3.rs:3:26
113
|
124
LL | let _: &'static _ = &String::new();
135
| ^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constants
14-
...
6+
LL |
157
LL | };
168
| - value is dropped here
179

18-
error[E0716]: temporary value dropped while borrowed
19-
--> $DIR/promoted_const_call3.rs:3:26
20-
|
21-
LL | let _: &'static _ = &String::new();
22-
| ---------- ^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
23-
| |
24-
| type annotation requires that borrow lasts for `'static`
25-
...
26-
LL | };
27-
| - temporary value is freed at the end of this statement
28-
29-
error[E0716]: temporary value dropped while borrowed
30-
--> $DIR/promoted_const_call3.rs:7:26
31-
|
32-
LL | let _: &'static _ = &id(&String::new());
33-
| ---------- ^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
34-
| |
35-
| type annotation requires that borrow lasts for `'static`
36-
...
37-
LL | };
38-
| - temporary value is freed at the end of this statement
39-
40-
error[E0716]: temporary value dropped while borrowed
41-
--> $DIR/promoted_const_call3.rs:7:30
10+
error[E0493]: destructor of `String` cannot be evaluated at compile-time
11+
--> $DIR/promoted_const_call3.rs:8:30
4212
|
4313
LL | let _: &'static _ = &id(&String::new());
44-
| ---------- ^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
45-
| | |
46-
| | creates a temporary value which is freed while still in use
47-
| type annotation requires that borrow lasts for `'static`
14+
| ^^^^^^^^^^^^^ - value is dropped here
15+
| |
16+
| the destructor for this type cannot be evaluated in constants
4817

4918
error[E0716]: temporary value dropped while borrowed
50-
--> $DIR/promoted_const_call3.rs:12:26
19+
--> $DIR/promoted_const_call3.rs:13:26
5120
|
5221
LL | let _: &'static _ = &std::mem::ManuallyDrop::new(String::new());
5322
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -58,7 +27,7 @@ LL | };
5827
| - temporary value is freed at the end of this statement
5928

6029
error[E0716]: temporary value dropped while borrowed
61-
--> $DIR/promoted_const_call3.rs:17:26
30+
--> $DIR/promoted_const_call3.rs:18:26
6231
|
6332
LL | let _: &'static _ = &String::new();
6433
| ---------- ^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -69,7 +38,7 @@ LL | }
6938
| - temporary value is freed at the end of this statement
7039

7140
error[E0716]: temporary value dropped while borrowed
72-
--> $DIR/promoted_const_call3.rs:20:26
41+
--> $DIR/promoted_const_call3.rs:21:26
7342
|
7443
LL | let _: &'static _ = &id(&String::new());
7544
| ---------- ^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -80,7 +49,7 @@ LL | }
8049
| - temporary value is freed at the end of this statement
8150

8251
error[E0716]: temporary value dropped while borrowed
83-
--> $DIR/promoted_const_call3.rs:20:30
52+
--> $DIR/promoted_const_call3.rs:21:30
8453
|
8554
LL | let _: &'static _ = &id(&String::new());
8655
| ---------- ^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
@@ -89,7 +58,7 @@ LL | let _: &'static _ = &id(&String::new());
8958
| type annotation requires that borrow lasts for `'static`
9059

9160
error[E0716]: temporary value dropped while borrowed
92-
--> $DIR/promoted_const_call3.rs:24:26
61+
--> $DIR/promoted_const_call3.rs:25:26
9362
|
9463
LL | let _: &'static _ = &std::mem::ManuallyDrop::new(String::new());
9564
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -99,7 +68,7 @@ LL |
9968
LL | }
10069
| - temporary value is freed at the end of this statement
10170

102-
error: aborting due to 10 previous errors
71+
error: aborting due to 7 previous errors
10372

10473
Some errors have detailed explanations: E0493, E0716.
10574
For more information about an error, try `rustc --explain E0493`.

tests/ui/consts/promoted_const_call5.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ pub const fn new_manually_drop<T>(t: T) -> std::mem::ManuallyDrop<T> {
2525
const C: () = {
2626
let _: &'static _ = &id(&new_string());
2727
//~^ ERROR destructor of `String` cannot be evaluated at compile-time
28-
//~| ERROR: temporary value dropped while borrowed
29-
//~| ERROR: temporary value dropped while borrowed
28+
};
3029

30+
const _: () = {
3131
let _: &'static _ = &new_manually_drop(new_string());
3232
//~^ ERROR: temporary value dropped while borrowed
3333
};

tests/ui/consts/promoted_const_call5.stderr

+1-21
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,6 @@ LL | let _: &'static _ = &id(&new_string());
66
| |
77
| the destructor for this type cannot be evaluated in constants
88

9-
error[E0716]: temporary value dropped while borrowed
10-
--> $DIR/promoted_const_call5.rs:26:26
11-
|
12-
LL | let _: &'static _ = &id(&new_string());
13-
| ---------- ^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
14-
| |
15-
| type annotation requires that borrow lasts for `'static`
16-
...
17-
LL | };
18-
| - temporary value is freed at the end of this statement
19-
20-
error[E0716]: temporary value dropped while borrowed
21-
--> $DIR/promoted_const_call5.rs:26:30
22-
|
23-
LL | let _: &'static _ = &id(&new_string());
24-
| ----^^^^^^^^^^^^-- temporary value is freed at the end of this statement
25-
| | |
26-
| | creates a temporary value which is freed while still in use
27-
| argument requires that borrow lasts for `'static`
28-
299
error[E0716]: temporary value dropped while borrowed
3010
--> $DIR/promoted_const_call5.rs:31:26
3111
|
@@ -68,7 +48,7 @@ LL |
6848
LL | }
6949
| - temporary value is freed at the end of this statement
7050

71-
error: aborting due to 7 previous errors
51+
error: aborting due to 5 previous errors
7252

7353
Some errors have detailed explanations: E0493, E0716.
7454
For more information about an error, try `rustc --explain E0493`.

tests/ui/mir/drop-elaboration-after-borrowck-error.rs

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ static A: () = {
66
//~^ ERROR destructor of
77
a[0] = String::new();
88
//~^ ERROR destructor of
9-
//~| ERROR binding `a` isn't initialized
109
};
1110

1211
struct B<T>([T; 1]);
@@ -17,7 +16,6 @@ impl<T> B<T> {
1716
//~^ ERROR destructor of
1817
self.0[0] = other;
1918
//~^ ERROR destructor of
20-
//~| ERROR use of moved value
2119
self
2220
}
2321
}

tests/ui/mir/drop-elaboration-after-borrowck-error.stderr

+4-30
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,8 @@ LL | let a: [String; 1];
1616
LL | };
1717
| - value is dropped here
1818

19-
error[E0381]: used binding `a` isn't initialized
20-
--> $DIR/drop-elaboration-after-borrowck-error.rs:7:5
21-
|
22-
LL | let a: [String; 1];
23-
| - binding declared here but left uninitialized
24-
LL |
25-
LL | a[0] = String::new();
26-
| ^^^^ `a` used here but it isn't initialized
27-
|
28-
help: consider assigning a value
29-
|
30-
LL | let a: [String; 1] = todo!();
31-
| +++++++++
32-
3319
error[E0493]: destructor of `T` cannot be evaluated at compile-time
34-
--> $DIR/drop-elaboration-after-borrowck-error.rs:18:9
20+
--> $DIR/drop-elaboration-after-borrowck-error.rs:17:9
3521
|
3622
LL | self.0[0] = other;
3723
| ^^^^^^^^^
@@ -40,26 +26,14 @@ LL | self.0[0] = other;
4026
| value is dropped here
4127

4228
error[E0493]: destructor of `B<T>` cannot be evaluated at compile-time
43-
--> $DIR/drop-elaboration-after-borrowck-error.rs:16:13
29+
--> $DIR/drop-elaboration-after-borrowck-error.rs:15:13
4430
|
4531
LL | let _this = self;
4632
| ^^^^^ the destructor for this type cannot be evaluated in constant functions
4733
...
4834
LL | }
4935
| - value is dropped here
5036

51-
error[E0382]: use of moved value: `self.0`
52-
--> $DIR/drop-elaboration-after-borrowck-error.rs:18:9
53-
|
54-
LL | pub const fn f(mut self, other: T) -> Self {
55-
| -------- move occurs because `self` has type `B<T>`, which does not implement the `Copy` trait
56-
LL | let _this = self;
57-
| ---- value moved here
58-
LL |
59-
LL | self.0[0] = other;
60-
| ^^^^^^^^^ value used here after move
61-
62-
error: aborting due to 6 previous errors
37+
error: aborting due to 4 previous errors
6338

64-
Some errors have detailed explanations: E0381, E0382, E0493.
65-
For more information about an error, try `rustc --explain E0381`.
39+
For more information about this error, try `rustc --explain E0493`.

tests/ui/static/static-drop-scope.rs

-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ impl Drop for WithDtor {
66

77
static PROMOTION_FAIL_S: Option<&'static WithDtor> = Some(&WithDtor);
88
//~^ ERROR destructor of
9-
//~| ERROR temporary value dropped while borrowed
109

1110
const PROMOTION_FAIL_C: Option<&'static WithDtor> = Some(&WithDtor);
1211
//~^ ERROR destructor of
13-
//~| ERROR temporary value dropped while borrowed
1412

1513
static EARLY_DROP_S: i32 = (WithDtor, 0).1;
1614
//~^ ERROR destructor of

0 commit comments

Comments
 (0)