Skip to content

Commit fe0cf82

Browse files
authored
Update rustc-driver-*.rs examples (#1095)
1 parent 9a676ee commit fe0cf82

3 files changed

+15
-9
lines changed

examples/rustc-driver-example.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// NOTE: For the example to compile, you will need to first run the following:
44
// rustup component add rustc-dev
55

6+
// version: 1.53.0-nightly (9b0edb7fd 2021-03-27)
7+
68
extern crate rustc_error_codes;
79
extern crate rustc_errors;
810
extern crate rustc_hash;
@@ -46,8 +48,9 @@ fn main() {
4648
diagnostic_output: rustc_session::DiagnosticOutput::Default,
4749
// Set to capture stderr output during compiler execution
4850
stderr: None, // Option<Arc<Mutex<Vec<u8>>>>
49-
crate_name: None, // Option<String>
5051
lint_caps: FxHashMap::default(), // FxHashMap<lint::LintId, lint::Level>
52+
// This is a callback from the driver that is called when [`ParseSess`] is created.
53+
parse_sess_created: None, //Option<Box<dyn FnOnce(&mut ParseSess) + Send>>
5154
// This is a callback from the driver that is called when we're registering lints;
5255
// it is called during plugin registration when we have the LintStore in a non-shared state.
5356
//
@@ -61,6 +64,7 @@ fn main() {
6164
override_queries: None, // Option<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)>
6265
// Registry of diagnostics codes.
6366
registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
67+
make_codegen_backend: None,
6468
};
6569
rustc_interface::run_compiler(config, |compiler| {
6670
compiler.enter(|queries| {
@@ -73,7 +77,7 @@ fn main() {
7377
match item.kind {
7478
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn(_, _, _) => {
7579
let name = item.ident;
76-
let ty = tcx.type_of(tcx.hir().local_def_id(item.hir_id));
80+
let ty = tcx.type_of(tcx.hir().local_def_id(item.hir_id()));
7781
println!("{:?}:\t{:?}", name, ty)
7882
}
7983
_ => (),

examples/rustc-driver-getting-diagnostics.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// NOTE: For the example to compile, you will need to first run the following:
44
// rustup component add rustc-dev
55

6+
// version: 1.53.0-nightly (9b0edb7fd 2021-03-27)
7+
68
extern crate rustc_error_codes;
79
extern crate rustc_errors;
810
extern crate rustc_hash;
@@ -68,11 +70,12 @@ fn main() {
6870
output_file: None,
6971
file_loader: None,
7072
stderr: None,
71-
crate_name: None,
7273
lint_caps: rustc_hash::FxHashMap::default(),
74+
parse_sess_created: None,
7375
register_lints: None,
7476
override_queries: None,
7577
registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
78+
make_codegen_backend: None,
7679
};
7780
rustc_interface::run_compiler(config, |compiler| {
7881
compiler.enter(|queries| {

examples/rustc-driver-interacting-with-the-ast.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// NOTE: For the example to compile, you will need to first run the following:
44
// rustup component add rustc-dev llvm-tools-preview
55

6+
// version: 1.53.0-nightly (9b0edb7fd 2021-03-27)
7+
68
extern crate rustc_ast_pretty;
79
extern crate rustc_error_codes;
810
extern crate rustc_errors;
@@ -15,8 +17,6 @@ extern crate rustc_span;
1517
use rustc_ast_pretty::pprust::item_to_string;
1618
use rustc_errors::registry;
1719
use rustc_session::config;
18-
use rustc_session::config::PpMode::PpmSource;
19-
use rustc_session::config::PpSourceMode::PpmExpanded;
2020
use rustc_span::source_map;
2121
use std::path;
2222
use std::process;
@@ -46,8 +46,8 @@ fn main() {
4646
output_file: None,
4747
file_loader: None,
4848
stderr: None,
49-
crate_name: None,
5049
lint_caps: rustc_hash::FxHashMap::default(),
50+
parse_sess_created: None,
5151
register_lints: None,
5252
override_queries: None,
5353
make_codegen_backend: None,
@@ -57,8 +57,7 @@ fn main() {
5757
compiler.enter(|queries| {
5858
// TODO: add this to -Z unpretty
5959
let ast_krate = queries.parse().unwrap().take();
60-
let ast_krate_mod = ast_krate.module;
61-
for item in ast_krate_mod.items {
60+
for item in ast_krate.items {
6261
println!("{}", item_to_string(&item));
6362
}
6463

@@ -75,7 +74,7 @@ fn main() {
7574
if let rustc_hir::StmtKind::Local(local) = block.stmts[0].kind {
7675
if let Some(expr) = local.init {
7776
let hir_id = expr.hir_id; // hir_id identifies the string "Hello, world!"
78-
let def_id = tcx.hir().local_def_id(item.hir_id); // def_id identifies the main function
77+
let def_id = tcx.hir().local_def_id(item.hir_id()); // def_id identifies the main function
7978
let ty = tcx.typeck(def_id).node_type(hir_id);
8079
println!("{:?}: {:?}", expr, ty); // prints expr(HirId { owner: DefIndex(3), local_id: 4 }: "Hello, world!"): &'static str
8180
}

0 commit comments

Comments
 (0)