|
| 1 | +// run-pass |
| 2 | +// Test that users are able to use stable mir APIs to retrieve information of the current crate |
| 3 | + |
| 4 | +// ignore-stage-1 |
| 5 | +// ignore-cross-compile |
| 6 | +// ignore-remote |
| 7 | + |
| 8 | +#![feature(rustc_private)] |
| 9 | + |
| 10 | +extern crate rustc_driver; |
| 11 | +extern crate rustc_hir; |
| 12 | +extern crate rustc_interface; |
| 13 | +extern crate rustc_middle; |
| 14 | +extern crate rustc_smir; |
| 15 | + |
| 16 | +use rustc_driver::{Callbacks, Compilation, RunCompiler}; |
| 17 | +use rustc_hir::def::DefKind; |
| 18 | +use rustc_interface::{interface, Queries}; |
| 19 | +use rustc_middle::ty::TyCtxt; |
| 20 | +use rustc_smir::{rustc_internal, stable_mir}; |
| 21 | +use std::io::Write; |
| 22 | + |
| 23 | +const CRATE_NAME: &str = "input"; |
| 24 | + |
| 25 | +/// This function uses the Stable MIR APIs to get information about the test crate. |
| 26 | +fn test_stable_mir(tcx: TyCtxt<'_>) { |
| 27 | + // Get the local crate using stable_mir API. |
| 28 | + let local = stable_mir::local_crate(); |
| 29 | + assert_eq!(&local.name, CRATE_NAME); |
| 30 | + |
| 31 | + // Find items in the local crate. |
| 32 | + let items = stable_mir::all_local_items(); |
| 33 | + assert!(has_item(tcx, &items, (DefKind::Fn, "foo_bar"))); |
| 34 | + assert!(has_item(tcx, &items, (DefKind::Fn, "foo::bar"))); |
| 35 | + |
| 36 | + // Find the `std` crate. |
| 37 | + assert!(stable_mir::find_crate("std").is_some()); |
| 38 | +} |
| 39 | + |
| 40 | +// Use internal API to find a function in a crate. |
| 41 | +fn has_item(tcx: TyCtxt, items: &stable_mir::CrateItems, item: (DefKind, &str)) -> bool { |
| 42 | + items.iter().any(|crate_item| { |
| 43 | + let def_id = rustc_internal::item_def_id(crate_item); |
| 44 | + tcx.def_kind(def_id) == item.0 && tcx.def_path_str(def_id) == item.1 |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +/// This test will generate and analyze a dummy crate using the stable mir. |
| 49 | +/// For that, it will first write the dummy crate into a file. |
| 50 | +/// It will invoke the compiler using a custom Callback implementation, which will |
| 51 | +/// invoke Stable MIR APIs after the compiler has finished its analysis. |
| 52 | +fn main() { |
| 53 | + let path = "input.rs"; |
| 54 | + generate_input(&path).unwrap(); |
| 55 | + let args = vec![ |
| 56 | + "rustc".to_string(), |
| 57 | + "--crate-type=lib".to_string(), |
| 58 | + "--crate-name".to_string(), |
| 59 | + CRATE_NAME.to_string(), |
| 60 | + path.to_string(), |
| 61 | + ]; |
| 62 | + rustc_driver::catch_fatal_errors(|| { |
| 63 | + RunCompiler::new(&args, &mut SMirCalls {}).run().unwrap(); |
| 64 | + }) |
| 65 | + .unwrap(); |
| 66 | +} |
| 67 | + |
| 68 | +struct SMirCalls {} |
| 69 | + |
| 70 | +impl Callbacks for SMirCalls { |
| 71 | + /// Called after analysis. Return value instructs the compiler whether to |
| 72 | + /// continue the compilation afterwards (defaults to `Compilation::Continue`) |
| 73 | + fn after_analysis<'tcx>( |
| 74 | + &mut self, |
| 75 | + _compiler: &interface::Compiler, |
| 76 | + queries: &'tcx Queries<'tcx>, |
| 77 | + ) -> Compilation { |
| 78 | + queries.global_ctxt().unwrap().enter(|tcx| { |
| 79 | + test_stable_mir(tcx); |
| 80 | + }); |
| 81 | + // No need to keep going. |
| 82 | + Compilation::Stop |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +fn generate_input(path: &str) -> std::io::Result<()> { |
| 87 | + let mut file = std::fs::File::create(path)?; |
| 88 | + write!( |
| 89 | + file, |
| 90 | + r#" |
| 91 | + mod foo {{ |
| 92 | + pub fn bar(i: i32) -> i64 {{ |
| 93 | + i as i64 |
| 94 | + }} |
| 95 | + }} |
| 96 | +
|
| 97 | + pub fn foo_bar(x: i32, y: i32) -> i64 {{ |
| 98 | + let x_64 = foo::bar(x); |
| 99 | + let y_64 = foo::bar(y); |
| 100 | + x_64.wrapping_add(y_64) |
| 101 | + }}"# |
| 102 | + )?; |
| 103 | + Ok(()) |
| 104 | +} |
0 commit comments