Skip to content

Commit 4383648

Browse files
authored
improve rustc_interface examples a little (rust-lang#1362)
1 parent 72a3895 commit 4383648

5 files changed

+35
-21
lines changed

examples/rustc-driver-example.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ extern crate rustc_interface;
1313
extern crate rustc_session;
1414
extern crate rustc_span;
1515

16+
use std::{path, process, str};
17+
1618
use rustc_errors::registry;
1719
use rustc_hash::{FxHashMap, FxHashSet};
1820
use rustc_session::config::{self, CheckCfg};
1921
use rustc_span::source_map;
20-
use std::path;
21-
use std::process;
22-
use std::str;
2322

2423
fn main() {
2524
let out = process::Command::new("rustc")
@@ -38,9 +37,14 @@ fn main() {
3837
crate_cfg: FxHashSet::default(), // FxHashSet<(String, Option<String>)>
3938
crate_check_cfg: CheckCfg::default(), // CheckCfg
4039
input: config::Input::Str {
41-
name: source_map::FileName::Custom("main.rs".to_string()),
42-
input: "static HELLO: &str = \"Hello, world!\"; fn main() { println!(\"{}\", HELLO); }"
43-
.to_string(),
40+
name: source_map::FileName::Custom("main.rs".into()),
41+
input: r#"
42+
static HELLO: &str = "Hello, world!";
43+
fn main() {
44+
println!("{HELLO}");
45+
}
46+
"#
47+
.into(),
4448
},
4549
input_path: None, // Option<PathBuf>
4650
output_dir: None, // Option<PathBuf>
@@ -69,16 +73,17 @@ fn main() {
6973
compiler.enter(|queries| {
7074
// Parse the program and print the syntax tree.
7175
let parse = queries.parse().unwrap().take();
72-
println!("{:#?}", parse);
76+
println!("{parse:?}");
7377
// Analyze the program and inspect the types of definitions.
7478
queries.global_ctxt().unwrap().take().enter(|tcx| {
7579
for id in tcx.hir().items() {
76-
let item = tcx.hir().item(id);
80+
let hir = tcx.hir();
81+
let item = hir.item(id);
7782
match item.kind {
7883
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn(_, _, _) => {
7984
let name = item.ident;
80-
let ty = tcx.type_of(tcx.hir().local_def_id(item.hir_id()));
81-
println!("{:?}:\t{:?}", name, ty)
85+
let ty = tcx.type_of(hir.local_def_id(item.hir_id()));
86+
println!("{name:?}:\t{ty:?}")
8287
}
8388
_ => (),
8489
}

examples/rustc-driver-getting-diagnostics.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,13 @@ fn main() {
5757
},
5858
// This program contains a type error.
5959
input: config::Input::Str {
60-
name: source_map::FileName::Custom("main.rs".to_string()),
61-
input: "fn main() { let x: &str = 1; }".to_string(),
60+
name: source_map::FileName::Custom("main.rs".into()),
61+
input: "
62+
fn main() {
63+
let x: &str = 1;
64+
}
65+
"
66+
.into(),
6267
},
6368
// Redirect the diagnostic output of the compiler to a buffer.
6469
diagnostic_output: rustc_session::DiagnosticOutput::Raw(Box::from(DiagnosticSink(
@@ -87,5 +92,5 @@ fn main() {
8792
});
8893
// Read buffered diagnostics.
8994
let diagnostics = String::from_utf8(buffer.lock().unwrap().clone()).unwrap();
90-
println!("{}", diagnostics);
95+
println!("{diagnostics}");
9196
}

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ extern crate rustc_interface;
1414
extern crate rustc_session;
1515
extern crate rustc_span;
1616

17+
use std::{path, process, str};
18+
1719
use rustc_ast_pretty::pprust::item_to_string;
1820
use rustc_errors::registry;
1921
use rustc_session::config::{self, CheckCfg};
2022
use rustc_span::source_map;
21-
use std::path;
22-
use std::process;
23-
use std::str;
2423

2524
fn main() {
2625
let out = process::Command::new("rustc")
@@ -36,8 +35,13 @@ fn main() {
3635
},
3736
input: config::Input::Str {
3837
name: source_map::FileName::Custom("main.rs".to_string()),
39-
input: "fn main() { let message = \"Hello, world!\"; println!(\"{}\", message); }"
40-
.to_string(),
38+
input: r#"
39+
fn main() {
40+
let message = "Hello, World!";
41+
println!("{message}");
42+
}
43+
"#
44+
.to_string(),
4145
},
4246
diagnostic_output: rustc_session::DiagnosticOutput::Default,
4347
crate_cfg: rustc_hash::FxHashSet::default(),
@@ -77,7 +81,7 @@ fn main() {
7781
let hir_id = expr.hir_id; // hir_id identifies the string "Hello, world!"
7882
let def_id = tcx.hir().local_def_id(item.hir_id()); // def_id identifies the main function
7983
let ty = tcx.typeck(def_id).node_type(hir_id);
80-
println!("{:?}: {:?}", expr, ty);
84+
println!("{expr:#?}: {ty:?}");
8185
}
8286
}
8387
}

src/rustc-driver-getting-diagnostics.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
To get diagnostics from the compiler,
88
configure `rustc_interface::Config` to output diagnostic to a buffer,
99
and run `TyCtxt.analysis`. The following should be compiled
10-
with <!-- date: 2022-05 --> `nightly-2021-04-30` (See [here][example]
10+
with <!-- date: 2022-06 --> `nightly-2022-06-05` (See [here][example]
1111
for the complete example):
1212

1313
[example]: https://github.com/rust-lang/rustc-dev-guide/blob/master/examples/rustc-driver-getting-diagnostics.rs

src/rustc-driver-interacting-with-the-ast.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## Getting the type of an expression
66

77
To get the type of an expression, use the `global_ctxt` to get a `TyCtxt`.
8-
The following should be compiled with <!-- date: 2022-05 --> `nightly-2022-04-30`
8+
The following should be compiled with <!-- date: 2022-06 --> `nightly-2022-06-05`
99
(see [here][example] for the complete example):
1010

1111
[example]: https://github.com/rust-lang/rustc-dev-guide/blob/master/examples/rustc-driver-interacting-with-the-ast.rs

0 commit comments

Comments
 (0)