|
| 1 | +// run-pass |
| 2 | +//! Test that users are able to retrieve information about trait declarations and implementations. |
| 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::CrateDef; |
| 24 | +use std::collections::HashSet; |
| 25 | +use std::io::Write; |
| 26 | +use std::ops::ControlFlow; |
| 27 | + |
| 28 | +const CRATE_NAME: &str = "trait_test"; |
| 29 | + |
| 30 | +/// This function uses the Stable MIR APIs to get information about the test crate. |
| 31 | +fn test_traits() -> ControlFlow<()> { |
| 32 | + let local_crate = stable_mir::local_crate(); |
| 33 | + let local_traits = local_crate.trait_decls(); |
| 34 | + assert_eq!(local_traits.len(), 1, "Expected `Max` trait, but found {:?}", local_traits); |
| 35 | + assert_eq!(&local_traits[0].name(), "Max"); |
| 36 | + |
| 37 | + let local_impls = local_crate.trait_impls(); |
| 38 | + let impl_names = local_impls.iter().map(|trait_impl| trait_impl.name()).collect::<HashSet<_>>(); |
| 39 | + assert_impl(&impl_names, "<Positive as Max>"); |
| 40 | + assert_impl(&impl_names, "<Positive as std::marker::Copy>"); |
| 41 | + assert_impl(&impl_names, "<Positive as std::clone::Clone>"); |
| 42 | + assert_impl(&impl_names, "<Positive as std::fmt::Debug>"); |
| 43 | + assert_impl(&impl_names, "<Positive as std::cmp::PartialEq>"); |
| 44 | + assert_impl(&impl_names, "<Positive as std::cmp::Eq>"); |
| 45 | + assert_impl(&impl_names, "<Positive as std::convert::TryFrom<u64>>"); |
| 46 | + assert_impl(&impl_names, "<u64 as Max>"); |
| 47 | + assert_impl(&impl_names, "<impl std::convert::From<Positive> for u64>"); |
| 48 | + |
| 49 | + let all_traits = stable_mir::all_trait_decls(); |
| 50 | + assert!(all_traits.len() > local_traits.len()); |
| 51 | + assert!( |
| 52 | + local_traits.iter().all(|t| all_traits.contains(t)), |
| 53 | + "Local: {local_traits:#?}, All: {all_traits:#?}" |
| 54 | + ); |
| 55 | + |
| 56 | + let all_impls = stable_mir::all_trait_impls(); |
| 57 | + assert!(all_impls.len() > local_impls.len()); |
| 58 | + assert!( |
| 59 | + local_impls.iter().all(|t| all_impls.contains(t)), |
| 60 | + "Local: {local_impls:#?}, All: {all_impls:#?}" |
| 61 | + ); |
| 62 | + ControlFlow::Continue(()) |
| 63 | +} |
| 64 | + |
| 65 | +fn assert_impl(impl_names: &HashSet<String>, target: &str) { |
| 66 | + assert!( |
| 67 | + impl_names.contains(target), |
| 68 | + "Failed to find `{target}`. Implementations available: {impl_names:?}", |
| 69 | + ); |
| 70 | +} |
| 71 | + |
| 72 | +/// This test will generate and analyze a dummy crate using the stable mir. |
| 73 | +/// For that, it will first write the dummy crate into a file. |
| 74 | +/// Then it will create a `StableMir` using custom arguments and then |
| 75 | +/// it will run the compiler. |
| 76 | +fn main() { |
| 77 | + let path = "trait_queries.rs"; |
| 78 | + generate_input(&path).unwrap(); |
| 79 | + let args = vec![ |
| 80 | + "rustc".to_string(), |
| 81 | + "--crate-type=lib".to_string(), |
| 82 | + "--crate-name".to_string(), |
| 83 | + CRATE_NAME.to_string(), |
| 84 | + path.to_string(), |
| 85 | + ]; |
| 86 | + run!(args, test_traits()).unwrap(); |
| 87 | +} |
| 88 | + |
| 89 | +fn generate_input(path: &str) -> std::io::Result<()> { |
| 90 | + let mut file = std::fs::File::create(path)?; |
| 91 | + write!( |
| 92 | + file, |
| 93 | + r#" |
| 94 | + use std::convert::TryFrom; |
| 95 | +
|
| 96 | + #[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 97 | + pub struct Positive(u64); |
| 98 | +
|
| 99 | + impl TryFrom<u64> for Positive {{ |
| 100 | + type Error = (); |
| 101 | + fn try_from(val: u64) -> Result<Positive, Self::Error> {{ |
| 102 | + if val > 0 {{ Ok(Positive(val)) }} else {{ Err(()) }} |
| 103 | + }} |
| 104 | + }} |
| 105 | +
|
| 106 | + impl From<Positive> for u64 {{ |
| 107 | + fn from(val: Positive) -> u64 {{ val.0 }} |
| 108 | + }} |
| 109 | +
|
| 110 | + pub trait Max {{ |
| 111 | + fn is_max(&self) -> bool; |
| 112 | + }} |
| 113 | +
|
| 114 | + impl Max for u64 {{ |
| 115 | + fn is_max(&self) -> bool {{ *self == u64::MAX }} |
| 116 | + }} |
| 117 | +
|
| 118 | + impl Max for Positive {{ |
| 119 | + fn is_max(&self) -> bool {{ self.0.is_max() }} |
| 120 | + }} |
| 121 | +
|
| 122 | + "# |
| 123 | + )?; |
| 124 | + Ok(()) |
| 125 | +} |
0 commit comments