Skip to content

Commit c66b508

Browse files
authored
Rollup merge of rust-lang#66575 - Mark-Simulacrum:no-uii, r=petrochenkov
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. This is intended to simplify our CLI parsing a bit and remove a non-uncomplicated piece of it; I largely did this to remove the dependency on NodeId from librustc/session but it's not really necessary to do so in this invasive a way. The alternative is moving it to librustc_interface or driver, probably.
2 parents afc78e1 + 7ec20dd commit c66b508

File tree

9 files changed

+17
-215
lines changed

9 files changed

+17
-215
lines changed

src/librustc/session/config.rs

Lines changed: 7 additions & 110 deletions
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

@@ -2562,7 +2559,7 @@ fn parse_pretty(
25622559
matches: &getopts::Matches,
25632560
debugging_opts: &DebuggingOptions,
25642561
efmt: ErrorOutputType,
2565-
) -> Option<(PpMode, Option<UserIdentifiedItem>)> {
2562+
) -> Option<PpMode> {
25662563
let pretty = if debugging_opts.unstable_options {
25672564
matches.opt_default("pretty", "normal").map(|a| {
25682565
// stable pretty-print variants only
@@ -2585,13 +2582,10 @@ fn parse_pretty(
25852582
efmt: ErrorOutputType,
25862583
name: &str,
25872584
extended: bool,
2588-
) -> (PpMode, Option<UserIdentifiedItem>) {
2585+
) -> PpMode {
25892586
use PpMode::*;
25902587
use PpSourceMode::*;
2591-
let mut split = name.splitn(2, '=');
2592-
let first = split.next().unwrap();
2593-
let opt_second = split.next();
2594-
let first = match (first, extended) {
2588+
let first = match (name, extended) {
25952589
("normal", _) => PpmSource(PpmNormal),
25962590
("identified", _) => PpmSource(PpmIdentified),
25972591
("everybody_loops", true) => PpmSource(PpmEveryBodyLoops),
@@ -2619,8 +2613,7 @@ fn parse_pretty(
26192613
}
26202614
}
26212615
};
2622-
let opt_second = opt_second.and_then(|s| s.parse::<UserIdentifiedItem>().ok());
2623-
(first, opt_second)
2616+
first
26242617
}
26252618
}
26262619

@@ -2752,13 +2745,13 @@ pub enum PpMode {
27522745
}
27532746

27542747
impl PpMode {
2755-
pub fn needs_ast_map(&self, opt_uii: &Option<UserIdentifiedItem>) -> bool {
2748+
pub fn needs_ast_map(&self) -> bool {
27562749
use PpMode::*;
27572750
use PpSourceMode::*;
27582751
match *self {
27592752
PpmSource(PpmNormal) |
27602753
PpmSource(PpmEveryBodyLoops) |
2761-
PpmSource(PpmIdentified) => opt_uii.is_some(),
2754+
PpmSource(PpmIdentified) => false,
27622755

27632756
PpmSource(PpmExpanded) |
27642757
PpmSource(PpmExpandedIdentified) |
@@ -2780,102 +2773,6 @@ impl PpMode {
27802773
}
27812774
}
27822775

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

src/librustc_driver/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,15 @@ pub fn run_compiler(
285285

286286
compiler.parse()?;
287287

288-
if let Some((ppm, opt_uii)) = &sess.opts.pretty {
289-
if ppm.needs_ast_map(&opt_uii) {
288+
if let Some(ppm) = &sess.opts.pretty {
289+
if ppm.needs_ast_map() {
290290
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
291291
let expanded_crate = compiler.expansion()?.take().0;
292292
pretty::print_after_hir_lowering(
293293
tcx,
294294
compiler.input(),
295295
&expanded_crate,
296296
*ppm,
297-
opt_uii.clone(),
298297
compiler.output_file().as_ref().map(|p| &**p),
299298
);
300299
Ok(())

src/librustc_driver/pretty.rs

Lines changed: 7 additions & 58 deletions
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;
@@ -444,14 +443,12 @@ pub fn print_after_hir_lowering<'tcx>(
444443
input: &Input,
445444
krate: &ast::Crate,
446445
ppm: PpMode,
447-
opt_uii: Option<UserIdentifiedItem>,
448446
ofile: Option<&Path>,
449447
) {
450448
if ppm.needs_analysis() {
451449
abort_on_err(print_with_analysis(
452450
tcx,
453451
ppm,
454-
opt_uii,
455452
ofile
456453
), tcx.sess);
457454
return;
@@ -461,8 +458,8 @@ pub fn print_after_hir_lowering<'tcx>(
461458

462459
let mut out = String::new();
463460

464-
match (ppm, opt_uii) {
465-
(PpmSource(s), _) => {
461+
match ppm {
462+
PpmSource(s) => {
466463
// Silently ignores an identified node.
467464
let out = &mut out;
468465
let src = src.clone();
@@ -479,7 +476,7 @@ pub fn print_after_hir_lowering<'tcx>(
479476
})
480477
}
481478

482-
(PpmHir(s), None) => {
479+
PpmHir(s) => {
483480
let out = &mut out;
484481
let src = src.clone();
485482
call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
@@ -494,52 +491,14 @@ pub fn print_after_hir_lowering<'tcx>(
494491
})
495492
}
496493

497-
(PpmHirTree(s), None) => {
494+
PpmHirTree(s) => {
498495
let out = &mut out;
499496
call_with_pp_support_hir(&s, tcx, move |_annotation, krate| {
500497
debug!("pretty printing source code {:?}", s);
501498
*out = format!("{:#?}", krate);
502499
});
503500
}
504501

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

@@ -553,27 +512,17 @@ pub fn print_after_hir_lowering<'tcx>(
553512
fn print_with_analysis(
554513
tcx: TyCtxt<'_>,
555514
ppm: PpMode,
556-
uii: Option<UserIdentifiedItem>,
557515
ofile: Option<&Path>,
558516
) -> Result<(), ErrorReported> {
559-
let nodeid = if let Some(uii) = uii {
560-
debug!("pretty printing for {:?}", uii);
561-
Some(uii.to_one_node_id("-Z unpretty", tcx.sess, tcx.hir()))
562-
} else {
563-
debug!("pretty printing for whole crate");
564-
None
565-
};
566-
567517
let mut out = Vec::new();
568518

569519
tcx.analysis(LOCAL_CRATE)?;
570520

571521
match ppm {
572522
PpmMir | PpmMirCFG => {
573-
let def_id = nodeid.map(|nid| tcx.hir().local_def_id_from_node_id(nid));
574523
match ppm {
575-
PpmMir => write_mir_pretty(tcx, def_id, &mut out),
576-
PpmMirCFG => write_mir_graphviz(tcx, def_id, &mut out),
524+
PpmMir => write_mir_pretty(tcx, None, &mut out),
525+
PpmMirCFG => write_mir_graphviz(tcx, None, &mut out),
577526
_ => unreachable!(),
578527
}
579528
}

src/librustc_interface/passes.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 9 deletions
This file was deleted.

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

Lines changed: 0 additions & 5 deletions
This file was deleted.

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

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 0 additions & 18 deletions
This file was deleted.

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

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)