Skip to content

Commit

Permalink
Rollup merge of rust-lang#80785 - petrochenkov:prettyparen, r=Aaron1011
Browse files Browse the repository at this point in the history
rustc_ast_pretty: Remove `PrintState::insert_extra_parens`

It's no longer necessary after rust-lang#79472.

r? `@Aaron1011`
  • Loading branch information
JohnTitor authored Jan 8, 2021
2 parents c851ae9 + f915e3e commit f6c58af
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 36 deletions.
5 changes: 0 additions & 5 deletions compiler/rustc_ast_pretty/src/pprust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ use rustc_ast as ast;
use rustc_ast::token::{Nonterminal, Token, TokenKind};
use rustc_ast::tokenstream::{TokenStream, TokenTree};

pub fn nonterminal_to_string_no_extra_parens(nt: &Nonterminal) -> String {
let state = State::without_insert_extra_parens();
state.nonterminal_to_string(nt)
}

pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
State::new().nonterminal_to_string(nt)
}
Expand Down
28 changes: 2 additions & 26 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ pub struct State<'a> {
comments: Option<Comments<'a>>,
ann: &'a (dyn PpAnn + 'a),
is_expanded: bool,
// If `true`, additional parenthesis (separate from `ExprKind::Paren`)
// are inserted to ensure that proper precedence is preserved
// in the pretty-printed output.
//
// This is usually `true`, except when performing the pretty-print/reparse
// check in `nt_to_tokenstream`
insert_extra_parens: bool,
}

crate const INDENT_UNIT: usize = 4;
Expand All @@ -115,7 +108,6 @@ pub fn print_crate<'a>(
comments: Some(Comments::new(sm, filename, input)),
ann,
is_expanded,
insert_extra_parens: true,
};

if is_expanded && !krate.attrs.iter().any(|attr| attr.has_name(sym::no_core)) {
Expand Down Expand Up @@ -235,7 +227,6 @@ impl std::ops::DerefMut for State<'_> {
}

pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::DerefMut {
fn insert_extra_parens(&self) -> bool;
fn comments(&mut self) -> &mut Option<Comments<'a>>;
fn print_ident(&mut self, ident: Ident);
fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool);
Expand Down Expand Up @@ -819,16 +810,12 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere

fn to_string(&self, f: impl FnOnce(&mut State<'_>)) -> String {
let mut printer = State::new();
printer.insert_extra_parens = self.insert_extra_parens();
f(&mut printer);
printer.s.eof()
}
}

impl<'a> PrintState<'a> for State<'a> {
fn insert_extra_parens(&self) -> bool {
self.insert_extra_parens
}
fn comments(&mut self) -> &mut Option<Comments<'a>> {
&mut self.comments
}
Expand Down Expand Up @@ -865,17 +852,7 @@ impl<'a> PrintState<'a> for State<'a> {

impl<'a> State<'a> {
pub fn new() -> State<'a> {
State {
s: pp::mk_printer(),
comments: None,
ann: &NoAnn,
is_expanded: false,
insert_extra_parens: true,
}
}

pub(super) fn without_insert_extra_parens() -> State<'a> {
State { insert_extra_parens: false, ..State::new() }
State { s: pp::mk_printer(), comments: None, ann: &NoAnn, is_expanded: false }
}

// Synthesizes a comment that was not textually present in the original source
Expand Down Expand Up @@ -1680,8 +1657,7 @@ impl<'a> State<'a> {
}

/// Prints `expr` or `(expr)` when `needs_par` holds.
fn print_expr_cond_paren(&mut self, expr: &ast::Expr, mut needs_par: bool) {
needs_par &= self.insert_extra_parens;
fn print_expr_cond_paren(&mut self, expr: &ast::Expr, needs_par: bool) {
if needs_par {
self.popen();
}
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,6 @@ impl std::ops::DerefMut for State<'_> {
}

impl<'a> PrintState<'a> for State<'a> {
fn insert_extra_parens(&self) -> bool {
true
}
fn comments(&mut self) -> &mut Option<Comments<'a>> {
&mut self.comments
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,7 @@ pub fn nt_to_tokenstream(
} else if matches!(synthesize_tokens, CanSynthesizeMissingTokens::Yes) {
return fake_token_stream(sess, nt);
} else {
let pretty = rustc_ast_pretty::pprust::nonterminal_to_string_no_extra_parens(&nt);
panic!("Missing tokens for nt {:?}", pretty);
panic!("Missing tokens for nt {:?}", pprust::nonterminal_to_string(nt));
}
}

Expand Down

0 comments on commit f6c58af

Please sign in to comment.