Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler/rustc_public/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TyConst>, end: Option<TyConst>, include_end: bool },
Range { start: TyConst, end: TyConst, include_end: bool },
NotNull,
Or(Vec<Pattern>),
}

/// Represents a constant in the type system
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_public/src/unstable/convert/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
),
})
}
}
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_public/src/unstable/convert/stable/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_public/src/unstable/internal_cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I, T>(self, iter: I) -> T::Output
where
I: Iterator<Item = T>,
T: ty::CollectAndApply<ty::Pattern<'tcx>, &'tcx List<ty::Pattern<'tcx>>>,
{
TyCtxt::mk_patterns_from_iter(self, iter)
}
}
5 changes: 5 additions & 0 deletions compiler/rustc_public/src/unstable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ pub trait InternalCx<'tcx>: Copy + Clone {
fn mk_place_elems(self, v: &[mir::PlaceElem<'tcx>]) -> &'tcx List<mir::PlaceElem<'tcx>>;

fn adt_def(self, def_id: rustc_hir::def_id::DefId) -> ty::AdtDef<'tcx>;

fn mk_patterns_from_iter<I, T>(self, iter: I) -> T::Output
where
I: Iterator<Item = T>,
T: ty::CollectAndApply<ty::Pattern<'tcx>, &'tcx List<ty::Pattern<'tcx>>>;
}

/// Trait used to convert between an internal MIR type to a rustc_public's IR type.
Expand Down
60 changes: 60 additions & 0 deletions tests/ui-fulldeps/rustc_public/check_fmt_macro.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
Loading