Skip to content

Commit e52c8b5

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

11 files changed

+64
-171
lines changed

compiler/rustc_borrowck/src/lib.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,19 @@ 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() {
113+
let input_body: &Body<'_> = &input_body.borrow();
114+
115+
if let Some(guar) = input_body.tainted_by_errors {
116+
let result = BorrowCheckResult {
117+
concrete_opaque_types: FxIndexMap::default(),
118+
closure_requirements: None,
119+
used_mut_upvars: SmallVec::new(),
120+
tainted_by_errors: Some(guar),
121+
};
122+
return tcx.arena.alloc(result);
123+
}
124+
125+
if input_body.should_skip() {
114126
debug!("Skipping borrowck because of injected body");
115127
// Let's make up a borrowck result! Fun times!
116128
let result = BorrowCheckResult {
@@ -126,7 +138,6 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
126138

127139
let infcx =
128140
tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id)).build();
129-
let input_body: &Body<'_> = &input_body.borrow();
130141
let promoted: &IndexSlice<_, _> = &promoted.borrow();
131142
let opt_closure_req = do_mir_borrowck(&infcx, input_body, promoted, None).0;
132143
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

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

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

14+
pub const _: () = {
1215
let _: &'static _ = &std::mem::ManuallyDrop::new(String::new());
1316
//~^ ERROR: temporary value dropped while borrowed
1417
};
+13-44
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,22 @@
11
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-
9-
error[E0493]: destructor of `String` cannot be evaluated at compile-time
10-
--> $DIR/promoted_const_call3.rs:3:26
2+
--> $DIR/promoted_const_call3.rs:5: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:10: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:15: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:20: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:23: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:23: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:27: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

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#[rustc_promotable]
66
#[stable(feature = "a", since = "1.0.0")]
77
#[rustc_const_stable(feature = "a", since = "1.0.0")]
8-
pub const fn id<T>(x: &'static T) -> &'static T { x }
8+
pub const fn id<T>(x: &'static T) -> &'static T {
9+
x
10+
}
911

1012
#[rustc_promotable]
1113
#[stable(feature = "a", since = "1.0.0")]
@@ -17,17 +19,16 @@ pub const fn new_string() -> String {
1719
#[rustc_promotable]
1820
#[stable(feature = "a", since = "1.0.0")]
1921
#[rustc_const_stable(feature = "a", since = "1.0.0")]
20-
pub const fn new_manually_drop<T>(t: T) -> std::mem::ManuallyDrop<T> {
22+
pub const fn new_manually_drop<T>(t: T) -> std::mem::ManuallyDrop<T> {
2123
std::mem::ManuallyDrop::new(t)
2224
}
2325

24-
25-
const C: () = {
26+
const _: () = {
2627
let _: &'static _ = &id(&new_string());
2728
//~^ ERROR destructor of `String` cannot be evaluated at compile-time
28-
//~| ERROR: temporary value dropped while borrowed
29-
//~| ERROR: temporary value dropped while borrowed
29+
};
3030

31+
const _: () = {
3132
let _: &'static _ = &new_manually_drop(new_string());
3233
//~^ ERROR: temporary value dropped while borrowed
3334
};
+6-26
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,13 @@
11
error[E0493]: destructor of `String` cannot be evaluated at compile-time
2-
--> $DIR/promoted_const_call5.rs:26:30
2+
--> $DIR/promoted_const_call5.rs:27:30
33
|
44
LL | let _: &'static _ = &id(&new_string());
55
| ^^^^^^^^^^^^ - value is dropped here
66
| |
77
| the destructor for this type cannot be evaluated in constants
88

99
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-
29-
error[E0716]: temporary value dropped while borrowed
30-
--> $DIR/promoted_const_call5.rs:31:26
10+
--> $DIR/promoted_const_call5.rs:32:26
3111
|
3212
LL | let _: &'static _ = &new_manually_drop(new_string());
3313
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -38,7 +18,7 @@ LL | };
3818
| - temporary value is freed at the end of this statement
3919

4020
error[E0716]: temporary value dropped while borrowed
41-
--> $DIR/promoted_const_call5.rs:36:26
21+
--> $DIR/promoted_const_call5.rs:37:26
4222
|
4323
LL | let _: &'static _ = &id(&new_string());
4424
| ---------- ^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -49,7 +29,7 @@ LL | }
4929
| - temporary value is freed at the end of this statement
5030

5131
error[E0716]: temporary value dropped while borrowed
52-
--> $DIR/promoted_const_call5.rs:36:30
32+
--> $DIR/promoted_const_call5.rs:37:30
5333
|
5434
LL | let _: &'static _ = &id(&new_string());
5535
| ----^^^^^^^^^^^^-- temporary value is freed at the end of this statement
@@ -58,7 +38,7 @@ LL | let _: &'static _ = &id(&new_string());
5838
| argument requires that borrow lasts for `'static`
5939

6040
error[E0716]: temporary value dropped while borrowed
61-
--> $DIR/promoted_const_call5.rs:40:26
41+
--> $DIR/promoted_const_call5.rs:41:26
6242
|
6343
LL | let _: &'static _ = &new_manually_drop(new_string());
6444
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -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

+1-3
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
@@ -34,4 +32,4 @@ const HELPER: Option<WithDtor> = Some(WithDtor);
3432
const EARLY_DROP_C_OPTION_CONSTANT: i32 = (HELPER, 0).1;
3533
//~^ ERROR destructor of
3634

37-
fn main () {}
35+
fn main() {}

0 commit comments

Comments
 (0)