diff --git a/examples/rustc-driver-example.rs b/examples/rustc-driver-example.rs index dc3bf98ce..576bbcea9 100644 --- a/examples/rustc-driver-example.rs +++ b/examples/rustc-driver-example.rs @@ -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 { - if path == "main.rs" { + if path == Path::new("main.rs") { Ok(r#" fn main() { let message = "Hello, World!"; @@ -42,7 +43,7 @@ fn main() { } } - fn read_binary_file(&self, path: &Path) -> io::Result> { + fn read_binary_file(&self, _path: &Path) -> io::Result> { Err(io::Error::other("oops")) } } @@ -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(); @@ -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(); + } + } } diff --git a/examples/rustc-driver-interacting-with-the-ast.rs b/examples/rustc-driver-interacting-with-the-ast.rs index 29f67adde..90a85d5db 100644 --- a/examples/rustc-driver-interacting-with-the-ast.rs +++ b/examples/rustc-driver-interacting-with-the-ast.rs @@ -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 { - if path == "main.rs" { + if path == Path::new("main.rs") { Ok(r#" fn main() { let message = "Hello, World!"; @@ -42,7 +43,7 @@ fn main() { } } - fn read_binary_file(&self, path: &Path) -> io::Result> { + fn read_binary_file(&self, _path: &Path) -> io::Result> { Err(io::Error::other("oops")) } } @@ -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. @@ -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(); + } + } } diff --git a/examples/rustc-interface-example.rs b/examples/rustc-interface-example.rs index e249e4da0..30f48ea52 100644 --- a/examples/rustc-interface-example.rs +++ b/examples/rustc-interface-example.rs @@ -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; @@ -35,7 +35,7 @@ fn main() { output_dir: None, // Option output_file: None, // Option file_loader: None, // Option> - locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES, + locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES.to_owned(), lint_caps: FxHashMap::default(), // FxHashMap // This is a callback from the driver that is called when [`ParseSess`] is created. psess_created: None, //Option> @@ -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() { diff --git a/examples/rustc-interface-getting-diagnostics.rs b/examples/rustc-interface-getting-diagnostics.rs index 45f2eacca..be37dd867 100644 --- a/examples/rustc-interface-getting-diagnostics.rs +++ b/examples/rustc-interface-getting-diagnostics.rs @@ -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; @@ -9,16 +10,14 @@ 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, @@ -26,7 +25,7 @@ struct DebugEmitter { } impl Translate for DebugEmitter { - fn fluent_bundle(&self) -> Option<&Arc> { + fn fluent_bundle(&self) -> Option<&FluentBundle> { None } @@ -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> { + fn source_map(&self) -> Option<&SourceMap> { Some(&self.source_map) } } @@ -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 {