Skip to content

Commit

Permalink
Fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Dec 27, 2024
1 parent b4188ed commit c029892
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 43 deletions.
33 changes: 19 additions & 14 deletions examples/rustc-driver-example.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
#![feature(rustc_private)]

extern crate rustc_ast;
extern crate rustc_ast_pretty;
extern crate rustc_data_structures;
extern crate rustc_driver;
extern crate rustc_error_codes;
extern crate rustc_errors;
extern crate rustc_hash;
extern crate rustc_hir;
extern crate rustc_interface;
extern crate rustc_middle;
extern crate rustc_session;
extern crate rustc_span;

use std::io;
use std::path::Path;
use std::str;
use std::sync::Arc;

use rustc_ast_pretty::pprust::item_to_string;
use rustc_data_structures::sync::Lrc;
use rustc_driver::Compilation;
use rustc_errors::registry;
use rustc_session::config;
use rustc_driver::{Compilation, RunCompiler};
use rustc_interface::interface::Compiler;
use rustc_middle::ty::TyCtxt;

struct MyFileLoader;

impl rustc_span::source_map::FileLoader for MyFileLoader {
fn file_exists(&self, path: &Path) -> bool {
path == "main.rs"
path == Path::new("main.rs")
}

fn read_file(&self, path: &Path) -> io::Result<String> {
if path == "main.rs" {
if path == Path::new("main.rs") {
Ok(r#"
fn main() {
let message = "Hello, World!";
Expand All @@ -42,7 +43,7 @@ fn main() {
}
}

fn read_binary_file(&self, path: &Path) -> io::Result<Lrc<[u8]>> {
fn read_binary_file(&self, _path: &Path) -> io::Result<Lrc<[u8]>> {
Err(io::Error::other("oops"))
}
}
Expand All @@ -51,17 +52,18 @@ struct MyCallbacks;

impl rustc_driver::Callbacks for MyCallbacks {
fn after_crate_root_parsing(
_compiler: &rustc_interface::Compiler,
&mut self,
_compiler: &Compiler,
krate: &rustc_ast::Crate,
) -> Compilation {
for item in krate.items {
for item in &krate.items {
println!("{}", item_to_string(&item));
}

Compilation::Continue
}

fn after_analysis(_compiler: &rustc_interface::Compiler, tcx: TyCtxt<'_>) -> Compilation {
fn after_analysis(&mut self, _compiler: &Compiler, tcx: TyCtxt<'_>) -> Compilation {
// Analyze the program and inspect the types of definitions.
for id in tcx.hir().items() {
let hir = tcx.hir();
Expand All @@ -81,7 +83,10 @@ impl rustc_driver::Callbacks for MyCallbacks {
}

fn main() {
RunCompiler::new(&["main.rs".to_string()], MyCallbacks)
.set_file_loader(Some(Box::new(MyFileLoader)))
.run();
match RunCompiler::new(&["main.rs".to_string()], &mut MyCallbacks) {
mut compiler => {
compiler.set_file_loader(Some(Box::new(MyFileLoader)));
compiler.run();
}
}
}
33 changes: 19 additions & 14 deletions examples/rustc-driver-interacting-with-the-ast.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
#![feature(rustc_private)]

extern crate rustc_ast;
extern crate rustc_ast_pretty;
extern crate rustc_data_structures;
extern crate rustc_driver;
extern crate rustc_error_codes;
extern crate rustc_errors;
extern crate rustc_hash;
extern crate rustc_hir;
extern crate rustc_interface;
extern crate rustc_middle;
extern crate rustc_session;
extern crate rustc_span;

use std::io;
use std::path::Path;
use std::str;
use std::sync::Arc;

use rustc_ast_pretty::pprust::item_to_string;
use rustc_data_structures::sync::Lrc;
use rustc_driver::Compilation;
use rustc_errors::registry;
use rustc_session::config;
use rustc_driver::{Compilation, RunCompiler};
use rustc_interface::interface::Compiler;
use rustc_middle::ty::TyCtxt;

struct MyFileLoader;

impl rustc_span::source_map::FileLoader for MyFileLoader {
fn file_exists(&self, path: &Path) -> bool {
path == "main.rs"
path == Path::new("main.rs")
}

fn read_file(&self, path: &Path) -> io::Result<String> {
if path == "main.rs" {
if path == Path::new("main.rs") {
Ok(r#"
fn main() {
let message = "Hello, World!";
Expand All @@ -42,7 +43,7 @@ fn main() {
}
}

fn read_binary_file(&self, path: &Path) -> io::Result<Lrc<[u8]>> {
fn read_binary_file(&self, _path: &Path) -> io::Result<Lrc<[u8]>> {
Err(io::Error::other("oops"))
}
}
Expand All @@ -51,17 +52,18 @@ struct MyCallbacks;

impl rustc_driver::Callbacks for MyCallbacks {
fn after_crate_root_parsing(
_compiler: &rustc_interface::Compiler,
&mut self,
_compiler: &Compiler,
krate: &rustc_ast::Crate,
) -> Compilation {
for item in krate.items {
for item in &krate.items {
println!("{}", item_to_string(&item));
}

Compilation::Continue
}

fn after_analysis(_compiler: &rustc_interface::Compiler, tcx: TyCtxt<'_>) -> Compilation {
fn after_analysis(&mut self, _compiler: &Compiler, tcx: TyCtxt<'_>) -> Compilation {
// Every compilation contains a single crate.
let hir_krate = tcx.hir();
// Iterate over the top-level items in the crate, looking for the main function.
Expand All @@ -88,7 +90,10 @@ impl rustc_driver::Callbacks for MyCallbacks {
}

fn main() {
RunCompiler::new(&["main.rs".to_string()], MyCallbacks)
.set_file_loader(Some(Box::new(MyFileLoader)))
.run();
match RunCompiler::new(&["main.rs".to_string()], &mut MyCallbacks) {
mut compiler => {
compiler.set_file_loader(Some(Box::new(MyFileLoader)));
compiler.run();
}
}
}
8 changes: 4 additions & 4 deletions examples/rustc-interface-example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern crate rustc_interface;
extern crate rustc_session;
extern crate rustc_span;

use std::{path, str, sync::Arc};
use std::sync::Arc;

use rustc_errors::registry;
use rustc_hash::FxHashMap;
Expand All @@ -35,7 +35,7 @@ fn main() {
output_dir: None, // Option<PathBuf>
output_file: None, // Option<PathBuf>
file_loader: None, // Option<Box<dyn FileLoader + Send + Sync>>
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES.to_owned(),
lint_caps: FxHashMap::default(), // FxHashMap<lint::LintId, lint::Level>
// This is a callback from the driver that is called when [`ParseSess`] is created.
psess_created: None, //Option<Box<dyn FnOnce(&mut ParseSess) + Send>>
Expand All @@ -60,8 +60,8 @@ fn main() {
};
rustc_interface::run_compiler(config, |compiler| {
// Parse the program and print the syntax tree.
let parse = rustc_interface::passes::parse(&compiler.sess);
println!("{parse:?}");
let krate = rustc_interface::passes::parse(&compiler.sess);
println!("{krate:?}");
// Analyze the program and inspect the types of definitions.
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
for id in tcx.hir().items() {
Expand Down
21 changes: 10 additions & 11 deletions examples/rustc-interface-getting-diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(rustc_private)]

extern crate rustc_data_structures;
extern crate rustc_driver;
extern crate rustc_error_codes;
extern crate rustc_errors;
Expand All @@ -9,24 +10,22 @@ extern crate rustc_interface;
extern crate rustc_session;
extern crate rustc_span;

use rustc_errors::{
emitter::Emitter, registry, translation::Translate, DiagCtxt, DiagInner, FluentBundle,
};
use rustc_errors::emitter::Emitter;
use rustc_errors::registry::{self, Registry};
use rustc_errors::translation::Translate;
use rustc_errors::{DiagCtxt, DiagInner, FluentBundle};
use rustc_session::config;
use rustc_span::source_map::SourceMap;

use std::{
path, str,
sync::{Arc, Mutex},
};
use std::sync::{Arc, Mutex};

struct DebugEmitter {
source_map: Arc<SourceMap>,
diagnostics: Arc<Mutex<Vec<DiagInner>>>,
}

impl Translate for DebugEmitter {
fn fluent_bundle(&self) -> Option<&Arc<FluentBundle>> {
fn fluent_bundle(&self) -> Option<&FluentBundle> {
None
}

Expand All @@ -36,11 +35,11 @@ impl Translate for DebugEmitter {
}

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

fn source_map(&self) -> Option<&Arc<SourceMap>> {
fn source_map(&self) -> Option<&SourceMap> {
Some(&self.source_map)
}
}
Expand All @@ -65,7 +64,7 @@ fn main() {
output_dir: None,
output_file: None,
file_loader: None,
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES.to_owned(),
lint_caps: rustc_hash::FxHashMap::default(),
psess_created: Some(Box::new(|parse_sess| {
parse_sess.set_dcx(DiagCtxt::new(Box::new(DebugEmitter {
Expand Down

0 comments on commit c029892

Please sign in to comment.