2024-02-22 06:10:29 -06:00
|
|
|
//@ run-pass
|
2023-11-21 15:41:02 -06:00
|
|
|
//! Test that users are able to use stable mir APIs to retrieve information about crate definitions.
|
|
|
|
|
2024-02-22 06:10:29 -06:00
|
|
|
//@ ignore-stage1
|
|
|
|
//@ ignore-cross-compile
|
|
|
|
//@ ignore-remote
|
|
|
|
//@ ignore-windows-gnu mingw has troubles with linking https://github.com/rust-lang/rust/pull/116837
|
|
|
|
//@ edition: 2021
|
2023-11-21 15:41:02 -06:00
|
|
|
|
|
|
|
#![feature(rustc_private)]
|
|
|
|
#![feature(assert_matches)]
|
|
|
|
#![feature(control_flow_enum)]
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate rustc_smir;
|
|
|
|
extern crate rustc_driver;
|
|
|
|
extern crate rustc_interface;
|
|
|
|
extern crate stable_mir;
|
|
|
|
|
2023-12-01 18:20:35 -06:00
|
|
|
use std::assert_matches::assert_matches;
|
2023-11-21 15:41:02 -06:00
|
|
|
use mir::{mono::Instance, TerminatorKind::*};
|
2024-03-08 13:11:59 -06:00
|
|
|
use stable_mir::mir::mono::InstanceKind;
|
2023-11-21 15:41:02 -06:00
|
|
|
use rustc_smir::rustc_internal;
|
2023-12-01 18:20:35 -06:00
|
|
|
use stable_mir::ty::{RigidTy, TyKind, Ty, UintTy};
|
2023-11-21 15:41:02 -06:00
|
|
|
use stable_mir::*;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::ops::ControlFlow;
|
|
|
|
|
|
|
|
const CRATE_NAME: &str = "input";
|
|
|
|
|
|
|
|
/// This function uses the Stable MIR APIs to get information about the test crate.
|
2024-01-10 13:46:17 -06:00
|
|
|
fn test_stable_mir() -> ControlFlow<()> {
|
2023-11-21 15:41:02 -06:00
|
|
|
let entry = stable_mir::entry_fn().unwrap();
|
|
|
|
let main_fn = Instance::try_from(entry).unwrap();
|
|
|
|
assert_eq!(main_fn.name(), "main");
|
|
|
|
assert_eq!(main_fn.trimmed_name(), "main");
|
|
|
|
|
|
|
|
let instances = get_instances(main_fn.body().unwrap());
|
2024-03-08 13:11:59 -06:00
|
|
|
assert_eq!(instances.len(), 3);
|
2023-11-21 15:41:02 -06:00
|
|
|
test_fn(instances[0], "from_u32", "std::char::from_u32", "core");
|
|
|
|
test_fn(instances[1], "Vec::<u8>::new", "std::vec::Vec::<u8>::new", "alloc");
|
2024-03-08 13:11:59 -06:00
|
|
|
test_fn(instances[2], "ctpop::<u8>", "std::intrinsics::ctpop::<u8>", "core");
|
2023-12-01 18:20:35 -06:00
|
|
|
test_vec_new(instances[1]);
|
2023-11-21 15:41:02 -06:00
|
|
|
ControlFlow::Continue(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_fn(instance: Instance, expected_trimmed: &str, expected_qualified: &str, krate: &str) {
|
|
|
|
let trimmed = instance.trimmed_name();
|
|
|
|
let qualified = instance.name();
|
|
|
|
assert_eq!(&trimmed, expected_trimmed);
|
|
|
|
assert_eq!(&qualified, expected_qualified);
|
|
|
|
|
2024-03-08 13:11:59 -06:00
|
|
|
if instance.kind == InstanceKind::Intrinsic {
|
|
|
|
let intrinsic = instance.intrinsic_name().unwrap();
|
|
|
|
let (trimmed_base, _trimmed_args) = trimmed.split_once("::").unwrap();
|
|
|
|
assert_eq!(intrinsic, trimmed_base);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert!(instance.intrinsic_name().is_none());
|
|
|
|
|
2023-11-21 15:41:02 -06:00
|
|
|
let item = CrateItem::try_from(instance).unwrap();
|
|
|
|
let trimmed = item.trimmed_name();
|
|
|
|
let qualified = item.name();
|
|
|
|
assert_eq!(trimmed, expected_trimmed.replace("u8", "T"));
|
|
|
|
assert_eq!(qualified, expected_qualified.replace("u8", "T"));
|
|
|
|
assert_eq!(&item.krate().name, krate);
|
|
|
|
}
|
|
|
|
|
2023-12-01 18:20:35 -06:00
|
|
|
fn extract_elem_ty(ty: Ty) -> Ty {
|
|
|
|
match ty.kind() {
|
|
|
|
TyKind::RigidTy(RigidTy::Adt(_, args)) => {
|
|
|
|
*args.0[0].expect_ty()
|
|
|
|
}
|
|
|
|
_ => unreachable!("Expected Vec ADT, but found: {ty:?}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check signature and type of `Vec::<u8>::new` and its generic version.
|
|
|
|
fn test_vec_new(instance: mir::mono::Instance) {
|
2023-12-18 13:52:25 -06:00
|
|
|
let sig = instance.fn_abi().unwrap();
|
|
|
|
assert_eq!(&sig.args, &[]);
|
|
|
|
let elem_ty = extract_elem_ty(sig.ret.ty);
|
2023-12-01 18:20:35 -06:00
|
|
|
assert_matches!(elem_ty.kind(), TyKind::RigidTy(RigidTy::Uint(UintTy::U8)));
|
|
|
|
|
|
|
|
// Get the signature for Vec::<T>::new.
|
|
|
|
let item = CrateItem::try_from(instance).unwrap();
|
|
|
|
let ty = item.ty();
|
|
|
|
let gen_sig = ty.kind().fn_sig().unwrap().skip_binder();
|
|
|
|
let gen_ty = extract_elem_ty(gen_sig.output());
|
|
|
|
assert_matches!(gen_ty.kind(), TyKind::Param(_));
|
|
|
|
}
|
|
|
|
|
2023-11-21 15:41:02 -06:00
|
|
|
/// Inspect the instance body
|
|
|
|
fn get_instances(body: mir::Body) -> Vec<Instance> {
|
|
|
|
body.blocks.iter().filter_map(|bb| {
|
|
|
|
match &bb.terminator.kind {
|
|
|
|
Call { func, .. } => {
|
|
|
|
let TyKind::RigidTy(ty) = func.ty(body.locals()).unwrap().kind() else { unreachable!
|
|
|
|
() };
|
|
|
|
let RigidTy::FnDef(def, args) = ty else { unreachable!() };
|
|
|
|
Instance::resolve(def, &args).ok()
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).collect::<Vec<_>>()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This test will generate and analyze a dummy crate using the stable mir.
|
|
|
|
/// For that, it will first write the dummy crate into a file.
|
|
|
|
/// Then it will create a `StableMir` using custom arguments and then
|
|
|
|
/// it will run the compiler.
|
|
|
|
fn main() {
|
|
|
|
let path = "defs_input.rs";
|
|
|
|
generate_input(&path).unwrap();
|
|
|
|
let args = vec![
|
|
|
|
"rustc".to_string(),
|
|
|
|
"-Cpanic=abort".to_string(),
|
|
|
|
"--crate-name".to_string(),
|
|
|
|
CRATE_NAME.to_string(),
|
|
|
|
path.to_string(),
|
|
|
|
];
|
2024-01-10 13:46:17 -06:00
|
|
|
run!(args, test_stable_mir).unwrap();
|
2023-11-21 15:41:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_input(path: &str) -> std::io::Result<()> {
|
|
|
|
let mut file = std::fs::File::create(path)?;
|
|
|
|
write!(
|
|
|
|
file,
|
|
|
|
r#"
|
2024-03-08 13:11:59 -06:00
|
|
|
#![feature(core_intrinsics)]
|
2023-11-21 15:41:02 -06:00
|
|
|
|
|
|
|
fn main() {{
|
|
|
|
let _c = core::char::from_u32(99);
|
|
|
|
let _v = Vec::<u8>::new();
|
2024-03-08 13:11:59 -06:00
|
|
|
let _i = std::intrinsics::ctpop::<u8>(0);
|
2023-11-21 15:41:02 -06:00
|
|
|
}}
|
|
|
|
"#
|
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
}
|