Skip to content

Commit 61c367c

Browse files
authored
Rollup merge of #115187 - ouz-a:smir_wrap, r=oli-obk
Add new interface to smir Removes the boiler plate from `crate-info.rs`, and creates new interface for the smir. Addressing rust-lang/project-stable-mir#23 r? `@spastorino`
2 parents d5b12a2 + c2fe0bf commit 61c367c

File tree

5 files changed

+54
-33
lines changed

5 files changed

+54
-33
lines changed

Cargo.lock

+3
Original file line numberDiff line numberDiff line change
@@ -4251,8 +4251,11 @@ dependencies = [
42514251
name = "rustc_smir"
42524252
version = "0.0.0"
42534253
dependencies = [
4254+
"rustc_driver",
42544255
"rustc_hir",
4256+
"rustc_interface",
42554257
"rustc_middle",
4258+
"rustc_session",
42564259
"rustc_span",
42574260
"rustc_target",
42584261
"scoped-tls",

compiler/rustc_smir/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ rustc_hir = { path = "../rustc_hir", optional = true }
99
rustc_middle = { path = "../rustc_middle", optional = true }
1010
rustc_span = { path = "../rustc_span", optional = true }
1111
rustc_target = { path = "../rustc_target", optional = true }
12+
rustc_driver = { path = "../rustc_driver", optional = true }
13+
rustc_interface = { path = "../rustc_interface", optional = true}
14+
rustc_session = {path = "../rustc_session", optional = true}
1215
tracing = "0.1"
1316
scoped-tls = "1.0"
1417

@@ -18,4 +21,7 @@ default = [
1821
"rustc_middle",
1922
"rustc_span",
2023
"rustc_target",
24+
"rustc_driver",
25+
"rustc_interface",
26+
"rustc_session",
2127
]

compiler/rustc_smir/src/rustc_internal/mod.rs

+41
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
use std::fmt::Debug;
77
use std::string::ToString;
88

9+
use crate::rustc_internal;
910
use crate::{
1011
rustc_smir::Tables,
1112
stable_mir::{self, with},
1213
};
14+
use rustc_driver::{Callbacks, Compilation, RunCompiler};
15+
use rustc_interface::{interface, Queries};
1316
use rustc_middle::ty::TyCtxt;
17+
use rustc_session::EarlyErrorHandler;
1418
pub use rustc_span::def_id::{CrateNum, DefId};
1519

1620
fn with_tables<R>(mut f: impl FnMut(&mut Tables<'_>) -> R) -> R {
@@ -163,3 +167,40 @@ pub type Opaque = impl Debug + ToString + Clone;
163167
pub(crate) fn opaque<T: Debug>(value: &T) -> Opaque {
164168
format!("{value:?}")
165169
}
170+
171+
pub struct StableMir {
172+
args: Vec<String>,
173+
callback: fn(TyCtxt<'_>),
174+
}
175+
176+
impl StableMir {
177+
/// Creates a new `StableMir` instance, with given test_function and arguments.
178+
pub fn new(args: Vec<String>, callback: fn(TyCtxt<'_>)) -> Self {
179+
StableMir { args, callback }
180+
}
181+
182+
/// Runs the compiler against given target and tests it with `test_function`
183+
pub fn run(&mut self) {
184+
rustc_driver::catch_fatal_errors(|| {
185+
RunCompiler::new(&self.args.clone(), self).run().unwrap();
186+
})
187+
.unwrap();
188+
}
189+
}
190+
191+
impl Callbacks for StableMir {
192+
/// Called after analysis. Return value instructs the compiler whether to
193+
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
194+
fn after_analysis<'tcx>(
195+
&mut self,
196+
_handler: &EarlyErrorHandler,
197+
_compiler: &interface::Compiler,
198+
queries: &'tcx Queries<'tcx>,
199+
) -> Compilation {
200+
queries.global_ctxt().unwrap().enter(|tcx| {
201+
rustc_internal::run(tcx, || (self.callback)(tcx));
202+
});
203+
// No need to keep going.
204+
Compilation::Stop
205+
}
206+
}

compiler/rustc_smir/src/stable_mir/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
1414
use std::cell::Cell;
1515

16-
use crate::rustc_smir::Tables;
17-
1816
use self::ty::{
1917
GenericDef, Generics, ImplDef, ImplTrait, PredicateKind, Span, TraitDecl, TraitDef, Ty, TyKind,
2018
};
19+
use crate::rustc_smir::Tables;
2120

2221
pub mod mir;
2322
pub mod ty;

tests/ui-fulldeps/stable-mir/crate-info.rs

+3-31
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,12 @@
99
#![feature(rustc_private)]
1010
#![feature(assert_matches)]
1111

12-
extern crate rustc_driver;
1312
extern crate rustc_hir;
14-
extern crate rustc_interface;
1513
extern crate rustc_middle;
16-
extern crate rustc_session;
1714
extern crate rustc_smir;
1815

19-
use rustc_driver::{Callbacks, Compilation, RunCompiler};
2016
use rustc_hir::def::DefKind;
21-
use rustc_interface::{interface, Queries};
2217
use rustc_middle::ty::TyCtxt;
23-
use rustc_session::EarlyErrorHandler;
2418
use rustc_smir::{rustc_internal, stable_mir};
2519
use std::assert_matches::assert_matches;
2620
use std::io::Write;
@@ -130,8 +124,8 @@ fn get_item<'a>(
130124

131125
/// This test will generate and analyze a dummy crate using the stable mir.
132126
/// For that, it will first write the dummy crate into a file.
133-
/// It will invoke the compiler using a custom Callback implementation, which will
134-
/// invoke Stable MIR APIs after the compiler has finished its analysis.
127+
/// Then it will create a `StableMir` using custom arguments and then
128+
/// it will run the compiler.
135129
fn main() {
136130
let path = "input.rs";
137131
generate_input(&path).unwrap();
@@ -142,29 +136,7 @@ fn main() {
142136
CRATE_NAME.to_string(),
143137
path.to_string(),
144138
];
145-
rustc_driver::catch_fatal_errors(|| {
146-
RunCompiler::new(&args, &mut SMirCalls {}).run().unwrap();
147-
})
148-
.unwrap();
149-
}
150-
151-
struct SMirCalls {}
152-
153-
impl Callbacks for SMirCalls {
154-
/// Called after analysis. Return value instructs the compiler whether to
155-
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
156-
fn after_analysis<'tcx>(
157-
&mut self,
158-
_handler: &EarlyErrorHandler,
159-
_compiler: &interface::Compiler,
160-
queries: &'tcx Queries<'tcx>,
161-
) -> Compilation {
162-
queries.global_ctxt().unwrap().enter(|tcx| {
163-
rustc_smir::rustc_internal::run(tcx, || test_stable_mir(tcx));
164-
});
165-
// No need to keep going.
166-
Compilation::Stop
167-
}
139+
rustc_internal::StableMir::new(args, test_stable_mir).run();
168140
}
169141

170142
fn generate_input(path: &str) -> std::io::Result<()> {

0 commit comments

Comments
 (0)