1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
//@ 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, IntegerLength, PassMode, Primitive, Scalar, ValueAbi,
VariantsShape,
};
use stable_mir::mir::mono::Instance;
use stable_mir::target::MachineInfo;
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(), 3);
check_ignore(&fn_abi.args[0]);
check_primitive(&fn_abi.args[1]);
check_niche(&fn_abi.args[2]);
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 { .. })
}
/// Check the niche information about: `NonZeroU8`
fn check_niche(abi: &ArgAbi) {
assert!(abi.ty.kind().is_struct());
assert_matches!(abi.mode, PassMode::Direct { .. });
let layout = abi.layout.shape();
assert!(layout.is_sized());
assert_eq!(layout.size.bytes(), 1);
let ValueAbi::Scalar(scalar) = layout.abi else { unreachable!() };
assert!(scalar.has_niche(&MachineInfo::target()), "Opps: {:?}", scalar);
let Scalar::Initialized { value, valid_range } = scalar else { unreachable!() };
assert_matches!(value, Primitive::Int { length: IntegerLength::I8, signed: false });
assert_eq!(valid_range.start, 1);
assert_eq!(valid_range.end, u8::MAX.into());
assert!(!valid_range.contains(0));
assert!(!valid_range.wraps_around());
}
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)]
use std::num::NonZeroU8;
pub fn fn_abi(
ignore: [u8; 0],
primitive: char,
niche: NonZeroU8,
) -> 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(())
}
|