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

improve rustc_interface examples a little #1362

Merged
merged 1 commit into from
Jun 6, 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
25 changes: 15 additions & 10 deletions examples/rustc-driver-example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ extern crate rustc_interface;
extern crate rustc_session;
extern crate rustc_span;

use std::{path, process, str};

use rustc_errors::registry;
use rustc_hash::{FxHashMap, FxHashSet};
use rustc_session::config::{self, CheckCfg};
use rustc_span::source_map;
use std::path;
use std::process;
use std::str;

fn main() {
let out = process::Command::new("rustc")
Expand All @@ -38,9 +37,14 @@ fn main() {
crate_cfg: FxHashSet::default(), // FxHashSet<(String, Option<String>)>
crate_check_cfg: CheckCfg::default(), // CheckCfg
input: config::Input::Str {
name: source_map::FileName::Custom("main.rs".to_string()),
input: "static HELLO: &str = \"Hello, world!\"; fn main() { println!(\"{}\", HELLO); }"
.to_string(),
name: source_map::FileName::Custom("main.rs".into()),
input: r#"
static HELLO: &str = "Hello, world!";
fn main() {
println!("{HELLO}");
}
"#
.into(),
},
input_path: None, // Option<PathBuf>
output_dir: None, // Option<PathBuf>
Expand Down Expand Up @@ -69,16 +73,17 @@ fn main() {
compiler.enter(|queries| {
// Parse the program and print the syntax tree.
let parse = queries.parse().unwrap().take();
println!("{:#?}", parse);
println!("{parse:?}");
// Analyze the program and inspect the types of definitions.
queries.global_ctxt().unwrap().take().enter(|tcx| {
for id in tcx.hir().items() {
let item = tcx.hir().item(id);
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(tcx.hir().local_def_id(item.hir_id()));
println!("{:?}:\t{:?}", name, ty)
let ty = tcx.type_of(hir.local_def_id(item.hir_id()));
println!("{name:?}:\t{ty:?}")
}
_ => (),
}
Expand Down
11 changes: 8 additions & 3 deletions examples/rustc-driver-getting-diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ fn main() {
},
// This program contains a type error.
input: config::Input::Str {
name: source_map::FileName::Custom("main.rs".to_string()),
input: "fn main() { let x: &str = 1; }".to_string(),
name: source_map::FileName::Custom("main.rs".into()),
input: "
fn main() {
let x: &str = 1;
}
"
.into(),
},
// Redirect the diagnostic output of the compiler to a buffer.
diagnostic_output: rustc_session::DiagnosticOutput::Raw(Box::from(DiagnosticSink(
Expand Down Expand Up @@ -87,5 +92,5 @@ fn main() {
});
// Read buffered diagnostics.
let diagnostics = String::from_utf8(buffer.lock().unwrap().clone()).unwrap();
println!("{}", diagnostics);
println!("{diagnostics}");
}
16 changes: 10 additions & 6 deletions examples/rustc-driver-interacting-with-the-ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ extern crate rustc_interface;
extern crate rustc_session;
extern crate rustc_span;

use std::{path, process, str};

use rustc_ast_pretty::pprust::item_to_string;
use rustc_errors::registry;
use rustc_session::config::{self, CheckCfg};
use rustc_span::source_map;
use std::path;
use std::process;
use std::str;

fn main() {
let out = process::Command::new("rustc")
Expand All @@ -36,8 +35,13 @@ fn main() {
},
input: config::Input::Str {
name: source_map::FileName::Custom("main.rs".to_string()),
input: "fn main() { let message = \"Hello, world!\"; println!(\"{}\", message); }"
.to_string(),
input: r#"
fn main() {
let message = "Hello, World!";
println!("{message}");
}
"#
.to_string(),
},
diagnostic_output: rustc_session::DiagnosticOutput::Default,
crate_cfg: rustc_hash::FxHashSet::default(),
Expand Down Expand Up @@ -77,7 +81,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);
println!("{expr:#?}: {ty:?}");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion 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: 2022-05 --> `nightly-2021-04-30` (See [here][example]
with <!-- date: 2022-06 --> `nightly-2022-06-05` (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
2 changes: 1 addition & 1 deletion 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: 2022-05 --> `nightly-2022-04-30`
The following should be compiled with <!-- date: 2022-06 --> `nightly-2022-06-05`
(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 Down