Skip to content

Commit d0ee17f

Browse files
authored
avoid code duplication by including files in docs (#1598)
1 parent fb4cc6f commit d0ee17f

File tree

2 files changed

+4
-67
lines changed

2 files changed

+4
-67
lines changed

Diff for: src/rustc-driver-getting-diagnostics.md

+2-28
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,8 @@
77
To get diagnostics from the compiler,
88
configure `rustc_interface::Config` to output diagnostic to a buffer,
99
and run `TyCtxt.analysis`. The following was tested
10-
with <!-- date-check: Feb 2023 --> `nightly-2023-02-06` (See [here][example]
11-
for the complete example):
12-
13-
[example]: https://github.com/rust-lang/rustc-dev-guide/blob/master/examples/rustc-driver-getting-diagnostics.rs
10+
with <!-- date-check: Feb 2023 --> `nightly-2023-02-13`:
1411

1512
```rust
16-
let buffer = sync::Arc::new(sync::Mutex::new(Vec::new()));
17-
let config = rustc_interface::Config {
18-
opts: config::Options {
19-
// Configure the compiler to emit diagnostics in compact JSON format.
20-
error_format: config::ErrorOutputType::Json {
21-
pretty: false,
22-
json_rendered: rustc_errors::emitter::HumanReadableErrorType::Default(
23-
rustc_errors::emitter::ColorConfig::Never,
24-
),
25-
},
26-
/* other config */
27-
},
28-
/* other config */
29-
};
30-
rustc_interface::run_compiler(config, |compiler| {
31-
compiler.enter(|queries| {
32-
queries.global_ctxt().unwrap().enter(|tcx| {
33-
// Run the analysis phase on the local crate to trigger the type error.
34-
let _ = tcx.analysis(());
35-
});
36-
});
37-
});
38-
// Read buffered diagnostics.
39-
let diagnostics = String::from_utf8(buffer.lock().unwrap().clone()).unwrap();
13+
{{#include ../examples/rustc-driver-getting-diagnostics.rs}}
4014
```

Diff for: src/rustc-driver-interacting-with-the-ast.md

+2-39
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,8 @@
55
## Getting the type of an expression
66

77
To get the type of an expression, use the `global_ctxt` to get a `TyCtxt`.
8-
The following was tested with <!-- date-check: Feb 2023 --> `nightly-2023-02-06`
9-
(see [here][example] for the complete example):
10-
11-
[example]: https://github.com/rust-lang/rustc-dev-guide/blob/master/examples/rustc-driver-interacting-with-the-ast.rs
8+
The following was tested with <!-- date-check: Feb 2023 --> `nightly-2023-02-13`:
129

1310
```rust
14-
let config = rustc_interface::Config {
15-
input: config::Input::Str {
16-
name: source_map::FileName::Custom("main.rs".to_string()),
17-
input: "fn main() { let message = \"Hello, world!\"; println!(\"{}\", message); }"
18-
.to_string(),
19-
},
20-
/* other config */
21-
};
22-
rustc_interface::run_compiler(config, |compiler| {
23-
compiler.enter(|queries| {
24-
// Analyze the crate and inspect the types under the cursor.
25-
queries.global_ctxt().unwrap().enter(|tcx| {
26-
// Every compilation contains a single crate.
27-
let hir_krate = tcx.hir();
28-
// Iterate over the top-level items in the crate, looking for the main function.
29-
for id in hir_krate.items() {
30-
let item = hir_krate.item(id);
31-
// Use pattern-matching to find a specific node inside the main function.
32-
if let rustc_hir::ItemKind::Fn(_, _, body_id) = item.kind {
33-
let expr = &tcx.hir().body(body_id).value;
34-
if let rustc_hir::ExprKind::Block(block, _) = expr.kind {
35-
if let rustc_hir::StmtKind::Local(local) = block.stmts[0].kind {
36-
if let Some(expr) = local.init {
37-
let hir_id = expr.hir_id; // hir_id identifies the string "Hello, world!"
38-
let def_id = item.hir_id().owner.def_id; // def_id identifies the main function
39-
let ty = tcx.typeck(def_id).node_type(hir_id);
40-
println!("{expr:#?}: {ty:?}");
41-
}
42-
}
43-
}
44-
}
45-
}
46-
})
47-
});
48-
});
11+
{{#include ../examples/rustc-driver-interacting-with-the-ast.rs}}
4912
```

0 commit comments

Comments
 (0)