avoid code duplication by including files in docs (#1598)
This commit is contained in:
parent
fb4cc6f8ac
commit
d0ee17f100
|
|
@ -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}}
|
||||
```
|
||||
|
|
|
|||
|
|
@ -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}}
|
||||
```
|
||||
|
|
|
|||
Loading…
Reference in New Issue