Skip to content

Commit

Permalink
Update rustc-driver-*.rs examples (#1095)
Browse files Browse the repository at this point in the history
  • Loading branch information
Undxxx authored Mar 28, 2021
1 parent 9a676ee commit fe0cf82
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
8 changes: 6 additions & 2 deletions examples/rustc-driver-example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,8 +48,9 @@ fn main() {
diagnostic_output: rustc_session::DiagnosticOutput::Default,
// Set to capture stderr output during compiler execution
stderr: None, // Option<Arc<Mutex<Vec<u8>>>>
crate_name: None, // Option<String>
lint_caps: FxHashMap::default(), // FxHashMap<lint::LintId, lint::Level>
// This is a callback from the driver that is called when [`ParseSess`] is created.
parse_sess_created: None, //Option<Box<dyn FnOnce(&mut ParseSess) + Send>>
// 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.
//
Expand All @@ -61,6 +64,7 @@ fn main() {
override_queries: None, // Option<fn(&Session, &mut ty::query::Providers<'_>, &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| {
Expand All @@ -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)
}
_ => (),
Expand Down
5 changes: 4 additions & 1 deletion examples/rustc-driver-getting-diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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| {
Expand Down
11 changes: 5 additions & 6 deletions examples/rustc-driver-interacting-with-the-ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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));
}

Expand All @@ -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
}
Expand Down

0 comments on commit fe0cf82

Please sign in to comment.