diff options
| author | bors <bors@rust-lang.org> | 2025-07-21 19:48:22 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-07-21 19:48:22 +0000 |
| commit | 9748d87dc70a9a6725c5dbd76ce29d04752b4f90 (patch) | |
| tree | 4cffbc92abe5995b0348e464ffe6a256f973e653 /tests/ui-fulldeps/rustc_public/check_trait_queries.rs | |
| parent | 3f9f20f71dd945fe7d044e274094a53c90788269 (diff) | |
| parent | 6628a4afb5d08555f09878a48f0c4179e7582aa2 (diff) | |
| download | rust-9748d87dc70a9a6725c5dbd76ce29d04752b4f90.tar.gz rust-9748d87dc70a9a6725c5dbd76ce29d04752b4f90.zip | |
Auto merge of #144269 - jieyouxu:rollup-137ysl2, r=jieyouxu
Rollup of 14 pull requests Successful merges: - rust-lang/rust#142097 (gpu offload host code generation) - rust-lang/rust#143430 (Lower extra lifetimes before normal generic params.) - rust-lang/rust#143768 (Constify Try, From, TryFrom and relevant traits) - rust-lang/rust#143816 (Implement `check` for compiletest and RA using tool macro) - rust-lang/rust#143985 (rustc_public: de-StableMIR-ize) - rust-lang/rust#144027 (clippy: make tests work in stage 1) - rust-lang/rust#144080 (Mitigate `#[align]` name resolution ambiguity regression with a rename) - rust-lang/rust#144176 (Add approval blocking labels for new bors) - rust-lang/rust#144187 (fix handling of base address for TypeId allocations) - rust-lang/rust#144212 (Remove the ptr_unique lang item) - rust-lang/rust#144243 (Subtree update of `rust-analyzer`) - rust-lang/rust#144246 (Don't use another main test file as auxiliary) - rust-lang/rust#144251 (rustc-dev-guide subtree update) - rust-lang/rust#144254 (opt-dist: make `artifact-dir` an absolute path for `opt-dist local`) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'tests/ui-fulldeps/rustc_public/check_trait_queries.rs')
| -rw-r--r-- | tests/ui-fulldeps/rustc_public/check_trait_queries.rs | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/tests/ui-fulldeps/rustc_public/check_trait_queries.rs b/tests/ui-fulldeps/rustc_public/check_trait_queries.rs new file mode 100644 index 00000000000..0dd13044fcc --- /dev/null +++ b/tests/ui-fulldeps/rustc_public/check_trait_queries.rs @@ -0,0 +1,121 @@ +//@ run-pass +//! Test that users are able to retrieve information about trait declarations and implementations. + +//@ ignore-stage1 +//@ ignore-cross-compile +//@ ignore-remote +//@ edition: 2021 + +#![feature(rustc_private)] +#![feature(assert_matches)] + +extern crate rustc_middle; + +extern crate rustc_driver; +extern crate rustc_interface; +#[macro_use] +extern crate rustc_public; + +use rustc_public::CrateDef; +use std::collections::HashSet; +use std::io::Write; +use std::ops::ControlFlow; + +const CRATE_NAME: &str = "trait_test"; + +/// This function uses the Stable MIR APIs to get information about the test crate. +fn test_traits() -> ControlFlow<()> { + let local_crate = rustc_public::local_crate(); + let local_traits = local_crate.trait_decls(); + assert_eq!(local_traits.len(), 1, "Expected `Max` trait, but found {:?}", local_traits); + assert_eq!(&local_traits[0].name(), "Max"); + + let local_impls = local_crate.trait_impls(); + let impl_names = local_impls.iter().map(|trait_impl| trait_impl.name()).collect::<HashSet<_>>(); + assert_impl(&impl_names, "<Positive as Max>"); + assert_impl(&impl_names, "<Positive as std::marker::Copy>"); + assert_impl(&impl_names, "<Positive as std::clone::Clone>"); + assert_impl(&impl_names, "<Positive as std::fmt::Debug>"); + assert_impl(&impl_names, "<Positive as std::cmp::PartialEq>"); + assert_impl(&impl_names, "<Positive as std::cmp::Eq>"); + assert_impl(&impl_names, "<Positive as std::convert::TryFrom<u64>>"); + assert_impl(&impl_names, "<u64 as Max>"); + assert_impl(&impl_names, "<impl std::convert::From<Positive> for u64>"); + + let all_traits = rustc_public::all_trait_decls(); + assert!(all_traits.len() > local_traits.len()); + assert!( + local_traits.iter().all(|t| all_traits.contains(t)), + "Local: {local_traits:#?}, All: {all_traits:#?}" + ); + + let all_impls = rustc_public::all_trait_impls(); + assert!(all_impls.len() > local_impls.len()); + assert!( + local_impls.iter().all(|t| all_impls.contains(t)), + "Local: {local_impls:#?}, All: {all_impls:#?}" + ); + ControlFlow::Continue(()) +} + +fn assert_impl(impl_names: &HashSet<String>, target: &str) { + assert!( + impl_names.contains(target), + "Failed to find `{target}`. Implementations available: {impl_names:?}", + ); +} + +/// This test will generate and analyze a dummy crate using the stable mir. +/// For that, it will first write the dummy crate into a file. +/// Then it will create a `RustcPublic` using custom arguments and then +/// it will run the compiler. +fn main() { + let path = "trait_queries.rs"; + generate_input(&path).unwrap(); + let args = &[ + "rustc".to_string(), + "--crate-type=lib".to_string(), + "--crate-name".to_string(), + CRATE_NAME.to_string(), + path.to_string(), + ]; + run!(args, test_traits).unwrap(); +} + +fn generate_input(path: &str) -> std::io::Result<()> { + let mut file = std::fs::File::create(path)?; + write!( + file, + r#" + use std::convert::TryFrom; + + #[derive(Copy, Clone, Debug, PartialEq, Eq)] + pub struct Positive(u64); + + impl TryFrom<u64> for Positive {{ + type Error = (); + fn try_from(val: u64) -> Result<Positive, Self::Error> {{ + if val > 0 {{ Ok(Positive(val)) }} else {{ Err(()) }} + }} + }} + + impl From<Positive> for u64 {{ + fn from(val: Positive) -> u64 {{ val.0 }} + }} + + pub trait Max {{ + fn is_max(&self) -> bool; + }} + + impl Max for u64 {{ + fn is_max(&self) -> bool {{ *self == u64::MAX }} + }} + + impl Max for Positive {{ + fn is_max(&self) -> bool {{ self.0.is_max() }} + }} + + "# + )?; + Ok(()) +} |
