Skip to content

Commit c029892

Browse files
committed
Fix examples
1 parent b4188ed commit c029892

File tree

4 files changed

+52
-43
lines changed

4 files changed

+52
-43
lines changed

examples/rustc-driver-example.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
#![feature(rustc_private)]
22

3+
extern crate rustc_ast;
34
extern crate rustc_ast_pretty;
5+
extern crate rustc_data_structures;
46
extern crate rustc_driver;
57
extern crate rustc_error_codes;
68
extern crate rustc_errors;
79
extern crate rustc_hash;
810
extern crate rustc_hir;
911
extern crate rustc_interface;
12+
extern crate rustc_middle;
1013
extern crate rustc_session;
1114
extern crate rustc_span;
1215

1316
use std::io;
1417
use std::path::Path;
15-
use std::str;
16-
use std::sync::Arc;
1718

1819
use rustc_ast_pretty::pprust::item_to_string;
1920
use rustc_data_structures::sync::Lrc;
20-
use rustc_driver::Compilation;
21-
use rustc_errors::registry;
22-
use rustc_session::config;
21+
use rustc_driver::{Compilation, RunCompiler};
22+
use rustc_interface::interface::Compiler;
23+
use rustc_middle::ty::TyCtxt;
2324

2425
struct MyFileLoader;
2526

2627
impl rustc_span::source_map::FileLoader for MyFileLoader {
2728
fn file_exists(&self, path: &Path) -> bool {
28-
path == "main.rs"
29+
path == Path::new("main.rs")
2930
}
3031

3132
fn read_file(&self, path: &Path) -> io::Result<String> {
32-
if path == "main.rs" {
33+
if path == Path::new("main.rs") {
3334
Ok(r#"
3435
fn main() {
3536
let message = "Hello, World!";
@@ -42,7 +43,7 @@ fn main() {
4243
}
4344
}
4445

45-
fn read_binary_file(&self, path: &Path) -> io::Result<Lrc<[u8]>> {
46+
fn read_binary_file(&self, _path: &Path) -> io::Result<Lrc<[u8]>> {
4647
Err(io::Error::other("oops"))
4748
}
4849
}
@@ -51,17 +52,18 @@ struct MyCallbacks;
5152

5253
impl rustc_driver::Callbacks for MyCallbacks {
5354
fn after_crate_root_parsing(
54-
_compiler: &rustc_interface::Compiler,
55+
&mut self,
56+
_compiler: &Compiler,
5557
krate: &rustc_ast::Crate,
5658
) -> Compilation {
57-
for item in krate.items {
59+
for item in &krate.items {
5860
println!("{}", item_to_string(&item));
5961
}
6062

6163
Compilation::Continue
6264
}
6365

64-
fn after_analysis(_compiler: &rustc_interface::Compiler, tcx: TyCtxt<'_>) -> Compilation {
66+
fn after_analysis(&mut self, _compiler: &Compiler, tcx: TyCtxt<'_>) -> Compilation {
6567
// Analyze the program and inspect the types of definitions.
6668
for id in tcx.hir().items() {
6769
let hir = tcx.hir();
@@ -81,7 +83,10 @@ impl rustc_driver::Callbacks for MyCallbacks {
8183
}
8284

8385
fn main() {
84-
RunCompiler::new(&["main.rs".to_string()], MyCallbacks)
85-
.set_file_loader(Some(Box::new(MyFileLoader)))
86-
.run();
86+
match RunCompiler::new(&["main.rs".to_string()], &mut MyCallbacks) {
87+
mut compiler => {
88+
compiler.set_file_loader(Some(Box::new(MyFileLoader)));
89+
compiler.run();
90+
}
91+
}
8792
}

examples/rustc-driver-interacting-with-the-ast.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
#![feature(rustc_private)]
22

3+
extern crate rustc_ast;
34
extern crate rustc_ast_pretty;
5+
extern crate rustc_data_structures;
46
extern crate rustc_driver;
57
extern crate rustc_error_codes;
68
extern crate rustc_errors;
79
extern crate rustc_hash;
810
extern crate rustc_hir;
911
extern crate rustc_interface;
12+
extern crate rustc_middle;
1013
extern crate rustc_session;
1114
extern crate rustc_span;
1215

1316
use std::io;
1417
use std::path::Path;
15-
use std::str;
16-
use std::sync::Arc;
1718

1819
use rustc_ast_pretty::pprust::item_to_string;
1920
use rustc_data_structures::sync::Lrc;
20-
use rustc_driver::Compilation;
21-
use rustc_errors::registry;
22-
use rustc_session::config;
21+
use rustc_driver::{Compilation, RunCompiler};
22+
use rustc_interface::interface::Compiler;
23+
use rustc_middle::ty::TyCtxt;
2324

2425
struct MyFileLoader;
2526

2627
impl rustc_span::source_map::FileLoader for MyFileLoader {
2728
fn file_exists(&self, path: &Path) -> bool {
28-
path == "main.rs"
29+
path == Path::new("main.rs")
2930
}
3031

3132
fn read_file(&self, path: &Path) -> io::Result<String> {
32-
if path == "main.rs" {
33+
if path == Path::new("main.rs") {
3334
Ok(r#"
3435
fn main() {
3536
let message = "Hello, World!";
@@ -42,7 +43,7 @@ fn main() {
4243
}
4344
}
4445

45-
fn read_binary_file(&self, path: &Path) -> io::Result<Lrc<[u8]>> {
46+
fn read_binary_file(&self, _path: &Path) -> io::Result<Lrc<[u8]>> {
4647
Err(io::Error::other("oops"))
4748
}
4849
}
@@ -51,17 +52,18 @@ struct MyCallbacks;
5152

5253
impl rustc_driver::Callbacks for MyCallbacks {
5354
fn after_crate_root_parsing(
54-
_compiler: &rustc_interface::Compiler,
55+
&mut self,
56+
_compiler: &Compiler,
5557
krate: &rustc_ast::Crate,
5658
) -> Compilation {
57-
for item in krate.items {
59+
for item in &krate.items {
5860
println!("{}", item_to_string(&item));
5961
}
6062

6163
Compilation::Continue
6264
}
6365

64-
fn after_analysis(_compiler: &rustc_interface::Compiler, tcx: TyCtxt<'_>) -> Compilation {
66+
fn after_analysis(&mut self, _compiler: &Compiler, tcx: TyCtxt<'_>) -> Compilation {
6567
// Every compilation contains a single crate.
6668
let hir_krate = tcx.hir();
6769
// Iterate over the top-level items in the crate, looking for the main function.
@@ -88,7 +90,10 @@ impl rustc_driver::Callbacks for MyCallbacks {
8890
}
8991

9092
fn main() {
91-
RunCompiler::new(&["main.rs".to_string()], MyCallbacks)
92-
.set_file_loader(Some(Box::new(MyFileLoader)))
93-
.run();
93+
match RunCompiler::new(&["main.rs".to_string()], &mut MyCallbacks) {
94+
mut compiler => {
95+
compiler.set_file_loader(Some(Box::new(MyFileLoader)));
96+
compiler.run();
97+
}
98+
}
9499
}

examples/rustc-interface-example.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ extern crate rustc_interface;
99
extern crate rustc_session;
1010
extern crate rustc_span;
1111

12-
use std::{path, str, sync::Arc};
12+
use std::sync::Arc;
1313

1414
use rustc_errors::registry;
1515
use rustc_hash::FxHashMap;
@@ -35,7 +35,7 @@ fn main() {
3535
output_dir: None, // Option<PathBuf>
3636
output_file: None, // Option<PathBuf>
3737
file_loader: None, // Option<Box<dyn FileLoader + Send + Sync>>
38-
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
38+
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES.to_owned(),
3939
lint_caps: FxHashMap::default(), // FxHashMap<lint::LintId, lint::Level>
4040
// This is a callback from the driver that is called when [`ParseSess`] is created.
4141
psess_created: None, //Option<Box<dyn FnOnce(&mut ParseSess) + Send>>
@@ -60,8 +60,8 @@ fn main() {
6060
};
6161
rustc_interface::run_compiler(config, |compiler| {
6262
// Parse the program and print the syntax tree.
63-
let parse = rustc_interface::passes::parse(&compiler.sess);
64-
println!("{parse:?}");
63+
let krate = rustc_interface::passes::parse(&compiler.sess);
64+
println!("{krate:?}");
6565
// Analyze the program and inspect the types of definitions.
6666
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
6767
for id in tcx.hir().items() {

examples/rustc-interface-getting-diagnostics.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(rustc_private)]
22

3+
extern crate rustc_data_structures;
34
extern crate rustc_driver;
45
extern crate rustc_error_codes;
56
extern crate rustc_errors;
@@ -9,24 +10,22 @@ extern crate rustc_interface;
910
extern crate rustc_session;
1011
extern crate rustc_span;
1112

12-
use rustc_errors::{
13-
emitter::Emitter, registry, translation::Translate, DiagCtxt, DiagInner, FluentBundle,
14-
};
13+
use rustc_errors::emitter::Emitter;
14+
use rustc_errors::registry::{self, Registry};
15+
use rustc_errors::translation::Translate;
16+
use rustc_errors::{DiagCtxt, DiagInner, FluentBundle};
1517
use rustc_session::config;
1618
use rustc_span::source_map::SourceMap;
1719

18-
use std::{
19-
path, str,
20-
sync::{Arc, Mutex},
21-
};
20+
use std::sync::{Arc, Mutex};
2221

2322
struct DebugEmitter {
2423
source_map: Arc<SourceMap>,
2524
diagnostics: Arc<Mutex<Vec<DiagInner>>>,
2625
}
2726

2827
impl Translate for DebugEmitter {
29-
fn fluent_bundle(&self) -> Option<&Arc<FluentBundle>> {
28+
fn fluent_bundle(&self) -> Option<&FluentBundle> {
3029
None
3130
}
3231

@@ -36,11 +35,11 @@ impl Translate for DebugEmitter {
3635
}
3736

3837
impl Emitter for DebugEmitter {
39-
fn emit_diagnostic(&mut self, diag: DiagInner) {
38+
fn emit_diagnostic(&mut self, diag: DiagInner, _: &Registry) {
4039
self.diagnostics.lock().unwrap().push(diag);
4140
}
4241

43-
fn source_map(&self) -> Option<&Arc<SourceMap>> {
42+
fn source_map(&self) -> Option<&SourceMap> {
4443
Some(&self.source_map)
4544
}
4645
}
@@ -65,7 +64,7 @@ fn main() {
6564
output_dir: None,
6665
output_file: None,
6766
file_loader: None,
68-
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
67+
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES.to_owned(),
6968
lint_caps: rustc_hash::FxHashMap::default(),
7069
psess_created: Some(Box::new(|parse_sess| {
7170
parse_sess.set_dcx(DiagCtxt::new(Box::new(DebugEmitter {

0 commit comments

Comments
 (0)