From 0c4d08945679d5632d5c3d61fd27e45a87085709 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 1 Feb 2024 22:42:36 +0000 Subject: [PATCH] Taint borrowck results without running any borrowck if the MIR body was already tainted --- compiler/rustc_borrowck/src/lib.rs | 9 +-- tests/ui/consts/promoted_const_call.stderr | 22 +------- tests/ui/consts/promoted_const_call3.rs | 7 ++- tests/ui/consts/promoted_const_call3.stderr | 55 ++++--------------- tests/ui/consts/promoted_const_call5.rs | 4 +- tests/ui/consts/promoted_const_call5.stderr | 22 +------- .../drop-elaboration-after-borrowck-error.rs | 2 - ...op-elaboration-after-borrowck-error.stderr | 34 ++---------- tests/ui/static/static-drop-scope.rs | 2 - tests/ui/static/static-drop-scope.stderr | 39 +++---------- tests/ui/typeof/issue-100183.stderr | 4 +- 11 files changed, 40 insertions(+), 160 deletions(-) diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index ea48e78509b7c..ae25cf2536815 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -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); } @@ -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"); diff --git a/tests/ui/consts/promoted_const_call.stderr b/tests/ui/consts/promoted_const_call.stderr index 1f6abc0ce7c82..ace449fae9ce6 100644 --- a/tests/ui/consts/promoted_const_call.stderr +++ b/tests/ui/consts/promoted_const_call.stderr @@ -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 | @@ -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`. diff --git a/tests/ui/consts/promoted_const_call3.rs b/tests/ui/consts/promoted_const_call3.rs index 6d68a2de70e47..dc05a3b584424 100644 --- a/tests/ui/consts/promoted_const_call3.rs +++ b/tests/ui/consts/promoted_const_call3.rs @@ -2,13 +2,14 @@ pub const fn id(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 }; diff --git a/tests/ui/consts/promoted_const_call3.stderr b/tests/ui/consts/promoted_const_call3.stderr index af17457a10a1b..34c833d5bb77c 100644 --- a/tests/ui/consts/promoted_const_call3.stderr +++ b/tests/ui/consts/promoted_const_call3.stderr @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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`. diff --git a/tests/ui/consts/promoted_const_call5.rs b/tests/ui/consts/promoted_const_call5.rs index 3ac8d358ce485..41b3988949e65 100644 --- a/tests/ui/consts/promoted_const_call5.rs +++ b/tests/ui/consts/promoted_const_call5.rs @@ -25,9 +25,9 @@ pub const fn new_manually_drop(t: T) -> std::mem::ManuallyDrop { 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 }; diff --git a/tests/ui/consts/promoted_const_call5.stderr b/tests/ui/consts/promoted_const_call5.stderr index f736220b18333..1b5fa4352837e 100644 --- a/tests/ui/consts/promoted_const_call5.stderr +++ b/tests/ui/consts/promoted_const_call5.stderr @@ -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 | @@ -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`. diff --git a/tests/ui/mir/drop-elaboration-after-borrowck-error.rs b/tests/ui/mir/drop-elaboration-after-borrowck-error.rs index 624b464ecef25..cbca3eebf19d1 100644 --- a/tests/ui/mir/drop-elaboration-after-borrowck-error.rs +++ b/tests/ui/mir/drop-elaboration-after-borrowck-error.rs @@ -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; 1]); @@ -17,7 +16,6 @@ impl B { //~^ ERROR destructor of self.0[0] = other; //~^ ERROR destructor of - //~| ERROR use of moved value self } } diff --git a/tests/ui/mir/drop-elaboration-after-borrowck-error.stderr b/tests/ui/mir/drop-elaboration-after-borrowck-error.stderr index d961061729d4c..22d05fa4ddab0 100644 --- a/tests/ui/mir/drop-elaboration-after-borrowck-error.stderr +++ b/tests/ui/mir/drop-elaboration-after-borrowck-error.stderr @@ -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; | ^^^^^^^^^ @@ -40,7 +26,7 @@ LL | self.0[0] = other; | value is dropped here error[E0493]: destructor of `B` 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 @@ -48,18 +34,6 @@ LL | let _this = self; 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`, 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`. diff --git a/tests/ui/static/static-drop-scope.rs b/tests/ui/static/static-drop-scope.rs index 34afa9873a388..74b224c9be085 100644 --- a/tests/ui/static/static-drop-scope.rs +++ b/tests/ui/static/static-drop-scope.rs @@ -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 diff --git a/tests/ui/static/static-drop-scope.stderr b/tests/ui/static/static-drop-scope.stderr index cedcb7367949f..2c55161628fb8 100644 --- a/tests/ui/static/static-drop-scope.stderr +++ b/tests/ui/static/static-drop-scope.stderr @@ -6,36 +6,16 @@ LL | static PROMOTION_FAIL_S: Option<&'static WithDtor> = Some(&WithDtor); | | | the destructor for this type cannot be evaluated in statics -error[E0716]: temporary value dropped while borrowed - --> $DIR/static-drop-scope.rs:7:60 - | -LL | static PROMOTION_FAIL_S: Option<&'static WithDtor> = Some(&WithDtor); - | ------^^^^^^^^- - | | | | - | | | temporary value is freed at the end of this statement - | | creates a temporary value which is freed while still in use - | using this value as a static requires that borrow lasts for `'static` - error[E0493]: destructor of `WithDtor` cannot be evaluated at compile-time - --> $DIR/static-drop-scope.rs:11:59 + --> $DIR/static-drop-scope.rs:10:59 | LL | const PROMOTION_FAIL_C: Option<&'static WithDtor> = Some(&WithDtor); | ^^^^^^^^- value is dropped here | | | the destructor for this type cannot be evaluated in constants -error[E0716]: temporary value dropped while borrowed - --> $DIR/static-drop-scope.rs:11:59 - | -LL | const PROMOTION_FAIL_C: Option<&'static WithDtor> = Some(&WithDtor); - | ------^^^^^^^^- - | | | | - | | | temporary value is freed at the end of this statement - | | creates a temporary value which is freed while still in use - | using this value as a constant requires that borrow lasts for `'static` - error[E0493]: destructor of `(WithDtor, i32)` cannot be evaluated at compile-time - --> $DIR/static-drop-scope.rs:15:28 + --> $DIR/static-drop-scope.rs:13:28 | LL | static EARLY_DROP_S: i32 = (WithDtor, 0).1; | ^^^^^^^^^^^^^ - value is dropped here @@ -43,7 +23,7 @@ LL | static EARLY_DROP_S: i32 = (WithDtor, 0).1; | the destructor for this type cannot be evaluated in statics error[E0493]: destructor of `(WithDtor, i32)` cannot be evaluated at compile-time - --> $DIR/static-drop-scope.rs:18:27 + --> $DIR/static-drop-scope.rs:16:27 | LL | const EARLY_DROP_C: i32 = (WithDtor, 0).1; | ^^^^^^^^^^^^^ - value is dropped here @@ -51,7 +31,7 @@ LL | const EARLY_DROP_C: i32 = (WithDtor, 0).1; | the destructor for this type cannot be evaluated in constants error[E0493]: destructor of `T` cannot be evaluated at compile-time - --> $DIR/static-drop-scope.rs:21:24 + --> $DIR/static-drop-scope.rs:19:24 | LL | const fn const_drop(_: T) {} | ^ - value is dropped here @@ -59,7 +39,7 @@ LL | const fn const_drop(_: T) {} | the destructor for this type cannot be evaluated in constant functions error[E0493]: destructor of `(T, ())` cannot be evaluated at compile-time - --> $DIR/static-drop-scope.rs:25:5 + --> $DIR/static-drop-scope.rs:23:5 | LL | (x, ()).1 | ^^^^^^^ the destructor for this type cannot be evaluated in constant functions @@ -68,7 +48,7 @@ LL | } | - value is dropped here error[E0493]: destructor of `(Option, i32)` cannot be evaluated at compile-time - --> $DIR/static-drop-scope.rs:29:34 + --> $DIR/static-drop-scope.rs:27:34 | LL | const EARLY_DROP_C_OPTION: i32 = (Some(WithDtor), 0).1; | ^^^^^^^^^^^^^^^^^^^ - value is dropped here @@ -76,14 +56,13 @@ LL | const EARLY_DROP_C_OPTION: i32 = (Some(WithDtor), 0).1; | the destructor for this type cannot be evaluated in constants error[E0493]: destructor of `(Option, i32)` cannot be evaluated at compile-time - --> $DIR/static-drop-scope.rs:34:43 + --> $DIR/static-drop-scope.rs:32:43 | LL | const EARLY_DROP_C_OPTION_CONSTANT: i32 = (HELPER, 0).1; | ^^^^^^^^^^^ - value is dropped here | | | the destructor for this type cannot be evaluated in constants -error: aborting due to 10 previous errors +error: aborting due to 8 previous errors -Some errors have detailed explanations: E0493, E0716. -For more information about an error, try `rustc --explain E0493`. +For more information about this error, try `rustc --explain E0493`. diff --git a/tests/ui/typeof/issue-100183.stderr b/tests/ui/typeof/issue-100183.stderr index 7be923d938357..57317d449cf3a 100644 --- a/tests/ui/typeof/issue-100183.stderr +++ b/tests/ui/typeof/issue-100183.stderr @@ -6,8 +6,8 @@ LL | y: (typeof("hey"),), | help: consider replacing `typeof(...)` with an actual type | -LL | y: (&'static str,), - | ~~~~~~~~~~~~ +LL | y: (&str,), + | ~~~~ error: aborting due to 1 previous error