Skip to content

Commit 1eed3f8

Browse files
committed
refactor: move convert to unstable
We want to keep StableMIR definitions and logic separate from any sort of conversion and usage of internal rustc code. So we bundle all unstable items that have no stability guarantees into `stable_mir::unstable`.
1 parent 66d471c commit 1eed3f8

File tree

9 files changed

+317
-386
lines changed

9 files changed

+317
-386
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::ty::TyCtxt;
99
use rustc_span::def_id::CrateNum;
1010
use scoped_tls::scoped_thread_local;
1111
use stable_mir::Error;
12-
use stable_mir::convert::{RustcInternal, Stable};
12+
use stable_mir::unstable::{RustcInternal, Stable};
1313

1414
use crate::rustc_smir::context::SmirCtxt;
1515
use crate::rustc_smir::{Bridge, SmirContainer, Tables};

compiler/rustc_smir/src/stable_mir/alloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use rustc_smir::{Tables, alloc};
1313

1414
use super::Error;
1515
use super::compiler_interface::BridgeTys;
16-
use super::convert::Stable;
1716
use super::mir::Mutability;
1817
use super::ty::{Allocation, ProvenanceMap};
18+
use super::unstable::Stable;
1919
use crate::rustc_smir;
2020

2121
/// Creates new empty `Allocation` from given `Align`.

compiler/rustc_smir/src/stable_mir/compiler_interface.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_hir::def::DefKind;
99
use rustc_smir::context::SmirCtxt;
1010
use rustc_smir::{Bridge, SmirContainer};
1111
use stable_mir::abi::{FnAbi, Layout, LayoutShape};
12-
use stable_mir::convert::{RustcInternal, Stable};
1312
use stable_mir::crate_def::Attribute;
1413
use stable_mir::mir::alloc::{AllocId, GlobalAlloc};
1514
use stable_mir::mir::mono::{Instance, InstanceDef, StaticDef};
@@ -21,9 +20,10 @@ use stable_mir::ty::{
2120
ImplDef, ImplTrait, IntrinsicDef, LineInfo, MirConst, PolyFnSig, RigidTy, Span, TraitDecl,
2221
TraitDef, Ty, TyConst, TyConstId, TyKind, UintTy, VariantDef,
2322
};
23+
use stable_mir::unstable::{RustcInternal, Stable, new_item_kind};
2424
use stable_mir::{
2525
AssocItems, Crate, CrateDef, CrateItem, CrateItems, CrateNum, DefId, Error, Filename,
26-
ImplTraitDecls, ItemKind, Symbol, TraitDecls, alloc, mir, new_item_kind,
26+
ImplTraitDecls, ItemKind, Symbol, TraitDecls, alloc, mir,
2727
};
2828
use tracing::debug;
2929

compiler/rustc_smir/src/stable_mir/mod.rs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
use std::fmt::Debug;
2828
use std::{fmt, io};
2929

30-
use rustc_hir::def::DefKind;
31-
use rustc_smir::IndexedVal;
30+
pub(crate) use rustc_smir::IndexedVal;
3231
use serde::Serialize;
3332
use stable_mir::compiler_interface::with;
3433
pub use stable_mir::crate_def::{CrateDef, CrateDefItems, CrateDefType, DefId};
@@ -41,7 +40,7 @@ use crate::{rustc_smir, stable_mir};
4140

4241
pub mod abi;
4342
mod alloc;
44-
pub(crate) mod convert;
43+
pub(crate) mod unstable;
4544
#[macro_use]
4645
pub mod crate_def;
4746
pub mod compiler_interface;
@@ -222,43 +221,6 @@ pub fn all_trait_impls() -> ImplTraitDecls {
222221
with(|cx| cx.all_trait_impls())
223222
}
224223

225-
pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
226-
match kind {
227-
DefKind::Mod
228-
| DefKind::Struct
229-
| DefKind::Union
230-
| DefKind::Enum
231-
| DefKind::Variant
232-
| DefKind::Trait
233-
| DefKind::TyAlias
234-
| DefKind::ForeignTy
235-
| DefKind::TraitAlias
236-
| DefKind::AssocTy
237-
| DefKind::TyParam
238-
| DefKind::ConstParam
239-
| DefKind::Macro(_)
240-
| DefKind::ExternCrate
241-
| DefKind::Use
242-
| DefKind::ForeignMod
243-
| DefKind::OpaqueTy
244-
| DefKind::Field
245-
| DefKind::LifetimeParam
246-
| DefKind::Impl { .. }
247-
| DefKind::GlobalAsm => {
248-
unreachable!("Not a valid item kind: {kind:?}");
249-
}
250-
DefKind::Closure | DefKind::AssocFn | DefKind::Fn | DefKind::SyntheticCoroutineBody => {
251-
ItemKind::Fn
252-
}
253-
DefKind::Const | DefKind::InlineConst | DefKind::AssocConst | DefKind::AnonConst => {
254-
ItemKind::Const
255-
}
256-
DefKind::Static { .. } => ItemKind::Static,
257-
DefKind::Ctor(_, rustc_hir::def::CtorKind::Const) => ItemKind::Ctor(CtorKind::Const),
258-
DefKind::Ctor(_, rustc_hir::def::CtorKind::Fn) => ItemKind::Ctor(CtorKind::Fn),
259-
}
260-
}
261-
262224
/// A type that provides internal information but that can still be used for debug purpose.
263225
#[derive(Clone, PartialEq, Eq, Hash, Serialize)]
264226
pub struct Opaque(String);
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
//! This module holds the logic to convert rustc internal ADTs into stable mir ADTs.
2+
//!
3+
//! The conversion from stable to internal is not meant to be complete,
4+
//! and it should be added as when needed to be passed as input to rustc_smir functions.
5+
//!
6+
//! For contributors, please make sure to avoid calling rustc's internal functions and queries.
7+
//! These should be done via `rustc_smir` APIs, but it's possible to access ADT fields directly.
8+
19
use std::ops::RangeInclusive;
210

11+
312
use rustc_smir::Tables;
413
use rustc_smir::context::SmirCtxt;
514

6-
use super::compiler_interface::BridgeTys;
7-
use crate::rustc_smir;
15+
use stable_mir::compiler_interface::BridgeTys;
16+
17+
use crate::{rustc_smir, stable_mir};
18+
19+
use super::Stable;
20+
821

922
mod internal;
1023
mod stable;
@@ -15,10 +28,10 @@ where
1528
{
1629
type T = T::T;
1730

18-
fn stable(
31+
fn stable<'cx>(
1932
&self,
20-
tables: &mut Tables<'tcx, BridgeTys>,
21-
cx: &SmirCtxt<'tcx, BridgeTys>,
33+
tables: &mut Tables<'cx, BridgeTys>,
34+
cx: &SmirCtxt<'cx, BridgeTys>,
2235
) -> Self::T {
2336
(*self).stable(tables, cx)
2437
}
@@ -30,10 +43,10 @@ where
3043
{
3144
type T = Option<T::T>;
3245

33-
fn stable(
46+
fn stable<'cx>(
3447
&self,
35-
tables: &mut Tables<'tcx, BridgeTys>,
36-
cx: &SmirCtxt<'tcx, BridgeTys>,
48+
tables: &mut Tables<'cx, BridgeTys>,
49+
cx: &SmirCtxt<'cx, BridgeTys>,
3750
) -> Self::T {
3851
self.as_ref().map(|value| value.stable(tables, cx))
3952
}
@@ -46,10 +59,10 @@ where
4659
{
4760
type T = Result<T::T, E::T>;
4861

49-
fn stable(
62+
fn stable<'cx>(
5063
&self,
51-
tables: &mut Tables<'tcx, BridgeTys>,
52-
cx: &SmirCtxt<'tcx, BridgeTys>,
64+
tables: &mut Tables<'cx, BridgeTys>,
65+
cx: &SmirCtxt<'cx, BridgeTys>,
5366
) -> Self::T {
5467
match self {
5568
Ok(val) => Ok(val.stable(tables, cx)),
@@ -63,10 +76,10 @@ where
6376
T: Stable<'tcx>,
6477
{
6578
type T = Vec<T::T>;
66-
fn stable(
79+
fn stable<'cx>(
6780
&self,
68-
tables: &mut Tables<'tcx, BridgeTys>,
69-
cx: &SmirCtxt<'tcx, BridgeTys>,
81+
tables: &mut Tables<'cx, BridgeTys>,
82+
cx: &SmirCtxt<'cx, BridgeTys>,
7083
) -> Self::T {
7184
self.iter().map(|e| e.stable(tables, cx)).collect()
7285
}
@@ -78,10 +91,10 @@ where
7891
U: Stable<'tcx>,
7992
{
8093
type T = (T::T, U::T);
81-
fn stable(
94+
fn stable<'cx>(
8295
&self,
83-
tables: &mut Tables<'tcx, BridgeTys>,
84-
cx: &SmirCtxt<'tcx, BridgeTys>,
96+
tables: &mut Tables<'cx, BridgeTys>,
97+
cx: &SmirCtxt<'cx, BridgeTys>,
8598
) -> Self::T {
8699
(self.0.stable(tables, cx), self.1.stable(tables, cx))
87100
}
@@ -92,35 +105,11 @@ where
92105
T: Stable<'tcx>,
93106
{
94107
type T = RangeInclusive<T::T>;
95-
fn stable(
108+
fn stable<'cx>(
96109
&self,
97-
tables: &mut Tables<'tcx, BridgeTys>,
98-
cx: &SmirCtxt<'tcx, BridgeTys>,
110+
tables: &mut Tables<'cx, BridgeTys>,
111+
cx: &SmirCtxt<'cx, BridgeTys>,
99112
) -> Self::T {
100113
RangeInclusive::new(self.start().stable(tables, cx), self.end().stable(tables, cx))
101114
}
102115
}
103-
104-
/// Trait used to convert between an internal MIR type to a Stable MIR type.
105-
pub trait Stable<'tcx> {
106-
/// The stable representation of the type implementing Stable.
107-
type T;
108-
/// Converts an object to the equivalent Stable MIR representation.
109-
fn stable(
110-
&self,
111-
tables: &mut Tables<'tcx, BridgeTys>,
112-
cx: &SmirCtxt<'tcx, BridgeTys>,
113-
) -> Self::T;
114-
}
115-
116-
/// Trait used to translate a stable construct to its rustc counterpart.
117-
///
118-
/// This is basically a mirror of [Stable].
119-
pub trait RustcInternal {
120-
type T<'tcx>;
121-
fn internal<'tcx>(
122-
&self,
123-
tables: &mut Tables<'_, BridgeTys>,
124-
cx: &SmirCtxt<'tcx, BridgeTys>,
125-
) -> Self::T<'tcx>;
126-
}

0 commit comments

Comments
 (0)