2023-12-18 13:52:25 -06:00
|
|
|
// run-pass
|
|
|
|
//! Test information regarding type layout.
|
|
|
|
|
|
|
|
// ignore-stage1
|
|
|
|
// ignore-cross-compile
|
|
|
|
// ignore-remote
|
|
|
|
// ignore-windows-gnu mingw has troubles with linking https://github.com/rust-lang/rust/pull/116837
|
|
|
|
|
|
|
|
#![feature(rustc_private)]
|
|
|
|
#![feature(assert_matches)]
|
|
|
|
#![feature(control_flow_enum)]
|
|
|
|
#![feature(ascii_char, ascii_char_variants)]
|
|
|
|
|
|
|
|
extern crate rustc_hir;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate rustc_smir;
|
|
|
|
extern crate rustc_driver;
|
|
|
|
extern crate rustc_interface;
|
|
|
|
extern crate stable_mir;
|
|
|
|
|
|
|
|
use rustc_smir::rustc_internal;
|
|
|
|
use stable_mir::abi::{ArgAbi, CallConvention, FieldsShape, PassMode, VariantsShape};
|
|
|
|
use stable_mir::mir::mono::Instance;
|
2023-12-19 13:04:34 -06:00
|
|
|
use stable_mir::{CrateDef, CrateItem, CrateItems, ItemKind};
|
2023-12-18 13:52:25 -06:00
|
|
|
use std::assert_matches::assert_matches;
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
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-12-18 13:52:25 -06:00
|
|
|
// Find items in the local crate.
|
|
|
|
let items = stable_mir::all_local_items();
|
2023-12-19 13:04:34 -06:00
|
|
|
|
|
|
|
// Test fn_abi
|
2023-12-18 13:52:25 -06:00
|
|
|
let target_fn = *get_item(&items, (ItemKind::Fn, "fn_abi")).unwrap();
|
|
|
|
let instance = Instance::try_from(target_fn).unwrap();
|
|
|
|
let fn_abi = instance.fn_abi().unwrap();
|
|
|
|
assert_eq!(fn_abi.conv, CallConvention::Rust);
|
|
|
|
assert_eq!(fn_abi.args.len(), 2);
|
|
|
|
|
|
|
|
check_ignore(&fn_abi.args[0]);
|
|
|
|
check_primitive(&fn_abi.args[1]);
|
|
|
|
check_result(fn_abi.ret);
|
|
|
|
|
2023-12-19 13:04:34 -06:00
|
|
|
// Test variadic function.
|
|
|
|
let variadic_fn = *get_item(&items, (ItemKind::Fn, "variadic_fn")).unwrap();
|
|
|
|
check_variadic(variadic_fn);
|
|
|
|
|
2023-12-18 13:52:25 -06:00
|
|
|
ControlFlow::Continue(())
|
|
|
|
}
|
|
|
|
|
2023-12-19 13:04:34 -06:00
|
|
|
/// Check the variadic function ABI:
|
|
|
|
/// ```no_run
|
|
|
|
/// pub unsafe extern "C" fn variadic_fn(n: usize, mut args: ...) -> usize {
|
|
|
|
/// 0
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
fn check_variadic(variadic_fn: CrateItem) {
|
|
|
|
let instance = Instance::try_from(variadic_fn).unwrap();
|
|
|
|
let abi = instance.fn_abi().unwrap();
|
|
|
|
assert!(abi.c_variadic);
|
|
|
|
assert_eq!(abi.args.len(), 1);
|
|
|
|
}
|
|
|
|
|
2023-12-18 13:52:25 -06:00
|
|
|
/// Check the argument to be ignored: `ignore: [u8; 0]`.
|
|
|
|
fn check_ignore(abi: &ArgAbi) {
|
|
|
|
assert!(abi.ty.kind().is_array());
|
|
|
|
assert_eq!(abi.mode, PassMode::Ignore);
|
|
|
|
let layout = abi.layout.shape();
|
|
|
|
assert!(layout.is_sized());
|
|
|
|
assert!(layout.is_1zst());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check the primitive argument: `primitive: char`.
|
|
|
|
fn check_primitive(abi: &ArgAbi) {
|
|
|
|
assert!(abi.ty.kind().is_char());
|
2023-12-19 13:04:34 -06:00
|
|
|
assert_matches!(abi.mode, PassMode::Direct(_));
|
2023-12-18 13:52:25 -06:00
|
|
|
let layout = abi.layout.shape();
|
|
|
|
assert!(layout.is_sized());
|
|
|
|
assert!(!layout.is_1zst());
|
|
|
|
assert_matches!(layout.fields, FieldsShape::Primitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check the return value: `Result<usize, &str>`.
|
|
|
|
fn check_result(abi: ArgAbi) {
|
|
|
|
assert!(abi.ty.kind().is_enum());
|
2023-12-19 13:04:34 -06:00
|
|
|
assert_matches!(abi.mode, PassMode::Indirect { .. });
|
2023-12-18 13:52:25 -06:00
|
|
|
let layout = abi.layout.shape();
|
|
|
|
assert!(layout.is_sized());
|
|
|
|
assert_matches!(layout.fields, FieldsShape::Arbitrary { .. });
|
|
|
|
assert_matches!(layout.variants, VariantsShape::Multiple { .. })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_item<'a>(
|
|
|
|
items: &'a CrateItems,
|
|
|
|
item: (ItemKind, &str),
|
|
|
|
) -> Option<&'a stable_mir::CrateItem> {
|
|
|
|
items.iter().find(|crate_item| (item.0 == crate_item.kind()) && crate_item.name() == item.1)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 = "alloc_input.rs";
|
|
|
|
generate_input(&path).unwrap();
|
|
|
|
let args = vec![
|
|
|
|
"rustc".to_string(),
|
|
|
|
"--crate-type=lib".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-12-18 13:52:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_input(path: &str) -> std::io::Result<()> {
|
|
|
|
let mut file = std::fs::File::create(path)?;
|
|
|
|
write!(
|
|
|
|
file,
|
|
|
|
r#"
|
2023-12-19 13:04:34 -06:00
|
|
|
#![feature(c_variadic)]
|
|
|
|
#![allow(unused_variables)]
|
|
|
|
|
2023-12-18 13:52:25 -06:00
|
|
|
pub fn fn_abi(ignore: [u8; 0], primitive: char) -> Result<usize, &'static str> {{
|
|
|
|
// We only care about the signature.
|
|
|
|
todo!()
|
|
|
|
}}
|
2023-12-19 13:04:34 -06:00
|
|
|
|
|
|
|
|
|
|
|
pub unsafe extern "C" fn variadic_fn(n: usize, mut args: ...) -> usize {{
|
|
|
|
0
|
|
|
|
}}
|
2023-12-18 13:52:25 -06:00
|
|
|
"#
|
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
}
|