Skip to content

Commit dce2652

Browse files
committed
Fix examples to work with nightly-2025-02-13
While there were comments indicating which nightly versions the examples were tested with, those versions did not work for me: neither did the examples compile, nor did they produce the expected output. This commit fixes the compilation issues, using nightly-2025-02-13 for all examples (previously the version differed between the examples) and, in the case of the `rustc_driver` examples, also fixes the argument passing: rustc ignores the first argument, so we need to pass the filename as the second (otherwise we only get the help text printed). Note that the `rustc-interface-getting-diagnostics.rs` example still does not produce any output, which I assume is not how it is intended. However, I don't know enough to fix it. To avoid inconsistencies between the documented version and the actually required version I've moved the version comment from the Markdown into the Rust code where it hopefully won't be forgotten as easily. Finally I've clarified in the examples' README that you also need to use the proper nightly version when compiling the examples, not just when running them.
1 parent 27566ab commit dce2652

7 files changed

+37
-14
lines changed

src/doc/rustc-dev-guide/examples/README

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ For each example to compile, you will need to first run the following:
44

55
To create an executable:
66

7-
rustc rustc-driver-example.rs
7+
rustup run nightly rustc rustc-driver-example.rs
8+
9+
You might need to be more specific about the exact nightly version. See the comments at the top of
10+
the examples for the version they were written for.
811

912
To run an executable:
1013

src/doc/rustc-dev-guide/examples/rustc-driver-example.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Tested with nightly-2025-02-13
2+
13
#![feature(rustc_private)]
24

35
extern crate rustc_ast;
@@ -73,7 +75,7 @@ impl rustc_driver::Callbacks for MyCallbacks {
7375
let hir = tcx.hir();
7476
let item = hir.item(id);
7577
match item.kind {
76-
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn(_, _, _) => {
78+
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn { .. } => {
7779
let name = item.ident;
7880
let ty = tcx.type_of(item.hir_id().owner.def_id);
7981
println!("{name:?}:\t{ty:?}")
@@ -87,5 +89,13 @@ impl rustc_driver::Callbacks for MyCallbacks {
8789
}
8890

8991
fn main() {
90-
run_compiler(&["main.rs".to_string()], &mut MyCallbacks);
92+
run_compiler(
93+
&[
94+
// The first argument, which in practice contains the name of the binary being executed
95+
// (i.e. "rustc") is ignored by rustc.
96+
"ignored".to_string(),
97+
"main.rs".to_string(),
98+
],
99+
&mut MyCallbacks,
100+
);
91101
}

src/doc/rustc-dev-guide/examples/rustc-driver-interacting-with-the-ast.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Tested with nightly-2025-02-13
2+
13
#![feature(rustc_private)]
24

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

9698
fn main() {
97-
run_compiler(&["main.rs".to_string()], &mut MyCallbacks);
99+
run_compiler(
100+
&[
101+
// The first argument, which in practice contains the name of the binary being executed
102+
// (i.e. "rustc") is ignored by rustc.
103+
"ignored".to_string(),
104+
"main.rs".to_string(),
105+
],
106+
&mut MyCallbacks,
107+
);
98108
}

src/doc/rustc-dev-guide/examples/rustc-interface-example.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Tested with nightly-2025-02-13
2+
13
#![feature(rustc_private)]
24

35
extern crate rustc_driver;
@@ -9,8 +11,6 @@ extern crate rustc_interface;
911
extern crate rustc_session;
1012
extern crate rustc_span;
1113

12-
use std::sync::Arc;
13-
1414
use rustc_errors::registry;
1515
use rustc_hash::FxHashMap;
1616
use rustc_session::config;
@@ -56,7 +56,7 @@ fn main() {
5656
expanded_args: Vec::new(),
5757
ice_file: None,
5858
hash_untracked_state: None,
59-
using_internal_features: Arc::default(),
59+
using_internal_features: &rustc_driver::USING_INTERNAL_FEATURES,
6060
};
6161
rustc_interface::run_compiler(config, |compiler| {
6262
// Parse the program and print the syntax tree.
@@ -68,7 +68,7 @@ fn main() {
6868
let hir = tcx.hir();
6969
let item = hir.item(id);
7070
match item.kind {
71-
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn(_, _, _) => {
71+
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn { .. } => {
7272
let name = item.ident;
7373
let ty = tcx.type_of(item.hir_id().owner.def_id);
7474
println!("{name:?}:\t{ty:?}")

src/doc/rustc-dev-guide/examples/rustc-interface-getting-diagnostics.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Tested with nightly-2025-02-13
2+
13
#![feature(rustc_private)]
24

35
extern crate rustc_data_structures;
@@ -15,7 +17,7 @@ use std::sync::{Arc, Mutex};
1517
use rustc_errors::emitter::Emitter;
1618
use rustc_errors::registry::{self, Registry};
1719
use rustc_errors::translation::Translate;
18-
use rustc_errors::{DiagCtxt, DiagInner, FluentBundle};
20+
use rustc_errors::{DiagInner, FluentBundle};
1921
use rustc_session::config;
2022
use rustc_span::source_map::SourceMap;
2123

@@ -79,7 +81,7 @@ fn main() {
7981
expanded_args: Vec::new(),
8082
ice_file: None,
8183
hash_untracked_state: None,
82-
using_internal_features: Arc::default(),
84+
using_internal_features: &rustc_driver::USING_INTERNAL_FEATURES,
8385
};
8486
rustc_interface::run_compiler(config, |compiler| {
8587
let krate = rustc_interface::passes::parse(&compiler.sess);

src/doc/rustc-dev-guide/src/rustc-driver/getting-diagnostics.md

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ otherwise be printed to stderr.
88
To get diagnostics from the compiler,
99
configure [`rustc_interface::Config`] to output diagnostic to a buffer,
1010
and run [`TyCtxt.analysis`].
11-
The following was tested with <!-- date-check: september 2024 --> `nightly-2024-09-16`:
1211

1312
```rust
1413
{{#include ../../examples/rustc-interface-getting-diagnostics.rs}}

src/doc/rustc-dev-guide/src/rustc-driver/interacting-with-the-ast.md

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
## Getting the type of an expression
66

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

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

0 commit comments

Comments
 (0)