update examples for rustc 1.69.0-nightly (e1eaa2d5d 2023-02-06) (#1590)
Closes https://github.com/rust-lang/rustc-dev-guide/issues/1581
This commit is contained in:
parent
b4c465275c
commit
342f72310d
|
|
@ -12,6 +12,7 @@ extern crate rustc_hir;
|
|||
extern crate rustc_interface;
|
||||
extern crate rustc_session;
|
||||
extern crate rustc_span;
|
||||
extern crate rustc_driver;
|
||||
|
||||
use std::{path, process, str};
|
||||
|
||||
|
|
@ -46,7 +47,6 @@ fn main() {
|
|||
"#
|
||||
.into(),
|
||||
},
|
||||
input_path: None, // Option<PathBuf>
|
||||
output_dir: None, // Option<PathBuf>
|
||||
output_file: None, // Option<PathBuf>
|
||||
file_loader: None, // Option<Box<dyn FileLoader + Send + Sync>>
|
||||
|
|
@ -71,17 +71,17 @@ fn main() {
|
|||
rustc_interface::run_compiler(config, |compiler| {
|
||||
compiler.enter(|queries| {
|
||||
// Parse the program and print the syntax tree.
|
||||
let parse = queries.parse().unwrap().take();
|
||||
let parse = queries.parse().unwrap().get_mut().clone();
|
||||
println!("{parse:?}");
|
||||
// Analyze the program and inspect the types of definitions.
|
||||
queries.global_ctxt().unwrap().take().enter(|tcx| {
|
||||
queries.global_ctxt().unwrap().enter(|tcx| {
|
||||
for id in tcx.hir().items() {
|
||||
let hir = tcx.hir();
|
||||
let item = hir.item(id);
|
||||
match item.kind {
|
||||
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn(_, _, _) => {
|
||||
let name = item.ident;
|
||||
let ty = tcx.type_of(hir.local_def_id(item.hir_id()));
|
||||
let ty = tcx.type_of(item.hir_id().owner.def_id);
|
||||
println!("{name:?}:\t{ty:?}")
|
||||
}
|
||||
_ => (),
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ extern crate rustc_hir;
|
|||
extern crate rustc_interface;
|
||||
extern crate rustc_session;
|
||||
extern crate rustc_span;
|
||||
extern crate rustc_driver;
|
||||
|
||||
use rustc_errors::registry;
|
||||
use rustc_session::config::{self, CheckCfg};
|
||||
|
|
@ -67,7 +68,6 @@ fn main() {
|
|||
},
|
||||
crate_cfg: rustc_hash::FxHashSet::default(),
|
||||
crate_check_cfg: CheckCfg::default(),
|
||||
input_path: None,
|
||||
output_dir: None,
|
||||
output_file: None,
|
||||
file_loader: None,
|
||||
|
|
@ -80,7 +80,7 @@ fn main() {
|
|||
};
|
||||
rustc_interface::run_compiler(config, |compiler| {
|
||||
compiler.enter(|queries| {
|
||||
queries.global_ctxt().unwrap().take().enter(|tcx| {
|
||||
queries.global_ctxt().unwrap().enter(|tcx| {
|
||||
// Run the analysis phase on the local crate to trigger the type error.
|
||||
let _ = tcx.analysis(());
|
||||
});
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ extern crate rustc_hir;
|
|||
extern crate rustc_interface;
|
||||
extern crate rustc_session;
|
||||
extern crate rustc_span;
|
||||
extern crate rustc_driver;
|
||||
|
||||
use std::{path, process, str};
|
||||
|
||||
|
|
@ -45,7 +46,6 @@ fn main() {
|
|||
},
|
||||
crate_cfg: rustc_hash::FxHashSet::default(),
|
||||
crate_check_cfg: CheckCfg::default(),
|
||||
input_path: None,
|
||||
output_dir: None,
|
||||
output_file: None,
|
||||
file_loader: None,
|
||||
|
|
@ -59,13 +59,12 @@ fn main() {
|
|||
rustc_interface::run_compiler(config, |compiler| {
|
||||
compiler.enter(|queries| {
|
||||
// TODO: add this to -Z unpretty
|
||||
let ast_krate = queries.parse().unwrap().take();
|
||||
let ast_krate = queries.parse().unwrap().get_mut().clone();
|
||||
for item in ast_krate.items {
|
||||
println!("{}", item_to_string(&item));
|
||||
}
|
||||
|
||||
// Analyze the crate and inspect the types under the cursor.
|
||||
queries.global_ctxt().unwrap().take().enter(|tcx| {
|
||||
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.
|
||||
|
|
@ -78,7 +77,7 @@ fn main() {
|
|||
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 = tcx.hir().local_def_id(item.hir_id()); // def_id identifies the main function
|
||||
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:?}");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
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: Jan 2023 --> `nightly-2022-12-19` (See [here][example]
|
||||
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
|
||||
|
|
@ -29,7 +29,7 @@ let config = rustc_interface::Config {
|
|||
};
|
||||
rustc_interface::run_compiler(config, |compiler| {
|
||||
compiler.enter(|queries| {
|
||||
queries.global_ctxt().unwrap().take().enter(|tcx| {
|
||||
queries.global_ctxt().unwrap().enter(|tcx| {
|
||||
// Run the analysis phase on the local crate to trigger the type error.
|
||||
let _ = tcx.analysis(());
|
||||
});
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
## 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: Jan 2023 --> `nightly-2022-12-19`
|
||||
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
|
||||
|
|
@ -22,7 +22,7 @@ let config = rustc_interface::Config {
|
|||
rustc_interface::run_compiler(config, |compiler| {
|
||||
compiler.enter(|queries| {
|
||||
// Analyze the crate and inspect the types under the cursor.
|
||||
queries.global_ctxt().unwrap().take().enter(|tcx| {
|
||||
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.
|
||||
|
|
@ -35,9 +35,9 @@ rustc_interface::run_compiler(config, |compiler| {
|
|||
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 = tcx.hir().local_def_id(item.hir_id()); // def_id identifies the main function
|
||||
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);
|
||||
println!("{expr:#?}: {ty:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue