Skip to content

Commit 48c7cc2

Browse files
committed
Add a small test for the case that was crashing
1 parent a8556da commit 48c7cc2

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// run-pass
2+
//! Test that item kind works as expected.
3+
4+
// ignore-stage1
5+
// ignore-cross-compile
6+
// ignore-remote
7+
// ignore-windows-gnu mingw has troubles with linking https://github.com/rust-lang/rust/pull/116837
8+
// edition: 2021
9+
10+
#![feature(rustc_private)]
11+
#![feature(assert_matches)]
12+
#![feature(control_flow_enum)]
13+
14+
extern crate rustc_middle;
15+
#[macro_use]
16+
extern crate rustc_smir;
17+
extern crate rustc_driver;
18+
extern crate rustc_interface;
19+
extern crate stable_mir;
20+
21+
use rustc_middle::ty::TyCtxt;
22+
use rustc_smir::rustc_internal;
23+
use stable_mir::*;
24+
use std::io::Write;
25+
use std::ops::ControlFlow;
26+
27+
const CRATE_NAME: &str = "input";
28+
29+
/// This function uses the Stable MIR APIs to get information about the test crate.
30+
fn test_item_kind(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
31+
let items = stable_mir::all_local_items();
32+
assert_eq!(items.len(), 3);
33+
// Constructor item.
34+
for item in items {
35+
let expected_kind = match item.name().as_str() {
36+
"Dummy" => ItemKind::Fn,
37+
"dummy" => ItemKind::Fn,
38+
"DUMMY_CONST" => ItemKind::Const,
39+
name => unreachable!("Unexpected item {name}"),
40+
};
41+
assert_eq!(item.kind(), expected_kind, "Mismatched type for {}", item.name());
42+
}
43+
ControlFlow::Continue(())
44+
}
45+
46+
/// This test will generate and analyze a dummy crate using the stable mir.
47+
/// For that, it will first write the dummy crate into a file.
48+
/// Then it will create a `StableMir` using custom arguments and then
49+
/// it will run the compiler.
50+
fn main() {
51+
let path = "item_kind_input.rs";
52+
generate_input(&path).unwrap();
53+
let args = vec![
54+
"rustc".to_string(),
55+
"-Cpanic=abort".to_string(),
56+
"--crate-type=lib".to_string(),
57+
"--crate-name".to_string(),
58+
CRATE_NAME.to_string(),
59+
path.to_string(),
60+
];
61+
run!(args, tcx, test_item_kind(tcx)).unwrap();
62+
}
63+
64+
fn generate_input(path: &str) -> std::io::Result<()> {
65+
let mut file = std::fs::File::create(path)?;
66+
write!(
67+
file,
68+
r#"
69+
pub struct Dummy(u32);
70+
pub const DUMMY_CONST: Dummy = Dummy(0);
71+
72+
pub fn dummy() -> Dummy {{
73+
Dummy(5)
74+
}}
75+
"#
76+
)?;
77+
Ok(())
78+
}

0 commit comments

Comments
 (0)