Skip to content

Commit a206009

Browse files
JohnTitortshepang
authored andcommitted
Update rustc-driver related examples
1 parent 6692629 commit a206009

6 files changed

+19
-16
lines changed

examples/rustc-driver-example.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![feature(rustc_private)]
22

33
// NOTE: For the example to compile, you will need to first run the following:
4-
// rustup component add rustc-dev
4+
// rustup component add rustc-dev llvm-tools-preview
55

6-
// version: 1.61.0-nightly (68369a041 2022-02-22)
6+
// version: 1.62.0-nightly (7c4b47696 2022-04-30)
77

88
extern crate rustc_error_codes;
99
extern crate rustc_errors;
@@ -72,7 +72,8 @@ fn main() {
7272
println!("{:#?}", parse);
7373
// Analyze the program and inspect the types of definitions.
7474
queries.global_ctxt().unwrap().take().enter(|tcx| {
75-
for item in tcx.hir().items() {
75+
for id in tcx.hir().items() {
76+
let item = tcx.hir().item(id);
7677
match item.kind {
7778
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn(_, _, _) => {
7879
let name = item.ident;

examples/rustc-driver-getting-diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![feature(rustc_private)]
22

33
// NOTE: For the example to compile, you will need to first run the following:
4-
// rustup component add rustc-dev
4+
// rustup component add rustc-dev llvm-tools-preview
55

6-
// version: 1.61.0-nightly (68369a041 2022-02-22)
6+
// version: 1.62.0-nightly (7c4b47696 2022-04-30)
77

88
extern crate rustc_error_codes;
99
extern crate rustc_errors;

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![feature(rustc_private)]
22

33
// NOTE: For the example to compile, you will need to first run the following:
4-
// rustup component add rustc-dev llvm-tools-preview
4+
// rustup component add rustc-dev llvm-tools-preview
55

6-
// version: 1.61.0-nightly (68369a041 2022-02-22)
6+
// version: 1.62.0-nightly (7c4b47696 2022-04-30)
77

88
extern crate rustc_ast_pretty;
99
extern crate rustc_error_codes;
@@ -66,7 +66,8 @@ fn main() {
6666
// Every compilation contains a single crate.
6767
let hir_krate = tcx.hir();
6868
// Iterate over the top-level items in the crate, looking for the main function.
69-
for item in hir_krate.items() {
69+
for id in hir_krate.items() {
70+
let item = hir_krate.item(id);
7071
// Use pattern-matching to find a specific node inside the main function.
7172
if let rustc_hir::ItemKind::Fn(_, _, body_id) = item.kind {
7273
let expr = &tcx.hir().body(body_id).value;
@@ -76,7 +77,7 @@ fn main() {
7677
let hir_id = expr.hir_id; // hir_id identifies the string "Hello, world!"
7778
let def_id = tcx.hir().local_def_id(item.hir_id()); // def_id identifies the main function
7879
let ty = tcx.typeck(def_id).node_type(hir_id);
79-
println!("{:?}: {:?}", expr, ty); // prints expr(HirId { owner: DefIndex(3), local_id: 4 }: "Hello, world!"): &'static str
80+
println!("{:?}: {:?}", expr, ty);
8081
}
8182
}
8283
}

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 should be compiled
10-
with <!-- date: 2021-03 --> `nightly-2021-03-28` (See [here][example]
10+
with <!-- date: 2022-05 --> `nightly-2021-04-30` (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
@@ -35,7 +35,7 @@ rustc_interface::run_compiler(config, |compiler| {
3535
compiler.enter(|queries| {
3636
queries.global_ctxt().unwrap().take().enter(|tcx| {
3737
// Run the analysis phase on the local crate to trigger the type error.
38-
tcx.analysis(rustc_hir::def_id::LOCAL_CRATE);
38+
let _ = tcx.analysis(());
3939
});
4040
});
4141
});

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

+5-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 should be compiled with <!-- date: 2021-03 --> `nightly-2021-03-28`
8+
The following should be compiled with <!-- date: 2022-05 --> `nightly-2022-04-30`
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
@@ -24,9 +24,10 @@ rustc_interface::run_compiler(config, |compiler| {
2424
// Analyze the crate and inspect the types under the cursor.
2525
queries.global_ctxt().unwrap().take().enter(|tcx| {
2626
// Every compilation contains a single crate.
27-
let hir_krate = tcx.hir().krate();
27+
let hir_krate = tcx.hir();
2828
// Iterate over the top-level items in the crate, looking for the main function.
29-
for (_, item) in &hir_krate.items {
29+
for id in hir_krate.items() {
30+
let item = hir_krate.item(id);
3031
// Use pattern-matching to find a specific node inside the main function.
3132
if let rustc_hir::ItemKind::Fn(_, _, body_id) = item.kind {
3233
let expr = &tcx.hir().body(body_id).value;
@@ -36,7 +37,7 @@ rustc_interface::run_compiler(config, |compiler| {
3637
let hir_id = expr.hir_id; // hir_id identifies the string "Hello, world!"
3738
let def_id = tcx.hir().local_def_id(item.hir_id()); // def_id identifies the main function
3839
let ty = tcx.typeck(def_id).node_type(hir_id);
39-
println!("{:?}: {:?}", expr, ty); // prints expr(HirId { owner: DefIndex(3), local_id: 4 }: "Hello, world!"): &'static str
40+
println!("{:?}: {:?}", expr, ty);
4041
}
4142
}
4243
}

src/rustc-driver.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using the interface defined in the [`rustc_interface`] crate.
77
The `rustc_interface` crate provides external users with an (unstable) API
88
for running code at particular times during the compilation process, allowing
99
third parties to effectively use `rustc`'s internals as a library for
10-
analysing a crate or emulating the compiler in-process (e.g. the RLS or rustdoc).
10+
analyzing a crate or emulating the compiler in-process (e.g. the RLS or rustdoc).
1111

1212
For those using `rustc` as a library, the [`rustc_interface::run_compiler()`][i_rc]
1313
function is the main entrypoint to the compiler. It takes a configuration for the compiler

0 commit comments

Comments
 (0)