Adapt to rust-lang/rust#136466:

Start removing `rustc_middle::hir::map::Map`

Following commit f86f7ad from pull request #136466
in the Rust project
(https://github.com/rust-lang/rust),
some methods in `Map` were moved to `TyCtxt`.
This update reimplements `rustc-drive-example.rs`,
`rustc-driver-interacting-with-the-ast.rs`,
and `rustc-interface-example.rs` using the new
versions of these methods, ensuring compatibility
with the nightly-2025-03-08 toolchain.
This commit is contained in:
Yang Lin 2025-03-16 23:27:10 +08:00
parent 077fae6ed9
commit 971366b58b
3 changed files with 9 additions and 13 deletions

View File

@ -1,4 +1,4 @@
// Tested with nightly-2025-02-13
// Tested with nightly-2025-03-08
#![feature(rustc_private)]
@ -71,9 +71,8 @@ impl rustc_driver::Callbacks for MyCallbacks {
fn after_analysis(&mut self, _compiler: &Compiler, tcx: TyCtxt<'_>) -> Compilation {
// Analyze the program and inspect the types of definitions.
for id in tcx.hir().items() {
let hir = tcx.hir();
let item = hir.item(id);
for id in tcx.hir_free_items(){
let item = &tcx.hir_item(id);
match item.kind {
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn { .. } => {
let name = item.ident;

View File

@ -1,4 +1,4 @@
// Tested with nightly-2025-02-13
// Tested with nightly-2025-03-08
#![feature(rustc_private)]
@ -70,11 +70,9 @@ impl rustc_driver::Callbacks for MyCallbacks {
}
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.
for id in hir_krate.items() {
let item = hir_krate.item(id);
for id in tcx.hir_free_items(){
let item = &tcx.hir_item(id);
// Use pattern-matching to find a specific node inside the main function.
if let rustc_hir::ItemKind::Fn { body, .. } = item.kind {
let expr = &tcx.hir_body(body).value;

View File

@ -1,4 +1,4 @@
// Tested with nightly-2025-02-13
// Tested with nightly-2025-03-08
#![feature(rustc_private)]
@ -64,9 +64,8 @@ fn main() {
println!("{krate:?}");
// Analyze the program and inspect the types of definitions.
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
for id in tcx.hir().items() {
let hir = tcx.hir();
let item = hir.item(id);
for id in tcx.hir_free_items() {
let item = tcx.hir_item(id);
match item.kind {
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn { .. } => {
let name = item.ident;