Skip to content

Commit

Permalink
avoid code duplication by including files in docs (#1598)
Browse files Browse the repository at this point in the history
  • Loading branch information
tshepang committed Feb 15, 2023
1 parent fb4cc6f commit d0ee17f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 67 deletions.
30 changes: 2 additions & 28 deletions src/rustc-driver-getting-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,8 @@
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: Feb 2023 --> `nightly-2023-02-06` (See [here][example]
for the complete example):

[example]: https://github.com/rust-lang/rustc-dev-guide/blob/master/examples/rustc-driver-getting-diagnostics.rs
with <!-- date-check: Feb 2023 --> `nightly-2023-02-13`:

```rust
let buffer = sync::Arc::new(sync::Mutex::new(Vec::new()));
let config = rustc_interface::Config {
opts: config::Options {
// Configure the compiler to emit diagnostics in compact JSON format.
error_format: config::ErrorOutputType::Json {
pretty: false,
json_rendered: rustc_errors::emitter::HumanReadableErrorType::Default(
rustc_errors::emitter::ColorConfig::Never,
),
},
/* other config */
},
/* other config */
};
rustc_interface::run_compiler(config, |compiler| {
compiler.enter(|queries| {
queries.global_ctxt().unwrap().enter(|tcx| {
// Run the analysis phase on the local crate to trigger the type error.
let _ = tcx.analysis(());
});
});
});
// Read buffered diagnostics.
let diagnostics = String::from_utf8(buffer.lock().unwrap().clone()).unwrap();
{{#include ../examples/rustc-driver-getting-diagnostics.rs}}
```
41 changes: 2 additions & 39 deletions src/rustc-driver-interacting-with-the-ast.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,8 @@
## Getting the type of an expression

To get the type of an expression, use the `global_ctxt` to get a `TyCtxt`.
The following was tested with <!-- date-check: Feb 2023 --> `nightly-2023-02-06`
(see [here][example] for the complete example):

[example]: https://github.com/rust-lang/rustc-dev-guide/blob/master/examples/rustc-driver-interacting-with-the-ast.rs
The following was tested with <!-- date-check: Feb 2023 --> `nightly-2023-02-13`:

```rust
let config = rustc_interface::Config {
input: config::Input::Str {
name: source_map::FileName::Custom("main.rs".to_string()),
input: "fn main() { let message = \"Hello, world!\"; println!(\"{}\", message); }"
.to_string(),
},
/* other config */
};
rustc_interface::run_compiler(config, |compiler| {
compiler.enter(|queries| {
// Analyze the crate and inspect the types under the cursor.
queries.global_ctxt().unwrap().enter(|tcx| {
// 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);
// 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::ExprKind::Block(block, _) = expr.kind {
if let rustc_hir::StmtKind::Local(local) = block.stmts[0].kind {
if let Some(expr) = local.init {
let hir_id = expr.hir_id; // hir_id identifies the string "Hello, world!"
let def_id = item.hir_id().owner.def_id; // def_id identifies the main function
let ty = tcx.typeck(def_id).node_type(hir_id);
println!("{expr:#?}: {ty:?}");
}
}
}
}
}
})
});
});
{{#include ../examples/rustc-driver-interacting-with-the-ast.rs}}
```

0 comments on commit d0ee17f

Please sign in to comment.