Skip to content

Commit

Permalink
Taint borrowck results without running any borrowck if the MIR body w…
Browse files Browse the repository at this point in the history
…as already tainted
  • Loading branch information
oli-obk committed Feb 2, 2024
1 parent 009f970 commit 0c4d089
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 160 deletions.
9 changes: 5 additions & 4 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,16 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
let (input_body, promoted) = tcx.mir_promoted(def);
debug!("run query mir_borrowck: {}", tcx.def_path_str(def));

if input_body.borrow().should_skip() {
debug!("Skipping borrowck because of injected body");
let input_body: &Body<'_> = &input_body.borrow();

if input_body.should_skip() || input_body.tainted_by_errors.is_some() {
debug!("Skipping borrowck because of injected body or tainted body");
// Let's make up a borrowck result! Fun times!
let result = BorrowCheckResult {
concrete_opaque_types: FxIndexMap::default(),
closure_requirements: None,
used_mut_upvars: SmallVec::new(),
tainted_by_errors: None,
tainted_by_errors: input_body.tainted_by_errors,
};
return tcx.arena.alloc(result);
}
Expand All @@ -126,7 +128,6 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {

let infcx =
tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id)).build();
let input_body: &Body<'_> = &input_body.borrow();
let promoted: &IndexSlice<_, _> = &promoted.borrow();
let opt_closure_req = do_mir_borrowck(&infcx, input_body, promoted, None).0;
debug!("mir_borrowck done");
Expand Down
22 changes: 1 addition & 21 deletions tests/ui/consts/promoted_const_call.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,6 @@ LL | let _: &'static _ = &id(&Panic);
| |
| the destructor for this type cannot be evaluated in constants

error[E0716]: temporary value dropped while borrowed
--> $DIR/promoted_const_call.rs:11:26
|
LL | let _: &'static _ = &id(&Panic);
| ---------- ^^^^^^^^^^ creates a temporary value which is freed while still in use
| |
| type annotation requires that borrow lasts for `'static`
...
LL | };
| - temporary value is freed at the end of this statement

error[E0716]: temporary value dropped while borrowed
--> $DIR/promoted_const_call.rs:11:30
|
LL | let _: &'static _ = &id(&Panic);
| ---------- ^^^^^ - temporary value is freed at the end of this statement
| | |
| | creates a temporary value which is freed while still in use
| type annotation requires that borrow lasts for `'static`

error[E0716]: temporary value dropped while borrowed
--> $DIR/promoted_const_call.rs:17:26
|
Expand Down Expand Up @@ -68,7 +48,7 @@ LL | let _: &'static _ = &&(Panic, 0).1;
LL | }
| - temporary value is freed at the end of this statement

error: aborting due to 7 previous errors
error: aborting due to 5 previous errors

Some errors have detailed explanations: E0493, E0716.
For more information about an error, try `rustc --explain E0493`.
7 changes: 4 additions & 3 deletions tests/ui/consts/promoted_const_call3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ pub const fn id<T>(x: T) -> T { x }
pub const C: () = {
let _: &'static _ = &String::new();
//~^ ERROR: destructor of `String` cannot be evaluated at compile-time
//~| ERROR: temporary value dropped while borrowed
};

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

pub const _: () = {
let _: &'static _ = &std::mem::ManuallyDrop::new(String::new());
//~^ ERROR: temporary value dropped while borrowed
};
Expand Down
55 changes: 12 additions & 43 deletions tests/ui/consts/promoted_const_call3.stderr
Original file line number Diff line number Diff line change
@@ -1,53 +1,22 @@
error[E0493]: destructor of `String` cannot be evaluated at compile-time
--> $DIR/promoted_const_call3.rs:7:30
|
LL | let _: &'static _ = &id(&String::new());
| ^^^^^^^^^^^^^ - value is dropped here
| |
| the destructor for this type cannot be evaluated in constants

error[E0493]: destructor of `String` cannot be evaluated at compile-time
--> $DIR/promoted_const_call3.rs:3:26
|
LL | let _: &'static _ = &String::new();
| ^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constants
...
LL |
LL | };
| - value is dropped here

error[E0716]: temporary value dropped while borrowed
--> $DIR/promoted_const_call3.rs:3:26
|
LL | let _: &'static _ = &String::new();
| ---------- ^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
| |
| type annotation requires that borrow lasts for `'static`
...
LL | };
| - temporary value is freed at the end of this statement

error[E0716]: temporary value dropped while borrowed
--> $DIR/promoted_const_call3.rs:7:26
|
LL | let _: &'static _ = &id(&String::new());
| ---------- ^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
| |
| type annotation requires that borrow lasts for `'static`
...
LL | };
| - temporary value is freed at the end of this statement

error[E0716]: temporary value dropped while borrowed
--> $DIR/promoted_const_call3.rs:7:30
error[E0493]: destructor of `String` cannot be evaluated at compile-time
--> $DIR/promoted_const_call3.rs:8:30
|
LL | let _: &'static _ = &id(&String::new());
| ---------- ^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| | |
| | creates a temporary value which is freed while still in use
| type annotation requires that borrow lasts for `'static`
| ^^^^^^^^^^^^^ - value is dropped here
| |
| the destructor for this type cannot be evaluated in constants

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

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

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

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

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

error: aborting due to 10 previous errors
error: aborting due to 7 previous errors

Some errors have detailed explanations: E0493, E0716.
For more information about an error, try `rustc --explain E0493`.
4 changes: 2 additions & 2 deletions tests/ui/consts/promoted_const_call5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ pub const fn new_manually_drop<T>(t: T) -> std::mem::ManuallyDrop<T> {
const C: () = {
let _: &'static _ = &id(&new_string());
//~^ ERROR destructor of `String` cannot be evaluated at compile-time
//~| ERROR: temporary value dropped while borrowed
//~| ERROR: temporary value dropped while borrowed
};

const _: () = {
let _: &'static _ = &new_manually_drop(new_string());
//~^ ERROR: temporary value dropped while borrowed
};
Expand Down
22 changes: 1 addition & 21 deletions tests/ui/consts/promoted_const_call5.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,6 @@ LL | let _: &'static _ = &id(&new_string());
| |
| the destructor for this type cannot be evaluated in constants

error[E0716]: temporary value dropped while borrowed
--> $DIR/promoted_const_call5.rs:26:26
|
LL | let _: &'static _ = &id(&new_string());
| ---------- ^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
| |
| type annotation requires that borrow lasts for `'static`
...
LL | };
| - temporary value is freed at the end of this statement

error[E0716]: temporary value dropped while borrowed
--> $DIR/promoted_const_call5.rs:26:30
|
LL | let _: &'static _ = &id(&new_string());
| ----^^^^^^^^^^^^-- temporary value is freed at the end of this statement
| | |
| | creates a temporary value which is freed while still in use
| argument requires that borrow lasts for `'static`

error[E0716]: temporary value dropped while borrowed
--> $DIR/promoted_const_call5.rs:31:26
|
Expand Down Expand Up @@ -68,7 +48,7 @@ LL |
LL | }
| - temporary value is freed at the end of this statement

error: aborting due to 7 previous errors
error: aborting due to 5 previous errors

Some errors have detailed explanations: E0493, E0716.
For more information about an error, try `rustc --explain E0493`.
2 changes: 0 additions & 2 deletions tests/ui/mir/drop-elaboration-after-borrowck-error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ static A: () = {
//~^ ERROR destructor of
a[0] = String::new();
//~^ ERROR destructor of
//~| ERROR binding `a` isn't initialized
};

struct B<T>([T; 1]);
Expand All @@ -17,7 +16,6 @@ impl<T> B<T> {
//~^ ERROR destructor of
self.0[0] = other;
//~^ ERROR destructor of
//~| ERROR use of moved value
self
}
}
Expand Down
34 changes: 4 additions & 30 deletions tests/ui/mir/drop-elaboration-after-borrowck-error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,8 @@ LL | let a: [String; 1];
LL | };
| - value is dropped here

error[E0381]: used binding `a` isn't initialized
--> $DIR/drop-elaboration-after-borrowck-error.rs:7:5
|
LL | let a: [String; 1];
| - binding declared here but left uninitialized
LL |
LL | a[0] = String::new();
| ^^^^ `a` used here but it isn't initialized
|
help: consider assigning a value
|
LL | let a: [String; 1] = todo!();
| +++++++++

error[E0493]: destructor of `T` cannot be evaluated at compile-time
--> $DIR/drop-elaboration-after-borrowck-error.rs:18:9
--> $DIR/drop-elaboration-after-borrowck-error.rs:17:9
|
LL | self.0[0] = other;
| ^^^^^^^^^
Expand All @@ -40,26 +26,14 @@ LL | self.0[0] = other;
| value is dropped here

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

error[E0382]: use of moved value: `self.0`
--> $DIR/drop-elaboration-after-borrowck-error.rs:18:9
|
LL | pub const fn f(mut self, other: T) -> Self {
| -------- move occurs because `self` has type `B<T>`, which does not implement the `Copy` trait
LL | let _this = self;
| ---- value moved here
LL |
LL | self.0[0] = other;
| ^^^^^^^^^ value used here after move

error: aborting due to 6 previous errors
error: aborting due to 4 previous errors

Some errors have detailed explanations: E0381, E0382, E0493.
For more information about an error, try `rustc --explain E0381`.
For more information about this error, try `rustc --explain E0493`.
2 changes: 0 additions & 2 deletions tests/ui/static/static-drop-scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ impl Drop for WithDtor {

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

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

static EARLY_DROP_S: i32 = (WithDtor, 0).1;
//~^ ERROR destructor of
Expand Down
Loading

0 comments on commit 0c4d089

Please sign in to comment.