Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update examples for rustc 1.69.0-nightly (e1eaa2d5d 2023-02-06) #1590

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/rustc-driver-example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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>>
Expand All @@ -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:?}")
}
_ => (),
Expand Down
4 changes: 2 additions & 2 deletions examples/rustc-driver-getting-diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand All @@ -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(());
});
Expand Down
9 changes: 4 additions & 5 deletions examples/rustc-driver-interacting-with-the-ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand All @@ -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:?}");
}
Expand Down
4 changes: 2 additions & 2 deletions src/rustc-driver-getting-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(());
});
Expand Down
8 changes: 4 additions & 4 deletions src/rustc-driver-interacting-with-the-ast.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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:?}");
}
}
}
Expand Down