|
| 1 | +// run-pass |
| 2 | +//! Test that item kind works as expected. |
| 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 | +// edition: 2021 |
| 9 | + |
| 10 | +#![feature(rustc_private)] |
| 11 | +#![feature(assert_matches)] |
| 12 | +#![feature(control_flow_enum)] |
| 13 | + |
| 14 | +extern crate rustc_middle; |
| 15 | +#[macro_use] |
| 16 | +extern crate rustc_smir; |
| 17 | +extern crate rustc_driver; |
| 18 | +extern crate rustc_interface; |
| 19 | +extern crate stable_mir; |
| 20 | + |
| 21 | +use rustc_middle::ty::TyCtxt; |
| 22 | +use rustc_smir::rustc_internal; |
| 23 | +use stable_mir::*; |
| 24 | +use std::io::Write; |
| 25 | +use std::ops::ControlFlow; |
| 26 | + |
| 27 | +const CRATE_NAME: &str = "input"; |
| 28 | + |
| 29 | +/// This function uses the Stable MIR APIs to get information about the test crate. |
| 30 | +fn test_item_kind(_tcx: TyCtxt<'_>) -> ControlFlow<()> { |
| 31 | + let items = stable_mir::all_local_items(); |
| 32 | + assert_eq!(items.len(), 4); |
| 33 | + // Constructor item. |
| 34 | + for item in items { |
| 35 | + let expected_kind = match item.name().as_str() { |
| 36 | + "Dummy" => ItemKind::Ctor(CtorKind::Fn), |
| 37 | + "dummy" => ItemKind::Fn, |
| 38 | + "unit" => ItemKind::Fn, |
| 39 | + "DUMMY_CONST" => ItemKind::Const, |
| 40 | + name => unreachable!("Unexpected item {name}"), |
| 41 | + }; |
| 42 | + assert_eq!(item.kind(), expected_kind, "Mismatched type for {}", item.name()); |
| 43 | + } |
| 44 | + ControlFlow::Continue(()) |
| 45 | +} |
| 46 | + |
| 47 | +/// This test will generate and analyze a dummy crate using the stable mir. |
| 48 | +/// For that, it will first write the dummy crate into a file. |
| 49 | +/// Then it will create a `StableMir` using custom arguments and then |
| 50 | +/// it will run the compiler. |
| 51 | +fn main() { |
| 52 | + let path = "item_kind_input.rs"; |
| 53 | + generate_input(&path).unwrap(); |
| 54 | + let args = vec![ |
| 55 | + "rustc".to_string(), |
| 56 | + "-Cpanic=abort".to_string(), |
| 57 | + "--crate-type=lib".to_string(), |
| 58 | + "--crate-name".to_string(), |
| 59 | + CRATE_NAME.to_string(), |
| 60 | + path.to_string(), |
| 61 | + ]; |
| 62 | + run!(args, tcx, test_item_kind(tcx)).unwrap(); |
| 63 | +} |
| 64 | + |
| 65 | +fn generate_input(path: &str) -> std::io::Result<()> { |
| 66 | + let mut file = std::fs::File::create(path)?; |
| 67 | + write!( |
| 68 | + file, |
| 69 | + r#" |
| 70 | + pub struct Dummy(u32); |
| 71 | + pub const DUMMY_CONST: Dummy = Dummy(0); |
| 72 | + pub struct DummyUnit; |
| 73 | +
|
| 74 | + pub fn dummy() -> Dummy {{ |
| 75 | + Dummy(5) |
| 76 | + }} |
| 77 | +
|
| 78 | + pub fn unit() -> DummyUnit {{ |
| 79 | + DummyUnit |
| 80 | + }} |
| 81 | + "# |
| 82 | + )?; |
| 83 | + Ok(()) |
| 84 | +} |
0 commit comments