about summary refs log tree commit diff
path: root/src/doc/rustc-dev-guide/examples/rustc-driver-example.rs
diff options
context:
space:
mode:
authorTshepang Mbambo <tshepang@gmail.com>2022-06-07 01:42:07 +0200
committerGitHub <noreply@github.com>2022-06-07 08:42:07 +0900
commit370fe5964b709cd92fa5eaf2abf5cb25c050567b (patch)
treed8264978f561b48bfb1b0184b3d1d25a8d308119 /src/doc/rustc-dev-guide/examples/rustc-driver-example.rs
parent8cc4c20c1dc316fec00bfbef0f23939130e44426 (diff)
downloadrust-370fe5964b709cd92fa5eaf2abf5cb25c050567b.tar.gz
rust-370fe5964b709cd92fa5eaf2abf5cb25c050567b.zip
improve rustc_interface examples a little (#1362)
Diffstat (limited to 'src/doc/rustc-dev-guide/examples/rustc-driver-example.rs')
-rw-r--r--src/doc/rustc-dev-guide/examples/rustc-driver-example.rs25
1 files changed, 15 insertions, 10 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:?}")
                         }
                         _ => (),
                     }