Skip to content

Commit

Permalink
Add a test to evaluate type intrinsic.
Browse files Browse the repository at this point in the history
  • Loading branch information
celinval committed Dec 8, 2023
1 parent 0a0e7e6 commit 9e15c49
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions tests/ui-fulldeps/stable-mir/check_allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ use rustc_middle::ty::TyCtxt;
use rustc_smir::rustc_internal;
use stable_mir::crate_def::CrateDef;
use stable_mir::mir::alloc::GlobalAlloc;
use stable_mir::mir::mono::{Instance, StaticDef};
use stable_mir::mir::Body;
use stable_mir::ty::{Allocation, ConstantKind};
use stable_mir::mir::mono::{Instance, InstanceKind, StaticDef};
use stable_mir::mir::{Body, TerminatorKind};
use stable_mir::ty::{Allocation, ConstantKind, RigidTy, TyKind};
use stable_mir::{CrateItem, CrateItems, ItemKind};
use std::ascii::Char;
use std::assert_matches::assert_matches;
Expand All @@ -46,6 +46,7 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
check_bar(*get_item(&items, (ItemKind::Static, "BAR")).unwrap());
check_len(*get_item(&items, (ItemKind::Static, "LEN")).unwrap());
check_other_consts(*get_item(&items, (ItemKind::Fn, "other_consts")).unwrap());
check_type_id(*get_item(&items, (ItemKind::Fn, "check_type_id")).unwrap());
ControlFlow::Continue(())
}

Expand Down Expand Up @@ -139,6 +140,30 @@ fn check_other_consts(item: CrateItem) {
}
}

/// Check that we can retrieve the type id of char and bool, and that they have different values.
fn check_type_id(item: CrateItem) {
let body = Instance::try_from(item).unwrap().body().unwrap();
let mut ids: Vec<u128> = vec![];
for term in body.blocks.iter().map(|bb| &bb.terminator) {
match &term.kind {
TerminatorKind::Call { func, destination, .. } => {
let TyKind::RigidTy(ty) = func.ty(body.locals()).unwrap().kind() else {
unreachable!()
};
let RigidTy::FnDef(def, args) = ty else { unreachable!() };
let instance = Instance::resolve(def, &args).unwrap();
assert_eq!(instance.kind, InstanceKind::Intrinsic);
let dest_ty = destination.ty(body.locals()).unwrap();
let alloc = instance.try_const_eval(dest_ty).unwrap();
ids.push(alloc.read_uint().unwrap());
}
_ => { /* Do nothing */ }
}
}
assert_eq!(ids.len(), 2);
assert_ne!(ids[0], ids[1]);
}

/// Collects all the constant assignments.
pub fn collect_consts(body: &Body) -> HashMap<String, &Allocation> {
body.var_debug_info
Expand Down Expand Up @@ -193,6 +218,9 @@ fn generate_input(path: &str) -> std::io::Result<()> {
write!(
file,
r#"
#![feature(core_intrinsics)]
use std::intrinsics::type_id;
static LEN: usize = 2;
static FOO: [&str; 2] = ["hi", "there"];
static BAR: &str = "Bar";
Expand All @@ -211,6 +239,11 @@ fn generate_input(path: &str) -> std::io::Result<()> {
let _tuple = TUPLE;
}}
fn check_type_id() {{
let _char_id = type_id::<char>();
let _bool_id = type_id::<bool>();
}}
pub fn main() {{
println!("{{FOO:?}}! {{BAR}}");
assert_eq!(FOO.len(), LEN);
Expand Down

0 comments on commit 9e15c49

Please sign in to comment.