Merge pull request #2289 from y1lan/fix_compiler_err_of_examples
Fix compile errors of all the examples
This commit is contained in:
commit
01bbdec63c
|
|
@ -1,4 +1,4 @@
|
||||||
// Tested with nightly-2025-02-13
|
// Tested with nightly-2025-03-08
|
||||||
|
|
||||||
#![feature(rustc_private)]
|
#![feature(rustc_private)]
|
||||||
|
|
||||||
|
|
@ -71,9 +71,8 @@ impl rustc_driver::Callbacks for MyCallbacks {
|
||||||
|
|
||||||
fn after_analysis(&mut self, _compiler: &Compiler, tcx: TyCtxt<'_>) -> Compilation {
|
fn after_analysis(&mut self, _compiler: &Compiler, tcx: TyCtxt<'_>) -> Compilation {
|
||||||
// Analyze the program and inspect the types of definitions.
|
// Analyze the program and inspect the types of definitions.
|
||||||
for id in tcx.hir().items() {
|
for id in tcx.hir_free_items(){
|
||||||
let hir = tcx.hir();
|
let item = &tcx.hir_item(id);
|
||||||
let item = hir.item(id);
|
|
||||||
match item.kind {
|
match item.kind {
|
||||||
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn { .. } => {
|
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn { .. } => {
|
||||||
let name = item.ident;
|
let name = item.ident;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
// Tested with nightly-2025-02-13
|
// Tested with nightly-2025-03-08
|
||||||
|
|
||||||
#![feature(rustc_private)]
|
#![feature(rustc_private)]
|
||||||
|
|
||||||
|
|
@ -70,11 +70,9 @@ impl rustc_driver::Callbacks for MyCallbacks {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn after_analysis(&mut self, _compiler: &Compiler, tcx: TyCtxt<'_>) -> Compilation {
|
fn after_analysis(&mut self, _compiler: &Compiler, tcx: TyCtxt<'_>) -> Compilation {
|
||||||
// Every compilation contains a single crate.
|
|
||||||
let hir_krate = tcx.hir();
|
|
||||||
// Iterate over the top-level items in the crate, looking for the main function.
|
// Iterate over the top-level items in the crate, looking for the main function.
|
||||||
for id in hir_krate.items() {
|
for id in tcx.hir_free_items(){
|
||||||
let item = hir_krate.item(id);
|
let item = &tcx.hir_item(id);
|
||||||
// Use pattern-matching to find a specific node inside the main function.
|
// Use pattern-matching to find a specific node inside the main function.
|
||||||
if let rustc_hir::ItemKind::Fn { body, .. } = item.kind {
|
if let rustc_hir::ItemKind::Fn { body, .. } = item.kind {
|
||||||
let expr = &tcx.hir_body(body).value;
|
let expr = &tcx.hir_body(body).value;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
// Tested with nightly-2025-02-13
|
// Tested with nightly-2025-03-08
|
||||||
|
|
||||||
#![feature(rustc_private)]
|
#![feature(rustc_private)]
|
||||||
|
|
||||||
|
|
@ -64,9 +64,8 @@ fn main() {
|
||||||
println!("{krate:?}");
|
println!("{krate:?}");
|
||||||
// Analyze the program and inspect the types of definitions.
|
// Analyze the program and inspect the types of definitions.
|
||||||
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
|
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
|
||||||
for id in tcx.hir().items() {
|
for id in tcx.hir_free_items() {
|
||||||
let hir = tcx.hir();
|
let item = tcx.hir_item(id);
|
||||||
let item = hir.item(id);
|
|
||||||
match item.kind {
|
match item.kind {
|
||||||
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn { .. } => {
|
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn { .. } => {
|
||||||
let name = item.ident;
|
let name = item.ident;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
// Tested with nightly-2025-02-13
|
// Tested with nightly-2025-03-08
|
||||||
|
|
||||||
#![feature(rustc_private)]
|
#![feature(rustc_private)]
|
||||||
|
|
||||||
|
|
@ -86,8 +86,10 @@ fn main() {
|
||||||
rustc_interface::run_compiler(config, |compiler| {
|
rustc_interface::run_compiler(config, |compiler| {
|
||||||
let krate = rustc_interface::passes::parse(&compiler.sess);
|
let krate = rustc_interface::passes::parse(&compiler.sess);
|
||||||
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
|
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
|
||||||
// Run the analysis phase on the local crate to trigger the type error.
|
// Iterate all the items defined and perform type checking.
|
||||||
let _ = tcx.analysis(());
|
tcx.par_hir_body_owners(|item_def_id| {
|
||||||
|
tcx.ensure_ok().typeck(item_def_id);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
// If the compiler has encountered errors when this closure returns, it will abort (!) the program.
|
// If the compiler has encountered errors when this closure returns, it will abort (!) the program.
|
||||||
// We avoid this by resetting the error count before returning
|
// We avoid this by resetting the error count before returning
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ otherwise be printed to stderr.
|
||||||
|
|
||||||
To get diagnostics from the compiler,
|
To get diagnostics from the compiler,
|
||||||
configure [`rustc_interface::Config`] to output diagnostic to a buffer,
|
configure [`rustc_interface::Config`] to output diagnostic to a buffer,
|
||||||
and run [`TyCtxt.analysis`].
|
and run [`rustc_hir_typeck::typeck`] for each item.
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
{{#include ../../examples/rustc-interface-getting-diagnostics.rs}}
|
{{#include ../../examples/rustc-interface-getting-diagnostics.rs}}
|
||||||
|
|
@ -16,3 +16,4 @@ and run [`TyCtxt.analysis`].
|
||||||
[`rustc_interface`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/index.html
|
[`rustc_interface`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/index.html
|
||||||
[`rustc_interface::Config`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/interface/struct.Config.html
|
[`rustc_interface::Config`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/interface/struct.Config.html
|
||||||
[`TyCtxt.analysis`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/passes/fn.analysis.html
|
[`TyCtxt.analysis`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/passes/fn.analysis.html
|
||||||
|
[`rustc_hir_typeck::typeck`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir_typeck/fn.typeck.html
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue