Skip to content

Commit 5bfb19c

Browse files
author
Stephen Skeirik
committed
add serde serialize test
1 parent 73a4d2b commit 5bfb19c

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//@ run-pass
2+
//! Test that users are able to use retrieve internal constructs from stable ones to help with
3+
//! the migration.
4+
5+
//@ ignore-stage1
6+
//@ ignore-cross-compile
7+
//@ ignore-remote
8+
//@ ignore-windows-gnu mingw has troubles with linking https://github.com/rust-lang/rust/pull/116837
9+
//@ edition: 2021
10+
11+
#![feature(rustc_private)]
12+
#![feature(assert_matches)]
13+
#![feature(control_flow_enum)]
14+
15+
#[macro_use]
16+
extern crate rustc_smir;
17+
extern crate rustc_driver;
18+
extern crate rustc_interface;
19+
extern crate rustc_middle;
20+
extern crate stable_mir;
21+
extern crate serde;
22+
extern crate serde_json;
23+
24+
use rustc_middle::ty::TyCtxt;
25+
use rustc_smir::rustc_internal;
26+
use stable_mir::mir::Body;
27+
use std::io::{Write, BufWriter};
28+
use std::ops::ControlFlow;
29+
use serde_json::to_string;
30+
31+
32+
const CRATE_NAME: &str = "input";
33+
34+
fn serialize_to_json(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
35+
let path = "output.json";
36+
let mut writer = BufWriter::new(std::fs::File::create(path).expect("Failed to create path"));
37+
let local_crate = stable_mir::local_crate();
38+
let items: Vec<Body> = stable_mir::all_local_items().iter().map(|item| { item.body() }).collect();
39+
let crate_data = ( local_crate.name, items );
40+
writer.write_all(to_string(&crate_data).expect("serde_json failed").as_bytes()).expect("JSON serialization failed");
41+
ControlFlow::Continue(())
42+
}
43+
44+
/// This test will generate and analyze a dummy crate using the stable mir.
45+
/// For that, it will first write the dummy crate into a file.
46+
/// Then it will create a `StableMir` using custom arguments and then
47+
/// it will run the compiler.
48+
fn main() {
49+
let path = "internal_input.rs";
50+
generate_input(&path).unwrap();
51+
let args = vec![
52+
"rustc".to_string(),
53+
"--crate-name".to_string(),
54+
CRATE_NAME.to_string(),
55+
path.to_string(),
56+
];
57+
run_with_tcx!(args, serialize_to_json).unwrap();
58+
}
59+
60+
fn generate_input(path: &str) -> std::io::Result<()> {
61+
let mut file = std::fs::File::create(path)?;
62+
write!(
63+
file,
64+
r#"
65+
pub fn main() {{
66+
}}
67+
"#
68+
)?;
69+
Ok(())
70+
}

0 commit comments

Comments
 (0)