From 06154f0ccd26dffb4424a8624caffa654514a923 Mon Sep 17 00:00:00 2001 From: adpthegreat Date: Sun, 15 Nov 2026 18:50:55 +0100 Subject: [PATCH 1/6] feat: added more AdtDef queries --- compiler/rustc_public/src/compiler_interface.rs | 13 +++++++++++++ compiler/rustc_public/src/ty/def.rs | 12 ++++++++++++ compiler/rustc_public_bridge/src/context/impls.rs | 14 ++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/compiler/rustc_public/src/compiler_interface.rs b/compiler/rustc_public/src/compiler_interface.rs index 0c77abee2dab6..ba3e253ce71f0 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) -> ReprOptions { + 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) -> ReprOptions { + 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) -> ReprOptions { + 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..bad54dffe073a 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..ccd1bc5bedb60 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 box. + pub fn adt_is_phantom_data(&self, def: AdtDef<'tcx>) -> bool { + def.is_phantom_data() + } + + /// Returns if the ADT is a box. + pub fn adt_is_manually_drop(&self, def: AdtDef<'tcx>) -> bool { + def.is_manually_drop() + } + + /// Returns if the ADT is a box. + 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(); From b0fdb914d9ba61c8ebd3046683fc4a4699a3f5e3 Mon Sep 17 00:00:00 2001 From: adpthegreat Date: Sun, 15 Nov 2026 23:47:10 +0100 Subject: [PATCH 2/6] fix: fixed wrong return type --- compiler/rustc_public/src/compiler_interface.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_public/src/compiler_interface.rs b/compiler/rustc_public/src/compiler_interface.rs index ba3e253ce71f0..1e7c8aad22369 100644 --- a/compiler/rustc_public/src/compiler_interface.rs +++ b/compiler/rustc_public/src/compiler_interface.rs @@ -390,15 +390,15 @@ impl<'tcx> CompilerInterface<'tcx> { } /// Returns whether this ADT is a phantom data - pub(crate) fn adt_is_phantom_data(&self, def: AdtDef) -> ReprOptions { + 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) -> ReprOptions { + 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) -> ReprOptions { + 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))) } From 6a4efb5f42f085afe4de3e8697ea6ffa7e775a34 Mon Sep 17 00:00:00 2001 From: adpthegreat Date: Sun, 15 Nov 2026 23:49:16 +0100 Subject: [PATCH 3/6] fix : fixed comments --- compiler/rustc_public_bridge/src/context/impls.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_public_bridge/src/context/impls.rs b/compiler/rustc_public_bridge/src/context/impls.rs index ccd1bc5bedb60..e9f2c4cce6d59 100644 --- a/compiler/rustc_public_bridge/src/context/impls.rs +++ b/compiler/rustc_public_bridge/src/context/impls.rs @@ -376,17 +376,17 @@ impl<'tcx, B: Bridge> CompilerCtxt<'tcx, B> { def.repr() } - /// Returns if the ADT is a box. + /// 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 a box. + /// 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 a box. + /// Returns if the ADT is an UnsafeCell pub fn adt_is_unsafe_cell(&self, def: AdtDef<'tcx>) -> bool { def.is_unsafe_cell() } From bf8536803e3905a8b446f0a8bc9a0f07dc109011 Mon Sep 17 00:00:00 2001 From: adpthegreat Date: Fri, 20 Nov 2026 05:00:40 +0100 Subject: [PATCH 4/6] feat: write tests for new and existing adtdef queries --- .../rustc_public/check_adt_queries.rs | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 tests/ui-fulldeps/rustc_public/check_adt_queries.rs 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..d4e12fc46bd07 --- /dev/null +++ b/tests/ui-fulldeps/rustc_public/check_adt_queries.rs @@ -0,0 +1,159 @@ +//@ run-pass +//! Test that users are able to retrieve infromation 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(()) +} From 2f5945c44d4e81f799db80665556e6069015740e Mon Sep 17 00:00:00 2001 From: adpthegreat Date: Fri, 20 Nov 2026 11:56:50 +0100 Subject: [PATCH 5/6] chore: ran ./x fmt --- compiler/rustc_public/src/compiler_interface.rs | 2 +- compiler/rustc_public/src/ty/def.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_public/src/compiler_interface.rs b/compiler/rustc_public/src/compiler_interface.rs index 1e7c8aad22369..d8021447be79d 100644 --- a/compiler/rustc_public/src/compiler_interface.rs +++ b/compiler/rustc_public/src/compiler_interface.rs @@ -397,7 +397,7 @@ impl<'tcx> CompilerInterface<'tcx> { 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 + /// 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))) } diff --git a/compiler/rustc_public/src/ty/def.rs b/compiler/rustc_public/src/ty/def.rs index bad54dffe073a..2ce9c1d572c53 100644 --- a/compiler/rustc_public/src/ty/def.rs +++ b/compiler/rustc_public/src/ty/def.rs @@ -215,7 +215,7 @@ impl AdtDef { 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)) From 45426d2982a69a42f43c1d09a11fa351fb1d98b0 Mon Sep 17 00:00:00 2001 From: adpthegreat Date: Fri, 20 Nov 2026 16:04:08 +0100 Subject: [PATCH 6/6] chore: remove whitespace, fix typo --- tests/ui-fulldeps/rustc_public/check_adt_queries.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/ui-fulldeps/rustc_public/check_adt_queries.rs b/tests/ui-fulldeps/rustc_public/check_adt_queries.rs index d4e12fc46bd07..ebad1b56903e3 100644 --- a/tests/ui-fulldeps/rustc_public/check_adt_queries.rs +++ b/tests/ui-fulldeps/rustc_public/check_adt_queries.rs @@ -1,5 +1,5 @@ //@ run-pass -//! Test that users are able to retrieve infromation about ADTs +//! Test that users are able to retrieve information about ADTs //@ ignore-stage1 //@ ignore-cross-compile @@ -86,7 +86,6 @@ fn test_adts() -> ControlFlow<()> { 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!( @@ -149,7 +148,7 @@ fn generate_input(path: &str) -> std::io::Result<()> { let mut _manually_drop = ManuallyDrop::new(42); - let _cs: &CStr = c"hello"; + let _cs: &CStr = c"hello"; let _simd = f32x4::splat(10.0); }}