From 64504ada9051f691ff450aee98682530a4ae59e4 Mon Sep 17 00:00:00 2001 From: Undxxx <32825301+Undxxx@users.noreply.github.com> Date: Sun, 28 Mar 2021 18:15:09 +0800 Subject: [PATCH] Update rustc-driver-*.rs examples (#1095) --- examples/rustc-driver-example.rs | 8 ++++++-- examples/rustc-driver-getting-diagnostics.rs | 5 ++++- examples/rustc-driver-interacting-with-the-ast.rs | 11 +++++------ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/examples/rustc-driver-example.rs b/examples/rustc-driver-example.rs index f01072b7..51af6f5a 100644 --- a/examples/rustc-driver-example.rs +++ b/examples/rustc-driver-example.rs @@ -3,6 +3,8 @@ // NOTE: For the example to compile, you will need to first run the following: // rustup component add rustc-dev +// version: 1.53.0-nightly (9b0edb7fd 2021-03-27) + extern crate rustc_error_codes; extern crate rustc_errors; extern crate rustc_hash; @@ -46,8 +48,9 @@ fn main() { diagnostic_output: rustc_session::DiagnosticOutput::Default, // Set to capture stderr output during compiler execution stderr: None, // Option>>> - crate_name: None, // Option lint_caps: FxHashMap::default(), // FxHashMap + // This is a callback from the driver that is called when [`ParseSess`] is created. + parse_sess_created: None, //Option> // This is a callback from the driver that is called when we're registering lints; // it is called during plugin registration when we have the LintStore in a non-shared state. // @@ -61,6 +64,7 @@ fn main() { override_queries: None, // Option, &mut ty::query::Providers<'_>)> // Registry of diagnostics codes. registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS), + make_codegen_backend: None, }; rustc_interface::run_compiler(config, |compiler| { compiler.enter(|queries| { @@ -73,7 +77,7 @@ fn main() { 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)); + let ty = tcx.type_of(tcx.hir().local_def_id(item.hir_id())); println!("{:?}:\t{:?}", name, ty) } _ => (), diff --git a/examples/rustc-driver-getting-diagnostics.rs b/examples/rustc-driver-getting-diagnostics.rs index e1caab32..ee9a20c0 100644 --- a/examples/rustc-driver-getting-diagnostics.rs +++ b/examples/rustc-driver-getting-diagnostics.rs @@ -3,6 +3,8 @@ // NOTE: For the example to compile, you will need to first run the following: // rustup component add rustc-dev +// version: 1.53.0-nightly (9b0edb7fd 2021-03-27) + extern crate rustc_error_codes; extern crate rustc_errors; extern crate rustc_hash; @@ -68,11 +70,12 @@ fn main() { output_file: None, file_loader: None, stderr: None, - crate_name: None, lint_caps: rustc_hash::FxHashMap::default(), + parse_sess_created: None, register_lints: None, override_queries: None, registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS), + make_codegen_backend: None, }; rustc_interface::run_compiler(config, |compiler| { compiler.enter(|queries| { diff --git a/examples/rustc-driver-interacting-with-the-ast.rs b/examples/rustc-driver-interacting-with-the-ast.rs index 8ee38206..2c753349 100644 --- a/examples/rustc-driver-interacting-with-the-ast.rs +++ b/examples/rustc-driver-interacting-with-the-ast.rs @@ -3,6 +3,8 @@ // NOTE: For the example to compile, you will need to first run the following: // rustup component add rustc-dev llvm-tools-preview +// version: 1.53.0-nightly (9b0edb7fd 2021-03-27) + extern crate rustc_ast_pretty; extern crate rustc_error_codes; extern crate rustc_errors; @@ -15,8 +17,6 @@ extern crate rustc_span; use rustc_ast_pretty::pprust::item_to_string; use rustc_errors::registry; use rustc_session::config; -use rustc_session::config::PpMode::PpmSource; -use rustc_session::config::PpSourceMode::PpmExpanded; use rustc_span::source_map; use std::path; use std::process; @@ -46,8 +46,8 @@ fn main() { output_file: None, file_loader: None, stderr: None, - crate_name: None, lint_caps: rustc_hash::FxHashMap::default(), + parse_sess_created: None, register_lints: None, override_queries: None, make_codegen_backend: None, @@ -57,8 +57,7 @@ fn main() { compiler.enter(|queries| { // TODO: add this to -Z unpretty let ast_krate = queries.parse().unwrap().take(); - let ast_krate_mod = ast_krate.module; - for item in ast_krate_mod.items { + for item in ast_krate.items { println!("{}", item_to_string(&item)); } @@ -75,7 +74,7 @@ fn main() { if let rustc_hir::StmtKind::Local(local) = block.stmts[0].kind { if let Some(expr) = local.init { 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 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); // prints expr(HirId { owner: DefIndex(3), local_id: 4 }: "Hello, world!"): &'static str }