Skip to content

Commit

Permalink
Update the rustc_interface examples for current rustc (#1974)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattheww committed May 10, 2024
1 parent 4b692c0 commit 610b875
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions examples/rustc-driver-example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn main() {
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
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>>
psess_created: None, //Option<Box<dyn FnOnce(&mut ParseSess) + Send>>
// 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.
//
Expand All @@ -60,7 +60,7 @@ fn main() {
// 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<'_>)>
// Registry of diagnostics codes.
registry: registry::Registry::new(rustc_error_codes::DIAGNOSTICS),
registry: registry::Registry::new(rustc_errors::codes::DIAGNOSTICS),
make_codegen_backend: None,
expanded_args: Vec::new(),
ice_file: None,
Expand Down
10 changes: 5 additions & 5 deletions examples/rustc-driver-getting-diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ impl Translate for DebugEmitter {
}

impl Emitter for DebugEmitter {
fn emit_diagnostic(&mut self, diag: &DiagInner) {
self.diagnostics.lock().unwrap().push(diag.clone());
fn emit_diagnostic(&mut self, diag: DiagInner) {
self.diagnostics.lock().unwrap().push(diag);
}

fn source_map(&self) -> Option<&Arc<SourceMap>> {
Expand Down Expand Up @@ -76,15 +76,15 @@ fn main() {
file_loader: None,
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
lint_caps: rustc_hash::FxHashMap::default(),
parse_sess_created: Some(Box::new(|parse_sess| {
parse_sess.dcx = DiagCtxt::with_emitter(Box::new(DebugEmitter {
psess_created: Some(Box::new(|parse_sess| {
parse_sess.dcx = DiagCtxt::new(Box::new(DebugEmitter {
source_map: parse_sess.clone_source_map(),
diagnostics,
}))
})),
register_lints: None,
override_queries: None,
registry: registry::Registry::new(rustc_error_codes::DIAGNOSTICS),
registry: registry::Registry::new(rustc_errors::codes::DIAGNOSTICS),
make_codegen_backend: None,
expanded_args: Vec::new(),
ice_file: None,
Expand Down
8 changes: 4 additions & 4 deletions examples/rustc-driver-interacting-with-the-ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ fn main() {
file_loader: None,
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
lint_caps: rustc_hash::FxHashMap::default(),
parse_sess_created: None,
psess_created: None,
register_lints: None,
override_queries: None,
make_codegen_backend: None,
registry: registry::Registry::new(rustc_error_codes::DIAGNOSTICS),
registry: registry::Registry::new(rustc_errors::codes::DIAGNOSTICS),
expanded_args: Vec::new(),
ice_file: None,
hash_untracked_state: None,
Expand All @@ -73,8 +73,8 @@ fn main() {
if let rustc_hir::ItemKind::Fn(_, _, body_id) = item.kind {
let expr = &tcx.hir().body(body_id).value;
if let rustc_hir::ExprKind::Block(block, _) = expr.kind {
if let rustc_hir::StmtKind::Local(local) = block.stmts[0].kind {
if let Some(expr) = local.init {
if let rustc_hir::StmtKind::Let(let_stmt) = block.stmts[0].kind {
if let Some(expr) = let_stmt.init {
let hir_id = expr.hir_id; // hir_id identifies the string "Hello, world!"
let def_id = item.hir_id().owner.def_id; // def_id identifies the main function
let ty = tcx.typeck(def_id).node_type(hir_id);
Expand Down
4 changes: 2 additions & 2 deletions src/rustc-driver-getting-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ otherwise be printed to stderr.
To get diagnostics from the compiler,
configure [`rustc_interface::Config`] to output diagnostic to a buffer,
and run [`TyCtxt.analysis`]. The following was tested
with <!-- date-check: jan 2024 --> `nightly-2024-01-19`:
with <!-- date-check: may 2024 --> `nightly-2024-05-09`:

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

[`rustc_interface`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/index.html
[`rustc_interface::Config`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/interface/struct.Config.html
[`TyCtxt.analysis`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/passes/fn.analysis.html
[`TyCtxt.analysis`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/passes/fn.analysis.html
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 @@ The [`rustc_interface`] allows you to interact with Rust code at various stages
## Getting the type of an expression

To get the type of an expression, use the [`global_ctxt`] query to [get] a [`TyCtxt`].
The following was tested with <!-- date-check: jan 2024 --> `nightly-2024-01-19`:
The following was tested with <!-- date-check: may 2024 --> `nightly-2024-05-09`:

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

0 comments on commit 610b875

Please sign in to comment.