Skip to content

Commit 303f01a

Browse files
committed
refactor: make SmirInterface a trait and impl it for SmirContainer
- rewrite all `SmirInterface` apis. - add `BridgeTys` to impl those associated types in `Bridge`. - move `**_def()` stuffs living in `impl Tables` from `rustc_internal` to `stable_mir`.
1 parent 5151cd9 commit 303f01a

File tree

5 files changed

+912
-331
lines changed

5 files changed

+912
-331
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rustc_span::Span;
1717
use rustc_span::def_id::{CrateNum, DefId};
1818
use scoped_tls::scoped_thread_local;
1919
use stable_mir::Error;
20-
use stable_mir::compiler_interface::SmirInterface;
2120
use stable_mir::ty::IndexedVal;
2221

2322
use crate::rustc_smir::context::SmirCtxt;

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//!
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10-
use std::ops::RangeInclusive;
1110
use std::cell::RefCell;
1211
use std::fmt::Debug;
1312

@@ -18,8 +17,6 @@ use rustc_middle::mir;
1817
use rustc_middle::mir::interpret::AllocId;
1918
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
2019
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
21-
use stable_mir::{CtorKind, ItemKind};
22-
use tracing::debug;
2320

2421
use crate::rustc_internal::IndexMap;
2522
use crate::stable_mir;
@@ -153,124 +150,3 @@ where
153150
.collect()
154151
}
155152
}
156-
157-
/// Build a stable mir crate from a given crate number.
158-
pub(crate) fn smir_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> stable_mir::Crate {
159-
let crate_name = tcx.crate_name(crate_num).to_string();
160-
let is_local = crate_num == LOCAL_CRATE;
161-
debug!(?crate_name, ?crate_num, "smir_crate");
162-
stable_mir::Crate { id: crate_num.into(), name: crate_name, is_local }
163-
}
164-
165-
pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind {
166-
match kind {
167-
DefKind::Mod
168-
| DefKind::Struct
169-
| DefKind::Union
170-
| DefKind::Enum
171-
| DefKind::Variant
172-
| DefKind::Trait
173-
| DefKind::TyAlias
174-
| DefKind::ForeignTy
175-
| DefKind::TraitAlias
176-
| DefKind::AssocTy
177-
| DefKind::TyParam
178-
| DefKind::ConstParam
179-
| DefKind::Macro(_)
180-
| DefKind::ExternCrate
181-
| DefKind::Use
182-
| DefKind::ForeignMod
183-
| DefKind::OpaqueTy
184-
| DefKind::Field
185-
| DefKind::LifetimeParam
186-
| DefKind::Impl { .. }
187-
| DefKind::GlobalAsm => {
188-
unreachable!("Not a valid item kind: {kind:?}");
189-
}
190-
DefKind::Closure | DefKind::AssocFn | DefKind::Fn | DefKind::SyntheticCoroutineBody => {
191-
ItemKind::Fn
192-
}
193-
DefKind::Const | DefKind::InlineConst | DefKind::AssocConst | DefKind::AnonConst => {
194-
ItemKind::Const
195-
}
196-
DefKind::Static { .. } => ItemKind::Static,
197-
DefKind::Ctor(_, rustc_hir::def::CtorKind::Const) => ItemKind::Ctor(CtorKind::Const),
198-
DefKind::Ctor(_, rustc_hir::def::CtorKind::Fn) => ItemKind::Ctor(CtorKind::Fn),
199-
}
200-
}
201-
202-
/// Trait used to convert between an internal MIR type to a Stable MIR type.
203-
pub trait Stable<'cx>: PointeeSized {
204-
/// The stable representation of the type implementing Stable.
205-
type T;
206-
/// Converts an object to the equivalent Stable MIR representation.
207-
fn stable(&self, tables: &mut Tables<'_>) -> Self::T;
208-
}
209-
210-
impl<'tcx, T> Stable<'tcx> for &T
211-
where
212-
T: Stable<'tcx>,
213-
{
214-
type T = T::T;
215-
216-
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
217-
(*self).stable(tables)
218-
}
219-
}
220-
221-
impl<'tcx, T> Stable<'tcx> for Option<T>
222-
where
223-
T: Stable<'tcx>,
224-
{
225-
type T = Option<T::T>;
226-
227-
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
228-
self.as_ref().map(|value| value.stable(tables))
229-
}
230-
}
231-
232-
impl<'tcx, T, E> Stable<'tcx> for Result<T, E>
233-
where
234-
T: Stable<'tcx>,
235-
E: Stable<'tcx>,
236-
{
237-
type T = Result<T::T, E::T>;
238-
239-
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
240-
match self {
241-
Ok(val) => Ok(val.stable(tables)),
242-
Err(error) => Err(error.stable(tables)),
243-
}
244-
}
245-
}
246-
247-
impl<'tcx, T> Stable<'tcx> for &[T]
248-
where
249-
T: Stable<'tcx>,
250-
{
251-
type T = Vec<T::T>;
252-
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
253-
self.iter().map(|e| e.stable(tables)).collect()
254-
}
255-
}
256-
257-
impl<'tcx, T, U> Stable<'tcx> for (T, U)
258-
where
259-
T: Stable<'tcx>,
260-
U: Stable<'tcx>,
261-
{
262-
type T = (T::T, U::T);
263-
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
264-
(self.0.stable(tables), self.1.stable(tables))
265-
}
266-
}
267-
268-
impl<'tcx, T> Stable<'tcx> for RangeInclusive<T>
269-
where
270-
T: Stable<'tcx>,
271-
{
272-
type T = RangeInclusive<T::T>;
273-
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
274-
RangeInclusive::new(self.start().stable(tables), self.end().stable(tables))
275-
}
276-
}

0 commit comments

Comments
 (0)