Skip to content

Commit 7ec20dd

Browse files
Remove pretty printing of specific nodes in AST
The ability to print a specific item as identified by NodeId or path seems not particularly useful, and certainly carries quite a bit of complexity with it.
1 parent f50d6ea commit 7ec20dd

File tree

9 files changed

+17
-215
lines changed

9 files changed

+17
-215
lines changed

src/librustc/session/config.rs

+7-110
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
//! Contains infrastructure for configuring the compiler, including parsing
22
//! command-line options.
33
4-
// ignore-tidy-filelength
5-
64
use crate::lint;
75
use crate::middle::cstore;
86
use crate::session::{early_error, early_warn, Session};
97
use crate::session::search_paths::SearchPath;
10-
use crate::hir::map as hir_map;
118

129
use rustc_data_structures::fx::FxHashSet;
1310

@@ -444,7 +441,7 @@ top_level_options!(
444441
// by the compiler.
445442
json_artifact_notifications: bool [TRACKED],
446443

447-
pretty: Option<(PpMode, Option<UserIdentifiedItem>)> [UNTRACKED],
444+
pretty: Option<PpMode> [UNTRACKED],
448445
}
449446
);
450447

@@ -2560,7 +2557,7 @@ fn parse_pretty(
25602557
matches: &getopts::Matches,
25612558
debugging_opts: &DebuggingOptions,
25622559
efmt: ErrorOutputType,
2563-
) -> Option<(PpMode, Option<UserIdentifiedItem>)> {
2560+
) -> Option<PpMode> {
25642561
let pretty = if debugging_opts.unstable_options {
25652562
matches.opt_default("pretty", "normal").map(|a| {
25662563
// stable pretty-print variants only
@@ -2583,13 +2580,10 @@ fn parse_pretty(
25832580
efmt: ErrorOutputType,
25842581
name: &str,
25852582
extended: bool,
2586-
) -> (PpMode, Option<UserIdentifiedItem>) {
2583+
) -> PpMode {
25872584
use PpMode::*;
25882585
use PpSourceMode::*;
2589-
let mut split = name.splitn(2, '=');
2590-
let first = split.next().unwrap();
2591-
let opt_second = split.next();
2592-
let first = match (first, extended) {
2586+
let first = match (name, extended) {
25932587
("normal", _) => PpmSource(PpmNormal),
25942588
("identified", _) => PpmSource(PpmIdentified),
25952589
("everybody_loops", true) => PpmSource(PpmEveryBodyLoops),
@@ -2617,8 +2611,7 @@ fn parse_pretty(
26172611
}
26182612
}
26192613
};
2620-
let opt_second = opt_second.and_then(|s| s.parse::<UserIdentifiedItem>().ok());
2621-
(first, opt_second)
2614+
first
26222615
}
26232616
}
26242617

@@ -2750,13 +2743,13 @@ pub enum PpMode {
27502743
}
27512744

27522745
impl PpMode {
2753-
pub fn needs_ast_map(&self, opt_uii: &Option<UserIdentifiedItem>) -> bool {
2746+
pub fn needs_ast_map(&self) -> bool {
27542747
use PpMode::*;
27552748
use PpSourceMode::*;
27562749
match *self {
27572750
PpmSource(PpmNormal) |
27582751
PpmSource(PpmEveryBodyLoops) |
2759-
PpmSource(PpmIdentified) => opt_uii.is_some(),
2752+
PpmSource(PpmIdentified) => false,
27602753

27612754
PpmSource(PpmExpanded) |
27622755
PpmSource(PpmExpandedIdentified) |
@@ -2778,102 +2771,6 @@ impl PpMode {
27782771
}
27792772
}
27802773

2781-
#[derive(Clone, Debug)]
2782-
pub enum UserIdentifiedItem {
2783-
ItemViaNode(ast::NodeId),
2784-
ItemViaPath(Vec<String>),
2785-
}
2786-
2787-
impl FromStr for UserIdentifiedItem {
2788-
type Err = ();
2789-
fn from_str(s: &str) -> Result<UserIdentifiedItem, ()> {
2790-
use UserIdentifiedItem::*;
2791-
Ok(s.parse()
2792-
.map(ast::NodeId::from_u32)
2793-
.map(ItemViaNode)
2794-
.unwrap_or_else(|_| ItemViaPath(s.split("::").map(|s| s.to_string()).collect())))
2795-
}
2796-
}
2797-
2798-
pub enum NodesMatchingUII<'a> {
2799-
NodesMatchingDirect(std::option::IntoIter<ast::NodeId>),
2800-
NodesMatchingSuffix(Box<dyn Iterator<Item = ast::NodeId> + 'a>),
2801-
}
2802-
2803-
impl<'a> Iterator for NodesMatchingUII<'a> {
2804-
type Item = ast::NodeId;
2805-
2806-
fn next(&mut self) -> Option<ast::NodeId> {
2807-
use NodesMatchingUII::*;
2808-
match self {
2809-
&mut NodesMatchingDirect(ref mut iter) => iter.next(),
2810-
&mut NodesMatchingSuffix(ref mut iter) => iter.next(),
2811-
}
2812-
}
2813-
2814-
fn size_hint(&self) -> (usize, Option<usize>) {
2815-
use NodesMatchingUII::*;
2816-
match self {
2817-
&NodesMatchingDirect(ref iter) => iter.size_hint(),
2818-
&NodesMatchingSuffix(ref iter) => iter.size_hint(),
2819-
}
2820-
}
2821-
}
2822-
2823-
impl UserIdentifiedItem {
2824-
pub fn reconstructed_input(&self) -> String {
2825-
use UserIdentifiedItem::*;
2826-
match *self {
2827-
ItemViaNode(node_id) => node_id.to_string(),
2828-
ItemViaPath(ref parts) => parts.join("::"),
2829-
}
2830-
}
2831-
2832-
pub fn all_matching_node_ids<'a, 'hir>(&'a self,
2833-
map: &'a hir_map::Map<'hir>)
2834-
-> NodesMatchingUII<'a> {
2835-
use UserIdentifiedItem::*;
2836-
use NodesMatchingUII::*;
2837-
match *self {
2838-
ItemViaNode(node_id) => NodesMatchingDirect(Some(node_id).into_iter()),
2839-
ItemViaPath(ref parts) => {
2840-
NodesMatchingSuffix(Box::new(map.nodes_matching_suffix(&parts)))
2841-
}
2842-
}
2843-
}
2844-
2845-
pub fn to_one_node_id(self,
2846-
user_option: &str,
2847-
sess: &Session,
2848-
map: &hir_map::Map<'_>)
2849-
-> ast::NodeId {
2850-
let fail_because = |is_wrong_because| -> ast::NodeId {
2851-
let message = format!("{} needs NodeId (int) or unique path suffix (b::c::d); got \
2852-
{}, which {}",
2853-
user_option,
2854-
self.reconstructed_input(),
2855-
is_wrong_because);
2856-
sess.fatal(&message)
2857-
};
2858-
2859-
let mut saw_node = ast::DUMMY_NODE_ID;
2860-
let mut seen = 0;
2861-
for node in self.all_matching_node_ids(map) {
2862-
saw_node = node;
2863-
seen += 1;
2864-
if seen > 1 {
2865-
fail_because("does not resolve uniquely");
2866-
}
2867-
}
2868-
if seen == 0 {
2869-
fail_because("does not resolve to any item");
2870-
}
2871-
2872-
assert!(seen == 1);
2873-
return saw_node;
2874-
}
2875-
}
2876-
28772774
/// Command-line arguments passed to the compiler have to be incorporated with
28782775
/// the dependency tracking system for incremental compilation. This module
28792776
/// provides some utilities to make this more convenient.

src/librustc_driver/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,15 @@ pub fn run_compiler(
292292

293293
compiler.parse()?;
294294

295-
if let Some((ppm, opt_uii)) = &sess.opts.pretty {
296-
if ppm.needs_ast_map(&opt_uii) {
295+
if let Some(ppm) = &sess.opts.pretty {
296+
if ppm.needs_ast_map() {
297297
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
298298
let expanded_crate = compiler.expansion()?.take().0;
299299
pretty::print_after_hir_lowering(
300300
tcx,
301301
compiler.input(),
302302
&expanded_crate,
303303
*ppm,
304-
opt_uii.clone(),
305304
compiler.output_file().as_ref().map(|p| &**p),
306305
);
307306
Ok(())

src/librustc_driver/pretty.rs

+7-58
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc::hir::map as hir_map;
55
use rustc::hir::print as pprust_hir;
66
use rustc::hir::def_id::LOCAL_CRATE;
77
use rustc::session::Session;
8-
use rustc::session::config::{PpMode, PpSourceMode, UserIdentifiedItem, Input};
8+
use rustc::session::config::{PpMode, PpSourceMode, Input};
99
use rustc::ty::{self, TyCtxt};
1010
use rustc::util::common::ErrorReported;
1111
use rustc_mir::util::{write_mir_pretty, write_mir_graphviz};
@@ -19,7 +19,6 @@ use std::fs::File;
1919
use std::io::Write;
2020
use std::path::Path;
2121

22-
pub use self::UserIdentifiedItem::*;
2322
pub use self::PpSourceMode::*;
2423
pub use self::PpMode::*;
2524
use crate::abort_on_err;
@@ -448,14 +447,12 @@ pub fn print_after_hir_lowering<'tcx>(
448447
input: &Input,
449448
krate: &ast::Crate,
450449
ppm: PpMode,
451-
opt_uii: Option<UserIdentifiedItem>,
452450
ofile: Option<&Path>,
453451
) {
454452
if ppm.needs_analysis() {
455453
abort_on_err(print_with_analysis(
456454
tcx,
457455
ppm,
458-
opt_uii,
459456
ofile
460457
), tcx.sess);
461458
return;
@@ -465,8 +462,8 @@ pub fn print_after_hir_lowering<'tcx>(
465462

466463
let mut out = String::new();
467464

468-
match (ppm, opt_uii) {
469-
(PpmSource(s), _) => {
465+
match ppm {
466+
PpmSource(s) => {
470467
// Silently ignores an identified node.
471468
let out = &mut out;
472469
let src = src.clone();
@@ -483,7 +480,7 @@ pub fn print_after_hir_lowering<'tcx>(
483480
})
484481
}
485482

486-
(PpmHir(s), None) => {
483+
PpmHir(s) => {
487484
let out = &mut out;
488485
let src = src.clone();
489486
call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
@@ -498,52 +495,14 @@ pub fn print_after_hir_lowering<'tcx>(
498495
})
499496
}
500497

501-
(PpmHirTree(s), None) => {
498+
PpmHirTree(s) => {
502499
let out = &mut out;
503500
call_with_pp_support_hir(&s, tcx, move |_annotation, krate| {
504501
debug!("pretty printing source code {:?}", s);
505502
*out = format!("{:#?}", krate);
506503
});
507504
}
508505

509-
(PpmHir(s), Some(uii)) => {
510-
let out = &mut out;
511-
let src = src.clone();
512-
call_with_pp_support_hir(&s, tcx, move |annotation, _| {
513-
debug!("pretty printing source code {:?}", s);
514-
let sess = annotation.sess();
515-
let hir_map = annotation.hir_map().expect("-Z unpretty missing HIR map");
516-
let mut pp_state = pprust_hir::State::new_from_input(sess.source_map(),
517-
&sess.parse_sess,
518-
src_name,
519-
src,
520-
annotation.pp_ann());
521-
for node_id in uii.all_matching_node_ids(hir_map) {
522-
let hir_id = tcx.hir().node_to_hir_id(node_id);
523-
let node = hir_map.get(hir_id);
524-
pp_state.print_node(node);
525-
pp_state.s.space();
526-
let path = annotation.node_path(hir_id)
527-
.expect("-Z unpretty missing node paths");
528-
pp_state.synth_comment(path);
529-
pp_state.s.hardbreak();
530-
}
531-
*out = pp_state.s.eof();
532-
})
533-
}
534-
535-
(PpmHirTree(s), Some(uii)) => {
536-
let out = &mut out;
537-
call_with_pp_support_hir(&s, tcx, move |_annotation, _krate| {
538-
debug!("pretty printing source code {:?}", s);
539-
for node_id in uii.all_matching_node_ids(tcx.hir()) {
540-
let hir_id = tcx.hir().node_to_hir_id(node_id);
541-
let node = tcx.hir().get(hir_id);
542-
out.push_str(&format!("{:#?}", node));
543-
}
544-
})
545-
}
546-
547506
_ => unreachable!(),
548507
}
549508

@@ -557,27 +516,17 @@ pub fn print_after_hir_lowering<'tcx>(
557516
fn print_with_analysis(
558517
tcx: TyCtxt<'_>,
559518
ppm: PpMode,
560-
uii: Option<UserIdentifiedItem>,
561519
ofile: Option<&Path>,
562520
) -> Result<(), ErrorReported> {
563-
let nodeid = if let Some(uii) = uii {
564-
debug!("pretty printing for {:?}", uii);
565-
Some(uii.to_one_node_id("-Z unpretty", tcx.sess, tcx.hir()))
566-
} else {
567-
debug!("pretty printing for whole crate");
568-
None
569-
};
570-
571521
let mut out = Vec::new();
572522

573523
tcx.analysis(LOCAL_CRATE)?;
574524

575525
match ppm {
576526
PpmMir | PpmMirCFG => {
577-
let def_id = nodeid.map(|nid| tcx.hir().local_def_id_from_node_id(nid));
578527
match ppm {
579-
PpmMir => write_mir_pretty(tcx, def_id, &mut out),
580-
PpmMirCFG => write_mir_graphviz(tcx, def_id, &mut out),
528+
PpmMir => write_mir_pretty(tcx, None, &mut out),
529+
PpmMirCFG => write_mir_graphviz(tcx, None, &mut out),
581530
_ => unreachable!(),
582531
}
583532
}

src/librustc_interface/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ fn configure_and_expand_inner<'a>(
388388
// If we're actually rustdoc then there's no need to actually compile
389389
// anything, so switch everything to just looping
390390
let mut should_loop = sess.opts.actually_rustdoc;
391-
if let Some((PpMode::PpmSource(PpSourceMode::PpmEveryBodyLoops), _)) = sess.opts.pretty {
391+
if let Some(PpMode::PpmSource(PpSourceMode::PpmEveryBodyLoops)) = sess.opts.pretty {
392392
should_loop |= true;
393393
}
394394
if should_loop {

src/test/run-make-fulldeps/pretty-print-path-suffix/Makefile

-9
This file was deleted.

src/test/run-make-fulldeps/pretty-print-path-suffix/foo.pp

-5
This file was deleted.

src/test/run-make-fulldeps/pretty-print-path-suffix/foo_method.pp

-7
This file was deleted.

src/test/run-make-fulldeps/pretty-print-path-suffix/input.rs

-18
This file was deleted.

src/test/run-make-fulldeps/pretty-print-path-suffix/nest_foo.pp

-4
This file was deleted.

0 commit comments

Comments
 (0)