Skip to content

Update rustc-driver related examples #1345

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

Merged
merged 1 commit into from
May 30, 2022
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
7 changes: 4 additions & 3 deletions examples/rustc-driver-example.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![feature(rustc_private)]

// NOTE: For the example to compile, you will need to first run the following:
// rustup component add rustc-dev
// rustup component add rustc-dev llvm-tools-preview

// version: 1.61.0-nightly (68369a041 2022-02-22)
// version: 1.62.0-nightly (7c4b47696 2022-04-30)

extern crate rustc_error_codes;
extern crate rustc_errors;
Expand Down Expand Up @@ -72,7 +72,8 @@ fn main() {
println!("{:#?}", parse);
// Analyze the program and inspect the types of definitions.
queries.global_ctxt().unwrap().take().enter(|tcx| {
for item in tcx.hir().items() {
for id in tcx.hir().items() {
let item = tcx.hir().item(id);
match item.kind {
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn(_, _, _) => {
let name = item.ident;
Expand Down
4 changes: 2 additions & 2 deletions examples/rustc-driver-getting-diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![feature(rustc_private)]

// NOTE: For the example to compile, you will need to first run the following:
// rustup component add rustc-dev
// rustup component add rustc-dev llvm-tools-preview

// version: 1.61.0-nightly (68369a041 2022-02-22)
// version: 1.62.0-nightly (7c4b47696 2022-04-30)

extern crate rustc_error_codes;
extern crate rustc_errors;
Expand Down
9 changes: 5 additions & 4 deletions examples/rustc-driver-interacting-with-the-ast.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![feature(rustc_private)]

// NOTE: For the example to compile, you will need to first run the following:
// rustup component add rustc-dev llvm-tools-preview
// rustup component add rustc-dev llvm-tools-preview

// version: 1.61.0-nightly (68369a041 2022-02-22)
// version: 1.62.0-nightly (7c4b47696 2022-04-30)

extern crate rustc_ast_pretty;
extern crate rustc_error_codes;
Expand Down Expand Up @@ -66,7 +66,8 @@ fn main() {
// 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 item in hir_krate.items() {
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;
Expand All @@ -76,7 +77,7 @@ fn main() {
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 ty = tcx.typeck(def_id).node_type(hir_id);
println!("{:?}: {:?}", expr, ty); // prints expr(HirId { owner: DefIndex(3), local_id: 4 }: "Hello, world!"): &'static str
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 should be compiled
with <!-- date: 2021-03 --> `nightly-2021-03-28` (See [here][example]
with <!-- date: 2022-05 --> `nightly-2021-04-30` (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 Down Expand Up @@ -35,7 +35,7 @@ rustc_interface::run_compiler(config, |compiler| {
compiler.enter(|queries| {
queries.global_ctxt().unwrap().take().enter(|tcx| {
// Run the analysis phase on the local crate to trigger the type error.
tcx.analysis(rustc_hir::def_id::LOCAL_CRATE);
let _ = tcx.analysis(());
});
});
});
Expand Down
9 changes: 5 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 should be compiled with <!-- date: 2021-03 --> `nightly-2021-03-28`
The following should be compiled with <!-- date: 2022-05 --> `nightly-2022-04-30`
(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 @@ -24,9 +24,10 @@ rustc_interface::run_compiler(config, |compiler| {
// Analyze the crate and inspect the types under the cursor.
queries.global_ctxt().unwrap().take().enter(|tcx| {
// Every compilation contains a single crate.
let hir_krate = tcx.hir().krate();
let hir_krate = tcx.hir();
// Iterate over the top-level items in the crate, looking for the main function.
for (_, item) in &hir_krate.items {
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;
Expand All @@ -36,7 +37,7 @@ rustc_interface::run_compiler(config, |compiler| {
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 ty = tcx.typeck(def_id).node_type(hir_id);
println!("{:?}: {:?}", expr, ty); // prints expr(HirId { owner: DefIndex(3), local_id: 4 }: "Hello, world!"): &'static str
println!("{:?}: {:?}", expr, ty);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rustc-driver.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using the interface defined in the [`rustc_interface`] crate.
The `rustc_interface` crate provides external users with an (unstable) API
for running code at particular times during the compilation process, allowing
third parties to effectively use `rustc`'s internals as a library for
analysing a crate or emulating the compiler in-process (e.g. the RLS or rustdoc).
analyzing a crate or emulating the compiler in-process (e.g. the RLS or rustdoc).

For those using `rustc` as a library, the [`rustc_interface::run_compiler()`][i_rc]
function is the main entrypoint to the compiler. It takes a configuration for the compiler
Expand Down