]> git.proxmox.com Git - rustc.git/blobdiff - src/doc/rustc-dev-guide/examples/rustc-driver-example.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / doc / rustc-dev-guide / examples / rustc-driver-example.rs
index 7177a286892ff85860730fa9616dcc1c85f85f1f..4203fe96a0bd2dc0c2485ce25394991912d7a218 100644 (file)
@@ -1,9 +1,9 @@
 #![feature(rustc_private)]
 
 // NOTE: For the example to compile, you will need to first run the following:
-//   rustup component add rustc-dev
+//   rustup component add rustc-dev llvm-tools-preview
 
-// version: 1.61.0-nightly (68369a041 2022-02-22)
+// version: 1.62.0-nightly (7c4b47696 2022-04-30)
 
 extern crate rustc_error_codes;
 extern crate rustc_errors;
@@ -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,15 +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 item in tcx.hir().items() {
+                for id in tcx.hir().items() {
+                    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:?}")
                         }
                         _ => (),
                     }