Skip to content

Commit 4569aec

Browse files
authored
Unrolled build for #155513
Rollup merge of #155513 - makai410:rpub-pat, r=celinval rustc_public: implement `Pattern` type Fixes: rust-lang/project-stable-mir#120
2 parents 52b6e2c + 510f709 commit 4569aec

6 files changed

Lines changed: 88 additions & 8 deletions

File tree

compiler/rustc_public/src/ty.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ impl Ty {
109109
/// Represents a pattern in the type system
110110
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
111111
pub enum Pattern {
112-
Range { start: Option<TyConst>, end: Option<TyConst>, include_end: bool },
112+
Range { start: TyConst, end: TyConst, include_end: bool },
113+
NotNull,
114+
Or(Vec<Pattern>),
113115
}
114116

115117
/// Represents a constant in the type system

compiler/rustc_public/src/unstable/convert/internal.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,13 @@ impl RustcInternal for Pattern {
125125
) -> Self::T<'tcx> {
126126
tcx.mk_pat(match self {
127127
Pattern::Range { start, end, include_end: _ } => rustc_ty::PatternKind::Range {
128-
start: start.as_ref().unwrap().internal(tables, tcx),
129-
end: end.as_ref().unwrap().internal(tables, tcx),
128+
start: start.internal(tables, tcx),
129+
end: end.internal(tables, tcx),
130130
},
131+
Pattern::NotNull => rustc_ty::PatternKind::NotNull,
132+
Pattern::Or(patterns) => rustc_ty::PatternKind::Or(
133+
tcx.mk_patterns_from_iter(patterns.iter().map(|p| p.internal(tables, tcx))),
134+
),
131135
})
132136
}
133137
}

compiler/rustc_public/src/unstable/convert/stable/ty.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,14 @@ impl<'tcx> Stable<'tcx> for ty::Pattern<'tcx> {
506506
) -> Self::T {
507507
match **self {
508508
ty::PatternKind::Range { start, end } => crate::ty::Pattern::Range {
509-
// FIXME(SMIR): update data structures to not have an Option here anymore
510-
start: Some(start.stable(tables, cx)),
511-
end: Some(end.stable(tables, cx)),
509+
start: start.stable(tables, cx),
510+
end: end.stable(tables, cx),
512511
include_end: true,
513512
},
514-
ty::PatternKind::NotNull => todo!(),
515-
ty::PatternKind::Or(_) => todo!(),
513+
ty::PatternKind::NotNull => crate::ty::Pattern::NotNull,
514+
ty::PatternKind::Or(pats) => {
515+
crate::ty::Pattern::Or(pats.iter().map(|pat| pat.stable(tables, cx)).collect())
516+
}
516517
}
517518
}
518519
}

compiler/rustc_public/src/unstable/internal_cx/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,12 @@ impl<'tcx> InternalCx<'tcx> for TyCtxt<'tcx> {
9393
fn adt_def(self, def_id: rustc_hir::def_id::DefId) -> ty::AdtDef<'tcx> {
9494
self.adt_def(def_id)
9595
}
96+
97+
fn mk_patterns_from_iter<I, T>(self, iter: I) -> T::Output
98+
where
99+
I: Iterator<Item = T>,
100+
T: ty::CollectAndApply<ty::Pattern<'tcx>, &'tcx List<ty::Pattern<'tcx>>>,
101+
{
102+
TyCtxt::mk_patterns_from_iter(self, iter)
103+
}
96104
}

compiler/rustc_public/src/unstable/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ pub trait InternalCx<'tcx>: Copy + Clone {
5555
fn mk_place_elems(self, v: &[mir::PlaceElem<'tcx>]) -> &'tcx List<mir::PlaceElem<'tcx>>;
5656

5757
fn adt_def(self, def_id: rustc_hir::def_id::DefId) -> ty::AdtDef<'tcx>;
58+
59+
fn mk_patterns_from_iter<I, T>(self, iter: I) -> T::Output
60+
where
61+
I: Iterator<Item = T>,
62+
T: ty::CollectAndApply<ty::Pattern<'tcx>, &'tcx List<ty::Pattern<'tcx>>>;
5863
}
5964

6065
/// Trait used to convert between an internal MIR type to a rustc_public's IR type.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//@ run-pass
2+
//! Test that users are able to use rustc_public to consume formatting macros.
3+
//@ ignore-stage1
4+
//@ ignore-cross-compile
5+
//@ ignore-remote
6+
//@ edition: 2021
7+
8+
#![feature(rustc_private)]
9+
10+
extern crate rustc_middle;
11+
12+
extern crate rustc_driver;
13+
extern crate rustc_interface;
14+
extern crate rustc_public;
15+
use std::io::Write;
16+
use std::ops::ControlFlow;
17+
use rustc_public::run;
18+
19+
const CRATE_NAME: &str = "fmt_macro";
20+
21+
/// Test if we can pass the compilation.
22+
fn test_fmt_macro() -> ControlFlow<()> {
23+
let entry_fn = rustc_public::entry_fn().unwrap().body().unwrap();
24+
for bb in &entry_fn.blocks {
25+
for stmt in &bb.statements {
26+
let _ = stmt;
27+
}
28+
}
29+
ControlFlow::Continue(())
30+
}
31+
32+
/// This test will generate and analyze a dummy crate using the stable mir.
33+
/// For that, it will first write the dummy crate into a file.
34+
/// Then it will create a `RustcPublic` using custom arguments and then
35+
/// it will run the compiler.
36+
fn main() {
37+
let path = "fmt_macro_input.rs";
38+
generate_input(&path).unwrap();
39+
let args = &[
40+
"rustc".to_string(),
41+
"-Cpanic=abort".to_string(),
42+
"--crate-name".to_string(),
43+
CRATE_NAME.to_string(),
44+
path.to_string(),
45+
];
46+
run!(args, test_fmt_macro).unwrap();
47+
}
48+
49+
fn generate_input(path: &str) -> std::io::Result<()> {
50+
let mut file = std::fs::File::create(path)?;
51+
write!(
52+
file,
53+
r#"
54+
fn main() {{
55+
println!("hello world!");
56+
}}
57+
"#
58+
)?;
59+
Ok(())
60+
}

0 commit comments

Comments
 (0)