mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 06:44:35 +00:00
Auto merge of #116619 - nnethercote:rustc_driver_impl, r=compiler-errors
Streamline `rustc_driver_impl` pretty-printing. This PR simplifies a lot of unnecessary structure in `rustc_driver_impl/src/pretty.rs`. It removes some traits and functions, simplifies some structs, renames some things for increased consistency, and eliminates some boilerplate code. Overall it cuts more than 150 lines of code. r? `@compiler-errors`
This commit is contained in:
commit
2763ca50da
@ -8,10 +8,10 @@
|
||||
#![cfg_attr(not(bootstrap), doc(rust_logo))]
|
||||
#![cfg_attr(not(bootstrap), feature(rustdoc_internals))]
|
||||
#![cfg_attr(not(bootstrap), allow(internal_features))]
|
||||
#![feature(lazy_cell)]
|
||||
#![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)]
|
||||
@ -395,7 +395,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(())
|
||||
})?;
|
||||
|
||||
@ -404,7 +404,7 @@ fn run_compiler(
|
||||
queries.global_ctxt()?.enter(|tcx| tcx.output_filenames(()));
|
||||
} else {
|
||||
let krate = queries.parse()?.steal();
|
||||
pretty::print_after_parsing(sess, &krate, *ppm);
|
||||
pretty::print(sess, *ppm, pretty::PrintExtra::AfterParsing { krate });
|
||||
}
|
||||
trace!("finished pretty-printing");
|
||||
return early_exit();
|
||||
@ -545,7 +545,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(),
|
||||
@ -657,7 +657,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, &[]);
|
||||
@ -698,7 +698,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,
|
||||
@ -1184,7 +1184,7 @@ 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.
|
||||
pub fn handle_options(handler: &EarlyErrorHandler, args: &[String]) -> Option<getopts::Matches> {
|
||||
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
|
||||
// include that extra information.
|
||||
@ -1283,9 +1283,9 @@ 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;
|
||||
@ -1394,7 +1394,7 @@ 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)) {
|
||||
let fallback_bundle =
|
||||
rustc_errors::fallback_fluent_bundle(crate::DEFAULT_LOCALE_RESOURCES.to_vec(), false);
|
||||
let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr(
|
||||
|
@ -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`.
|
||||
struct AstNoAnn;
|
||||
|
||||
/// 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)
|
||||
}
|
||||
impl pprust_ast::PpAnn for AstNoAnn {}
|
||||
|
||||
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()))
|
||||
}
|
||||
}
|
||||
struct HirNoAnn<'tcx> {
|
||||
tcx: TyCtxt<'tcx>,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
fn pp_ann(&self) -> &dyn pprust_hir::PpAnn {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
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>>,
|
||||
}
|
||||
struct AstIdentifiedAnn;
|
||||
|
||||
impl<'hir> PrinterSupport for IdentifiedAnnotation<'hir> {
|
||||
fn sess(&self) -> &Session {
|
||||
self.sess
|
||||
}
|
||||
|
||||
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<'_>) {
|
||||
match node {
|
||||
pprust::AnnNode::Crate(_) | pprust::AnnNode::Ident(_) | pprust::AnnNode::Name(_) => {}
|
||||
|
||||
pprust::AnnNode::Item(item) => {
|
||||
fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
|
||||
match node {
|
||||
pprust_ast::AnnNode::Crate(_)
|
||||
| pprust_ast::AnnNode::Ident(_)
|
||||
| pprust_ast::AnnNode::Name(_) => {}
|
||||
|
||||
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) {
|
||||
// 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: ast::Crate },
|
||||
NeedsAstMap { tcx: TyCtxt<'tcx> },
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
fn tcx(&self) -> TyCtxt<'tcx> {
|
||||
match self {
|
||||
PrintExtra::AfterParsing { .. } => bug!("PrintExtra::tcx"),
|
||||
PrintExtra::NeedsAstMap { tcx } => *tcx,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
|
||||
if ppm.needs_analysis() {
|
||||
abort_on_err(ex.tcx().analysis(()), sess);
|
||||
}
|
||||
|
||||
let (src, src_name) = get_source(sess);
|
||||
|
||||
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(
|
||||
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(),
|
||||
krate,
|
||||
src_name,
|
||||
src,
|
||||
annotation.pp_ann(),
|
||||
false,
|
||||
&*annotation,
|
||||
is_expanded,
|
||||
parse.edition,
|
||||
&sess.parse_sess.attr_id_generator,
|
||||
)
|
||||
})
|
||||
}
|
||||
AstTree(PpAstTreeMode::Normal) => {
|
||||
AstTree => {
|
||||
debug!("pretty printing AST tree");
|
||||
format!("{krate:#?}")
|
||||
ex.with_krate(|krate| format!("{krate:#?}"))
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
write_or_print(&out, sess);
|
||||
}
|
||||
|
||||
pub fn print_after_hir_lowering<'tcx>(tcx: TyCtxt<'tcx>, ppm: PpMode) {
|
||||
if ppm.needs_analysis() {
|
||||
abort_on_err(print_with_analysis(tcx, ppm), tcx.sess);
|
||||
return;
|
||||
}
|
||||
|
||||
let (src, src_name) = get_source(tcx.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(
|
||||
sess.source_map(),
|
||||
&tcx.resolver_for_lowering(()).borrow().1,
|
||||
AstTreeExpanded => {
|
||||
debug!("pretty-printing expanded AST");
|
||||
format!("{:#?}", ex.tcx().resolver_for_lowering(()).borrow().1)
|
||||
}
|
||||
Hir(s) => {
|
||||
debug!("pretty printing HIR {:?}", s);
|
||||
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,
|
||||
annotation.pp_ann(),
|
||||
true,
|
||||
parse.edition,
|
||||
&sess.parse_sess.attr_id_generator,
|
||||
&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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AstTree(PpAstTreeMode::Expanded) => {
|
||||
debug!("pretty-printing expanded AST");
|
||||
format!("{:#?}", tcx.resolver_for_lowering(()).borrow().1)
|
||||
}
|
||||
|
||||
Hir(s) => call_with_pp_support_hir(&s, tcx, move |annotation, hir_map| {
|
||||
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(),
|
||||
)
|
||||
}),
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -2925,8 +2925,8 @@ fn parse_pretty(handler: &EarlyErrorHandler, unstable_opts: &UnstableOptions) ->
|
||||
"expanded" => Source(PpSourceMode::Expanded),
|
||||
"expanded,identified" => Source(PpSourceMode::ExpandedIdentified),
|
||||
"expanded,hygiene" => Source(PpSourceMode::ExpandedHygiene),
|
||||
"ast-tree" => AstTree(PpAstTreeMode::Normal),
|
||||
"ast-tree,expanded" => AstTree(PpAstTreeMode::Expanded),
|
||||
"ast-tree" => AstTree,
|
||||
"ast-tree,expanded" => AstTreeExpanded,
|
||||
"hir" => Hir(PpHirMode::Normal),
|
||||
"hir,identified" => Hir(PpHirMode::Identified),
|
||||
"hir,typed" => Hir(PpHirMode::Typed),
|
||||
@ -3083,14 +3083,6 @@ pub enum PpSourceMode {
|
||||
ExpandedHygiene,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
pub enum PpAstTreeMode {
|
||||
/// `-Zunpretty=ast`
|
||||
Normal,
|
||||
/// `-Zunpretty=ast,expanded`
|
||||
Expanded,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
pub enum PpHirMode {
|
||||
/// `-Zunpretty=hir`
|
||||
@ -3106,7 +3098,10 @@ pub enum PpMode {
|
||||
/// Options that print the source code, i.e.
|
||||
/// `-Zunpretty=normal` and `-Zunpretty=expanded`
|
||||
Source(PpSourceMode),
|
||||
AstTree(PpAstTreeMode),
|
||||
/// `-Zunpretty=ast-tree`
|
||||
AstTree,
|
||||
/// `-Zunpretty=ast-tree,expanded`
|
||||
AstTreeExpanded,
|
||||
/// Options that print the HIR, i.e. `-Zunpretty=hir`
|
||||
Hir(PpHirMode),
|
||||
/// `-Zunpretty=hir-tree`
|
||||
@ -3126,10 +3121,10 @@ impl PpMode {
|
||||
use PpMode::*;
|
||||
use PpSourceMode::*;
|
||||
match *self {
|
||||
Source(Normal | Identified) | AstTree(PpAstTreeMode::Normal) => false,
|
||||
Source(Normal | Identified) | AstTree => false,
|
||||
|
||||
Source(Expanded | ExpandedIdentified | ExpandedHygiene)
|
||||
| AstTree(PpAstTreeMode::Expanded)
|
||||
| AstTreeExpanded
|
||||
| Hir(_)
|
||||
| HirTree
|
||||
| ThirTree
|
||||
@ -3141,7 +3136,7 @@ impl PpMode {
|
||||
pub fn needs_hir(&self) -> bool {
|
||||
use PpMode::*;
|
||||
match *self {
|
||||
Source(_) | AstTree(_) => false,
|
||||
Source(_) | AstTree | AstTreeExpanded => false,
|
||||
|
||||
Hir(_) | HirTree | ThirTree | ThirFlat | Mir | MirCFG => true,
|
||||
}
|
||||
@ -3149,7 +3144,7 @@ impl PpMode {
|
||||
|
||||
pub fn needs_analysis(&self) -> bool {
|
||||
use PpMode::*;
|
||||
matches!(*self, Mir | MirCFG | ThirTree | ThirFlat)
|
||||
matches!(*self, Hir(PpHirMode::Typed) | Mir | MirCFG | ThirTree | ThirFlat)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user