Skip to content

Commit c2717f6

Browse files
Update examples (#1856)
Co-authored-by: Yuki Okushi <jtitor@2k36.org>
1 parent 4b5116a commit c2717f6

5 files changed

+26
-37
lines changed

examples/rustc-driver-example.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ extern crate rustc_interface;
99
extern crate rustc_session;
1010
extern crate rustc_span;
1111

12-
use std::{path, process, str};
12+
use std::{path, process, str, sync::Arc};
1313

1414
use rustc_errors::registry;
15-
use rustc_hash::{FxHashMap, FxHashSet};
16-
use rustc_session::config::{self, CheckCfg};
17-
use rustc_span::source_map;
15+
use rustc_hash::FxHashMap;
16+
use rustc_session::config;
1817

1918
fn main() {
2019
let out = process::Command::new("rustc")
@@ -30,10 +29,10 @@ fn main() {
3029
..config::Options::default()
3130
},
3231
// cfg! configuration in addition to the default ones
33-
crate_cfg: FxHashSet::default(), // FxHashSet<(String, Option<String>)>
34-
crate_check_cfg: CheckCfg::default(), // CheckCfg
32+
crate_cfg: Vec::new(), // FxHashSet<(String, Option<String>)>
33+
crate_check_cfg: Vec::new(), // CheckCfg
3534
input: config::Input::Str {
36-
name: source_map::FileName::Custom("main.rs".into()),
35+
name: rustc_span::FileName::Custom("main.rs".into()),
3736
input: r#"
3837
static HELLO: &str = "Hello, world!";
3938
fn main() {
@@ -61,10 +60,12 @@ fn main() {
6160
// The second parameter is local providers and the third parameter is external providers.
6261
override_queries: None, // Option<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)>
6362
// Registry of diagnostics codes.
64-
registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
63+
registry: registry::Registry::new(rustc_error_codes::DIAGNOSTICS),
6564
make_codegen_backend: None,
6665
expanded_args: Vec::new(),
6766
ice_file: None,
67+
hash_untracked_state: None,
68+
using_internal_features: Arc::default(),
6869
};
6970
rustc_interface::run_compiler(config, |compiler| {
7071
compiler.enter(|queries| {

examples/rustc-driver-getting-diagnostics.rs

+7-20
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,12 @@ extern crate rustc_session;
1010
extern crate rustc_span;
1111

1212
use rustc_errors::registry;
13-
use rustc_session::config::{self, CheckCfg};
14-
use rustc_span::source_map;
15-
use std::io;
13+
use rustc_session::config;
1614
use std::path;
1715
use std::process;
1816
use std::str;
1917
use std::sync;
2018

21-
// Buffer diagnostics in a Vec<u8>.
22-
#[derive(Clone)]
23-
pub struct DiagnosticSink(sync::Arc<sync::Mutex<Vec<u8>>>);
24-
25-
impl io::Write for DiagnosticSink {
26-
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
27-
self.0.lock().unwrap().write(buf)
28-
}
29-
fn flush(&mut self) -> io::Result<()> {
30-
self.0.lock().unwrap().flush()
31-
}
32-
}
33-
3419
fn main() {
3520
let out = process::Command::new("rustc")
3621
.arg("--print=sysroot")
@@ -53,16 +38,16 @@ fn main() {
5338
},
5439
// This program contains a type error.
5540
input: config::Input::Str {
56-
name: source_map::FileName::Custom("main.rs".into()),
41+
name: rustc_span::FileName::Custom("main.rs".into()),
5742
input: "
5843
fn main() {
5944
let x: &str = 1;
6045
}
6146
"
6247
.into(),
6348
},
64-
crate_cfg: rustc_hash::FxHashSet::default(),
65-
crate_check_cfg: CheckCfg::default(),
49+
crate_cfg: Vec::new(),
50+
crate_check_cfg: Vec::new(),
6651
output_dir: None,
6752
output_file: None,
6853
file_loader: None,
@@ -71,10 +56,12 @@ fn main() {
7156
parse_sess_created: None,
7257
register_lints: None,
7358
override_queries: None,
74-
registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
59+
registry: registry::Registry::new(rustc_error_codes::DIAGNOSTICS),
7560
make_codegen_backend: None,
7661
expanded_args: Vec::new(),
7762
ice_file: None,
63+
hash_untracked_state: None,
64+
using_internal_features: sync::Arc::default(),
7865
};
7966
rustc_interface::run_compiler(config, |compiler| {
8067
compiler.enter(|queries| {

examples/rustc-driver-interacting-with-the-ast.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ extern crate rustc_interface;
1010
extern crate rustc_session;
1111
extern crate rustc_span;
1212

13-
use std::{path, process, str};
13+
use std::{path, process, str, sync::Arc};
1414

1515
use rustc_ast_pretty::pprust::item_to_string;
1616
use rustc_errors::registry;
17-
use rustc_session::config::{self, CheckCfg};
18-
use rustc_span::source_map;
17+
use rustc_session::config;
1918

2019
fn main() {
2120
let out = process::Command::new("rustc")
@@ -30,7 +29,7 @@ fn main() {
3029
..config::Options::default()
3130
},
3231
input: config::Input::Str {
33-
name: source_map::FileName::Custom("main.rs".to_string()),
32+
name: rustc_span::FileName::Custom("main.rs".to_string()),
3433
input: r#"
3534
fn main() {
3635
let message = "Hello, World!";
@@ -39,8 +38,8 @@ fn main() {
3938
"#
4039
.to_string(),
4140
},
42-
crate_cfg: rustc_hash::FxHashSet::default(),
43-
crate_check_cfg: CheckCfg::default(),
41+
crate_cfg: Vec::new(),
42+
crate_check_cfg: Vec::new(),
4443
output_dir: None,
4544
output_file: None,
4645
file_loader: None,
@@ -50,9 +49,11 @@ fn main() {
5049
register_lints: None,
5150
override_queries: None,
5251
make_codegen_backend: None,
53-
registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
52+
registry: registry::Registry::new(rustc_error_codes::DIAGNOSTICS),
5453
expanded_args: Vec::new(),
5554
ice_file: None,
55+
hash_untracked_state: None,
56+
using_internal_features: Arc::default(),
5657
};
5758
rustc_interface::run_compiler(config, |compiler| {
5859
compiler.enter(|queries| {

src/rustc-driver-getting-diagnostics.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
To get diagnostics from the compiler,
88
configure `rustc_interface::Config` to output diagnostic to a buffer,
99
and run `TyCtxt.analysis`. The following was tested
10-
with <!-- date-check: oct 2023 --> `nightly-2023-10-03`:
10+
with <!-- date-check: jan 2024 --> `nightly-2024-01-19`:
1111

1212
```rust
1313
{{#include ../examples/rustc-driver-getting-diagnostics.rs}}

src/rustc-driver-interacting-with-the-ast.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## Getting the type of an expression
66

77
To get the type of an expression, use the `global_ctxt` to get a `TyCtxt`.
8-
The following was tested with <!-- date-check: oct 2023 --> `nightly-2023-10-03`:
8+
The following was tested with <!-- date-check: jan 2024 --> `nightly-2024-01-19`:
99

1010
```rust
1111
{{#include ../examples/rustc-driver-interacting-with-the-ast.rs}}

0 commit comments

Comments
 (0)