Skip to content

Commit 342f723

Browse files
authored
update examples for rustc 1.69.0-nightly (e1eaa2d5d 2023-02-06) (rust-lang#1590)
Closes rust-lang#1581
1 parent b4c4652 commit 342f723

5 files changed

+16
-17
lines changed

examples/rustc-driver-example.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern crate rustc_hir;
1212
extern crate rustc_interface;
1313
extern crate rustc_session;
1414
extern crate rustc_span;
15+
extern crate rustc_driver;
1516

1617
use std::{path, process, str};
1718

@@ -46,7 +47,6 @@ fn main() {
4647
"#
4748
.into(),
4849
},
49-
input_path: None, // Option<PathBuf>
5050
output_dir: None, // Option<PathBuf>
5151
output_file: None, // Option<PathBuf>
5252
file_loader: None, // Option<Box<dyn FileLoader + Send + Sync>>
@@ -71,17 +71,17 @@ fn main() {
7171
rustc_interface::run_compiler(config, |compiler| {
7272
compiler.enter(|queries| {
7373
// Parse the program and print the syntax tree.
74-
let parse = queries.parse().unwrap().take();
74+
let parse = queries.parse().unwrap().get_mut().clone();
7575
println!("{parse:?}");
7676
// Analyze the program and inspect the types of definitions.
77-
queries.global_ctxt().unwrap().take().enter(|tcx| {
77+
queries.global_ctxt().unwrap().enter(|tcx| {
7878
for id in tcx.hir().items() {
7979
let hir = tcx.hir();
8080
let item = hir.item(id);
8181
match item.kind {
8282
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn(_, _, _) => {
8383
let name = item.ident;
84-
let ty = tcx.type_of(hir.local_def_id(item.hir_id()));
84+
let ty = tcx.type_of(item.hir_id().owner.def_id);
8585
println!("{name:?}:\t{ty:?}")
8686
}
8787
_ => (),

examples/rustc-driver-getting-diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern crate rustc_hir;
1212
extern crate rustc_interface;
1313
extern crate rustc_session;
1414
extern crate rustc_span;
15+
extern crate rustc_driver;
1516

1617
use rustc_errors::registry;
1718
use rustc_session::config::{self, CheckCfg};
@@ -67,7 +68,6 @@ fn main() {
6768
},
6869
crate_cfg: rustc_hash::FxHashSet::default(),
6970
crate_check_cfg: CheckCfg::default(),
70-
input_path: None,
7171
output_dir: None,
7272
output_file: None,
7373
file_loader: None,
@@ -80,7 +80,7 @@ fn main() {
8080
};
8181
rustc_interface::run_compiler(config, |compiler| {
8282
compiler.enter(|queries| {
83-
queries.global_ctxt().unwrap().take().enter(|tcx| {
83+
queries.global_ctxt().unwrap().enter(|tcx| {
8484
// Run the analysis phase on the local crate to trigger the type error.
8585
let _ = tcx.analysis(());
8686
});

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern crate rustc_hir;
1313
extern crate rustc_interface;
1414
extern crate rustc_session;
1515
extern crate rustc_span;
16+
extern crate rustc_driver;
1617

1718
use std::{path, process, str};
1819

@@ -45,7 +46,6 @@ fn main() {
4546
},
4647
crate_cfg: rustc_hash::FxHashSet::default(),
4748
crate_check_cfg: CheckCfg::default(),
48-
input_path: None,
4949
output_dir: None,
5050
output_file: None,
5151
file_loader: None,
@@ -59,13 +59,12 @@ fn main() {
5959
rustc_interface::run_compiler(config, |compiler| {
6060
compiler.enter(|queries| {
6161
// TODO: add this to -Z unpretty
62-
let ast_krate = queries.parse().unwrap().take();
62+
let ast_krate = queries.parse().unwrap().get_mut().clone();
6363
for item in ast_krate.items {
6464
println!("{}", item_to_string(&item));
6565
}
66-
6766
// Analyze the crate and inspect the types under the cursor.
68-
queries.global_ctxt().unwrap().take().enter(|tcx| {
67+
queries.global_ctxt().unwrap().enter(|tcx| {
6968
// Every compilation contains a single crate.
7069
let hir_krate = tcx.hir();
7170
// Iterate over the top-level items in the crate, looking for the main function.
@@ -78,7 +77,7 @@ fn main() {
7877
if let rustc_hir::StmtKind::Local(local) = block.stmts[0].kind {
7978
if let Some(expr) = local.init {
8079
let hir_id = expr.hir_id; // hir_id identifies the string "Hello, world!"
81-
let def_id = tcx.hir().local_def_id(item.hir_id()); // def_id identifies the main function
80+
let def_id = item.hir_id().owner.def_id; // def_id identifies the main function
8281
let ty = tcx.typeck(def_id).node_type(hir_id);
8382
println!("{expr:#?}: {ty:?}");
8483
}

src/rustc-driver-getting-diagnostics.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
To get diagnostics from the compiler,
88
configure `rustc_interface::Config` to output diagnostic to a buffer,
99
and run `TyCtxt.analysis`. The following was tested
10-
with <!-- date-check: Jan 2023 --> `nightly-2022-12-19` (See [here][example]
10+
with <!-- date-check: Feb 2023 --> `nightly-2023-02-06` (See [here][example]
1111
for the complete example):
1212

1313
[example]: https://github.com/rust-lang/rustc-dev-guide/blob/master/examples/rustc-driver-getting-diagnostics.rs
@@ -29,7 +29,7 @@ let config = rustc_interface::Config {
2929
};
3030
rustc_interface::run_compiler(config, |compiler| {
3131
compiler.enter(|queries| {
32-
queries.global_ctxt().unwrap().take().enter(|tcx| {
32+
queries.global_ctxt().unwrap().enter(|tcx| {
3333
// Run the analysis phase on the local crate to trigger the type error.
3434
let _ = tcx.analysis(());
3535
});

src/rustc-driver-interacting-with-the-ast.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## Getting the type of an expression
66

77
To get the type of an expression, use the `global_ctxt` to get a `TyCtxt`.
8-
The following was tested with <!-- date-check: Jan 2023 --> `nightly-2022-12-19`
8+
The following was tested with <!-- date-check: Feb 2023 --> `nightly-2023-02-06`
99
(see [here][example] for the complete example):
1010

1111
[example]: https://github.com/rust-lang/rustc-dev-guide/blob/master/examples/rustc-driver-interacting-with-the-ast.rs
@@ -22,7 +22,7 @@ let config = rustc_interface::Config {
2222
rustc_interface::run_compiler(config, |compiler| {
2323
compiler.enter(|queries| {
2424
// Analyze the crate and inspect the types under the cursor.
25-
queries.global_ctxt().unwrap().take().enter(|tcx| {
25+
queries.global_ctxt().unwrap().enter(|tcx| {
2626
// Every compilation contains a single crate.
2727
let hir_krate = tcx.hir();
2828
// Iterate over the top-level items in the crate, looking for the main function.
@@ -35,9 +35,9 @@ rustc_interface::run_compiler(config, |compiler| {
3535
if let rustc_hir::StmtKind::Local(local) = block.stmts[0].kind {
3636
if let Some(expr) = local.init {
3737
let hir_id = expr.hir_id; // hir_id identifies the string "Hello, world!"
38-
let def_id = tcx.hir().local_def_id(item.hir_id()); // def_id identifies the main function
38+
let def_id = item.hir_id().owner.def_id; // def_id identifies the main function
3939
let ty = tcx.typeck(def_id).node_type(hir_id);
40-
println!("{:?}: {:?}", expr, ty);
40+
println!("{expr:#?}: {ty:?}");
4141
}
4242
}
4343
}

0 commit comments

Comments
 (0)