diff --git a/compiler/rustc_public/src/compiler_interface.rs b/compiler/rustc_public/src/compiler_interface.rs index 0c77abee2dab6..d8021447be79d 100644 --- a/compiler/rustc_public/src/compiler_interface.rs +++ b/compiler/rustc_public/src/compiler_interface.rs @@ -389,6 +389,19 @@ impl<'tcx> CompilerInterface<'tcx> { self.with_cx(|tables, cx| cx.adt_repr(def.internal(tables, cx.tcx)).stable(tables, cx)) } + /// Returns whether this ADT is a phantom data + pub(crate) fn adt_is_phantom_data(&self, def: AdtDef) -> bool { + self.with_cx(|tables, cx| cx.adt_is_phantom_data(def.internal(tables, cx.tcx))) + } + /// Returns whether this ADT is manually drop + pub(crate) fn adt_is_manually_drop(&self, def: AdtDef) -> bool { + self.with_cx(|tables, cx| cx.adt_is_manually_drop(def.internal(tables, cx.tcx))) + } + /// Returns whether this ADT is unsafe cell + pub(crate) fn adt_is_unsafe_cell(&self, def: AdtDef) -> bool { + self.with_cx(|tables, cx| cx.adt_is_unsafe_cell(def.internal(tables, cx.tcx))) + } + /// Retrieve the function signature for the given generic arguments. pub(crate) fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig { self.with_cx(|tables, cx| { diff --git a/compiler/rustc_public/src/ty/def.rs b/compiler/rustc_public/src/ty/def.rs index 4e34e3bbb2e33..2ce9c1d572c53 100644 --- a/compiler/rustc_public/src/ty/def.rs +++ b/compiler/rustc_public/src/ty/def.rs @@ -204,6 +204,18 @@ impl AdtDef { with(|cx| cx.adt_is_simd(*self)) } + pub fn is_phantom_data(&self) -> bool { + with(|cx| cx.adt_is_phantom_data(*self)) + } + + pub fn is_manually_drop(&self) -> bool { + with(|cx| cx.adt_is_manually_drop(*self)) + } + + pub fn is_unsafe_cell(&self) -> bool { + with(|cx| cx.adt_is_unsafe_cell(*self)) + } + /// The number of variants in this ADT. pub fn num_variants(&self) -> usize { with(|cx| cx.adt_variants_len(*self)) diff --git a/compiler/rustc_public_bridge/src/context/impls.rs b/compiler/rustc_public_bridge/src/context/impls.rs index 945331d14b073..e9f2c4cce6d59 100644 --- a/compiler/rustc_public_bridge/src/context/impls.rs +++ b/compiler/rustc_public_bridge/src/context/impls.rs @@ -376,6 +376,20 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { def.repr() } + /// Returns if the ADT is a PhantomData + pub fn adt_is_phantom_data(&self, def: AdtDef<'tcx>) -> bool { + def.is_phantom_data() + } + + /// Returns if the ADT is ManuallyDrop + pub fn adt_is_manually_drop(&self, def: AdtDef<'tcx>) -> bool { + def.is_manually_drop() + } + + /// Returns if the ADT is an UnsafeCell + pub fn adt_is_unsafe_cell(&self, def: AdtDef<'tcx>) -> bool { + def.is_unsafe_cell() + } /// Retrieve the function signature for the given generic arguments. pub fn fn_sig(&self, def_id: DefId, args_ref: GenericArgs<'tcx>) -> Binder<'tcx, FnSig<'tcx>> { let sig = self.tcx.fn_sig(def_id).instantiate(self.tcx, args_ref).skip_norm_wip(); diff --git a/tests/ui-fulldeps/rustc_public/check_adt_queries.rs b/tests/ui-fulldeps/rustc_public/check_adt_queries.rs new file mode 100644 index 0000000000000..ebad1b56903e3 --- /dev/null +++ b/tests/ui-fulldeps/rustc_public/check_adt_queries.rs @@ -0,0 +1,158 @@ +//@ run-pass +//! Test that users are able to retrieve information about ADTs + +//@ ignore-stage1 +//@ ignore-cross-compile +//@ ignore-remote +//@ edition: 2024 + +#![feature(rustc_private)] + +extern crate rustc_middle; + +extern crate rustc_driver; +extern crate rustc_interface; +#[macro_use] +extern crate rustc_public; + +use std::{collections::HashSet, io::Write, ops::ControlFlow}; +use rustc_public::{ + mir::{mono::Instance, MirVisitor}, + ty::{AdtDef, RigidTy, Ty, TyKind} +}; + +const CRATE_NAME: &str = "adt_test"; + +#[derive(Default)] +struct AdtDefVisitor { + adt_defs: HashSet, +} + +impl MirVisitor for AdtDefVisitor { + fn visit_ty(&mut self, ty: &Ty, _location: rustc_public::mir::visit::Location) { + if let TyKind::RigidTy(RigidTy::Adt(adt, _)) = ty.kind() { + self.adt_defs.insert(adt); + } + self.super_ty(ty) + } +} + +/// This function uses the Stable MIR APIs to get information about the test crate. +fn test_adts() -> ControlFlow<()> { + let entry = rustc_public::entry_fn().expect("Expected main function"); + let main_fn = Instance::try_from(entry).expect("Expected main instance"); + let mut visitor = AdtDefVisitor::default(); + visitor.visit_body(&main_fn.body().expect("Expected main body")); + let AdtDefVisitor { adt_defs } = visitor; + + assert_eq!( + adt_defs.len(), + 5, + "Expected 5 ADTs, but found {:?}", adt_defs, + ); + + let has_box = adt_defs + .iter() + .any(|item| item.is_box()); + + let has_simd = adt_defs + .iter() + .any(|item| item.is_simd()); + + let has_phantom_data = adt_defs + .iter() + .any(|item| item.is_phantom_data()); + + let has_manually_drop = adt_defs + .iter() + .any(|item| item.is_manually_drop()); + + let has_unsafe_cell = adt_defs + .iter() + .any(|item| item.is_unsafe_cell()); + + assert!(has_box); + assert!(has_simd); + assert!(has_phantom_data); + assert!(has_manually_drop); + assert!(has_unsafe_cell); + + let adt_names = adt_defs.iter().map(|def| def.0.trimmed_name()).collect::>(); + + assert_name(&adt_names, "Box"); + assert_name(&adt_names, "PhantomData"); + assert_name(&adt_names, "Simd"); + assert_name(&adt_names, "ManuallyDrop"); + assert_name(&adt_names, "UnsafeCell"); + + // Verify all AdtDef::ty() returns the self type (Adt) + adt_defs.iter().for_each(|adt|{ + let ty = adt.ty(); + assert!( + matches!( + ty.kind(), + rustc_public::ty::TyKind::RigidTy(RigidTy::Adt(..)) + ), + ); + }); + + ControlFlow::Continue(()) +} + +fn assert_name(adt_names: &HashSet, target: &str) { + assert!( + adt_names.contains(target), + "Failed to find `{target}`. Implementations available: {adt_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 = "adt_queries.rs"; + generate_input(&path).unwrap(); + let args = &[ + "rustc".to_string(), + "-Cpanic=abort".to_string(), + "--edition".to_string(), + "2024".to_string(), + "--crate-name".to_string(), + CRATE_NAME.to_string(), + path.to_string(), + ]; + run!(args, test_adts).unwrap(); +} + +fn generate_input(path: &str) -> std::io::Result<()> { + let mut file = std::fs::File::create(path)?; + write!( + file, + r#" + #![feature(portable_simd)] + + use std::boxed::Box; + use std::cell::UnsafeCell; + use std::ffi::CStr; + use std::mem::ManuallyDrop; + use std::marker::PhantomData; + use std::simd::f32x4; + + fn main() {{ + let _box = Box::new(42); + + let _phantom = PhantomData::; + + let _unsafe_cell = UnsafeCell::new(42); + + let mut _manually_drop = ManuallyDrop::new(42); + + let _cs: &CStr = c"hello"; + + let _simd = f32x4::splat(10.0); + }} + "# + )?; + Ok(()) +}