Fix test by adding a stable way to get an opaque DefKind

This commit is contained in:
Oli Scherer 2023-09-18 11:02:26 +00:00
parent 55b6f64902
commit 33998a9751
3 changed files with 33 additions and 12 deletions

View File

@ -13,6 +13,7 @@
FloatTy, GenericParamDef, IntTy, Movability, RigidTy, Span, TyKind, UintTy,
};
use crate::stable_mir::{self, CompilerError, Context};
use hir::def::DefKind;
use rustc_hir as hir;
use rustc_middle::mir;
use rustc_middle::mir::interpret::{alloc_range, AllocId};
@ -48,6 +49,9 @@ fn print_span(&self, span: stable_mir::ty::Span) -> String {
self.tcx.sess.source_map().span_to_diagnostic_string(self[span])
}
fn def_kind(&mut self, def_id: stable_mir::DefId) -> stable_mir::DefKind {
self.tcx.def_kind(self[def_id]).stable(self)
}
fn span_of_an_item(&mut self, def_id: stable_mir::DefId) -> Span {
self.tcx.def_span(self[def_id]).stable(self)
@ -1519,3 +1523,12 @@ fn from(_error: ErrorGuaranteed) -> Self {
CompilerError::CompilationFailed
}
}
impl<'tcx> Stable<'tcx> for DefKind {
type T = stable_mir::DefKind;
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
// FIXME: add a real implementation of stable DefKind
opaque(self)
}
}

View File

@ -96,6 +96,14 @@ pub fn body(&self) -> mir::Body {
pub fn span(&self) -> Span {
with(|cx| cx.span_of_an_item(self.0))
}
pub fn name(&self) -> String {
with(|cx| cx.name_of_def_id(self.0))
}
pub fn kind(&self) -> DefKind {
with(|cx| cx.def_kind(self.0))
}
}
/// Return the function where execution starts if the current
@ -165,6 +173,8 @@ pub trait Context {
fn name_of_def_id(&self, def_id: DefId) -> String;
fn print_span(&self, span: Span) -> String;
fn def_kind(&mut self, def_id: DefId) -> DefKind;
/// `Span` of an item
fn span_of_an_item(&mut self, def_id: DefId) -> Span;

View File

@ -27,7 +27,7 @@
const CRATE_NAME: &str = "input";
/// This function uses the Stable MIR APIs to get information about the test crate.
fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
// Get the local crate using stable_mir API.
let local = stable_mir::local_crate();
assert_eq!(&local.name, CRATE_NAME);
@ -36,12 +36,12 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
// Find items in the local crate.
let items = stable_mir::all_local_items();
assert!(get_item(tcx, &items, (DefKind::Fn, "foo::bar")).is_some());
assert!(get_item(&items, (DefKind::Fn, "foo::bar")).is_some());
// Find the `std` crate.
assert!(stable_mir::find_crate("std").is_some());
let bar = get_item(tcx, &items, (DefKind::Fn, "bar")).unwrap();
let bar = get_item(&items, (DefKind::Fn, "bar")).unwrap();
let body = bar.body();
assert_eq!(body.locals.len(), 2);
assert_eq!(body.blocks.len(), 1);
@ -56,7 +56,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
other => panic!("{other:?}"),
}
let foo_bar = get_item(tcx, &items, (DefKind::Fn, "foo_bar")).unwrap();
let foo_bar = get_item(&items, (DefKind::Fn, "foo_bar")).unwrap();
let body = foo_bar.body();
assert_eq!(body.locals.len(), 7);
assert_eq!(body.blocks.len(), 4);
@ -66,7 +66,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
other => panic!("{other:?}"),
}
let types = get_item(tcx, &items, (DefKind::Fn, "types")).unwrap();
let types = get_item(&items, (DefKind::Fn, "types")).unwrap();
let body = types.body();
assert_eq!(body.locals.len(), 6);
assert_matches!(
@ -96,7 +96,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
))
);
let drop = get_item(tcx, &items, (DefKind::Fn, "drop")).unwrap();
let drop = get_item(&items, (DefKind::Fn, "drop")).unwrap();
let body = drop.body();
assert_eq!(body.blocks.len(), 2);
let block = &body.blocks[0];
@ -105,7 +105,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
other => panic!("{other:?}"),
}
let assert = get_item(tcx, &items, (DefKind::Fn, "assert")).unwrap();
let assert = get_item(&items, (DefKind::Fn, "assert")).unwrap();
let body = assert.body();
assert_eq!(body.blocks.len(), 2);
let block = &body.blocks[0];
@ -114,7 +114,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
other => panic!("{other:?}"),
}
let monomorphic = get_item(tcx, &items, (DefKind::Fn, "monomorphic")).unwrap();
let monomorphic = get_item(&items, (DefKind::Fn, "monomorphic")).unwrap();
for block in monomorphic.body().blocks {
match &block.terminator {
stable_mir::mir::Terminator::Call { func, .. } => match func {
@ -154,7 +154,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
}
}
let foo_const = get_item(tcx, &items, (DefKind::Const, "FOO")).unwrap();
let foo_const = get_item(&items, (DefKind::Const, "FOO")).unwrap();
// Ensure we don't panic trying to get the body of a constant.
foo_const.body();
@ -163,13 +163,11 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
// Use internal API to find a function in a crate.
fn get_item<'a>(
tcx: TyCtxt,
items: &'a stable_mir::CrateItems,
item: (DefKind, &str),
) -> Option<&'a stable_mir::CrateItem> {
items.iter().find(|crate_item| {
let def_id = rustc_internal::item_def_id(crate_item);
tcx.def_kind(def_id) == item.0 && tcx.def_path_str(def_id) == item.1
crate_item.kind().to_string() == format!("{:?}", item.0) && crate_item.name() == item.1
})
}