diff --git a/examples/rustc-driver-example.rs b/examples/rustc-driver-example.rs index 8a5d880fa..4203fe96a 100644 --- a/examples/rustc-driver-example.rs +++ b/examples/rustc-driver-example.rs @@ -13,13 +13,12 @@ extern crate rustc_interface; extern crate rustc_session; extern crate rustc_span; +use std::{path, process, str}; + use rustc_errors::registry; use rustc_hash::{FxHashMap, FxHashSet}; use rustc_session::config::{self, CheckCfg}; use rustc_span::source_map; -use std::path; -use std::process; -use std::str; fn main() { let out = process::Command::new("rustc") @@ -38,9 +37,14 @@ fn main() { crate_cfg: FxHashSet::default(), // FxHashSet<(String, Option)> crate_check_cfg: CheckCfg::default(), // CheckCfg input: config::Input::Str { - name: source_map::FileName::Custom("main.rs".to_string()), - input: "static HELLO: &str = \"Hello, world!\"; fn main() { println!(\"{}\", HELLO); }" - .to_string(), + name: source_map::FileName::Custom("main.rs".into()), + input: r#" +static HELLO: &str = "Hello, world!"; +fn main() { + println!("{HELLO}"); +} +"# + .into(), }, input_path: None, // Option output_dir: None, // Option @@ -69,16 +73,17 @@ fn main() { compiler.enter(|queries| { // Parse the program and print the syntax tree. let parse = queries.parse().unwrap().take(); - println!("{:#?}", parse); + println!("{parse:?}"); // Analyze the program and inspect the types of definitions. queries.global_ctxt().unwrap().take().enter(|tcx| { for id in tcx.hir().items() { - let item = tcx.hir().item(id); + let hir = tcx.hir(); + let item = hir.item(id); match item.kind { rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn(_, _, _) => { let name = item.ident; - let ty = tcx.type_of(tcx.hir().local_def_id(item.hir_id())); - println!("{:?}:\t{:?}", name, ty) + let ty = tcx.type_of(hir.local_def_id(item.hir_id())); + println!("{name:?}:\t{ty:?}") } _ => (), } diff --git a/examples/rustc-driver-getting-diagnostics.rs b/examples/rustc-driver-getting-diagnostics.rs index 3bb0d5450..1d3a4034e 100644 --- a/examples/rustc-driver-getting-diagnostics.rs +++ b/examples/rustc-driver-getting-diagnostics.rs @@ -57,8 +57,13 @@ fn main() { }, // This program contains a type error. input: config::Input::Str { - name: source_map::FileName::Custom("main.rs".to_string()), - input: "fn main() { let x: &str = 1; }".to_string(), + name: source_map::FileName::Custom("main.rs".into()), + input: " +fn main() { + let x: &str = 1; +} +" + .into(), }, // Redirect the diagnostic output of the compiler to a buffer. diagnostic_output: rustc_session::DiagnosticOutput::Raw(Box::from(DiagnosticSink( @@ -87,5 +92,5 @@ fn main() { }); // Read buffered diagnostics. let diagnostics = String::from_utf8(buffer.lock().unwrap().clone()).unwrap(); - println!("{}", diagnostics); + println!("{diagnostics}"); } diff --git a/examples/rustc-driver-interacting-with-the-ast.rs b/examples/rustc-driver-interacting-with-the-ast.rs index a0110f82d..231994a97 100644 --- a/examples/rustc-driver-interacting-with-the-ast.rs +++ b/examples/rustc-driver-interacting-with-the-ast.rs @@ -14,13 +14,12 @@ extern crate rustc_interface; extern crate rustc_session; extern crate rustc_span; +use std::{path, process, str}; + use rustc_ast_pretty::pprust::item_to_string; use rustc_errors::registry; use rustc_session::config::{self, CheckCfg}; use rustc_span::source_map; -use std::path; -use std::process; -use std::str; fn main() { let out = process::Command::new("rustc") @@ -36,8 +35,13 @@ fn main() { }, input: config::Input::Str { name: source_map::FileName::Custom("main.rs".to_string()), - input: "fn main() { let message = \"Hello, world!\"; println!(\"{}\", message); }" - .to_string(), + input: r#" +fn main() { + let message = "Hello, World!"; + println!("{message}"); +} +"# + .to_string(), }, diagnostic_output: rustc_session::DiagnosticOutput::Default, crate_cfg: rustc_hash::FxHashSet::default(), @@ -77,7 +81,7 @@ fn main() { let hir_id = expr.hir_id; // hir_id identifies the string "Hello, world!" let def_id = tcx.hir().local_def_id(item.hir_id()); // def_id identifies the main function let ty = tcx.typeck(def_id).node_type(hir_id); - println!("{:?}: {:?}", expr, ty); + println!("{expr:#?}: {ty:?}"); } } } diff --git a/src/rustc-driver-getting-diagnostics.md b/src/rustc-driver-getting-diagnostics.md index b3f18a67e..a7251c2f0 100644 --- a/src/rustc-driver-getting-diagnostics.md +++ b/src/rustc-driver-getting-diagnostics.md @@ -7,7 +7,7 @@ To get diagnostics from the compiler, configure `rustc_interface::Config` to output diagnostic to a buffer, and run `TyCtxt.analysis`. The following should be compiled -with `nightly-2021-04-30` (See [here][example] +with `nightly-2022-06-05` (See [here][example] for the complete example): [example]: https://github.com/rust-lang/rustc-dev-guide/blob/master/examples/rustc-driver-getting-diagnostics.rs diff --git a/src/rustc-driver-interacting-with-the-ast.md b/src/rustc-driver-interacting-with-the-ast.md index 1ec9c1c32..86732de18 100644 --- a/src/rustc-driver-interacting-with-the-ast.md +++ b/src/rustc-driver-interacting-with-the-ast.md @@ -5,7 +5,7 @@ ## Getting the type of an expression To get the type of an expression, use the `global_ctxt` to get a `TyCtxt`. -The following should be compiled with `nightly-2022-04-30` +The following should be compiled with `nightly-2022-06-05` (see [here][example] for the complete example): [example]: https://github.com/rust-lang/rustc-dev-guide/blob/master/examples/rustc-driver-interacting-with-the-ast.rs