Update rustc-driver-*.rs examples (#1095)
This commit is contained in:
parent
081624ceaa
commit
64504ada90
|
|
@ -3,6 +3,8 @@
|
||||||
// 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
|
||||||
|
|
||||||
|
// version: 1.53.0-nightly (9b0edb7fd 2021-03-27)
|
||||||
|
|
||||||
extern crate rustc_error_codes;
|
extern crate rustc_error_codes;
|
||||||
extern crate rustc_errors;
|
extern crate rustc_errors;
|
||||||
extern crate rustc_hash;
|
extern crate rustc_hash;
|
||||||
|
|
@ -46,8 +48,9 @@ fn main() {
|
||||||
diagnostic_output: rustc_session::DiagnosticOutput::Default,
|
diagnostic_output: rustc_session::DiagnosticOutput::Default,
|
||||||
// Set to capture stderr output during compiler execution
|
// Set to capture stderr output during compiler execution
|
||||||
stderr: None, // Option<Arc<Mutex<Vec<u8>>>>
|
stderr: None, // Option<Arc<Mutex<Vec<u8>>>>
|
||||||
crate_name: None, // Option<String>
|
|
||||||
lint_caps: FxHashMap::default(), // FxHashMap<lint::LintId, lint::Level>
|
lint_caps: FxHashMap::default(), // FxHashMap<lint::LintId, lint::Level>
|
||||||
|
// This is a callback from the driver that is called when [`ParseSess`] is created.
|
||||||
|
parse_sess_created: None, //Option<Box<dyn FnOnce(&mut ParseSess) + Send>>
|
||||||
// This is a callback from the driver that is called when we're registering lints;
|
// This is a callback from the driver that is called when we're registering lints;
|
||||||
// it is called during plugin registration when we have the LintStore in a non-shared state.
|
// it is called during plugin registration when we have the LintStore in a non-shared state.
|
||||||
//
|
//
|
||||||
|
|
@ -61,6 +64,7 @@ fn main() {
|
||||||
override_queries: None, // Option<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)>
|
override_queries: None, // Option<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)>
|
||||||
// Registry of diagnostics codes.
|
// Registry of diagnostics codes.
|
||||||
registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
|
registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
|
||||||
|
make_codegen_backend: None,
|
||||||
};
|
};
|
||||||
rustc_interface::run_compiler(config, |compiler| {
|
rustc_interface::run_compiler(config, |compiler| {
|
||||||
compiler.enter(|queries| {
|
compiler.enter(|queries| {
|
||||||
|
|
@ -73,7 +77,7 @@ fn main() {
|
||||||
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;
|
||||||
let ty = tcx.type_of(tcx.hir().local_def_id(item.hir_id));
|
let ty = tcx.type_of(tcx.hir().local_def_id(item.hir_id()));
|
||||||
println!("{:?}:\t{:?}", name, ty)
|
println!("{:?}:\t{:?}", name, ty)
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@
|
||||||
// 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
|
||||||
|
|
||||||
|
// version: 1.53.0-nightly (9b0edb7fd 2021-03-27)
|
||||||
|
|
||||||
extern crate rustc_error_codes;
|
extern crate rustc_error_codes;
|
||||||
extern crate rustc_errors;
|
extern crate rustc_errors;
|
||||||
extern crate rustc_hash;
|
extern crate rustc_hash;
|
||||||
|
|
@ -68,11 +70,12 @@ fn main() {
|
||||||
output_file: None,
|
output_file: None,
|
||||||
file_loader: None,
|
file_loader: None,
|
||||||
stderr: None,
|
stderr: None,
|
||||||
crate_name: None,
|
|
||||||
lint_caps: rustc_hash::FxHashMap::default(),
|
lint_caps: rustc_hash::FxHashMap::default(),
|
||||||
|
parse_sess_created: None,
|
||||||
register_lints: None,
|
register_lints: None,
|
||||||
override_queries: None,
|
override_queries: None,
|
||||||
registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
|
registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
|
||||||
|
make_codegen_backend: None,
|
||||||
};
|
};
|
||||||
rustc_interface::run_compiler(config, |compiler| {
|
rustc_interface::run_compiler(config, |compiler| {
|
||||||
compiler.enter(|queries| {
|
compiler.enter(|queries| {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@
|
||||||
// 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.53.0-nightly (9b0edb7fd 2021-03-27)
|
||||||
|
|
||||||
extern crate rustc_ast_pretty;
|
extern crate rustc_ast_pretty;
|
||||||
extern crate rustc_error_codes;
|
extern crate rustc_error_codes;
|
||||||
extern crate rustc_errors;
|
extern crate rustc_errors;
|
||||||
|
|
@ -15,8 +17,6 @@ extern crate rustc_span;
|
||||||
use rustc_ast_pretty::pprust::item_to_string;
|
use rustc_ast_pretty::pprust::item_to_string;
|
||||||
use rustc_errors::registry;
|
use rustc_errors::registry;
|
||||||
use rustc_session::config;
|
use rustc_session::config;
|
||||||
use rustc_session::config::PpMode::PpmSource;
|
|
||||||
use rustc_session::config::PpSourceMode::PpmExpanded;
|
|
||||||
use rustc_span::source_map;
|
use rustc_span::source_map;
|
||||||
use std::path;
|
use std::path;
|
||||||
use std::process;
|
use std::process;
|
||||||
|
|
@ -46,8 +46,8 @@ fn main() {
|
||||||
output_file: None,
|
output_file: None,
|
||||||
file_loader: None,
|
file_loader: None,
|
||||||
stderr: None,
|
stderr: None,
|
||||||
crate_name: None,
|
|
||||||
lint_caps: rustc_hash::FxHashMap::default(),
|
lint_caps: rustc_hash::FxHashMap::default(),
|
||||||
|
parse_sess_created: None,
|
||||||
register_lints: None,
|
register_lints: None,
|
||||||
override_queries: None,
|
override_queries: None,
|
||||||
make_codegen_backend: None,
|
make_codegen_backend: None,
|
||||||
|
|
@ -57,8 +57,7 @@ fn main() {
|
||||||
compiler.enter(|queries| {
|
compiler.enter(|queries| {
|
||||||
// TODO: add this to -Z unpretty
|
// TODO: add this to -Z unpretty
|
||||||
let ast_krate = queries.parse().unwrap().take();
|
let ast_krate = queries.parse().unwrap().take();
|
||||||
let ast_krate_mod = ast_krate.module;
|
for item in ast_krate.items {
|
||||||
for item in ast_krate_mod.items {
|
|
||||||
println!("{}", item_to_string(&item));
|
println!("{}", item_to_string(&item));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -75,7 +74,7 @@ fn main() {
|
||||||
if let rustc_hir::StmtKind::Local(local) = block.stmts[0].kind {
|
if let rustc_hir::StmtKind::Local(local) = block.stmts[0].kind {
|
||||||
if let Some(expr) = local.init {
|
if let Some(expr) = local.init {
|
||||||
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); // prints expr(HirId { owner: DefIndex(3), local_id: 4 }: "Hello, world!"): &'static str
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue