rust/tests/ui-fulldeps/stable-mir/check_abi.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

142 lines
4.2 KiB
Rust
Raw Normal View History

// 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;
use stable_mir::{CrateDef, CrateItem, CrateItems, ItemKind};
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.
fn test_stable_mir() -> ControlFlow<()> {
// Find items in the local crate.
let items = stable_mir::all_local_items();
// Test fn_abi
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);
// Test variadic function.
let variadic_fn = *get_item(&items, (ItemKind::Fn, "variadic_fn")).unwrap();
check_variadic(variadic_fn);
ControlFlow::Continue(())
}
/// 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);
}
/// 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());
assert_matches!(abi.mode, PassMode::Direct(_));
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());
assert_matches!(abi.mode, PassMode::Indirect { .. });
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(),
];
run!(args, test_stable_mir).unwrap();
}
fn generate_input(path: &str) -> std::io::Result<()> {
let mut file = std::fs::File::create(path)?;
write!(
file,
r#"
#![feature(c_variadic)]
#![allow(unused_variables)]
pub fn fn_abi(ignore: [u8; 0], primitive: char) -> Result<usize, &'static str> {{
// We only care about the signature.
todo!()
}}
pub unsafe extern "C" fn variadic_fn(n: usize, mut args: ...) -> usize {{
0
}}
"#
)?;
Ok(())
}