parent
66c56a44fd
commit
a51498f7ed
|
|
@ -9,12 +9,11 @@ extern crate rustc_interface;
|
||||||
extern crate rustc_session;
|
extern crate rustc_session;
|
||||||
extern crate rustc_span;
|
extern crate rustc_span;
|
||||||
|
|
||||||
use std::{path, process, str};
|
use std::{path, process, str, sync::Arc};
|
||||||
|
|
||||||
use rustc_errors::registry;
|
use rustc_errors::registry;
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::FxHashMap;
|
||||||
use rustc_session::config::{self, CheckCfg};
|
use rustc_session::config;
|
||||||
use rustc_span::source_map;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let out = process::Command::new("rustc")
|
let out = process::Command::new("rustc")
|
||||||
|
|
@ -30,10 +29,10 @@ fn main() {
|
||||||
..config::Options::default()
|
..config::Options::default()
|
||||||
},
|
},
|
||||||
// cfg! configuration in addition to the default ones
|
// cfg! configuration in addition to the default ones
|
||||||
crate_cfg: FxHashSet::default(), // FxHashSet<(String, Option<String>)>
|
crate_cfg: Vec::new(), // FxHashSet<(String, Option<String>)>
|
||||||
crate_check_cfg: CheckCfg::default(), // CheckCfg
|
crate_check_cfg: Vec::new(), // CheckCfg
|
||||||
input: config::Input::Str {
|
input: config::Input::Str {
|
||||||
name: source_map::FileName::Custom("main.rs".into()),
|
name: rustc_span::FileName::Custom("main.rs".into()),
|
||||||
input: r#"
|
input: r#"
|
||||||
static HELLO: &str = "Hello, world!";
|
static HELLO: &str = "Hello, world!";
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
@ -61,10 +60,12 @@ fn main() {
|
||||||
// The second parameter is local providers and the third parameter is external providers.
|
// The second parameter is local providers and the third parameter is external providers.
|
||||||
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,
|
make_codegen_backend: None,
|
||||||
expanded_args: Vec::new(),
|
expanded_args: Vec::new(),
|
||||||
ice_file: None,
|
ice_file: None,
|
||||||
|
hash_untracked_state: None,
|
||||||
|
using_internal_features: Arc::default(),
|
||||||
};
|
};
|
||||||
rustc_interface::run_compiler(config, |compiler| {
|
rustc_interface::run_compiler(config, |compiler| {
|
||||||
compiler.enter(|queries| {
|
compiler.enter(|queries| {
|
||||||
|
|
|
||||||
|
|
@ -10,27 +10,12 @@ extern crate rustc_session;
|
||||||
extern crate rustc_span;
|
extern crate rustc_span;
|
||||||
|
|
||||||
use rustc_errors::registry;
|
use rustc_errors::registry;
|
||||||
use rustc_session::config::{self, CheckCfg};
|
use rustc_session::config;
|
||||||
use rustc_span::source_map;
|
|
||||||
use std::io;
|
|
||||||
use std::path;
|
use std::path;
|
||||||
use std::process;
|
use std::process;
|
||||||
use std::str;
|
use std::str;
|
||||||
use std::sync;
|
use std::sync;
|
||||||
|
|
||||||
// Buffer diagnostics in a Vec<u8>.
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct DiagnosticSink(sync::Arc<sync::Mutex<Vec<u8>>>);
|
|
||||||
|
|
||||||
impl io::Write for DiagnosticSink {
|
|
||||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
||||||
self.0.lock().unwrap().write(buf)
|
|
||||||
}
|
|
||||||
fn flush(&mut self) -> io::Result<()> {
|
|
||||||
self.0.lock().unwrap().flush()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let out = process::Command::new("rustc")
|
let out = process::Command::new("rustc")
|
||||||
.arg("--print=sysroot")
|
.arg("--print=sysroot")
|
||||||
|
|
@ -53,7 +38,7 @@ fn main() {
|
||||||
},
|
},
|
||||||
// This program contains a type error.
|
// This program contains a type error.
|
||||||
input: config::Input::Str {
|
input: config::Input::Str {
|
||||||
name: source_map::FileName::Custom("main.rs".into()),
|
name: rustc_span::FileName::Custom("main.rs".into()),
|
||||||
input: "
|
input: "
|
||||||
fn main() {
|
fn main() {
|
||||||
let x: &str = 1;
|
let x: &str = 1;
|
||||||
|
|
@ -61,8 +46,8 @@ fn main() {
|
||||||
"
|
"
|
||||||
.into(),
|
.into(),
|
||||||
},
|
},
|
||||||
crate_cfg: rustc_hash::FxHashSet::default(),
|
crate_cfg: Vec::new(),
|
||||||
crate_check_cfg: CheckCfg::default(),
|
crate_check_cfg: Vec::new(),
|
||||||
output_dir: None,
|
output_dir: None,
|
||||||
output_file: None,
|
output_file: None,
|
||||||
file_loader: None,
|
file_loader: None,
|
||||||
|
|
@ -71,10 +56,12 @@ fn main() {
|
||||||
parse_sess_created: None,
|
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,
|
make_codegen_backend: None,
|
||||||
expanded_args: Vec::new(),
|
expanded_args: Vec::new(),
|
||||||
ice_file: None,
|
ice_file: None,
|
||||||
|
hash_untracked_state: None,
|
||||||
|
using_internal_features: sync::Arc::default(),
|
||||||
};
|
};
|
||||||
rustc_interface::run_compiler(config, |compiler| {
|
rustc_interface::run_compiler(config, |compiler| {
|
||||||
compiler.enter(|queries| {
|
compiler.enter(|queries| {
|
||||||
|
|
|
||||||
|
|
@ -10,12 +10,11 @@ extern crate rustc_interface;
|
||||||
extern crate rustc_session;
|
extern crate rustc_session;
|
||||||
extern crate rustc_span;
|
extern crate rustc_span;
|
||||||
|
|
||||||
use std::{path, process, str};
|
use std::{path, process, str, sync::Arc};
|
||||||
|
|
||||||
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::{self, CheckCfg};
|
use rustc_session::config;
|
||||||
use rustc_span::source_map;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let out = process::Command::new("rustc")
|
let out = process::Command::new("rustc")
|
||||||
|
|
@ -30,7 +29,7 @@ fn main() {
|
||||||
..config::Options::default()
|
..config::Options::default()
|
||||||
},
|
},
|
||||||
input: config::Input::Str {
|
input: config::Input::Str {
|
||||||
name: source_map::FileName::Custom("main.rs".to_string()),
|
name: rustc_span::FileName::Custom("main.rs".to_string()),
|
||||||
input: r#"
|
input: r#"
|
||||||
fn main() {
|
fn main() {
|
||||||
let message = "Hello, World!";
|
let message = "Hello, World!";
|
||||||
|
|
@ -39,8 +38,8 @@ fn main() {
|
||||||
"#
|
"#
|
||||||
.to_string(),
|
.to_string(),
|
||||||
},
|
},
|
||||||
crate_cfg: rustc_hash::FxHashSet::default(),
|
crate_cfg: Vec::new(),
|
||||||
crate_check_cfg: CheckCfg::default(),
|
crate_check_cfg: Vec::new(),
|
||||||
output_dir: None,
|
output_dir: None,
|
||||||
output_file: None,
|
output_file: None,
|
||||||
file_loader: None,
|
file_loader: None,
|
||||||
|
|
@ -50,9 +49,11 @@ fn main() {
|
||||||
register_lints: None,
|
register_lints: None,
|
||||||
override_queries: None,
|
override_queries: None,
|
||||||
make_codegen_backend: None,
|
make_codegen_backend: None,
|
||||||
registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
|
registry: registry::Registry::new(rustc_error_codes::DIAGNOSTICS),
|
||||||
expanded_args: Vec::new(),
|
expanded_args: Vec::new(),
|
||||||
ice_file: None,
|
ice_file: None,
|
||||||
|
hash_untracked_state: None,
|
||||||
|
using_internal_features: Arc::default(),
|
||||||
};
|
};
|
||||||
rustc_interface::run_compiler(config, |compiler| {
|
rustc_interface::run_compiler(config, |compiler| {
|
||||||
compiler.enter(|queries| {
|
compiler.enter(|queries| {
|
||||||
|
|
|
||||||
|
|
@ -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 was tested
|
and run `TyCtxt.analysis`. The following was tested
|
||||||
with <!-- date-check: oct 2023 --> `nightly-2023-10-03`:
|
with <!-- date-check: jan 2024 --> `nightly-2024-01-19`:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
{{#include ../examples/rustc-driver-getting-diagnostics.rs}}
|
{{#include ../examples/rustc-driver-getting-diagnostics.rs}}
|
||||||
|
|
|
||||||
|
|
@ -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 was tested with <!-- date-check: oct 2023 --> `nightly-2023-10-03`:
|
The following was tested with <!-- date-check: jan 2024 --> `nightly-2024-01-19`:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
{{#include ../examples/rustc-driver-interacting-with-the-ast.rs}}
|
{{#include ../examples/rustc-driver-interacting-with-the-ast.rs}}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue