Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Eliminate escape hatch
  • Loading branch information
oli-obk committed Sep 25, 2023
commit 55b6f649024b372af901b3412a6d36c4284ccf3d
9 changes: 9 additions & 0 deletions compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {
}
}

impl<'tcx> Index<stable_mir::ty::Span> for Tables<'tcx> {
type Output = Span;

#[inline(always)]
fn index(&self, index: stable_mir::ty::Span) -> &Self::Output {
&self.spans[index.0]
}
}

impl<'tcx> Tables<'tcx> {
pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem {
stable_mir::CrateItem(self.create_def_id(did))
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ impl<'tcx> Context for Tables<'tcx> {
self.tcx.def_path_str(self[def_id])
}

fn print_span(&self, span: stable_mir::ty::Span) -> String {
self.tcx.sess.source_map().span_to_diagnostic_string(self[span])
}


fn span_of_an_item(&mut self, def_id: stable_mir::DefId) -> Span {
self.tcx.def_span(self[def_id]).stable(self)
}
Expand Down Expand Up @@ -104,10 +109,6 @@ impl<'tcx> Context for Tables<'tcx> {
}
}

fn rustc_tables(&mut self, f: &mut dyn FnMut(&mut Tables<'_>)) {
f(self)
}

fn ty_kind(&mut self, ty: crate::stable_mir::ty::Ty) -> TyKind {
self.types[ty.0].clone().stable(self)
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_smir/src/stable_mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ use std::cell::Cell;
use std::fmt;
use std::fmt::Debug;

use crate::rustc_internal::Opaque;

use self::ty::{
GenericPredicates, Generics, ImplDef, ImplTrait, Span, TraitDecl, TraitDef, Ty, TyKind,
};
use crate::rustc_smir::Tables;

pub mod fold;
pub mod mir;
Expand Down Expand Up @@ -79,6 +80,8 @@ pub struct Crate {
pub is_local: bool,
}

pub type DefKind = Opaque;

/// Holds information about an item in the crate.
/// For now, it only stores the item DefId. Use functions inside `rustc_internal` module to
/// use this item.
Expand Down Expand Up @@ -161,6 +164,7 @@ pub trait Context {
/// Prints the name of given `DefId`
fn name_of_def_id(&self, def_id: DefId) -> String;

fn print_span(&self, span: Span) -> String;
/// `Span` of an item
fn span_of_an_item(&mut self, def_id: DefId) -> Span;

Expand All @@ -169,10 +173,6 @@ pub trait Context {

/// Create a new `Ty` from scratch without information from rustc.
fn mk_ty(&mut self, kind: TyKind) -> Ty;

/// HACK: Until we have fully stable consumers, we need an escape hatch
/// to get `DefId`s out of `CrateItem`s.
fn rustc_tables(&mut self, f: &mut dyn FnMut(&mut Tables<'_>));
}

// A thread local variable that stores a pointer to the tables mapping between TyCtxt
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_smir/src/stable_mir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ pub struct Span(pub(crate) usize);

impl Debug for Span {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut span = None;
with(|context| context.rustc_tables(&mut |tables| span = Some(tables.spans[self.0])));
f.write_fmt(format_args!("{:?}", &span.unwrap()))
f.debug_struct("Span")
.field("id", &self.0)
.field("repr", &with(|cx| cx.print_span(*self)))
.finish()
}
}

Expand Down