Skip to content

Commit b29189a

Browse files
committed
Eliminate escape hatch
1 parent 8003602 commit b29189a

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {
2525
}
2626
}
2727

28+
impl<'tcx> Index<stable_mir::ty::Span> for Tables<'tcx> {
29+
type Output = Span;
30+
31+
#[inline(always)]
32+
fn index(&self, index: stable_mir::ty::Span) -> &Self::Output {
33+
&self.spans[index.0]
34+
}
35+
}
36+
2837
impl<'tcx> Tables<'tcx> {
2938
pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem {
3039
stable_mir::CrateItem(self.create_def_id(did))

compiler/rustc_smir/src/rustc_smir/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ impl<'tcx> Context for Tables<'tcx> {
4444
self.tcx.def_path_str(self[def_id])
4545
}
4646

47+
fn print_span(&self, span: stable_mir::ty::Span) -> String {
48+
self.tcx.sess.source_map().span_to_diagnostic_string(self[span])
49+
}
50+
51+
4752
fn span_of_an_item(&mut self, def_id: stable_mir::DefId) -> Span {
4853
self.tcx.def_span(self[def_id]).stable(self)
4954
}
@@ -104,10 +109,6 @@ impl<'tcx> Context for Tables<'tcx> {
104109
}
105110
}
106111

107-
fn rustc_tables(&mut self, f: &mut dyn FnMut(&mut Tables<'_>)) {
108-
f(self)
109-
}
110-
111112
fn ty_kind(&mut self, ty: crate::stable_mir::ty::Ty) -> TyKind {
112113
self.types[ty.0].clone().stable(self)
113114
}

compiler/rustc_smir/src/stable_mir/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ use std::cell::Cell;
1515
use std::fmt;
1616
use std::fmt::Debug;
1717

18+
use crate::rustc_internal::Opaque;
19+
1820
use self::ty::{
1921
GenericPredicates, Generics, ImplDef, ImplTrait, Span, TraitDecl, TraitDef, Ty, TyKind,
2022
};
21-
use crate::rustc_smir::Tables;
2223

2324
pub mod fold;
2425
pub mod mir;
@@ -79,6 +80,8 @@ pub struct Crate {
7980
pub is_local: bool,
8081
}
8182

83+
pub type DefKind = Opaque;
84+
8285
/// Holds information about an item in the crate.
8386
/// For now, it only stores the item DefId. Use functions inside `rustc_internal` module to
8487
/// use this item.
@@ -161,6 +164,7 @@ pub trait Context {
161164
/// Prints the name of given `DefId`
162165
fn name_of_def_id(&self, def_id: DefId) -> String;
163166

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

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

170174
/// Create a new `Ty` from scratch without information from rustc.
171175
fn mk_ty(&mut self, kind: TyKind) -> Ty;
172-
173-
/// HACK: Until we have fully stable consumers, we need an escape hatch
174-
/// to get `DefId`s out of `CrateItem`s.
175-
fn rustc_tables(&mut self, f: &mut dyn FnMut(&mut Tables<'_>));
176176
}
177177

178178
// A thread local variable that stores a pointer to the tables mapping between TyCtxt

compiler/rustc_smir/src/stable_mir/ty.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ pub struct Span(pub(crate) usize);
4040

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

0 commit comments

Comments
 (0)