about summary refs log tree commit diff
path: root/compiler/rustc_smir/src/rustc_internal
diff options
context:
space:
mode:
authorMakai <m4kai410@gmail.com>2025-06-09 08:13:27 +0000
committerMakai <m4kai410@gmail.com>2025-07-04 01:57:38 +0000
commitefa26e1d64c374fa9960e5bf8709937fc251816b (patch)
treeda58b00d3661f13f84df5fcd1282b22f05527212 /compiler/rustc_smir/src/rustc_internal
parentb5a2e7d08057bc04c823ab1611e60cccb2d2cce4 (diff)
fix: resolve the unsoundness
add a new trait `InternalCx`, which defines the methods that are fine to call from `RustcInternal`. `RustcInternal::internal()` then takes a `impl InternalCx<'tcx>` instead of `TyCtxt<'tcx>`.

make `tcx` in `SmirCtxt` public, since we need to pass it to `RustcInternal::internal()` in `SmirInterface`.
Diffstat (limited to 'compiler/rustc_smir/src/rustc_internal')
-rw-r--r--compiler/rustc_smir/src/rustc_internal/mod.rs13
1 files changed, 8 insertions, 5 deletions
diff --git a/compiler/rustc_smir/src/rustc_internal/mod.rs b/compiler/rustc_smir/src/rustc_internal/mod.rs
index b99514a3b73..a8d2ab55d03 100644
--- a/compiler/rustc_smir/src/rustc_internal/mod.rs
+++ b/compiler/rustc_smir/src/rustc_internal/mod.rs
@@ -43,11 +43,14 @@ pub fn stable<'tcx, S: Stable<'tcx>>(item: S) -> S::T {
 /// # Panics
 ///
 /// This function will panic if StableMIR has not been properly initialized.
-pub fn internal<'tcx, S>(item: S) -> S::T<'tcx>
+pub fn internal<'tcx, S>(tcx: TyCtxt<'tcx>, item: S) -> S::T<'tcx>
 where
     S: RustcInternal,
 {
-    with_container(|tables, cx| item.internal(tables, cx))
+    // The tcx argument ensures that the item won't outlive the type context.
+    // See https://github.com/rust-lang/rust/pull/120128/commits/9aace6723572438a94378451793ca37deb768e72
+    // for more details.
+    with_container(|tables, _| item.internal(tables, tcx))
 }
 
 pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
@@ -69,14 +72,14 @@ where
 
 /// Loads the current context and calls a function with it.
 /// Do not nest these, as that will ICE.
-pub(crate) fn with_container<'tcx, R, B: Bridge>(
-    f: impl FnOnce(&mut Tables<'tcx, B>, &SmirCtxt<'tcx, B>) -> R,
+pub(crate) fn with_container<R, B: Bridge>(
+    f: impl for<'tcx> FnOnce(&mut Tables<'tcx, B>, &SmirCtxt<'tcx, B>) -> R,
 ) -> R {
     assert!(TLV.is_set());
     TLV.with(|tlv| {
         let ptr = tlv.get();
         assert!(!ptr.is_null());
-        let container = ptr as *const SmirContainer<'tcx, B>;
+        let container = ptr as *const SmirContainer<'_, B>;
         let mut tables = unsafe { (*container).tables.borrow_mut() };
         let cx = unsafe { (*container).cx.borrow() };
         f(&mut *tables, &*cx)