From 510f709c6dffc68803b13a653276a83aeb43987b Mon Sep 17 00:00:00 2001 From: Makai Date: Sun, 19 Apr 2026 15:46:15 +0800 Subject: [PATCH] rustc_public: implement `Pattern` type --- compiler/rustc_public/src/ty.rs | 4 +- .../src/unstable/convert/internal.rs | 8 ++- .../src/unstable/convert/stable/ty.rs | 11 ++-- .../src/unstable/internal_cx/mod.rs | 8 +++ compiler/rustc_public/src/unstable/mod.rs | 5 ++ .../rustc_public/check_fmt_macro.rs | 60 +++++++++++++++++++ 6 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 tests/ui-fulldeps/rustc_public/check_fmt_macro.rs diff --git a/compiler/rustc_public/src/ty.rs b/compiler/rustc_public/src/ty.rs index 7ec8b688402ad..ef485cb69aa6a 100644 --- a/compiler/rustc_public/src/ty.rs +++ b/compiler/rustc_public/src/ty.rs @@ -109,7 +109,9 @@ impl Ty { /// Represents a pattern in the type system #[derive(Clone, Debug, Eq, PartialEq, Serialize)] pub enum Pattern { - Range { start: Option, end: Option, include_end: bool }, + Range { start: TyConst, end: TyConst, include_end: bool }, + NotNull, + Or(Vec), } /// Represents a constant in the type system diff --git a/compiler/rustc_public/src/unstable/convert/internal.rs b/compiler/rustc_public/src/unstable/convert/internal.rs index 4f3e9f94c599b..770f4c80fc90e 100644 --- a/compiler/rustc_public/src/unstable/convert/internal.rs +++ b/compiler/rustc_public/src/unstable/convert/internal.rs @@ -125,9 +125,13 @@ impl RustcInternal for Pattern { ) -> Self::T<'tcx> { tcx.mk_pat(match self { Pattern::Range { start, end, include_end: _ } => rustc_ty::PatternKind::Range { - start: start.as_ref().unwrap().internal(tables, tcx), - end: end.as_ref().unwrap().internal(tables, tcx), + start: start.internal(tables, tcx), + end: end.internal(tables, tcx), }, + Pattern::NotNull => rustc_ty::PatternKind::NotNull, + Pattern::Or(patterns) => rustc_ty::PatternKind::Or( + tcx.mk_patterns_from_iter(patterns.iter().map(|p| p.internal(tables, tcx))), + ), }) } } diff --git a/compiler/rustc_public/src/unstable/convert/stable/ty.rs b/compiler/rustc_public/src/unstable/convert/stable/ty.rs index 9a9576d47efd2..3ae968fd0dc06 100644 --- a/compiler/rustc_public/src/unstable/convert/stable/ty.rs +++ b/compiler/rustc_public/src/unstable/convert/stable/ty.rs @@ -503,13 +503,14 @@ impl<'tcx> Stable<'tcx> for ty::Pattern<'tcx> { ) -> Self::T { match **self { ty::PatternKind::Range { start, end } => crate::ty::Pattern::Range { - // FIXME(SMIR): update data structures to not have an Option here anymore - start: Some(start.stable(tables, cx)), - end: Some(end.stable(tables, cx)), + start: start.stable(tables, cx), + end: end.stable(tables, cx), include_end: true, }, - ty::PatternKind::NotNull => todo!(), - ty::PatternKind::Or(_) => todo!(), + ty::PatternKind::NotNull => crate::ty::Pattern::NotNull, + ty::PatternKind::Or(pats) => { + crate::ty::Pattern::Or(pats.iter().map(|pat| pat.stable(tables, cx)).collect()) + } } } } diff --git a/compiler/rustc_public/src/unstable/internal_cx/mod.rs b/compiler/rustc_public/src/unstable/internal_cx/mod.rs index 4da86b9442f1d..161f2754bed87 100644 --- a/compiler/rustc_public/src/unstable/internal_cx/mod.rs +++ b/compiler/rustc_public/src/unstable/internal_cx/mod.rs @@ -93,4 +93,12 @@ impl<'tcx> InternalCx<'tcx> for TyCtxt<'tcx> { fn adt_def(self, def_id: rustc_hir::def_id::DefId) -> ty::AdtDef<'tcx> { self.adt_def(def_id) } + + fn mk_patterns_from_iter(self, iter: I) -> T::Output + where + I: Iterator, + T: ty::CollectAndApply, &'tcx List>>, + { + TyCtxt::mk_patterns_from_iter(self, iter) + } } diff --git a/compiler/rustc_public/src/unstable/mod.rs b/compiler/rustc_public/src/unstable/mod.rs index fa5ef5a9ca6d6..ec979eef40cd1 100644 --- a/compiler/rustc_public/src/unstable/mod.rs +++ b/compiler/rustc_public/src/unstable/mod.rs @@ -55,6 +55,11 @@ pub trait InternalCx<'tcx>: Copy + Clone { fn mk_place_elems(self, v: &[mir::PlaceElem<'tcx>]) -> &'tcx List>; fn adt_def(self, def_id: rustc_hir::def_id::DefId) -> ty::AdtDef<'tcx>; + + fn mk_patterns_from_iter(self, iter: I) -> T::Output + where + I: Iterator, + T: ty::CollectAndApply, &'tcx List>>; } /// Trait used to convert between an internal MIR type to a rustc_public's IR type. diff --git a/tests/ui-fulldeps/rustc_public/check_fmt_macro.rs b/tests/ui-fulldeps/rustc_public/check_fmt_macro.rs new file mode 100644 index 0000000000000..ae20797855b7f --- /dev/null +++ b/tests/ui-fulldeps/rustc_public/check_fmt_macro.rs @@ -0,0 +1,60 @@ +//@ run-pass +//! Test that users are able to use rustc_public to consume formatting macros. +//@ ignore-stage1 +//@ ignore-cross-compile +//@ ignore-remote +//@ edition: 2021 + +#![feature(rustc_private)] + +extern crate rustc_middle; + +extern crate rustc_driver; +extern crate rustc_interface; +extern crate rustc_public; +use std::io::Write; +use std::ops::ControlFlow; +use rustc_public::run; + +const CRATE_NAME: &str = "fmt_macro"; + +/// Test if we can pass the compilation. +fn test_fmt_macro() -> ControlFlow<()> { + let entry_fn = rustc_public::entry_fn().unwrap().body().unwrap(); + for bb in &entry_fn.blocks { + for stmt in &bb.statements { + let _ = stmt; + } + } + ControlFlow::Continue(()) +} + +/// 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 = "fmt_macro_input.rs"; + generate_input(&path).unwrap(); + let args = &[ + "rustc".to_string(), + "-Cpanic=abort".to_string(), + "--crate-name".to_string(), + CRATE_NAME.to_string(), + path.to_string(), + ]; + run!(args, test_fmt_macro).unwrap(); +} + +fn generate_input(path: &str) -> std::io::Result<()> { + let mut file = std::fs::File::create(path)?; + write!( + file, + r#" + fn main() {{ + println!("hello world!"); + }} + "# + )?; + Ok(()) +}