Eliminate escape hatch

This commit is contained in:
Oli Scherer 2023-09-14 15:43:30 +00:00
parent e02a139a23
commit 55b6f64902
4 changed files with 23 additions and 12 deletions

View File

@ -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))

View File

@ -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)
}
@ -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)
}

View File

@ -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;
@ -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.
@ -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;
@ -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

View File

@ -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()
}
}