Skip to content

Commit 01bbdec

Browse files
authored
Merge pull request #2289 from y1lan/fix_compiler_err_of_examples
Fix compile errors of all the examples
2 parents fdca2d3 + 8322995 commit 01bbdec

5 files changed

+16
-17
lines changed

examples/rustc-driver-example.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Tested with nightly-2025-02-13
1+
// Tested with nightly-2025-03-08
22

33
#![feature(rustc_private)]
44

@@ -71,9 +71,8 @@ impl rustc_driver::Callbacks for MyCallbacks {
7171

7272
fn after_analysis(&mut self, _compiler: &Compiler, tcx: TyCtxt<'_>) -> Compilation {
7373
// Analyze the program and inspect the types of definitions.
74-
for id in tcx.hir().items() {
75-
let hir = tcx.hir();
76-
let item = hir.item(id);
74+
for id in tcx.hir_free_items(){
75+
let item = &tcx.hir_item(id);
7776
match item.kind {
7877
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn { .. } => {
7978
let name = item.ident;

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Tested with nightly-2025-02-13
1+
// Tested with nightly-2025-03-08
22

33
#![feature(rustc_private)]
44

@@ -70,11 +70,9 @@ impl rustc_driver::Callbacks for MyCallbacks {
7070
}
7171

7272
fn after_analysis(&mut self, _compiler: &Compiler, tcx: TyCtxt<'_>) -> Compilation {
73-
// Every compilation contains a single crate.
74-
let hir_krate = tcx.hir();
7573
// Iterate over the top-level items in the crate, looking for the main function.
76-
for id in hir_krate.items() {
77-
let item = hir_krate.item(id);
74+
for id in tcx.hir_free_items(){
75+
let item = &tcx.hir_item(id);
7876
// Use pattern-matching to find a specific node inside the main function.
7977
if let rustc_hir::ItemKind::Fn { body, .. } = item.kind {
8078
let expr = &tcx.hir_body(body).value;

examples/rustc-interface-example.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Tested with nightly-2025-02-13
1+
// Tested with nightly-2025-03-08
22

33
#![feature(rustc_private)]
44

@@ -64,9 +64,8 @@ fn main() {
6464
println!("{krate:?}");
6565
// Analyze the program and inspect the types of definitions.
6666
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
67-
for id in tcx.hir().items() {
68-
let hir = tcx.hir();
69-
let item = hir.item(id);
67+
for id in tcx.hir_free_items() {
68+
let item = tcx.hir_item(id);
7069
match item.kind {
7170
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn { .. } => {
7271
let name = item.ident;

examples/rustc-interface-getting-diagnostics.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Tested with nightly-2025-02-13
1+
// Tested with nightly-2025-03-08
22

33
#![feature(rustc_private)]
44

@@ -86,8 +86,10 @@ fn main() {
8686
rustc_interface::run_compiler(config, |compiler| {
8787
let krate = rustc_interface::passes::parse(&compiler.sess);
8888
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
89-
// Run the analysis phase on the local crate to trigger the type error.
90-
let _ = tcx.analysis(());
89+
// Iterate all the items defined and perform type checking.
90+
tcx.par_hir_body_owners(|item_def_id| {
91+
tcx.ensure_ok().typeck(item_def_id);
92+
});
9193
});
9294
// If the compiler has encountered errors when this closure returns, it will abort (!) the program.
9395
// We avoid this by resetting the error count before returning

src/rustc-driver/getting-diagnostics.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ otherwise be printed to stderr.
77

88
To get diagnostics from the compiler,
99
configure [`rustc_interface::Config`] to output diagnostic to a buffer,
10-
and run [`TyCtxt.analysis`].
10+
and run [`rustc_hir_typeck::typeck`] for each item.
1111

1212
```rust
1313
{{#include ../../examples/rustc-interface-getting-diagnostics.rs}}
@@ -16,3 +16,4 @@ and run [`TyCtxt.analysis`].
1616
[`rustc_interface`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/index.html
1717
[`rustc_interface::Config`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/interface/struct.Config.html
1818
[`TyCtxt.analysis`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/passes/fn.analysis.html
19+
[`rustc_hir_typeck::typeck`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir_typeck/fn.typeck.html

0 commit comments

Comments
 (0)