Skip to content

Commit 5830382

Browse files
committed
Fix test by adding a stable way to get an opaque DefKind
1 parent b29189a commit 5830382

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::stable_mir::ty::{
1313
FloatTy, GenericParamDef, IntTy, Movability, RigidTy, Span, TyKind, UintTy,
1414
};
1515
use crate::stable_mir::{self, CompilerError, Context};
16+
use hir::def::DefKind;
1617
use rustc_hir as hir;
1718
use rustc_middle::mir::interpret::{alloc_range, AllocId};
1819
use rustc_middle::mir::{self, ConstantKind};
@@ -48,6 +49,9 @@ impl<'tcx> Context for Tables<'tcx> {
4849
self.tcx.sess.source_map().span_to_diagnostic_string(self[span])
4950
}
5051

52+
fn def_kind(&mut self, def_id: stable_mir::DefId) -> stable_mir::DefKind {
53+
self.tcx.def_kind(self[def_id]).stable(self)
54+
}
5155

5256
fn span_of_an_item(&mut self, def_id: stable_mir::DefId) -> Span {
5357
self.tcx.def_span(self[def_id]).stable(self)
@@ -1525,3 +1529,12 @@ impl<T> From<ErrorGuaranteed> for CompilerError<T> {
15251529
CompilerError::CompilationFailed
15261530
}
15271531
}
1532+
1533+
impl<'tcx> Stable<'tcx> for DefKind {
1534+
type T = stable_mir::DefKind;
1535+
1536+
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {
1537+
// FIXME: add a real implementation of stable DefKind
1538+
opaque(self)
1539+
}
1540+
}

compiler/rustc_smir/src/stable_mir/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ impl CrateItem {
9696
pub fn span(&self) -> Span {
9797
with(|cx| cx.span_of_an_item(self.0))
9898
}
99+
100+
pub fn name(&self) -> String {
101+
with(|cx| cx.name_of_def_id(self.0))
102+
}
103+
104+
pub fn kind(&self) -> DefKind {
105+
with(|cx| cx.def_kind(self.0))
106+
}
99107
}
100108

101109
/// Return the function where execution starts if the current
@@ -165,6 +173,8 @@ pub trait Context {
165173
fn name_of_def_id(&self, def_id: DefId) -> String;
166174

167175
fn print_span(&self, span: Span) -> String;
176+
177+
fn def_kind(&mut self, def_id: DefId) -> DefKind;
168178
/// `Span` of an item
169179
fn span_of_an_item(&mut self, def_id: DefId) -> Span;
170180

tests/ui-fulldeps/stable-mir/crate-info.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use std::ops::ControlFlow;
2727
const CRATE_NAME: &str = "input";
2828

2929
/// This function uses the Stable MIR APIs to get information about the test crate.
30-
fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
30+
fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
3131
// Get the local crate using stable_mir API.
3232
let local = stable_mir::local_crate();
3333
assert_eq!(&local.name, CRATE_NAME);
@@ -36,12 +36,12 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
3636

3737
// Find items in the local crate.
3838
let items = stable_mir::all_local_items();
39-
assert!(get_item(tcx, &items, (DefKind::Fn, "foo::bar")).is_some());
39+
assert!(get_item(&items, (DefKind::Fn, "foo::bar")).is_some());
4040

4141
// Find the `std` crate.
4242
assert!(stable_mir::find_crate("std").is_some());
4343

44-
let bar = get_item(tcx, &items, (DefKind::Fn, "bar")).unwrap();
44+
let bar = get_item(&items, (DefKind::Fn, "bar")).unwrap();
4545
let body = bar.body();
4646
assert_eq!(body.locals.len(), 2);
4747
assert_eq!(body.blocks.len(), 1);
@@ -56,7 +56,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
5656
other => panic!("{other:?}"),
5757
}
5858

59-
let foo_bar = get_item(tcx, &items, (DefKind::Fn, "foo_bar")).unwrap();
59+
let foo_bar = get_item(&items, (DefKind::Fn, "foo_bar")).unwrap();
6060
let body = foo_bar.body();
6161
assert_eq!(body.locals.len(), 7);
6262
assert_eq!(body.blocks.len(), 4);
@@ -66,7 +66,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
6666
other => panic!("{other:?}"),
6767
}
6868

69-
let types = get_item(tcx, &items, (DefKind::Fn, "types")).unwrap();
69+
let types = get_item(&items, (DefKind::Fn, "types")).unwrap();
7070
let body = types.body();
7171
assert_eq!(body.locals.len(), 6);
7272
assert_matches!(
@@ -96,7 +96,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
9696
))
9797
);
9898

99-
let drop = get_item(tcx, &items, (DefKind::Fn, "drop")).unwrap();
99+
let drop = get_item(&items, (DefKind::Fn, "drop")).unwrap();
100100
let body = drop.body();
101101
assert_eq!(body.blocks.len(), 2);
102102
let block = &body.blocks[0];
@@ -105,7 +105,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
105105
other => panic!("{other:?}"),
106106
}
107107

108-
let assert = get_item(tcx, &items, (DefKind::Fn, "assert")).unwrap();
108+
let assert = get_item(&items, (DefKind::Fn, "assert")).unwrap();
109109
let body = assert.body();
110110
assert_eq!(body.blocks.len(), 2);
111111
let block = &body.blocks[0];
@@ -114,7 +114,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
114114
other => panic!("{other:?}"),
115115
}
116116

117-
let monomorphic = get_item(tcx, &items, (DefKind::Fn, "monomorphic")).unwrap();
117+
let monomorphic = get_item(&items, (DefKind::Fn, "monomorphic")).unwrap();
118118
for block in monomorphic.body().blocks {
119119
match &block.terminator {
120120
stable_mir::mir::Terminator::Call { func, .. } => match func {
@@ -154,7 +154,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
154154
}
155155
}
156156

157-
let foo_const = get_item(tcx, &items, (DefKind::Const, "FOO")).unwrap();
157+
let foo_const = get_item(&items, (DefKind::Const, "FOO")).unwrap();
158158
// Ensure we don't panic trying to get the body of a constant.
159159
foo_const.body();
160160

@@ -163,13 +163,11 @@ fn test_stable_mir(tcx: TyCtxt<'_>) -> ControlFlow<()> {
163163

164164
// Use internal API to find a function in a crate.
165165
fn get_item<'a>(
166-
tcx: TyCtxt,
167166
items: &'a stable_mir::CrateItems,
168167
item: (DefKind, &str),
169168
) -> Option<&'a stable_mir::CrateItem> {
170169
items.iter().find(|crate_item| {
171-
let def_id = rustc_internal::item_def_id(crate_item);
172-
tcx.def_kind(def_id) == item.0 && tcx.def_path_str(def_id) == item.1
170+
crate_item.kind().to_string() == format!("{:?}", item.0) && crate_item.name() == item.1
173171
})
174172
}
175173

0 commit comments

Comments
 (0)