Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix examples to work with nightly-2025-02-13 #2256

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion examples/README
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ For each example to compile, you will need to first run the following:

To create an executable:

rustc rustc-driver-example.rs
rustup run nightly rustc rustc-driver-example.rs

You might need to be more specific about the exact nightly version. See the comments at the top of
the examples for the version they were written for.

To run an executable:

Expand Down
14 changes: 12 additions & 2 deletions examples/rustc-driver-example.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Tested with nightly-2025-02-13

#![feature(rustc_private)]

extern crate rustc_ast;
Expand Down Expand Up @@ -73,7 +75,7 @@ impl rustc_driver::Callbacks for MyCallbacks {
let hir = tcx.hir();
let item = hir.item(id);
match item.kind {
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn(_, _, _) => {
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn { .. } => {
let name = item.ident;
let ty = tcx.type_of(item.hir_id().owner.def_id);
println!("{name:?}:\t{ty:?}")
Expand All @@ -87,5 +89,13 @@ impl rustc_driver::Callbacks for MyCallbacks {
}

fn main() {
run_compiler(&["main.rs".to_string()], &mut MyCallbacks);
run_compiler(
&[
// The first argument, which in practice contains the name of the binary being executed
// (i.e. "rustc") is ignored by rustc.
"ignored".to_string(),
"main.rs".to_string(),
],
&mut MyCallbacks,
);
}
16 changes: 13 additions & 3 deletions examples/rustc-driver-interacting-with-the-ast.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Tested with nightly-2025-02-13

#![feature(rustc_private)]

extern crate rustc_ast;
Expand Down Expand Up @@ -74,8 +76,8 @@ impl rustc_driver::Callbacks for MyCallbacks {
for id in hir_krate.items() {
let item = hir_krate.item(id);
// Use pattern-matching to find a specific node inside the main function.
if let rustc_hir::ItemKind::Fn(_, _, body_id) = item.kind {
let expr = &tcx.hir().body(body_id).value;
if let rustc_hir::ItemKind::Fn { body, .. } = item.kind {
let expr = &tcx.hir().body(body).value;
if let rustc_hir::ExprKind::Block(block, _) = expr.kind {
if let rustc_hir::StmtKind::Let(let_stmt) = block.stmts[0].kind {
if let Some(expr) = let_stmt.init {
Expand All @@ -94,5 +96,13 @@ impl rustc_driver::Callbacks for MyCallbacks {
}

fn main() {
run_compiler(&["main.rs".to_string()], &mut MyCallbacks);
run_compiler(
&[
// The first argument, which in practice contains the name of the binary being executed
// (i.e. "rustc") is ignored by rustc.
"ignored".to_string(),
"main.rs".to_string(),
],
&mut MyCallbacks,
);
}
8 changes: 4 additions & 4 deletions examples/rustc-interface-example.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Tested with nightly-2025-02-13

#![feature(rustc_private)]

extern crate rustc_driver;
Expand All @@ -9,8 +11,6 @@ extern crate rustc_interface;
extern crate rustc_session;
extern crate rustc_span;

use std::sync::Arc;

use rustc_errors::registry;
use rustc_hash::FxHashMap;
use rustc_session::config;
Expand Down Expand Up @@ -56,7 +56,7 @@ fn main() {
expanded_args: Vec::new(),
ice_file: None,
hash_untracked_state: None,
using_internal_features: Arc::default(),
using_internal_features: &rustc_driver::USING_INTERNAL_FEATURES,
};
rustc_interface::run_compiler(config, |compiler| {
// Parse the program and print the syntax tree.
Expand All @@ -68,7 +68,7 @@ fn main() {
let hir = tcx.hir();
let item = hir.item(id);
match item.kind {
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn(_, _, _) => {
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn { .. } => {
let name = item.ident;
let ty = tcx.type_of(item.hir_id().owner.def_id);
println!("{name:?}:\t{ty:?}")
Expand Down
6 changes: 4 additions & 2 deletions examples/rustc-interface-getting-diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Tested with nightly-2025-02-13

#![feature(rustc_private)]

extern crate rustc_data_structures;
Expand All @@ -15,7 +17,7 @@ use std::sync::{Arc, Mutex};
use rustc_errors::emitter::Emitter;
use rustc_errors::registry::{self, Registry};
use rustc_errors::translation::Translate;
use rustc_errors::{DiagCtxt, DiagInner, FluentBundle};
use rustc_errors::{DiagInner, FluentBundle};
use rustc_session::config;
use rustc_span::source_map::SourceMap;

Expand Down Expand Up @@ -79,7 +81,7 @@ fn main() {
expanded_args: Vec::new(),
ice_file: None,
hash_untracked_state: None,
using_internal_features: Arc::default(),
using_internal_features: &rustc_driver::USING_INTERNAL_FEATURES,
};
rustc_interface::run_compiler(config, |compiler| {
let krate = rustc_interface::passes::parse(&compiler.sess);
Expand Down
1 change: 0 additions & 1 deletion src/rustc-driver/getting-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ otherwise be printed to stderr.
To get diagnostics from the compiler,
configure [`rustc_interface::Config`] to output diagnostic to a buffer,
and run [`TyCtxt.analysis`].
The following was tested with <!-- date-check: september 2024 --> `nightly-2024-09-16`:

```rust
{{#include ../../examples/rustc-interface-getting-diagnostics.rs}}
Expand Down
1 change: 0 additions & 1 deletion src/rustc-driver/interacting-with-the-ast.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
## Getting the type of an expression

To get the type of an expression, use the [`after_analysis`] callback to get a [`TyCtxt`].
The following was tested with <!-- date-check: december 2024 --> `nightly-2024-12-15`:

```rust
{{#include ../../examples/rustc-driver-interacting-with-the-ast.rs}}
Expand Down