diff options
Diffstat (limited to 'tests/ui-fulldeps/stable-mir/crate-info.rs')
-rw-r--r-- | tests/ui-fulldeps/stable-mir/crate-info.rs | 117 |
1 files changed, 75 insertions, 42 deletions
diff --git a/tests/ui-fulldeps/stable-mir/crate-info.rs b/tests/ui-fulldeps/stable-mir/crate-info.rs index f55d7d599..ce4ee3c24 100644 --- a/tests/ui-fulldeps/stable-mir/crate-info.rs +++ b/tests/ui-fulldeps/stable-mir/crate-info.rs @@ -8,27 +8,26 @@ #![feature(rustc_private)] #![feature(assert_matches)] +#![feature(control_flow_enum)] -extern crate rustc_driver; extern crate rustc_hir; -extern crate rustc_interface; extern crate rustc_middle; -extern crate rustc_session; extern crate rustc_smir; +extern crate stable_mir; -use rustc_driver::{Callbacks, Compilation, RunCompiler}; use rustc_hir::def::DefKind; -use rustc_interface::{interface, Queries}; use rustc_middle::ty::TyCtxt; -use rustc_session::EarlyErrorHandler; -use rustc_smir::{rustc_internal, stable_mir}; +use rustc_smir::rustc_internal; + +use stable_mir::fold::Foldable; use std::assert_matches::assert_matches; 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(tcx: TyCtxt<'_>) { +fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> { // Get the local crate using stable_mir API. let local = stable_mir::local_crate(); assert_eq!(&local.name, CRATE_NAME); @@ -37,12 +36,12 @@ fn test_stable_mir(tcx: TyCtxt<'_>) { // Find items in the local crate. let items = stable_mir::all_local_items(); - assert!(get_item(tcx, &items, (DefKind::Fn, "foo::bar")).is_some()); + assert!(get_item(&items, (DefKind::Fn, "foo::bar")).is_some()); // Find the `std` crate. assert!(stable_mir::find_crate("std").is_some()); - let bar = get_item(tcx, &items, (DefKind::Fn, "bar")).unwrap(); + let bar = get_item(&items, (DefKind::Fn, "bar")).unwrap(); let body = bar.body(); assert_eq!(body.locals.len(), 2); assert_eq!(body.blocks.len(), 1); @@ -57,7 +56,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) { other => panic!("{other:?}"), } - let foo_bar = get_item(tcx, &items, (DefKind::Fn, "foo_bar")).unwrap(); + let foo_bar = get_item(&items, (DefKind::Fn, "foo_bar")).unwrap(); let body = foo_bar.body(); assert_eq!(body.locals.len(), 7); assert_eq!(body.blocks.len(), 4); @@ -67,7 +66,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) { other => panic!("{other:?}"), } - let types = get_item(tcx, &items, (DefKind::Fn, "types")).unwrap(); + let types = get_item(&items, (DefKind::Fn, "types")).unwrap(); let body = types.body(); assert_eq!(body.locals.len(), 6); assert_matches!( @@ -97,7 +96,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) { )) ); - let drop = get_item(tcx, &items, (DefKind::Fn, "drop")).unwrap(); + let drop = get_item(&items, (DefKind::Fn, "drop")).unwrap(); let body = drop.body(); assert_eq!(body.blocks.len(), 2); let block = &body.blocks[0]; @@ -106,7 +105,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) { other => panic!("{other:?}"), } - let assert = get_item(tcx, &items, (DefKind::Fn, "assert")).unwrap(); + let assert = get_item(&items, (DefKind::Fn, "assert")).unwrap(); let body = assert.body(); assert_eq!(body.blocks.len(), 2); let block = &body.blocks[0]; @@ -114,24 +113,68 @@ fn test_stable_mir(tcx: TyCtxt<'_>) { stable_mir::mir::Terminator::Assert { .. } => {} other => panic!("{other:?}"), } + + let monomorphic = get_item(&items, (DefKind::Fn, "monomorphic")).unwrap(); + for block in monomorphic.body().blocks { + match &block.terminator { + stable_mir::mir::Terminator::Call { func, .. } => match func { + stable_mir::mir::Operand::Constant(c) => match &c.literal.literal { + stable_mir::ty::ConstantKind::Allocated(alloc) => { + assert!(alloc.bytes.is_empty()); + match c.literal.ty.kind() { + stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::FnDef( + def, + mut args, + )) => { + let func = def.body(); + match func.locals[1] + .fold(&mut args) + .continue_value() + .unwrap() + .kind() + { + stable_mir::ty::TyKind::RigidTy( + stable_mir::ty::RigidTy::Uint(_), + ) => {} + stable_mir::ty::TyKind::RigidTy( + stable_mir::ty::RigidTy::Tuple(_), + ) => {} + other => panic!("{other:?}"), + } + } + other => panic!("{other:?}"), + } + } + other => panic!("{other:?}"), + }, + other => panic!("{other:?}"), + }, + stable_mir::mir::Terminator::Return => {} + other => panic!("{other:?}"), + } + } + + let foo_const = get_item(&items, (DefKind::Const, "FOO")).unwrap(); + // Ensure we don't panic trying to get the body of a constant. + foo_const.body(); + + ControlFlow::Continue(()) } // Use internal API to find a function in a crate. fn get_item<'a>( - tcx: TyCtxt, items: &'a stable_mir::CrateItems, item: (DefKind, &str), ) -> Option<&'a stable_mir::CrateItem> { items.iter().find(|crate_item| { - let def_id = rustc_internal::item_def_id(crate_item); - tcx.def_kind(def_id) == item.0 && tcx.def_path_str(def_id) == item.1 + crate_item.kind().to_string() == format!("{:?}", item.0) && 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. -/// It will invoke the compiler using a custom Callback implementation, which will -/// invoke Stable MIR APIs after the compiler has finished its analysis. +/// Then it will create a `StableMir` using custom arguments and then +/// it will run the compiler. fn main() { let path = "input.rs"; generate_input(&path).unwrap(); @@ -142,29 +185,7 @@ fn main() { CRATE_NAME.to_string(), path.to_string(), ]; - rustc_driver::catch_fatal_errors(|| { - RunCompiler::new(&args, &mut SMirCalls {}).run().unwrap(); - }) - .unwrap(); -} - -struct SMirCalls {} - -impl Callbacks for SMirCalls { - /// Called after analysis. Return value instructs the compiler whether to - /// continue the compilation afterwards (defaults to `Compilation::Continue`) - fn after_analysis<'tcx>( - &mut self, - _handler: &EarlyErrorHandler, - _compiler: &interface::Compiler, - queries: &'tcx Queries<'tcx>, - ) -> Compilation { - queries.global_ctxt().unwrap().enter(|tcx| { - rustc_smir::rustc_internal::run(tcx, || test_stable_mir(tcx)); - }); - // No need to keep going. - Compilation::Stop - } + rustc_internal::StableMir::new(args, test_stable_mir).run().unwrap(); } fn generate_input(path: &str) -> std::io::Result<()> { @@ -172,6 +193,18 @@ fn generate_input(path: &str) -> std::io::Result<()> { write!( file, r#" + pub const FOO: u32 = 1 + 2; + + fn generic<T, const U: usize>(t: T) -> [(); U] {{ + _ = t; + [(); U] + }} + + pub fn monomorphic() {{ + generic::<(), 5>(()); + generic::<u32, 0>(45); + }} + mod foo {{ pub fn bar(i: i32) -> i64 {{ i as i64 |