|
| 1 | +//@ run-pass |
| 2 | +//! Test information about crate definitions (local and external). |
| 3 | +
|
| 4 | +//@ ignore-stage1 |
| 5 | +//@ ignore-cross-compile |
| 6 | +//@ ignore-remote |
| 7 | +//@ ignore-windows-gnu mingw has troubles with linking https://github.com/rust-lang/rust/pull/116837 |
| 8 | + |
| 9 | +#![feature(rustc_private)] |
| 10 | +#![feature(assert_matches)] |
| 11 | + |
| 12 | +extern crate rustc_hir; |
| 13 | +#[macro_use] |
| 14 | +extern crate rustc_smir; |
| 15 | +extern crate rustc_driver; |
| 16 | +extern crate rustc_interface; |
| 17 | +extern crate stable_mir; |
| 18 | + |
| 19 | +use rustc_smir::rustc_internal; |
| 20 | +use stable_mir::CrateDef; |
| 21 | +use std::collections::HashSet; |
| 22 | +use std::io::Write; |
| 23 | +use std::ops::ControlFlow; |
| 24 | + |
| 25 | +const CRATE_NAME: &str = "crate_defs"; |
| 26 | + |
| 27 | +/// This function uses the Stable MIR APIs to get information about the test crate. |
| 28 | +fn test_stable_mir() -> ControlFlow<()> { |
| 29 | + // Find items in the local crate. |
| 30 | + let local = stable_mir::local_crate(); |
| 31 | + check_items(&local.statics(), &["PRIVATE_STATIC", "dummy::PUBLIC_STATIC"]); |
| 32 | + check_items( |
| 33 | + &local.fn_defs(), |
| 34 | + &[ |
| 35 | + "top_level", |
| 36 | + "dummy::public_fn", |
| 37 | + "dummy::private_fn", |
| 38 | + "dummy::PrivateStruct::new", |
| 39 | + "<dummy::PrivateStruct as std::ops::Drop>::drop", |
| 40 | + "DummyTrait::method", |
| 41 | + "<T as DummyTrait>::method", |
| 42 | + ], |
| 43 | + ); |
| 44 | + |
| 45 | + // Find items inside core crate. |
| 46 | + // FIXME: We are currently missing primitive type methods and trait implementations for external |
| 47 | + // crates. |
| 48 | + let core = stable_mir::find_crates("core").pop().expect("Cannot find `core` crate"); |
| 49 | + contains( |
| 50 | + &core.fn_defs(), |
| 51 | + &[ |
| 52 | + "std::fmt::Debug::fmt", |
| 53 | + "std::option::Option::<T>::is_some", |
| 54 | + "std::ptr::swap", |
| 55 | + "<std::slice::Iter<'a, T> as std::iter::Iterator>::next", |
| 56 | + "core::num::<impl u8>::abs_diff", |
| 57 | + ], |
| 58 | + ); |
| 59 | + // Ensure nothing crashes. There is no public static in core that we can test here. |
| 60 | + let _ = core.statics(); |
| 61 | + |
| 62 | + ControlFlow::Continue(()) |
| 63 | +} |
| 64 | + |
| 65 | +/// Check if the list of definitions matches the expected list. |
| 66 | +/// Note that order doesn't matter. |
| 67 | +fn check_items<T: CrateDef>(items: &[T], expected: &[&str]) { |
| 68 | + let expected: HashSet<_> = expected.iter().map(|s| s.to_string()).collect(); |
| 69 | + let item_names: HashSet<_> = items.iter().map(|item| item.name()).collect(); |
| 70 | + assert_eq!(item_names, expected); |
| 71 | +} |
| 72 | + |
| 73 | +/// Check that the list contains the expected items. |
| 74 | +fn contains<T: CrateDef + std::fmt::Debug>(items: &[T], expected: &[&str]) { |
| 75 | + let expected: HashSet<_> = expected.iter().map(|s| s.to_string()).collect(); |
| 76 | + let item_names = items.iter().map(|item| item.name()).collect(); |
| 77 | + let not_found: Vec<_> = expected.difference(&item_names).collect(); |
| 78 | + assert!(not_found.is_empty(), "Missing items: {:?}", not_found); |
| 79 | +} |
| 80 | + |
| 81 | +/// This test will generate and analyze a dummy crate using the stable mir. |
| 82 | +/// For that, it will first write the dummy crate into a file. |
| 83 | +/// Then it will create a `StableMir` using custom arguments and then |
| 84 | +/// it will run the compiler. |
| 85 | +fn main() { |
| 86 | + let path = "crate_definitions.rs"; |
| 87 | + generate_input(&path).unwrap(); |
| 88 | + let args = vec![ |
| 89 | + "rustc".to_string(), |
| 90 | + "--crate-type=lib".to_string(), |
| 91 | + "--crate-name".to_string(), |
| 92 | + CRATE_NAME.to_string(), |
| 93 | + path.to_string(), |
| 94 | + ]; |
| 95 | + run!(args, test_stable_mir).unwrap(); |
| 96 | +} |
| 97 | + |
| 98 | +fn generate_input(path: &str) -> std::io::Result<()> { |
| 99 | + let mut file = std::fs::File::create(path)?; |
| 100 | + write!( |
| 101 | + file, |
| 102 | + r#" |
| 103 | + #![allow(dead_code, unused_variables)] |
| 104 | + static PRIVATE_STATIC: u8 = 0; |
| 105 | + fn top_level() -> &'static str {{ |
| 106 | + "hello" |
| 107 | + }} |
| 108 | +
|
| 109 | + pub trait DummyTrait {{ |
| 110 | + fn method(&self) -> Self; |
| 111 | + }} |
| 112 | +
|
| 113 | + impl<T: Copy> DummyTrait for T {{ |
| 114 | + fn method(&self) -> T {{ |
| 115 | + *self |
| 116 | + }} |
| 117 | + }} |
| 118 | +
|
| 119 | + pub mod dummy {{ |
| 120 | + pub static mut PUBLIC_STATIC: Option<char> = None; |
| 121 | +
|
| 122 | + pub fn public_fn(input: bool) -> bool {{ |
| 123 | + private_fn(!input) |
| 124 | + }} |
| 125 | +
|
| 126 | + fn private_fn(input: bool) -> bool {{ |
| 127 | + todo!() |
| 128 | + }} |
| 129 | +
|
| 130 | + struct PrivateStruct {{ |
| 131 | + field: u32, |
| 132 | + }} |
| 133 | +
|
| 134 | + impl PrivateStruct {{ |
| 135 | + fn new() -> Self {{ |
| 136 | + Self {{ field: 42 }} |
| 137 | + }} |
| 138 | + }} |
| 139 | +
|
| 140 | + impl Drop for PrivateStruct {{ |
| 141 | + fn drop(&mut self) {{ |
| 142 | + println!("Dropping PrivateStruct"); |
| 143 | + }} |
| 144 | + }} |
| 145 | + }} |
| 146 | + "# |
| 147 | + )?; |
| 148 | + Ok(()) |
| 149 | +} |
0 commit comments