diff options
| author | Tshepang Mbambo <tshepang@gmail.com> | 2022-06-07 01:42:07 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-07 08:42:07 +0900 |
| commit | 370fe5964b709cd92fa5eaf2abf5cb25c050567b (patch) | |
| tree | d8264978f561b48bfb1b0184b3d1d25a8d308119 /src/doc/rustc-dev-guide/examples | |
| parent | 8cc4c20c1dc316fec00bfbef0f23939130e44426 (diff) | |
| download | rust-370fe5964b709cd92fa5eaf2abf5cb25c050567b.tar.gz rust-370fe5964b709cd92fa5eaf2abf5cb25c050567b.zip | |
improve rustc_interface examples a little (#1362)
Diffstat (limited to 'src/doc/rustc-dev-guide/examples')
3 files changed, 33 insertions, 19 deletions
diff --git a/src/doc/rustc-dev-guide/examples/rustc-driver-example.rs b/src/doc/rustc-dev-guide/examples/rustc-driver-example.rs index 8a5d880fab2..4203fe96a0b 100644 --- a/src/doc/rustc-dev-guide/examples/rustc-driver-example.rs +++ b/src/doc/rustc-dev-guide/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<String>)> 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<PathBuf> output_dir: None, // Option<PathBuf> @@ -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/src/doc/rustc-dev-guide/examples/rustc-driver-getting-diagnostics.rs b/src/doc/rustc-dev-guide/examples/rustc-driver-getting-diagnostics.rs index 3bb0d545041..1d3a4034e4d 100644 --- a/src/doc/rustc-dev-guide/examples/rustc-driver-getting-diagnostics.rs +++ b/src/doc/rustc-dev-guide/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/src/doc/rustc-dev-guide/examples/rustc-driver-interacting-with-the-ast.rs b/src/doc/rustc-dev-guide/examples/rustc-driver-interacting-with-the-ast.rs index a0110f82d92..231994a97d1 100644 --- a/src/doc/rustc-dev-guide/examples/rustc-driver-interacting-with-the-ast.rs +++ b/src/doc/rustc-dev-guide/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:?}"); } } } |
