|
| 1 | +//@ run-pass |
| 2 | +//! Test information regarding type layout. |
| 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(control_flow_enum)] |
| 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, CrateItems}; |
| 21 | +use std::io::Write; |
| 22 | +use std::ops::ControlFlow; |
| 23 | + |
| 24 | +const CRATE_NAME: &str = "input"; |
| 25 | + |
| 26 | +/// This function uses the Stable MIR APIs to get information about the test crate. |
| 27 | +fn test_stable_mir() -> ControlFlow<()> { |
| 28 | + // Find items in the local crate. |
| 29 | + let items = stable_mir::all_local_items(); |
| 30 | + |
| 31 | + test_builtins(&items); |
| 32 | + test_derive(&items); |
| 33 | + test_tool(&items); |
| 34 | + test_all_attrs(&items); |
| 35 | + |
| 36 | + ControlFlow::Continue(()) |
| 37 | +} |
| 38 | + |
| 39 | +// Test built-in attributes. |
| 40 | +fn test_builtins(items: &CrateItems) { |
| 41 | + let target_fn = *get_item(&items, "builtins_fn").unwrap(); |
| 42 | + let allow_attrs = target_fn.attrs_by_path(&["allow".to_string()]); |
| 43 | + assert_eq!(allow_attrs[0].as_str(), "#![allow(unused_variables)]"); |
| 44 | + |
| 45 | + let inline_attrs = target_fn.attrs_by_path(&["inline".to_string()]); |
| 46 | + assert_eq!(inline_attrs[0].as_str(), "#[inline]"); |
| 47 | + |
| 48 | + let deprecated_attrs = target_fn.attrs_by_path(&["deprecated".to_string()]); |
| 49 | + assert_eq!(deprecated_attrs[0].as_str(), "#[deprecated(since = \"5.2.0\")]"); |
| 50 | +} |
| 51 | + |
| 52 | +// Test derive attribute. |
| 53 | +fn test_derive(items: &CrateItems) { |
| 54 | + let target_struct = *get_item(&items, "Foo").unwrap(); |
| 55 | + let attrs = target_struct.attrs_by_path(&["derive".to_string()]); |
| 56 | + // No `derive` attribute since it's expanded before MIR. |
| 57 | + assert_eq!(attrs.len(), 0); |
| 58 | + |
| 59 | + // Check derived trait method's attributes. |
| 60 | + let derived_fmt = *get_item(&items, "<Foo as std::fmt::Debug>::fmt").unwrap(); |
| 61 | + // The Rust reference lies about this attribute. It doesn't show up in `clone` or `fmt` impl. |
| 62 | + let _fmt_attrs = derived_fmt.attrs_by_path(&["automatically_derived".to_string()]); |
| 63 | +} |
| 64 | + |
| 65 | +// Test tool attributes. |
| 66 | +fn test_tool(items: &CrateItems) { |
| 67 | + let rustfmt_fn = *get_item(&items, "do_not_format").unwrap(); |
| 68 | + let rustfmt_attrs = rustfmt_fn.attrs_by_path(&["rustfmt".to_string(), "skip".to_string()]); |
| 69 | + assert_eq!(rustfmt_attrs[0].as_str(), "#[rustfmt::skip]"); |
| 70 | + |
| 71 | + let clippy_fn = *get_item(&items, "complex_fn").unwrap(); |
| 72 | + let clippy_attrs = clippy_fn.attrs_by_path(&["clippy".to_string(), |
| 73 | + "cyclomatic_complexity".to_string()]); |
| 74 | + assert_eq!(clippy_attrs[0].as_str(), "#[clippy::cyclomatic_complexity = \"100\"]"); |
| 75 | +} |
| 76 | + |
| 77 | +fn test_all_attrs(items: &CrateItems) { |
| 78 | + let target_fn = *get_item(&items, "many_attrs").unwrap(); |
| 79 | + let all_attrs = target_fn.all_attrs(); |
| 80 | + assert_eq!(all_attrs[0].as_str(), "#[inline]"); |
| 81 | + assert_eq!(all_attrs[1].as_str(), "#[allow(unused_variables)]"); |
| 82 | + assert_eq!(all_attrs[2].as_str(), "#[allow(dead_code)]"); |
| 83 | + assert_eq!(all_attrs[3].as_str(), "#[allow(unused_imports)]"); |
| 84 | + assert_eq!(all_attrs[4].as_str(), "#![allow(clippy::filter_map)]"); |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +fn get_item<'a>( |
| 89 | + items: &'a CrateItems, |
| 90 | + name: &str, |
| 91 | +) -> Option<&'a stable_mir::CrateItem> { |
| 92 | + items.iter().find(|crate_item| crate_item.name() == name) |
| 93 | +} |
| 94 | + |
| 95 | +/// This test will generate and analyze a dummy crate using the stable mir. |
| 96 | +/// For that, it will first write the dummy crate into a file. |
| 97 | +/// Then it will create a `StableMir` using custom arguments and then |
| 98 | +/// it will run the compiler. |
| 99 | +fn main() { |
| 100 | + let path = "attribute_input.rs"; |
| 101 | + generate_input(&path).unwrap(); |
| 102 | + let args = vec![ |
| 103 | + "rustc".to_string(), |
| 104 | + "--crate-type=lib".to_string(), |
| 105 | + "--crate-name".to_string(), |
| 106 | + CRATE_NAME.to_string(), |
| 107 | + path.to_string(), |
| 108 | + ]; |
| 109 | + run!(args, test_stable_mir).unwrap(); |
| 110 | +} |
| 111 | + |
| 112 | +fn generate_input(path: &str) -> std::io::Result<()> { |
| 113 | + let mut file = std::fs::File::create(path)?; |
| 114 | + write!( |
| 115 | + file, |
| 116 | + r#" |
| 117 | + // General metadata applied to the enclosing module or crate. |
| 118 | + #![crate_type = "lib"] |
| 119 | +
|
| 120 | + // Mixed inner and outer attributes. |
| 121 | + #[inline] |
| 122 | + #[deprecated(since = "5.2.0")] |
| 123 | + fn builtins_fn() {{ |
| 124 | + #![allow(unused_variables)] |
| 125 | +
|
| 126 | + let x = (); |
| 127 | + let y = (); |
| 128 | + let z = (); |
| 129 | + }} |
| 130 | +
|
| 131 | + // A derive attribute to automatically implement a trait. |
| 132 | + #[derive(Debug, Clone, Copy)] |
| 133 | + struct Foo(u32); |
| 134 | +
|
| 135 | + // A rustfmt tool attribute. |
| 136 | + #[rustfmt::skip] |
| 137 | + fn do_not_format() {{}} |
| 138 | +
|
| 139 | + // A clippy tool attribute. |
| 140 | + #[clippy::cyclomatic_complexity = "100"] |
| 141 | + pub fn complex_fn() {{}} |
| 142 | +
|
| 143 | + // A function with many attributes. |
| 144 | + #[inline] |
| 145 | + #[allow(unused_variables)] |
| 146 | + #[allow(dead_code)] |
| 147 | + #[allow(unused_imports)] |
| 148 | + fn many_attrs() {{ |
| 149 | + #![allow(clippy::filter_map)] |
| 150 | + todo!() |
| 151 | + }} |
| 152 | + "# |
| 153 | + )?; |
| 154 | + Ok(()) |
| 155 | +} |
0 commit comments