From d4609f8d4298ec3068b7dc957bad1df91031f347 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 20 Feb 2025 03:36:58 +0000 Subject: [PATCH] Use a probe to avoid registering stray region obligations when re-checking drops in MIR typeck --- .../src/type_check/liveness/trace.rs | 22 ++++++++++++------- compiler/rustc_infer/src/infer/mod.rs | 2 +- tests/ui/borrowck/bad-drop-side-effects.rs | 18 +++++++++++++++ .../ui/borrowck/bad-drop-side-effects.stderr | 9 ++++++++ 4 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 tests/ui/borrowck/bad-drop-side-effects.rs create mode 100644 tests/ui/borrowck/bad-drop-side-effects.stderr diff --git a/compiler/rustc_borrowck/src/type_check/liveness/trace.rs b/compiler/rustc_borrowck/src/type_check/liveness/trace.rs index 148e75aa84cc7..12e8be41614bd 100644 --- a/compiler/rustc_borrowck/src/type_check/liveness/trace.rs +++ b/compiler/rustc_borrowck/src/type_check/liveness/trace.rs @@ -613,9 +613,14 @@ impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> { // types, so there's no guarantee that it succeeds. We also // can't rely on the the `ErrorGuaranteed` from `fully_perform` here // because it comes from delay_span_bug. - let ocx = ObligationCtxt::new_with_diagnostics(&typeck.infcx); - let errors = - match dropck_outlives::compute_dropck_outlives_with_errors(&ocx, op, span) { + // + // Do this inside of a probe because we don't particularly care (or want) + // any region side-effects of this operation in our infcx. + typeck.infcx.probe(|_| { + let ocx = ObligationCtxt::new_with_diagnostics(&typeck.infcx); + let errors = match dropck_outlives::compute_dropck_outlives_with_errors( + &ocx, op, span, + ) { Ok(_) => ocx.select_all_or_error(), Err(e) => { if e.is_empty() { @@ -626,11 +631,12 @@ impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> { } }; - if !errors.is_empty() { - typeck.infcx.err_ctxt().report_fulfillment_errors(errors); - } else { - span_bug!(span, "Rerunning drop data query produced no error."); - } + if !errors.is_empty() { + typeck.infcx.err_ctxt().report_fulfillment_errors(errors); + } else { + span_bug!(span, "Rerunning drop data query produced no error."); + } + }); DropData { dropck_result: Default::default(), region_constraint_data: None } } } diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index c2513a1af1988..88a325a39be52 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -950,7 +950,7 @@ impl<'tcx> InferCtxt<'tcx> { let inner = self.inner.borrow(); assert!(!UndoLogs::>::in_snapshot(&inner.undo_log)); let storage = inner.region_constraint_storage.as_ref().expect("regions already resolved"); - assert!(storage.data.is_empty()); + assert!(storage.data.is_empty(), "{:#?}", storage.data); // We clone instead of taking because borrowck still wants to use the // inference context after calling this for diagnostics and the new // trait solver. diff --git a/tests/ui/borrowck/bad-drop-side-effects.rs b/tests/ui/borrowck/bad-drop-side-effects.rs new file mode 100644 index 0000000000000..a09b7087608f6 --- /dev/null +++ b/tests/ui/borrowck/bad-drop-side-effects.rs @@ -0,0 +1,18 @@ +// Regression test for . + +trait B { + type C; +} + +impl B for &Missing { +//~^ ERROR cannot find type `Missing` in this scope + type C = (); +} + +struct E { + g: ::C, +} + +fn h(i: Box>) {} + +fn main() {} diff --git a/tests/ui/borrowck/bad-drop-side-effects.stderr b/tests/ui/borrowck/bad-drop-side-effects.stderr new file mode 100644 index 0000000000000..0a5998c7e4830 --- /dev/null +++ b/tests/ui/borrowck/bad-drop-side-effects.stderr @@ -0,0 +1,9 @@ +error[E0412]: cannot find type `Missing` in this scope + --> $DIR/bad-drop-side-effects.rs:7:16 + | +LL | impl B for &Missing { + | ^^^^^^^ not found in this scope + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0412`.