summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_driver_impl/src
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_driver_impl/src')
-rw-r--r--compiler/rustc_driver_impl/src/lib.rs194
-rw-r--r--compiler/rustc_driver_impl/src/pretty.rs408
-rw-r--r--compiler/rustc_driver_impl/src/session_diagnostics.rs10
3 files changed, 262 insertions, 350 deletions
diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs
index 65c7aed3f..84ae45d6a 100644
--- a/compiler/rustc_driver_impl/src/lib.rs
+++ b/compiler/rustc_driver_impl/src/lib.rs
@@ -5,10 +5,13 @@
//! This API is completely unstable and subject to change.
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
-#![feature(lazy_cell)]
+#![cfg_attr(not(bootstrap), doc(rust_logo))]
+#![cfg_attr(not(bootstrap), feature(rustdoc_internals))]
+#![cfg_attr(not(bootstrap), allow(internal_features))]
#![feature(decl_macro)]
-#![feature(panic_update_hook)]
+#![feature(lazy_cell)]
#![feature(let_chains)]
+#![feature(panic_update_hook)]
#![recursion_limit = "256"]
#![allow(rustc::potential_query_instability)]
#![deny(rustc::untranslatable_diagnostic)]
@@ -17,8 +20,6 @@
#[macro_use]
extern crate tracing;
-pub extern crate rustc_plugin_impl as plugin;
-
use rustc_ast as ast;
use rustc_codegen_ssa::{traits::CodegenBackend, CodegenErrors, CodegenResults};
use rustc_data_structures::profiling::{
@@ -40,8 +41,10 @@ use rustc_session::cstore::MetadataLoader;
use rustc_session::getopts::{self, Matches};
use rustc_session::lint::{Lint, LintId};
use rustc_session::{config, EarlyErrorHandler, Session};
-use rustc_span::source_map::{FileLoader, FileName};
+use rustc_span::def_id::LOCAL_CRATE;
+use rustc_span::source_map::FileLoader;
use rustc_span::symbol::sym;
+use rustc_span::FileName;
use rustc_target::json::ToJson;
use rustc_target::spec::{Target, TargetTriple};
@@ -57,9 +60,8 @@ use std::path::PathBuf;
use std::process::{self, Command, Stdio};
use std::str;
use std::sync::atomic::{AtomicBool, Ordering};
-use std::sync::OnceLock;
+use std::sync::{Arc, OnceLock};
use std::time::{Instant, SystemTime};
-use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime;
#[allow(unused_macros)]
@@ -129,12 +131,10 @@ pub static DEFAULT_LOCALE_RESOURCES: &[&str] = &[
rustc_monomorphize::DEFAULT_LOCALE_RESOURCE,
rustc_parse::DEFAULT_LOCALE_RESOURCE,
rustc_passes::DEFAULT_LOCALE_RESOURCE,
- rustc_plugin_impl::DEFAULT_LOCALE_RESOURCE,
rustc_privacy::DEFAULT_LOCALE_RESOURCE,
rustc_query_system::DEFAULT_LOCALE_RESOURCE,
rustc_resolve::DEFAULT_LOCALE_RESOURCE,
rustc_session::DEFAULT_LOCALE_RESOURCE,
- rustc_symbol_mangling::DEFAULT_LOCALE_RESOURCE,
rustc_trait_selection::DEFAULT_LOCALE_RESOURCE,
rustc_ty_utils::DEFAULT_LOCALE_RESOURCE,
// tidy-alphabetical-end
@@ -221,11 +221,18 @@ pub struct RunCompiler<'a, 'b> {
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
make_codegen_backend:
Option<Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>>,
+ using_internal_features: Arc<std::sync::atomic::AtomicBool>,
}
impl<'a, 'b> RunCompiler<'a, 'b> {
pub fn new(at_args: &'a [String], callbacks: &'b mut (dyn Callbacks + Send)) -> Self {
- Self { at_args, callbacks, file_loader: None, make_codegen_backend: None }
+ Self {
+ at_args,
+ callbacks,
+ file_loader: None,
+ make_codegen_backend: None,
+ using_internal_features: Arc::default(),
+ }
}
/// Set a custom codegen backend.
@@ -257,9 +264,23 @@ impl<'a, 'b> RunCompiler<'a, 'b> {
self
}
+ /// Set the session-global flag that checks whether internal features have been used,
+ /// suppressing the message about submitting an issue in ICEs when enabled.
+ #[must_use]
+ pub fn set_using_internal_features(mut self, using_internal_features: Arc<AtomicBool>) -> Self {
+ self.using_internal_features = using_internal_features;
+ self
+ }
+
/// Parse args and run the compiler.
pub fn run(self) -> interface::Result<()> {
- run_compiler(self.at_args, self.callbacks, self.file_loader, self.make_codegen_backend)
+ run_compiler(
+ self.at_args,
+ self.callbacks,
+ self.file_loader,
+ self.make_codegen_backend,
+ self.using_internal_features,
+ )
}
}
@@ -270,6 +291,7 @@ fn run_compiler(
make_codegen_backend: Option<
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
>,
+ using_internal_features: Arc<std::sync::atomic::AtomicBool>,
) -> interface::Result<()> {
let mut early_error_handler = EarlyErrorHandler::new(ErrorOutputType::default());
@@ -294,13 +316,11 @@ fn run_compiler(
return Ok(());
}
- let cfg = interface::parse_cfgspecs(&early_error_handler, matches.opt_strs("cfg"));
- let check_cfg = interface::parse_check_cfg(&early_error_handler, matches.opt_strs("check-cfg"));
let (odir, ofile) = make_output(&matches);
let mut config = interface::Config {
opts: sopts,
- crate_cfg: cfg,
- crate_check_cfg: check_cfg,
+ crate_cfg: matches.opt_strs("cfg"),
+ crate_check_cfg: matches.opt_strs("check-cfg"),
input: Input::File(PathBuf::new()),
output_file: ofile,
output_dir: odir,
@@ -309,10 +329,12 @@ fn run_compiler(
locale_resources: DEFAULT_LOCALE_RESOURCES,
lint_caps: Default::default(),
parse_sess_created: None,
+ hash_untracked_state: None,
register_lints: None,
override_queries: None,
make_codegen_backend,
registry: diagnostics_registry(),
+ using_internal_features,
expanded_args: args,
};
@@ -392,7 +414,7 @@ fn run_compiler(
if ppm.needs_ast_map() {
queries.global_ctxt()?.enter(|tcx| {
tcx.ensure().early_lint_checks(());
- pretty::print_after_hir_lowering(tcx, *ppm);
+ pretty::print(sess, *ppm, pretty::PrintExtra::NeedsAstMap { tcx });
Ok(())
})?;
@@ -400,8 +422,12 @@ fn run_compiler(
// effects of writing the dep-info and reporting errors.
queries.global_ctxt()?.enter(|tcx| tcx.output_filenames(()));
} else {
- let krate = queries.parse()?.steal();
- pretty::print_after_parsing(sess, &krate, *ppm);
+ let krate = queries.parse()?;
+ pretty::print(
+ sess,
+ *ppm,
+ pretty::PrintExtra::AfterParsing { krate: &*krate.borrow() },
+ );
}
trace!("finished pretty-printing");
return early_exit();
@@ -456,8 +482,7 @@ fn run_compiler(
}
if sess.opts.unstable_opts.print_vtable_sizes {
- let crate_name =
- compiler.session().opts.crate_name.as_deref().unwrap_or("<UNKNOWN_CRATE>");
+ let crate_name = queries.global_ctxt()?.enter(|tcx| tcx.crate_name(LOCAL_CRATE));
sess.code_stats.print_vtable_sizes(crate_name);
}
@@ -542,7 +567,7 @@ pub enum Compilation {
}
impl Compilation {
- pub fn and_then<F: FnOnce() -> Compilation>(self, next: F) -> Compilation {
+ fn and_then<F: FnOnce() -> Compilation>(self, next: F) -> Compilation {
match self {
Compilation::Stop => Compilation::Stop,
Compilation::Continue => next(),
@@ -654,7 +679,7 @@ fn show_md_content_with_pager(content: &str, color: ColorConfig) {
}
}
-pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Compilation {
+fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Compilation {
if sess.opts.unstable_opts.link_only {
if let Input::File(file) = &sess.io.input {
let outputs = compiler.build_output_filenames(sess, &[]);
@@ -695,7 +720,7 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp
}
}
-pub fn list_metadata(
+fn list_metadata(
handler: &EarlyErrorHandler,
sess: &Session,
metadata_loader: &dyn MetadataLoader,
@@ -970,16 +995,14 @@ the command line flag directly.
}
/// Write to stdout lint command options, together with a list of all available lints
-pub fn describe_lints(sess: &Session, lint_store: &LintStore, loaded_plugins: bool) {
+pub fn describe_lints(sess: &Session, lint_store: &LintStore, loaded_lints: bool) {
safe_println!(
"
Available lint options:
-W <foo> Warn about <foo>
- -A <foo> \
- Allow <foo>
+ -A <foo> Allow <foo>
-D <foo> Deny <foo>
- -F <foo> Forbid <foo> \
- (deny <foo> and all attempts to override)
+ -F <foo> Forbid <foo> (deny <foo> and all attempts to override)
"
);
@@ -998,18 +1021,18 @@ Available lint options:
lints
}
- let (plugin, builtin): (Vec<_>, _) =
- lint_store.get_lints().iter().cloned().partition(|&lint| lint.is_plugin);
- let plugin = sort_lints(sess, plugin);
+ let (loaded, builtin): (Vec<_>, _) =
+ lint_store.get_lints().iter().cloned().partition(|&lint| lint.is_loaded);
+ let loaded = sort_lints(sess, loaded);
let builtin = sort_lints(sess, builtin);
- let (plugin_groups, builtin_groups): (Vec<_>, _) =
+ let (loaded_groups, builtin_groups): (Vec<_>, _) =
lint_store.get_lint_groups().partition(|&(.., p)| p);
- let plugin_groups = sort_lint_groups(plugin_groups);
+ let loaded_groups = sort_lint_groups(loaded_groups);
let builtin_groups = sort_lint_groups(builtin_groups);
let max_name_len =
- plugin.iter().chain(&builtin).map(|&s| s.name.chars().count()).max().unwrap_or(0);
+ loaded.iter().chain(&builtin).map(|&s| s.name.chars().count()).max().unwrap_or(0);
let padded = |x: &str| {
let mut s = " ".repeat(max_name_len - x.chars().count());
s.push_str(x);
@@ -1037,7 +1060,7 @@ Available lint options:
let max_name_len = max(
"warnings".len(),
- plugin_groups
+ loaded_groups
.iter()
.chain(&builtin_groups)
.map(|&(s, _)| s.chars().count())
@@ -1075,20 +1098,22 @@ Available lint options:
print_lint_groups(builtin_groups, true);
- match (loaded_plugins, plugin.len(), plugin_groups.len()) {
+ match (loaded_lints, loaded.len(), loaded_groups.len()) {
(false, 0, _) | (false, _, 0) => {
- safe_println!("Lint tools like Clippy can provide additional lints and lint groups.");
+ safe_println!("Lint tools like Clippy can load additional lints and lint groups.");
+ }
+ (false, ..) => panic!("didn't load additional lints but got them anyway!"),
+ (true, 0, 0) => {
+ safe_println!("This crate does not load any additional lints or lint groups.")
}
- (false, ..) => panic!("didn't load lint plugins but got them anyway!"),
- (true, 0, 0) => safe_println!("This crate does not load any lint plugins or lint groups."),
(true, l, g) => {
if l > 0 {
- safe_println!("Lint checks provided by plugins loaded by this crate:\n");
- print_lints(plugin);
+ safe_println!("Lint checks loaded by this crate:\n");
+ print_lints(loaded);
}
if g > 0 {
- safe_println!("Lint groups provided by plugins loaded by this crate:\n");
- print_lint_groups(plugin_groups, false);
+ safe_println!("Lint groups loaded by this crate:\n");
+ print_lint_groups(loaded_groups, false);
}
}
}
@@ -1105,7 +1130,7 @@ pub fn describe_flag_categories(handler: &EarlyErrorHandler, matches: &Matches)
rustc_errors::FatalError.raise();
}
- // Don't handle -W help here, because we might first load plugins.
+ // Don't handle -W help here, because we might first load additional lints.
let debug_flags = matches.opt_strs("Z");
if debug_flags.iter().any(|x| *x == "help") {
describe_debug_flags();
@@ -1181,6 +1206,10 @@ fn print_flag_list<T>(
///
/// So with all that in mind, the comments below have some more detail about the
/// contortions done here to get things to work out correctly.
+///
+/// This does not need to be `pub` for rustc itself, but @chaosite needs it to
+/// be public when using rustc as a library, see
+/// <https://github.com/rust-lang/rust/commit/2b4c33817a5aaecabf4c6598d41e190080ec119e>
pub fn handle_options(handler: &EarlyErrorHandler, args: &[String]) -> Option<getopts::Matches> {
if args.is_empty() {
// user did not write `-v` nor `-Z unstable-options`, so do not
@@ -1280,24 +1309,36 @@ pub fn catch_with_exit_code(f: impl FnOnce() -> interface::Result<()>) -> i32 {
}
}
-pub static ICE_PATH: OnceLock<Option<PathBuf>> = OnceLock::new();
+static ICE_PATH: OnceLock<Option<PathBuf>> = OnceLock::new();
-pub fn ice_path() -> &'static Option<PathBuf> {
+fn ice_path() -> &'static Option<PathBuf> {
ICE_PATH.get_or_init(|| {
if !rustc_feature::UnstableFeatures::from_environment(None).is_nightly_build() {
return None;
}
- if let Ok("0") = std::env::var("RUST_BACKTRACE").as_deref() {
+ if let Some(s) = std::env::var_os("RUST_BACKTRACE")
+ && s == "0"
+ {
return None;
}
- let mut path = match std::env::var("RUSTC_ICE").as_deref() {
- // Explicitly opting out of writing ICEs to disk.
- Ok("0") => return None,
- Ok(s) => PathBuf::from(s),
- Err(_) => std::env::current_dir().unwrap_or_default(),
+ let mut path = match std::env::var_os("RUSTC_ICE") {
+ Some(s) => {
+ if s == "0" {
+ // Explicitly opting out of writing ICEs to disk.
+ return None;
+ }
+ PathBuf::from(s)
+ }
+ None => std::env::current_dir().unwrap_or_default(),
};
let now: OffsetDateTime = SystemTime::now().into();
- let file_now = now.format(&Rfc3339).unwrap_or(String::new());
+ let file_now = now
+ .format(
+ // Don't use a standard datetime format because Windows doesn't support `:` in paths
+ &time::format_description::parse("[year]-[month]-[day]T[hour]_[minute]_[second]")
+ .unwrap(),
+ )
+ .unwrap_or_default();
let pid = std::process::id();
path.push(format!("rustc-ice-{file_now}-{pid}.txt"));
Some(path)
@@ -1314,18 +1355,24 @@ pub fn ice_path() -> &'static Option<PathBuf> {
/// If you have no extra info to report, pass the empty closure `|_| ()` as the argument to
/// extra_info.
///
+/// Returns a flag that can be set to disable the note for submitting a bug. This can be passed to
+/// [`RunCompiler::set_using_internal_features`] to let macro expansion set it when encountering
+/// internal features.
+///
/// A custom rustc driver can skip calling this to set up a custom ICE hook.
-pub fn install_ice_hook(bug_report_url: &'static str, extra_info: fn(&Handler)) {
+pub fn install_ice_hook(bug_report_url: &'static str, extra_info: fn(&Handler)) -> Arc<AtomicBool> {
// If the user has not explicitly overridden "RUST_BACKTRACE", then produce
// full backtraces. When a compiler ICE happens, we want to gather
// as much information as possible to present in the issue opened
// by the user. Compiler developers and other rustc users can
// opt in to less-verbose backtraces by manually setting "RUST_BACKTRACE"
// (e.g. `RUST_BACKTRACE=1`)
- if std::env::var("RUST_BACKTRACE").is_err() {
+ if std::env::var_os("RUST_BACKTRACE").is_none() {
std::env::set_var("RUST_BACKTRACE", "full");
}
+ let using_internal_features = Arc::new(std::sync::atomic::AtomicBool::default());
+ let using_internal_features_hook = using_internal_features.clone();
panic::update_hook(Box::new(
move |default_hook: &(dyn Fn(&PanicInfo<'_>) + Send + Sync + 'static),
info: &PanicInfo<'_>| {
@@ -1350,8 +1397,7 @@ pub fn install_ice_hook(bug_report_url: &'static str, extra_info: fn(&Handler))
eprintln!();
if let Some(ice_path) = ice_path()
- && let Ok(mut out) =
- File::options().create(true).append(true).open(&ice_path)
+ && let Ok(mut out) = File::options().create(true).append(true).open(&ice_path)
{
// The current implementation always returns `Some`.
let location = info.location().unwrap();
@@ -1376,9 +1422,11 @@ pub fn install_ice_hook(bug_report_url: &'static str, extra_info: fn(&Handler))
}
// Print the ICE message
- report_ice(info, bug_report_url, extra_info);
+ report_ice(info, bug_report_url, extra_info, &using_internal_features_hook);
},
));
+
+ using_internal_features
}
/// Prints the ICE message, including query stack, but without backtrace.
@@ -1387,7 +1435,12 @@ pub fn install_ice_hook(bug_report_url: &'static str, extra_info: fn(&Handler))
///
/// When `install_ice_hook` is called, this function will be called as the panic
/// hook.
-pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str, extra_info: fn(&Handler)) {
+fn report_ice(
+ info: &panic::PanicInfo<'_>,
+ bug_report_url: &str,
+ extra_info: fn(&Handler),
+ using_internal_features: &AtomicBool,
+) {
let fallback_bundle =
rustc_errors::fallback_fluent_bundle(crate::DEFAULT_LOCALE_RESOURCES.to_vec(), false);
let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr(
@@ -1404,19 +1457,22 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str, extra_info:
handler.emit_err(session_diagnostics::Ice);
}
- handler.emit_note(session_diagnostics::IceBugReport { bug_report_url });
+ if using_internal_features.load(std::sync::atomic::Ordering::Relaxed) {
+ handler.emit_note(session_diagnostics::IceBugReportInternalFeature);
+ } else {
+ handler.emit_note(session_diagnostics::IceBugReport { bug_report_url });
+ }
let version = util::version_str!().unwrap_or("unknown_version");
let triple = config::host_triple();
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
- let file = if let Some(path) = ice_path().as_ref() {
+ let file = if let Some(path) = ice_path() {
// Create the ICE dump target file.
match crate::fs::File::options().create(true).append(true).open(&path) {
Ok(mut file) => {
- handler
- .emit_note(session_diagnostics::IcePath { path: path.display().to_string() });
+ handler.emit_note(session_diagnostics::IcePath { path: path.clone() });
if FIRST_PANIC.swap(false, Ordering::SeqCst) {
let _ = write!(file, "\n\nrustc version: {version}\nplatform: {triple}");
}
@@ -1425,10 +1481,10 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str, extra_info:
Err(err) => {
// The path ICE couldn't be written to disk, provide feedback to the user as to why.
handler.emit_warning(session_diagnostics::IcePathError {
- path: path.display().to_string(),
+ path: path.clone(),
error: err.to_string(),
- env_var: std::env::var("RUSTC_ICE")
- .ok()
+ env_var: std::env::var_os("RUSTC_ICE")
+ .map(PathBuf::from)
.map(|env_var| session_diagnostics::IcePathErrorEnv { env_var }),
});
handler.emit_note(session_diagnostics::IceVersion { version, triple });
@@ -1489,7 +1545,7 @@ pub fn main() -> ! {
init_rustc_env_logger(&handler);
signal_handler::install();
let mut callbacks = TimePassesCallbacks::default();
- install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ());
+ let using_internal_features = install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ());
let exit_code = catch_with_exit_code(|| {
let args = env::args_os()
.enumerate()
@@ -1499,7 +1555,9 @@ pub fn main() -> ! {
})
})
.collect::<Vec<_>>();
- RunCompiler::new(&args, &mut callbacks).run()
+ RunCompiler::new(&args, &mut callbacks)
+ .set_using_internal_features(using_internal_features)
+ .run()
});
if let Some(format) = callbacks.time_passes {
diff --git a/compiler/rustc_driver_impl/src/pretty.rs b/compiler/rustc_driver_impl/src/pretty.rs
index 222c7b5d6..cc533b994 100644
--- a/compiler/rustc_driver_impl/src/pretty.rs
+++ b/compiler/rustc_driver_impl/src/pretty.rs
@@ -1,14 +1,13 @@
//! The various pretty-printing routines.
use rustc_ast as ast;
-use rustc_ast_pretty::pprust;
-use rustc_errors::ErrorGuaranteed;
+use rustc_ast_pretty::pprust as pprust_ast;
use rustc_hir as hir;
use rustc_hir_pretty as pprust_hir;
-use rustc_middle::hir::map as hir_map;
+use rustc_middle::bug;
use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty};
use rustc_middle::ty::{self, TyCtxt};
-use rustc_session::config::{OutFileName, PpAstTreeMode, PpHirMode, PpMode, PpSourceMode};
+use rustc_session::config::{OutFileName, PpHirMode, PpMode, PpSourceMode};
use rustc_session::Session;
use rustc_span::symbol::Ident;
use rustc_span::FileName;
@@ -20,174 +19,57 @@ pub use self::PpMode::*;
pub use self::PpSourceMode::*;
use crate::abort_on_err;
-// This slightly awkward construction is to allow for each PpMode to
-// choose whether it needs to do analyses (which can consume the
-// Session) and then pass through the session (now attached to the
-// analysis results) on to the chosen pretty-printer, along with the
-// `&PpAnn` object.
-//
-// Note that since the `&PrinterSupport` is freshly constructed on each
-// call, it would not make sense to try to attach the lifetime of `self`
-// to the lifetime of the `&PrinterObject`.
-
-/// Constructs a `PrinterSupport` object and passes it to `f`.
-fn call_with_pp_support<'tcx, A, F>(
- ppmode: &PpSourceMode,
- sess: &'tcx Session,
- tcx: Option<TyCtxt<'tcx>>,
- f: F,
-) -> A
-where
- F: FnOnce(&dyn PrinterSupport) -> A,
-{
- match *ppmode {
- Normal | Expanded => {
- let annotation = NoAnn { sess, tcx };
- f(&annotation)
- }
-
- Identified | ExpandedIdentified => {
- let annotation = IdentifiedAnnotation { sess, tcx };
- f(&annotation)
- }
- ExpandedHygiene => {
- let annotation = HygieneAnnotation { sess };
- f(&annotation)
- }
- }
-}
-fn call_with_pp_support_hir<A, F>(ppmode: &PpHirMode, tcx: TyCtxt<'_>, f: F) -> A
-where
- F: FnOnce(&dyn HirPrinterSupport<'_>, hir_map::Map<'_>) -> A,
-{
- match *ppmode {
- PpHirMode::Normal => {
- let annotation = NoAnn { sess: tcx.sess, tcx: Some(tcx) };
- f(&annotation, tcx.hir())
- }
-
- PpHirMode::Identified => {
- let annotation = IdentifiedAnnotation { sess: tcx.sess, tcx: Some(tcx) };
- f(&annotation, tcx.hir())
- }
- PpHirMode::Typed => {
- abort_on_err(tcx.analysis(()), tcx.sess);
-
- let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) };
- tcx.dep_graph.with_ignore(|| f(&annotation, tcx.hir()))
- }
- }
-}
-
-trait PrinterSupport: pprust::PpAnn {
- /// Provides a uniform interface for re-extracting a reference to a
- /// `Session` from a value that now owns it.
- fn sess(&self) -> &Session;
-
- /// Produces the pretty-print annotation object.
- ///
- /// (Rust does not yet support upcasting from a trait object to
- /// an object for one of its supertraits.)
- fn pp_ann(&self) -> &dyn pprust::PpAnn;
-}
-
-trait HirPrinterSupport<'hir>: pprust_hir::PpAnn {
- /// Provides a uniform interface for re-extracting a reference to a
- /// `Session` from a value that now owns it.
- fn sess(&self) -> &Session;
-
- /// Provides a uniform interface for re-extracting a reference to an
- /// `hir_map::Map` from a value that now owns it.
- fn hir_map(&self) -> Option<hir_map::Map<'hir>>;
-
- /// Produces the pretty-print annotation object.
- ///
- /// (Rust does not yet support upcasting from a trait object to
- /// an object for one of its supertraits.)
- fn pp_ann(&self) -> &dyn pprust_hir::PpAnn;
-}
-
-struct NoAnn<'hir> {
- sess: &'hir Session,
- tcx: Option<TyCtxt<'hir>>,
-}
-
-impl<'hir> PrinterSupport for NoAnn<'hir> {
- fn sess(&self) -> &Session {
- self.sess
- }
-
- fn pp_ann(&self) -> &dyn pprust::PpAnn {
- self
- }
-}
+struct AstNoAnn;
-impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> {
- fn sess(&self) -> &Session {
- self.sess
- }
-
- fn hir_map(&self) -> Option<hir_map::Map<'hir>> {
- self.tcx.map(|tcx| tcx.hir())
- }
+impl pprust_ast::PpAnn for AstNoAnn {}
- fn pp_ann(&self) -> &dyn pprust_hir::PpAnn {
- self
- }
+struct HirNoAnn<'tcx> {
+ tcx: TyCtxt<'tcx>,
}
-impl<'hir> pprust::PpAnn for NoAnn<'hir> {}
-impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
+impl<'tcx> pprust_hir::PpAnn for HirNoAnn<'tcx> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
- if let Some(tcx) = self.tcx {
- pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
- }
+ pprust_hir::PpAnn::nested(
+ &(&self.tcx.hir() as &dyn hir::intravisit::Map<'_>),
+ state,
+ nested,
+ )
}
}
-struct IdentifiedAnnotation<'hir> {
- sess: &'hir Session,
- tcx: Option<TyCtxt<'hir>>,
-}
-
-impl<'hir> PrinterSupport for IdentifiedAnnotation<'hir> {
- fn sess(&self) -> &Session {
- self.sess
- }
+struct AstIdentifiedAnn;
- fn pp_ann(&self) -> &dyn pprust::PpAnn {
- self
- }
-}
-
-impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
- fn pre(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
- if let pprust::AnnNode::Expr(_) = node {
+impl pprust_ast::PpAnn for AstIdentifiedAnn {
+ fn pre(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
+ if let pprust_ast::AnnNode::Expr(_) = node {
s.popen();
}
}
- fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
+
+ fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
match node {
- pprust::AnnNode::Crate(_) | pprust::AnnNode::Ident(_) | pprust::AnnNode::Name(_) => {}
+ pprust_ast::AnnNode::Crate(_)
+ | pprust_ast::AnnNode::Ident(_)
+ | pprust_ast::AnnNode::Name(_) => {}
- pprust::AnnNode::Item(item) => {
+ pprust_ast::AnnNode::Item(item) => {
s.s.space();
s.synth_comment(item.id.to_string())
}
- pprust::AnnNode::SubItem(id) => {
+ pprust_ast::AnnNode::SubItem(id) => {
s.s.space();
s.synth_comment(id.to_string())
}
- pprust::AnnNode::Block(blk) => {
+ pprust_ast::AnnNode::Block(blk) => {
s.s.space();
s.synth_comment(format!("block {}", blk.id))
}
- pprust::AnnNode::Expr(expr) => {
+ pprust_ast::AnnNode::Expr(expr) => {
s.s.space();
s.synth_comment(expr.id.to_string());
s.pclose()
}
- pprust::AnnNode::Pat(pat) => {
+ pprust_ast::AnnNode::Pat(pat) => {
s.s.space();
s.synth_comment(format!("pat {}", pat.id));
}
@@ -195,31 +77,25 @@ impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
}
}
-impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
- fn sess(&self) -> &Session {
- self.sess
- }
-
- fn hir_map(&self) -> Option<hir_map::Map<'hir>> {
- self.tcx.map(|tcx| tcx.hir())
- }
-
- fn pp_ann(&self) -> &dyn pprust_hir::PpAnn {
- self
- }
+struct HirIdentifiedAnn<'tcx> {
+ tcx: TyCtxt<'tcx>,
}
-impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
+impl<'tcx> pprust_hir::PpAnn for HirIdentifiedAnn<'tcx> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
- if let Some(ref tcx) = self.tcx {
- pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
- }
+ pprust_hir::PpAnn::nested(
+ &(&self.tcx.hir() as &dyn hir::intravisit::Map<'_>),
+ state,
+ nested,
+ )
}
+
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
if let pprust_hir::AnnNode::Expr(_) = node {
s.popen();
}
}
+
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
match node {
pprust_hir::AnnNode::Name(_) => {}
@@ -252,32 +128,22 @@ impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
}
}
-struct HygieneAnnotation<'a> {
+struct AstHygieneAnn<'a> {
sess: &'a Session,
}
-impl<'a> PrinterSupport for HygieneAnnotation<'a> {
- fn sess(&self) -> &Session {
- self.sess
- }
-
- fn pp_ann(&self) -> &dyn pprust::PpAnn {
- self
- }
-}
-
-impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
- fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
+impl<'a> pprust_ast::PpAnn for AstHygieneAnn<'a> {
+ fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
match node {
- pprust::AnnNode::Ident(&Ident { name, span }) => {
+ pprust_ast::AnnNode::Ident(&Ident { name, span }) => {
s.s.space();
s.synth_comment(format!("{}{:?}", name.as_u32(), span.ctxt()))
}
- pprust::AnnNode::Name(&name) => {
+ pprust_ast::AnnNode::Name(&name) => {
s.s.space();
s.synth_comment(name.as_u32().to_string())
}
- pprust::AnnNode::Crate(_) => {
+ pprust_ast::AnnNode::Crate(_) => {
s.s.hardbreak();
let verbose = self.sess.verbose();
s.synth_comment(rustc_span::hygiene::debug_hygiene_data(verbose));
@@ -288,26 +154,12 @@ impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
}
}
-struct TypedAnnotation<'tcx> {
+struct HirTypedAnn<'tcx> {
tcx: TyCtxt<'tcx>,
maybe_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>,
}
-impl<'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'tcx> {
- fn sess(&self) -> &Session {
- self.tcx.sess
- }
-
- fn hir_map(&self) -> Option<hir_map::Map<'tcx>> {
- Some(self.tcx.hir())
- }
-
- fn pp_ann(&self) -> &dyn pprust_hir::PpAnn {
- self
- }
-}
-
-impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
+impl<'tcx> pprust_hir::PpAnn for HirTypedAnn<'tcx> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
let old_maybe_typeck_results = self.maybe_typeck_results.get();
if let pprust_hir::Nested::Body(id) = nested {
@@ -317,11 +169,13 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
pprust_hir::PpAnn::nested(pp_ann, state, nested);
self.maybe_typeck_results.set(old_maybe_typeck_results);
}
+
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
if let pprust_hir::AnnNode::Expr(_) = node {
s.popen();
}
}
+
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
if let pprust_hir::AnnNode::Expr(expr) = node {
let typeck_results = self.maybe_typeck_results.get().or_else(|| {
@@ -360,119 +214,119 @@ fn write_or_print(out: &str, sess: &Session) {
sess.io.output_file.as_ref().unwrap_or(&OutFileName::Stdout).overwrite(out, sess);
}
-pub fn print_after_parsing(sess: &Session, krate: &ast::Crate, ppm: PpMode) {
- let (src, src_name) = get_source(sess);
+// Extra data for pretty-printing, the form of which depends on what kind of
+// pretty-printing we are doing.
+pub enum PrintExtra<'tcx> {
+ AfterParsing { krate: &'tcx ast::Crate },
+ NeedsAstMap { tcx: TyCtxt<'tcx> },
+}
- let out = match ppm {
- Source(s) => {
- // Silently ignores an identified node.
- call_with_pp_support(&s, sess, None, move |annotation| {
- debug!("pretty printing source code {:?}", s);
- let sess = annotation.sess();
- let parse = &sess.parse_sess;
- pprust::print_crate(
- sess.source_map(),
- krate,
- src_name,
- src,
- annotation.pp_ann(),
- false,
- parse.edition,
- &sess.parse_sess.attr_id_generator,
- )
- })
+impl<'tcx> PrintExtra<'tcx> {
+ fn with_krate<F, R>(&self, f: F) -> R
+ where
+ F: FnOnce(&ast::Crate) -> R,
+ {
+ match self {
+ PrintExtra::AfterParsing { krate, .. } => f(krate),
+ PrintExtra::NeedsAstMap { tcx } => f(&tcx.resolver_for_lowering(()).borrow().1),
}
- AstTree(PpAstTreeMode::Normal) => {
- debug!("pretty printing AST tree");
- format!("{krate:#?}")
- }
- _ => unreachable!(),
- };
+ }
- write_or_print(&out, sess);
+ fn tcx(&self) -> TyCtxt<'tcx> {
+ match self {
+ PrintExtra::AfterParsing { .. } => bug!("PrintExtra::tcx"),
+ PrintExtra::NeedsAstMap { tcx } => *tcx,
+ }
+ }
}
-pub fn print_after_hir_lowering<'tcx>(tcx: TyCtxt<'tcx>, ppm: PpMode) {
+pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
if ppm.needs_analysis() {
- abort_on_err(print_with_analysis(tcx, ppm), tcx.sess);
- return;
+ abort_on_err(ex.tcx().analysis(()), sess);
}
- let (src, src_name) = get_source(tcx.sess);
+ let (src, src_name) = get_source(sess);
let out = match ppm {
Source(s) => {
- // Silently ignores an identified node.
- call_with_pp_support(&s, tcx.sess, Some(tcx), move |annotation| {
- debug!("pretty printing source code {:?}", s);
- let sess = annotation.sess();
- let parse = &sess.parse_sess;
- pprust::print_crate(
+ debug!("pretty printing source code {:?}", s);
+ let annotation: Box<dyn pprust_ast::PpAnn> = match s {
+ Normal => Box::new(AstNoAnn),
+ Expanded => Box::new(AstNoAnn),
+ Identified => Box::new(AstIdentifiedAnn),
+ ExpandedIdentified => Box::new(AstIdentifiedAnn),
+ ExpandedHygiene => Box::new(AstHygieneAnn { sess }),
+ };
+ let parse = &sess.parse_sess;
+ let is_expanded = ppm.needs_ast_map();
+ ex.with_krate(|krate| {
+ pprust_ast::print_crate(
sess.source_map(),
- &tcx.resolver_for_lowering(()).borrow().1,
+ krate,
src_name,
src,
- annotation.pp_ann(),
- true,
+ &*annotation,
+ is_expanded,
parse.edition,
&sess.parse_sess.attr_id_generator,
)
})
}
-
- AstTree(PpAstTreeMode::Expanded) => {
+ AstTree => {
+ debug!("pretty printing AST tree");
+ ex.with_krate(|krate| format!("{krate:#?}"))
+ }
+ AstTreeExpanded => {
debug!("pretty-printing expanded AST");
- format!("{:#?}", tcx.resolver_for_lowering(()).borrow().1)
+ format!("{:#?}", ex.tcx().resolver_for_lowering(()).borrow().1)
}
-
- Hir(s) => call_with_pp_support_hir(&s, tcx, move |annotation, hir_map| {
+ Hir(s) => {
debug!("pretty printing HIR {:?}", s);
- let sess = annotation.sess();
- let sm = sess.source_map();
- let attrs = |id| hir_map.attrs(id);
- pprust_hir::print_crate(
- sm,
- hir_map.root_module(),
- src_name,
- src,
- &attrs,
- annotation.pp_ann(),
- )
- }),
-
+ let tcx = ex.tcx();
+ let f = |annotation: &dyn pprust_hir::PpAnn| {
+ let sm = sess.source_map();
+ let hir_map = tcx.hir();
+ let attrs = |id| hir_map.attrs(id);
+ pprust_hir::print_crate(
+ sm,
+ hir_map.root_module(),
+ src_name,
+ src,
+ &attrs,
+ annotation,
+ )
+ };
+ match s {
+ PpHirMode::Normal => {
+ let annotation = HirNoAnn { tcx };
+ f(&annotation)
+ }
+ PpHirMode::Identified => {
+ let annotation = HirIdentifiedAnn { tcx };
+ f(&annotation)
+ }
+ PpHirMode::Typed => {
+ let annotation = HirTypedAnn { tcx, maybe_typeck_results: Cell::new(None) };
+ tcx.dep_graph.with_ignore(|| f(&annotation))
+ }
+ }
+ }
HirTree => {
- call_with_pp_support_hir(&PpHirMode::Normal, tcx, move |_annotation, hir_map| {
- debug!("pretty printing HIR tree");
- format!("{:#?}", hir_map.krate())
- })
+ debug!("pretty printing HIR tree");
+ format!("{:#?}", ex.tcx().hir().krate())
}
-
- _ => unreachable!(),
- };
-
- write_or_print(&out, tcx.sess);
-}
-
-// In an ideal world, this would be a public function called by the driver after
-// analysis is performed. However, we want to call `phase_3_run_analysis_passes`
-// with a different callback than the standard driver, so that isn't easy.
-// Instead, we call that function ourselves.
-fn print_with_analysis(tcx: TyCtxt<'_>, ppm: PpMode) -> Result<(), ErrorGuaranteed> {
- tcx.analysis(())?;
- let out = match ppm {
Mir => {
let mut out = Vec::new();
- write_mir_pretty(tcx, None, &mut out).unwrap();
+ write_mir_pretty(ex.tcx(), None, &mut out).unwrap();
String::from_utf8(out).unwrap()
}
-
MirCFG => {
let mut out = Vec::new();
- write_mir_graphviz(tcx, None, &mut out).unwrap();
+ write_mir_graphviz(ex.tcx(), None, &mut out).unwrap();
String::from_utf8(out).unwrap()
}
-
ThirTree => {
+ let tcx = ex.tcx();
let mut out = String::new();
abort_on_err(rustc_hir_analysis::check_crate(tcx), tcx.sess);
debug!("pretty printing THIR tree");
@@ -481,8 +335,8 @@ fn print_with_analysis(tcx: TyCtxt<'_>, ppm: PpMode) -> Result<(), ErrorGuarante
}
out
}
-
ThirFlat => {
+ let tcx = ex.tcx();
let mut out = String::new();
abort_on_err(rustc_hir_analysis::check_crate(tcx), tcx.sess);
debug!("pretty printing THIR flat");
@@ -491,11 +345,7 @@ fn print_with_analysis(tcx: TyCtxt<'_>, ppm: PpMode) -> Result<(), ErrorGuarante
}
out
}
-
- _ => unreachable!(),
};
- write_or_print(&out, tcx.sess);
-
- Ok(())
+ write_or_print(&out, sess);
}
diff --git a/compiler/rustc_driver_impl/src/session_diagnostics.rs b/compiler/rustc_driver_impl/src/session_diagnostics.rs
index 5eb587c54..2b31fdd77 100644
--- a/compiler/rustc_driver_impl/src/session_diagnostics.rs
+++ b/compiler/rustc_driver_impl/src/session_diagnostics.rs
@@ -43,6 +43,10 @@ pub(crate) struct IceBugReport<'a> {
}
#[derive(Diagnostic)]
+#[diag(driver_impl_ice_bug_report_internal_feature)]
+pub(crate) struct IceBugReportInternalFeature;
+
+#[derive(Diagnostic)]
#[diag(driver_impl_ice_version)]
pub(crate) struct IceVersion<'a> {
pub version: &'a str,
@@ -52,13 +56,13 @@ pub(crate) struct IceVersion<'a> {
#[derive(Diagnostic)]
#[diag(driver_impl_ice_path)]
pub(crate) struct IcePath {
- pub path: String,
+ pub path: std::path::PathBuf,
}
#[derive(Diagnostic)]
#[diag(driver_impl_ice_path_error)]
pub(crate) struct IcePathError {
- pub path: String,
+ pub path: std::path::PathBuf,
pub error: String,
#[subdiagnostic]
pub env_var: Option<IcePathErrorEnv>,
@@ -67,7 +71,7 @@ pub(crate) struct IcePathError {
#[derive(Subdiagnostic)]
#[note(driver_impl_ice_path_error_env)]
pub(crate) struct IcePathErrorEnv {
- pub env_var: String,
+ pub env_var: std::path::PathBuf,
}
#[derive(Diagnostic)]