Update rustc-driver related examples
This commit is contained in:
parent
f2a18eae27
commit
388142f167
|
|
@ -1,9 +1,9 @@
|
||||||
#![feature(rustc_private)]
|
#![feature(rustc_private)]
|
||||||
|
|
||||||
// NOTE: For the example to compile, you will need to first run the following:
|
// 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_error_codes;
|
||||||
extern crate rustc_errors;
|
extern crate rustc_errors;
|
||||||
|
|
@ -72,7 +72,8 @@ fn main() {
|
||||||
println!("{:#?}", parse);
|
println!("{:#?}", parse);
|
||||||
// Analyze the program and inspect the types of definitions.
|
// Analyze the program and inspect the types of definitions.
|
||||||
queries.global_ctxt().unwrap().take().enter(|tcx| {
|
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 {
|
match item.kind {
|
||||||
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn(_, _, _) => {
|
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn(_, _, _) => {
|
||||||
let name = item.ident;
|
let name = item.ident;
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
#![feature(rustc_private)]
|
#![feature(rustc_private)]
|
||||||
|
|
||||||
// NOTE: For the example to compile, you will need to first run the following:
|
// 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_error_codes;
|
||||||
extern crate rustc_errors;
|
extern crate rustc_errors;
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
#![feature(rustc_private)]
|
#![feature(rustc_private)]
|
||||||
|
|
||||||
// NOTE: For the example to compile, you will need to first run the following:
|
// 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_ast_pretty;
|
||||||
extern crate rustc_error_codes;
|
extern crate rustc_error_codes;
|
||||||
|
|
@ -66,7 +66,8 @@ fn main() {
|
||||||
// Every compilation contains a single crate.
|
// Every compilation contains a single crate.
|
||||||
let hir_krate = tcx.hir();
|
let hir_krate = tcx.hir();
|
||||||
// Iterate over the top-level items in the crate, looking for the main function.
|
// 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.
|
// Use pattern-matching to find a specific node inside the main function.
|
||||||
if let rustc_hir::ItemKind::Fn(_, _, body_id) = item.kind {
|
if let rustc_hir::ItemKind::Fn(_, _, body_id) = item.kind {
|
||||||
let expr = &tcx.hir().body(body_id).value;
|
let expr = &tcx.hir().body(body_id).value;
|
||||||
|
|
@ -76,7 +77,7 @@ fn main() {
|
||||||
let hir_id = expr.hir_id; // hir_id identifies the string "Hello, world!"
|
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 = tcx.hir().local_def_id(item.hir_id()); // def_id identifies the main function
|
||||||
let ty = tcx.typeck(def_id).node_type(hir_id);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
To get diagnostics from the compiler,
|
To get diagnostics from the compiler,
|
||||||
configure `rustc_interface::Config` to output diagnostic to a buffer,
|
configure `rustc_interface::Config` to output diagnostic to a buffer,
|
||||||
and run `TyCtxt.analysis`. The following should be compiled
|
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):
|
for the complete example):
|
||||||
|
|
||||||
[example]: https://github.com/rust-lang/rustc-dev-guide/blob/master/examples/rustc-driver-getting-diagnostics.rs
|
[example]: https://github.com/rust-lang/rustc-dev-guide/blob/master/examples/rustc-driver-getting-diagnostics.rs
|
||||||
|
|
@ -35,7 +35,7 @@ rustc_interface::run_compiler(config, |compiler| {
|
||||||
compiler.enter(|queries| {
|
compiler.enter(|queries| {
|
||||||
queries.global_ctxt().unwrap().take().enter(|tcx| {
|
queries.global_ctxt().unwrap().take().enter(|tcx| {
|
||||||
// Run the analysis phase on the local crate to trigger the type error.
|
// Run the analysis phase on the local crate to trigger the type error.
|
||||||
tcx.analysis(rustc_hir::def_id::LOCAL_CRATE);
|
let _ = tcx.analysis(());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
## Getting the type of an expression
|
## Getting the type of an expression
|
||||||
|
|
||||||
To get the type of an expression, use the `global_ctxt` to get a `TyCtxt`.
|
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):
|
(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
|
[example]: https://github.com/rust-lang/rustc-dev-guide/blob/master/examples/rustc-driver-interacting-with-the-ast.rs
|
||||||
|
|
@ -24,9 +24,10 @@ rustc_interface::run_compiler(config, |compiler| {
|
||||||
// Analyze the crate and inspect the types under the cursor.
|
// Analyze the crate and inspect the types under the cursor.
|
||||||
queries.global_ctxt().unwrap().take().enter(|tcx| {
|
queries.global_ctxt().unwrap().take().enter(|tcx| {
|
||||||
// Every compilation contains a single crate.
|
// 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.
|
// 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.
|
// Use pattern-matching to find a specific node inside the main function.
|
||||||
if let rustc_hir::ItemKind::Fn(_, _, body_id) = item.kind {
|
if let rustc_hir::ItemKind::Fn(_, _, body_id) = item.kind {
|
||||||
let expr = &tcx.hir().body(body_id).value;
|
let expr = &tcx.hir().body(body_id).value;
|
||||||
|
|
@ -36,7 +37,7 @@ rustc_interface::run_compiler(config, |compiler| {
|
||||||
let hir_id = expr.hir_id; // hir_id identifies the string "Hello, world!"
|
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 = tcx.hir().local_def_id(item.hir_id()); // def_id identifies the main function
|
||||||
let ty = tcx.typeck(def_id).node_type(hir_id);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ using the interface defined in the [`rustc_interface`] crate.
|
||||||
The `rustc_interface` crate provides external users with an (unstable) API
|
The `rustc_interface` crate provides external users with an (unstable) API
|
||||||
for running code at particular times during the compilation process, allowing
|
for running code at particular times during the compilation process, allowing
|
||||||
third parties to effectively use `rustc`'s internals as a library for
|
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]
|
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
|
function is the main entrypoint to the compiler. It takes a configuration for the compiler
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue