From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_ast_pretty/src/helpers.rs | 48 + compiler/rustc_ast_pretty/src/lib.rs | 8 + compiler/rustc_ast_pretty/src/pp.rs | 451 +++++ compiler/rustc_ast_pretty/src/pp/convenience.rs | 94 ++ compiler/rustc_ast_pretty/src/pp/ring.rs | 77 + compiler/rustc_ast_pretty/src/pprust/mod.rs | 86 + compiler/rustc_ast_pretty/src/pprust/state.rs | 1770 ++++++++++++++++++++ .../rustc_ast_pretty/src/pprust/state/delimited.rs | 41 + compiler/rustc_ast_pretty/src/pprust/state/expr.rs | 621 +++++++ compiler/rustc_ast_pretty/src/pprust/state/item.rs | 708 ++++++++ compiler/rustc_ast_pretty/src/pprust/tests.rs | 63 + 11 files changed, 3967 insertions(+) create mode 100644 compiler/rustc_ast_pretty/src/helpers.rs create mode 100644 compiler/rustc_ast_pretty/src/lib.rs create mode 100644 compiler/rustc_ast_pretty/src/pp.rs create mode 100644 compiler/rustc_ast_pretty/src/pp/convenience.rs create mode 100644 compiler/rustc_ast_pretty/src/pp/ring.rs create mode 100644 compiler/rustc_ast_pretty/src/pprust/mod.rs create mode 100644 compiler/rustc_ast_pretty/src/pprust/state.rs create mode 100644 compiler/rustc_ast_pretty/src/pprust/state/delimited.rs create mode 100644 compiler/rustc_ast_pretty/src/pprust/state/expr.rs create mode 100644 compiler/rustc_ast_pretty/src/pprust/state/item.rs create mode 100644 compiler/rustc_ast_pretty/src/pprust/tests.rs (limited to 'compiler/rustc_ast_pretty/src') diff --git a/compiler/rustc_ast_pretty/src/helpers.rs b/compiler/rustc_ast_pretty/src/helpers.rs new file mode 100644 index 000000000..5ec71cddf --- /dev/null +++ b/compiler/rustc_ast_pretty/src/helpers.rs @@ -0,0 +1,48 @@ +use crate::pp::Printer; +use std::borrow::Cow; + +impl Printer { + pub fn word_space>>(&mut self, w: W) { + self.word(w); + self.space(); + } + + pub fn popen(&mut self) { + self.word("("); + } + + pub fn pclose(&mut self) { + self.word(")"); + } + + pub fn hardbreak_if_not_bol(&mut self) { + if !self.is_beginning_of_line() { + self.hardbreak() + } + } + + pub fn space_if_not_bol(&mut self) { + if !self.is_beginning_of_line() { + self.space(); + } + } + + pub fn nbsp(&mut self) { + self.word(" ") + } + + pub fn word_nbsp>>(&mut self, w: S) { + self.word(w); + self.nbsp() + } + + // Synthesizes a comment that was not textually present in the original + // source file. + pub fn synth_comment(&mut self, text: impl Into>) { + self.word("/*"); + self.space(); + self.word(text); + self.space(); + self.word("*/") + } +} diff --git a/compiler/rustc_ast_pretty/src/lib.rs b/compiler/rustc_ast_pretty/src/lib.rs new file mode 100644 index 000000000..79178830b --- /dev/null +++ b/compiler/rustc_ast_pretty/src/lib.rs @@ -0,0 +1,8 @@ +#![feature(associated_type_bounds)] +#![feature(box_patterns)] +#![feature(with_negative_coherence)] +#![recursion_limit = "256"] + +mod helpers; +pub mod pp; +pub mod pprust; diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs new file mode 100644 index 000000000..c93022308 --- /dev/null +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -0,0 +1,451 @@ +//! This pretty-printer is a direct reimplementation of Philip Karlton's +//! Mesa pretty-printer, as described in the appendix to +//! Derek C. Oppen, "Pretty Printing" (1979), +//! Stanford Computer Science Department STAN-CS-79-770, +//! . +//! +//! The algorithm's aim is to break a stream into as few lines as possible +//! while respecting the indentation-consistency requirements of the enclosing +//! block, and avoiding breaking at silly places on block boundaries, for +//! example, between "x" and ")" in "x)". +//! +//! I am implementing this algorithm because it comes with 20 pages of +//! documentation explaining its theory, and because it addresses the set of +//! concerns I've seen other pretty-printers fall down on. Weirdly. Even though +//! it's 32 years old. What can I say? +//! +//! Despite some redundancies and quirks in the way it's implemented in that +//! paper, I've opted to keep the implementation here as similar as I can, +//! changing only what was blatantly wrong, a typo, or sufficiently +//! non-idiomatic rust that it really stuck out. +//! +//! In particular you'll see a certain amount of churn related to INTEGER vs. +//! CARDINAL in the Mesa implementation. Mesa apparently interconverts the two +//! somewhat readily? In any case, I've used usize for indices-in-buffers and +//! ints for character-sizes-and-indentation-offsets. This respects the need +//! for ints to "go negative" while carrying a pending-calculation balance, and +//! helps differentiate all the numbers flying around internally (slightly). +//! +//! I also inverted the indentation arithmetic used in the print stack, since +//! the Mesa implementation (somewhat randomly) stores the offset on the print +//! stack in terms of margin-col rather than col itself. I store col. +//! +//! I also implemented a small change in the String token, in that I store an +//! explicit length for the string. For most tokens this is just the length of +//! the accompanying string. But it's necessary to permit it to differ, for +//! encoding things that are supposed to "go on their own line" -- certain +//! classes of comment and blank-line -- where relying on adjacent +//! hardbreak-like Break tokens with long blankness indication doesn't actually +//! work. To see why, consider when there is a "thing that should be on its own +//! line" between two long blocks, say functions. If you put a hardbreak after +//! each function (or before each) and the breaking algorithm decides to break +//! there anyways (because the functions themselves are long) you wind up with +//! extra blank lines. If you don't put hardbreaks you can wind up with the +//! "thing which should be on its own line" not getting its own line in the +//! rare case of "really small functions" or such. This re-occurs with comments +//! and explicit blank lines. So in those cases we use a string with a payload +//! we want isolated to a line and an explicit length that's huge, surrounded +//! by two zero-length breaks. The algorithm will try its best to fit it on a +//! line (which it can't) and so naturally place the content on its own line to +//! avoid combining it with other lines and making matters even worse. +//! +//! # Explanation +//! +//! In case you do not have the paper, here is an explanation of what's going +//! on. +//! +//! There is a stream of input tokens flowing through this printer. +//! +//! The printer buffers up to 3N tokens inside itself, where N is linewidth. +//! Yes, linewidth is chars and tokens are multi-char, but in the worst +//! case every token worth buffering is 1 char long, so it's ok. +//! +//! Tokens are String, Break, and Begin/End to delimit blocks. +//! +//! Begin tokens can carry an offset, saying "how far to indent when you break +//! inside here", as well as a flag indicating "consistent" or "inconsistent" +//! breaking. Consistent breaking means that after the first break, no attempt +//! will be made to flow subsequent breaks together onto lines. Inconsistent +//! is the opposite. Inconsistent breaking example would be, say: +//! +//! ```ignore (illustrative) +//! foo(hello, there, good, friends) +//! ``` +//! +//! breaking inconsistently to become +//! +//! ```ignore (illustrative) +//! foo(hello, there, +//! good, friends); +//! ``` +//! +//! whereas a consistent breaking would yield: +//! +//! ```ignore (illustrative) +//! foo(hello, +//! there, +//! good, +//! friends); +//! ``` +//! +//! That is, in the consistent-break blocks we value vertical alignment +//! more than the ability to cram stuff onto a line. But in all cases if it +//! can make a block a one-liner, it'll do so. +//! +//! Carrying on with high-level logic: +//! +//! The buffered tokens go through a ring-buffer, 'tokens'. The 'left' and +//! 'right' indices denote the active portion of the ring buffer as well as +//! describing hypothetical points-in-the-infinite-stream at most 3N tokens +//! apart (i.e., "not wrapped to ring-buffer boundaries"). The paper will switch +//! between using 'left' and 'right' terms to denote the wrapped-to-ring-buffer +//! and point-in-infinite-stream senses freely. +//! +//! There is a parallel ring buffer, `size`, that holds the calculated size of +//! each token. Why calculated? Because for Begin/End pairs, the "size" +//! includes everything between the pair. That is, the "size" of Begin is +//! actually the sum of the sizes of everything between Begin and the paired +//! End that follows. Since that is arbitrarily far in the future, `size` is +//! being rewritten regularly while the printer runs; in fact most of the +//! machinery is here to work out `size` entries on the fly (and give up when +//! they're so obviously over-long that "infinity" is a good enough +//! approximation for purposes of line breaking). +//! +//! The "input side" of the printer is managed as an abstract process called +//! SCAN, which uses `scan_stack`, to manage calculating `size`. SCAN is, in +//! other words, the process of calculating 'size' entries. +//! +//! The "output side" of the printer is managed by an abstract process called +//! PRINT, which uses `print_stack`, `margin` and `space` to figure out what to +//! do with each token/size pair it consumes as it goes. It's trying to consume +//! the entire buffered window, but can't output anything until the size is >= +//! 0 (sizes are set to negative while they're pending calculation). +//! +//! So SCAN takes input and buffers tokens and pending calculations, while +//! PRINT gobbles up completed calculations and tokens from the buffer. The +//! theory is that the two can never get more than 3N tokens apart, because +//! once there's "obviously" too much data to fit on a line, in a size +//! calculation, SCAN will write "infinity" to the size and let PRINT consume +//! it. +//! +//! In this implementation (following the paper, again) the SCAN process is the +//! methods called `Printer::scan_*`, and the 'PRINT' process is the +//! method called `Printer::print`. + +mod convenience; +mod ring; + +use ring::RingBuffer; +use std::borrow::Cow; +use std::cmp; +use std::collections::VecDeque; +use std::iter; + +/// How to break. Described in more detail in the module docs. +#[derive(Clone, Copy, PartialEq)] +pub enum Breaks { + Consistent, + Inconsistent, +} + +#[derive(Clone, Copy, PartialEq)] +enum IndentStyle { + /// Vertically aligned under whatever column this block begins at. + /// + /// fn demo(arg1: usize, + /// arg2: usize) {} + Visual, + /// Indented relative to the indentation level of the previous line. + /// + /// fn demo( + /// arg1: usize, + /// arg2: usize, + /// ) {} + Block { offset: isize }, +} + +#[derive(Clone, Copy, Default, PartialEq)] +pub struct BreakToken { + offset: isize, + blank_space: isize, + pre_break: Option, +} + +#[derive(Clone, Copy, PartialEq)] +pub struct BeginToken { + indent: IndentStyle, + breaks: Breaks, +} + +#[derive(Clone, PartialEq)] +pub enum Token { + // In practice a string token contains either a `&'static str` or a + // `String`. `Cow` is overkill for this because we never modify the data, + // but it's more convenient than rolling our own more specialized type. + String(Cow<'static, str>), + Break(BreakToken), + Begin(BeginToken), + End, +} + +#[derive(Copy, Clone)] +enum PrintFrame { + Fits, + Broken { indent: usize, breaks: Breaks }, +} + +const SIZE_INFINITY: isize = 0xffff; + +/// Target line width. +const MARGIN: isize = 78; +/// Every line is allowed at least this much space, even if highly indented. +const MIN_SPACE: isize = 60; + +pub struct Printer { + out: String, + /// Number of spaces left on line + space: isize, + /// Ring-buffer of tokens and calculated sizes + buf: RingBuffer, + /// Running size of stream "...left" + left_total: isize, + /// Running size of stream "...right" + right_total: isize, + /// Pseudo-stack, really a ring too. Holds the + /// primary-ring-buffers index of the Begin that started the + /// current block, possibly with the most recent Break after that + /// Begin (if there is any) on top of it. Stuff is flushed off the + /// bottom as it becomes irrelevant due to the primary ring-buffer + /// advancing. + scan_stack: VecDeque, + /// Stack of blocks-in-progress being flushed by print + print_stack: Vec, + /// Level of indentation of current line + indent: usize, + /// Buffered indentation to avoid writing trailing whitespace + pending_indentation: isize, + /// The token most recently popped from the left boundary of the + /// ring-buffer for printing + last_printed: Option, +} + +#[derive(Clone)] +struct BufEntry { + token: Token, + size: isize, +} + +impl Printer { + pub fn new() -> Self { + Printer { + out: String::new(), + space: MARGIN, + buf: RingBuffer::new(), + left_total: 0, + right_total: 0, + scan_stack: VecDeque::new(), + print_stack: Vec::new(), + indent: 0, + pending_indentation: 0, + last_printed: None, + } + } + + pub fn last_token(&self) -> Option<&Token> { + self.last_token_still_buffered().or_else(|| self.last_printed.as_ref()) + } + + pub fn last_token_still_buffered(&self) -> Option<&Token> { + self.buf.last().map(|last| &last.token) + } + + /// Be very careful with this! + pub fn replace_last_token_still_buffered(&mut self, token: Token) { + self.buf.last_mut().unwrap().token = token; + } + + fn scan_eof(&mut self) { + if !self.scan_stack.is_empty() { + self.check_stack(0); + self.advance_left(); + } + } + + fn scan_begin(&mut self, token: BeginToken) { + if self.scan_stack.is_empty() { + self.left_total = 1; + self.right_total = 1; + self.buf.clear(); + } + let right = self.buf.push(BufEntry { token: Token::Begin(token), size: -self.right_total }); + self.scan_stack.push_back(right); + } + + fn scan_end(&mut self) { + if self.scan_stack.is_empty() { + self.print_end(); + } else { + let right = self.buf.push(BufEntry { token: Token::End, size: -1 }); + self.scan_stack.push_back(right); + } + } + + fn scan_break(&mut self, token: BreakToken) { + if self.scan_stack.is_empty() { + self.left_total = 1; + self.right_total = 1; + self.buf.clear(); + } else { + self.check_stack(0); + } + let right = self.buf.push(BufEntry { token: Token::Break(token), size: -self.right_total }); + self.scan_stack.push_back(right); + self.right_total += token.blank_space; + } + + fn scan_string(&mut self, string: Cow<'static, str>) { + if self.scan_stack.is_empty() { + self.print_string(&string); + } else { + let len = string.len() as isize; + self.buf.push(BufEntry { token: Token::String(string), size: len }); + self.right_total += len; + self.check_stream(); + } + } + + pub fn offset(&mut self, offset: isize) { + if let Some(BufEntry { token: Token::Break(token), .. }) = &mut self.buf.last_mut() { + token.offset += offset; + } + } + + fn check_stream(&mut self) { + while self.right_total - self.left_total > self.space { + if *self.scan_stack.front().unwrap() == self.buf.index_of_first() { + self.scan_stack.pop_front().unwrap(); + self.buf.first_mut().unwrap().size = SIZE_INFINITY; + } + self.advance_left(); + if self.buf.is_empty() { + break; + } + } + } + + fn advance_left(&mut self) { + while self.buf.first().unwrap().size >= 0 { + let left = self.buf.pop_first().unwrap(); + + match &left.token { + Token::String(string) => { + self.left_total += string.len() as isize; + self.print_string(string); + } + Token::Break(token) => { + self.left_total += token.blank_space; + self.print_break(*token, left.size); + } + Token::Begin(token) => self.print_begin(*token, left.size), + Token::End => self.print_end(), + } + + self.last_printed = Some(left.token); + + if self.buf.is_empty() { + break; + } + } + } + + fn check_stack(&mut self, mut depth: usize) { + while let Some(&index) = self.scan_stack.back() { + let mut entry = &mut self.buf[index]; + match entry.token { + Token::Begin(_) => { + if depth == 0 { + break; + } + self.scan_stack.pop_back().unwrap(); + entry.size += self.right_total; + depth -= 1; + } + Token::End => { + // paper says + not =, but that makes no sense. + self.scan_stack.pop_back().unwrap(); + entry.size = 1; + depth += 1; + } + _ => { + self.scan_stack.pop_back().unwrap(); + entry.size += self.right_total; + if depth == 0 { + break; + } + } + } + } + } + + fn get_top(&self) -> PrintFrame { + *self + .print_stack + .last() + .unwrap_or(&PrintFrame::Broken { indent: 0, breaks: Breaks::Inconsistent }) + } + + fn print_begin(&mut self, token: BeginToken, size: isize) { + if size > self.space { + self.print_stack.push(PrintFrame::Broken { indent: self.indent, breaks: token.breaks }); + self.indent = match token.indent { + IndentStyle::Block { offset } => { + usize::try_from(self.indent as isize + offset).unwrap() + } + IndentStyle::Visual => (MARGIN - self.space) as usize, + }; + } else { + self.print_stack.push(PrintFrame::Fits); + } + } + + fn print_end(&mut self) { + if let PrintFrame::Broken { indent, .. } = self.print_stack.pop().unwrap() { + self.indent = indent; + } + } + + fn print_break(&mut self, token: BreakToken, size: isize) { + let fits = match self.get_top() { + PrintFrame::Fits => true, + PrintFrame::Broken { breaks: Breaks::Consistent, .. } => false, + PrintFrame::Broken { breaks: Breaks::Inconsistent, .. } => size <= self.space, + }; + if fits { + self.pending_indentation += token.blank_space; + self.space -= token.blank_space; + } else { + if let Some(pre_break) = token.pre_break { + self.out.push(pre_break); + } + self.out.push('\n'); + let indent = self.indent as isize + token.offset; + self.pending_indentation = indent; + self.space = cmp::max(MARGIN - indent, MIN_SPACE); + } + } + + fn print_string(&mut self, string: &str) { + // Write the pending indent. A more concise way of doing this would be: + // + // write!(self.out, "{: >n$}", "", n = self.pending_indentation as usize)?; + // + // But that is significantly slower. This code is sufficiently hot, and indents can get + // sufficiently large, that the difference is significant on some workloads. + self.out.reserve(self.pending_indentation as usize); + self.out.extend(iter::repeat(' ').take(self.pending_indentation as usize)); + self.pending_indentation = 0; + + self.out.push_str(string); + self.space -= string.len() as isize; + } +} diff --git a/compiler/rustc_ast_pretty/src/pp/convenience.rs b/compiler/rustc_ast_pretty/src/pp/convenience.rs new file mode 100644 index 000000000..93310dd45 --- /dev/null +++ b/compiler/rustc_ast_pretty/src/pp/convenience.rs @@ -0,0 +1,94 @@ +use crate::pp::{BeginToken, BreakToken, Breaks, IndentStyle, Printer, Token, SIZE_INFINITY}; +use std::borrow::Cow; + +impl Printer { + /// "raw box" + pub fn rbox(&mut self, indent: isize, breaks: Breaks) { + self.scan_begin(BeginToken { indent: IndentStyle::Block { offset: indent }, breaks }) + } + + /// Inconsistent breaking box + pub fn ibox(&mut self, indent: isize) { + self.rbox(indent, Breaks::Inconsistent) + } + + /// Consistent breaking box + pub fn cbox(&mut self, indent: isize) { + self.rbox(indent, Breaks::Consistent) + } + + pub fn visual_align(&mut self) { + self.scan_begin(BeginToken { indent: IndentStyle::Visual, breaks: Breaks::Consistent }); + } + + pub fn break_offset(&mut self, n: usize, off: isize) { + self.scan_break(BreakToken { + offset: off, + blank_space: n as isize, + ..BreakToken::default() + }); + } + + pub fn end(&mut self) { + self.scan_end() + } + + pub fn eof(mut self) -> String { + self.scan_eof(); + self.out + } + + pub fn word>>(&mut self, wrd: S) { + let string = wrd.into(); + self.scan_string(string) + } + + fn spaces(&mut self, n: usize) { + self.break_offset(n, 0) + } + + pub fn zerobreak(&mut self) { + self.spaces(0) + } + + pub fn space(&mut self) { + self.spaces(1) + } + + pub fn hardbreak(&mut self) { + self.spaces(SIZE_INFINITY as usize) + } + + pub fn is_beginning_of_line(&self) -> bool { + match self.last_token() { + Some(last_token) => last_token.is_hardbreak_tok(), + None => true, + } + } + + pub fn hardbreak_tok_offset(off: isize) -> Token { + Token::Break(BreakToken { + offset: off, + blank_space: SIZE_INFINITY, + ..BreakToken::default() + }) + } + + pub fn trailing_comma(&mut self) { + self.scan_break(BreakToken { pre_break: Some(','), ..BreakToken::default() }); + } + + pub fn trailing_comma_or_space(&mut self) { + self.scan_break(BreakToken { + blank_space: 1, + pre_break: Some(','), + ..BreakToken::default() + }); + } +} + +impl Token { + pub fn is_hardbreak_tok(&self) -> bool { + *self == Printer::hardbreak_tok_offset(0) + } +} diff --git a/compiler/rustc_ast_pretty/src/pp/ring.rs b/compiler/rustc_ast_pretty/src/pp/ring.rs new file mode 100644 index 000000000..8187394fe --- /dev/null +++ b/compiler/rustc_ast_pretty/src/pp/ring.rs @@ -0,0 +1,77 @@ +use std::collections::VecDeque; +use std::ops::{Index, IndexMut}; + +/// A view onto a finite range of an infinitely long sequence of T. +/// +/// The Ts are indexed 0..infinity. A RingBuffer begins as a view of elements +/// 0..0 (i.e. nothing). The user of the RingBuffer advances its left and right +/// position independently, although only in the positive direction, and only +/// with left <= right at all times. +/// +/// Holding a RingBuffer whose view is elements left..right gives the ability to +/// use Index and IndexMut to access elements i in the infinitely long queue for +/// which left <= i < right. +pub struct RingBuffer { + data: VecDeque, + // Abstract index of data[0] in the infinitely sized queue. + offset: usize, +} + +impl RingBuffer { + pub fn new() -> Self { + RingBuffer { data: VecDeque::new(), offset: 0 } + } + + pub fn is_empty(&self) -> bool { + self.data.is_empty() + } + + pub fn push(&mut self, value: T) -> usize { + let index = self.offset + self.data.len(); + self.data.push_back(value); + index + } + + pub fn clear(&mut self) { + self.data.clear(); + } + + pub fn index_of_first(&self) -> usize { + self.offset + } + + pub fn first(&self) -> Option<&T> { + self.data.front() + } + + pub fn first_mut(&mut self) -> Option<&mut T> { + self.data.front_mut() + } + + pub fn pop_first(&mut self) -> Option { + let first = self.data.pop_front()?; + self.offset += 1; + Some(first) + } + + pub fn last(&self) -> Option<&T> { + self.data.back() + } + + pub fn last_mut(&mut self) -> Option<&mut T> { + self.data.back_mut() + } +} + +impl Index for RingBuffer { + type Output = T; + fn index(&self, index: usize) -> &Self::Output { + &self.data[index.checked_sub(self.offset).unwrap()] + } +} + +impl IndexMut for RingBuffer { + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + &mut self.data[index.checked_sub(self.offset).unwrap()] + } +} diff --git a/compiler/rustc_ast_pretty/src/pprust/mod.rs b/compiler/rustc_ast_pretty/src/pprust/mod.rs new file mode 100644 index 000000000..ac9e7d06c --- /dev/null +++ b/compiler/rustc_ast_pretty/src/pprust/mod.rs @@ -0,0 +1,86 @@ +#[cfg(test)] +mod tests; + +pub mod state; +pub use state::{print_crate, AnnNode, Comments, PpAnn, PrintState, State}; + +use rustc_ast as ast; +use rustc_ast::token::{Nonterminal, Token, TokenKind}; +use rustc_ast::tokenstream::{TokenStream, TokenTree}; + +use std::borrow::Cow; + +pub fn nonterminal_to_string(nt: &Nonterminal) -> String { + State::new().nonterminal_to_string(nt) +} + +/// Print the token kind precisely, without converting `$crate` into its respective crate name. +pub fn token_kind_to_string(tok: &TokenKind) -> Cow<'static, str> { + State::new().token_kind_to_string(tok) +} + +/// Print the token precisely, without converting `$crate` into its respective crate name. +pub fn token_to_string(token: &Token) -> Cow<'static, str> { + State::new().token_to_string(token) +} + +pub fn ty_to_string(ty: &ast::Ty) -> String { + State::new().ty_to_string(ty) +} + +pub fn bounds_to_string(bounds: &[ast::GenericBound]) -> String { + State::new().bounds_to_string(bounds) +} + +pub fn pat_to_string(pat: &ast::Pat) -> String { + State::new().pat_to_string(pat) +} + +pub fn expr_to_string(e: &ast::Expr) -> String { + State::new().expr_to_string(e) +} + +pub fn tt_to_string(tt: &TokenTree) -> String { + State::new().tt_to_string(tt) +} + +pub fn tts_to_string(tokens: &TokenStream) -> String { + State::new().tts_to_string(tokens) +} + +pub fn item_to_string(i: &ast::Item) -> String { + State::new().item_to_string(i) +} + +pub fn path_to_string(p: &ast::Path) -> String { + State::new().path_to_string(p) +} + +pub fn path_segment_to_string(p: &ast::PathSegment) -> String { + State::new().path_segment_to_string(p) +} + +pub fn vis_to_string(v: &ast::Visibility) -> String { + State::new().vis_to_string(v) +} + +pub fn meta_list_item_to_string(li: &ast::NestedMetaItem) -> String { + State::new().meta_list_item_to_string(li) +} + +pub fn attribute_to_string(attr: &ast::Attribute) -> String { + State::new().attribute_to_string(attr) +} + +pub fn to_string(f: impl FnOnce(&mut State<'_>)) -> String { + State::to_string(f) +} + +pub fn crate_to_string_for_macros(krate: &ast::Crate) -> String { + State::to_string(|s| { + s.print_inner_attributes(&krate.attrs); + for item in &krate.items { + s.print_item(item); + } + }) +} diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs new file mode 100644 index 000000000..5eb7bf634 --- /dev/null +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -0,0 +1,1770 @@ +mod delimited; +mod expr; +mod item; + +use crate::pp::Breaks::{Consistent, Inconsistent}; +use crate::pp::{self, Breaks}; + +use rustc_ast::ptr::P; +use rustc_ast::token::{self, BinOpToken, CommentKind, Delimiter, Nonterminal, Token, TokenKind}; +use rustc_ast::tokenstream::{TokenStream, TokenTree}; +use rustc_ast::util::classify; +use rustc_ast::util::comments::{gather_comments, Comment, CommentStyle}; +use rustc_ast::util::parser; +use rustc_ast::{self as ast, BlockCheckMode, PatKind, RangeEnd, RangeSyntax}; +use rustc_ast::{attr, Term}; +use rustc_ast::{GenericArg, MacArgs, MacArgsEq}; +use rustc_ast::{GenericBound, SelfKind, TraitBoundModifier}; +use rustc_ast::{InlineAsmOperand, InlineAsmRegOrRegClass}; +use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece}; +use rustc_span::edition::Edition; +use rustc_span::source_map::{SourceMap, Spanned}; +use rustc_span::symbol::{kw, sym, Ident, IdentPrinter, Symbol}; +use rustc_span::{BytePos, FileName, Span}; + +use std::borrow::Cow; + +pub use self::delimited::IterDelimited; + +pub enum MacHeader<'a> { + Path(&'a ast::Path), + Keyword(&'static str), +} + +pub enum AnnNode<'a> { + Ident(&'a Ident), + Name(&'a Symbol), + Block(&'a ast::Block), + Item(&'a ast::Item), + SubItem(ast::NodeId), + Expr(&'a ast::Expr), + Pat(&'a ast::Pat), + Crate(&'a ast::Crate), +} + +pub trait PpAnn { + fn pre(&self, _state: &mut State<'_>, _node: AnnNode<'_>) {} + fn post(&self, _state: &mut State<'_>, _node: AnnNode<'_>) {} +} + +#[derive(Copy, Clone)] +pub struct NoAnn; + +impl PpAnn for NoAnn {} + +pub struct Comments<'a> { + sm: &'a SourceMap, + comments: Vec, + current: usize, +} + +impl<'a> Comments<'a> { + pub fn new(sm: &'a SourceMap, filename: FileName, input: String) -> Comments<'a> { + let comments = gather_comments(sm, filename, input); + Comments { sm, comments, current: 0 } + } + + pub fn next(&self) -> Option { + self.comments.get(self.current).cloned() + } + + pub fn trailing_comment( + &self, + span: rustc_span::Span, + next_pos: Option, + ) -> Option { + if let Some(cmnt) = self.next() { + if cmnt.style != CommentStyle::Trailing { + return None; + } + let span_line = self.sm.lookup_char_pos(span.hi()); + let comment_line = self.sm.lookup_char_pos(cmnt.pos); + let next = next_pos.unwrap_or_else(|| cmnt.pos + BytePos(1)); + if span.hi() < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line { + return Some(cmnt); + } + } + + None + } +} + +pub struct State<'a> { + pub s: pp::Printer, + comments: Option>, + ann: &'a (dyn PpAnn + 'a), +} + +pub(crate) const INDENT_UNIT: isize = 4; + +/// Requires you to pass an input filename and reader so that +/// it can scan the input text for comments to copy forward. +pub fn print_crate<'a>( + sm: &'a SourceMap, + krate: &ast::Crate, + filename: FileName, + input: String, + ann: &'a dyn PpAnn, + is_expanded: bool, + edition: Edition, +) -> String { + let mut s = + State { s: pp::Printer::new(), comments: Some(Comments::new(sm, filename, input)), ann }; + + if is_expanded && !krate.attrs.iter().any(|attr| attr.has_name(sym::no_core)) { + // We need to print `#![no_std]` (and its feature gate) so that + // compiling pretty-printed source won't inject libstd again. + // However, we don't want these attributes in the AST because + // of the feature gate, so we fake them up here. + + // `#![feature(prelude_import)]` + let pi_nested = attr::mk_nested_word_item(Ident::with_dummy_span(sym::prelude_import)); + let list = attr::mk_list_item(Ident::with_dummy_span(sym::feature), vec![pi_nested]); + let fake_attr = attr::mk_attr_inner(list); + s.print_attribute(&fake_attr); + + // Currently, in Rust 2018 we don't have `extern crate std;` at the crate + // root, so this is not needed, and actually breaks things. + if edition == Edition::Edition2015 { + // `#![no_std]` + let no_std_meta = attr::mk_word_item(Ident::with_dummy_span(sym::no_std)); + let fake_attr = attr::mk_attr_inner(no_std_meta); + s.print_attribute(&fake_attr); + } + } + + s.print_inner_attributes(&krate.attrs); + for item in &krate.items { + s.print_item(item); + } + s.print_remaining_comments(); + s.ann.post(&mut s, AnnNode::Crate(krate)); + s.s.eof() +} + +/// This makes printed token streams look slightly nicer, +/// and also addresses some specific regressions described in #63896 and #73345. +fn tt_prepend_space(tt: &TokenTree, prev: &TokenTree) -> bool { + if let TokenTree::Token(token, _) = prev { + if matches!(token.kind, token::Dot | token::Dollar) { + return false; + } + if let token::DocComment(comment_kind, ..) = token.kind { + return comment_kind != CommentKind::Line; + } + } + match tt { + TokenTree::Token(token, _) => !matches!(token.kind, token::Comma | token::Not | token::Dot), + TokenTree::Delimited(_, Delimiter::Parenthesis, _) => { + !matches!(prev, TokenTree::Token(Token { kind: token::Ident(..), .. }, _)) + } + TokenTree::Delimited(_, Delimiter::Bracket, _) => { + !matches!(prev, TokenTree::Token(Token { kind: token::Pound, .. }, _)) + } + TokenTree::Delimited(..) => true, + } +} + +fn binop_to_string(op: BinOpToken) -> &'static str { + match op { + token::Plus => "+", + token::Minus => "-", + token::Star => "*", + token::Slash => "/", + token::Percent => "%", + token::Caret => "^", + token::And => "&", + token::Or => "|", + token::Shl => "<<", + token::Shr => ">>", + } +} + +fn doc_comment_to_string( + comment_kind: CommentKind, + attr_style: ast::AttrStyle, + data: Symbol, +) -> String { + match (comment_kind, attr_style) { + (CommentKind::Line, ast::AttrStyle::Outer) => format!("///{}", data), + (CommentKind::Line, ast::AttrStyle::Inner) => format!("//!{}", data), + (CommentKind::Block, ast::AttrStyle::Outer) => format!("/**{}*/", data), + (CommentKind::Block, ast::AttrStyle::Inner) => format!("/*!{}*/", data), + } +} + +pub fn literal_to_string(lit: token::Lit) -> String { + let token::Lit { kind, symbol, suffix } = lit; + let mut out = match kind { + token::Byte => format!("b'{}'", symbol), + token::Char => format!("'{}'", symbol), + token::Str => format!("\"{}\"", symbol), + token::StrRaw(n) => { + format!("r{delim}\"{string}\"{delim}", delim = "#".repeat(n as usize), string = symbol) + } + token::ByteStr => format!("b\"{}\"", symbol), + token::ByteStrRaw(n) => { + format!("br{delim}\"{string}\"{delim}", delim = "#".repeat(n as usize), string = symbol) + } + token::Integer | token::Float | token::Bool | token::Err => symbol.to_string(), + }; + + if let Some(suffix) = suffix { + out.push_str(suffix.as_str()) + } + + out +} + +impl std::ops::Deref for State<'_> { + type Target = pp::Printer; + fn deref(&self) -> &Self::Target { + &self.s + } +} + +impl std::ops::DerefMut for State<'_> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.s + } +} + +pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefMut { + fn comments(&mut self) -> &mut Option>; + fn print_ident(&mut self, ident: Ident); + fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool); + + fn strsep( + &mut self, + sep: &'static str, + space_before: bool, + b: Breaks, + elts: &[T], + mut op: F, + ) where + F: FnMut(&mut Self, &T), + { + self.rbox(0, b); + if let Some((first, rest)) = elts.split_first() { + op(self, first); + for elt in rest { + if space_before { + self.space(); + } + self.word_space(sep); + op(self, elt); + } + } + self.end(); + } + + fn commasep(&mut self, b: Breaks, elts: &[T], op: F) + where + F: FnMut(&mut Self, &T), + { + self.strsep(",", false, b, elts, op) + } + + fn maybe_print_comment(&mut self, pos: BytePos) -> bool { + let mut has_comment = false; + while let Some(ref cmnt) = self.next_comment() { + if cmnt.pos < pos { + has_comment = true; + self.print_comment(cmnt); + } else { + break; + } + } + has_comment + } + + fn print_comment(&mut self, cmnt: &Comment) { + match cmnt.style { + CommentStyle::Mixed => { + if !self.is_beginning_of_line() { + self.zerobreak(); + } + if let Some((last, lines)) = cmnt.lines.split_last() { + self.ibox(0); + + for line in lines { + self.word(line.clone()); + self.hardbreak() + } + + self.word(last.clone()); + self.space(); + + self.end(); + } + self.zerobreak() + } + CommentStyle::Isolated => { + self.hardbreak_if_not_bol(); + for line in &cmnt.lines { + // Don't print empty lines because they will end up as trailing + // whitespace. + if !line.is_empty() { + self.word(line.clone()); + } + self.hardbreak(); + } + } + CommentStyle::Trailing => { + if !self.is_beginning_of_line() { + self.word(" "); + } + if cmnt.lines.len() == 1 { + self.word(cmnt.lines[0].clone()); + self.hardbreak() + } else { + self.visual_align(); + for line in &cmnt.lines { + if !line.is_empty() { + self.word(line.clone()); + } + self.hardbreak(); + } + self.end(); + } + } + CommentStyle::BlankLine => { + // We need to do at least one, possibly two hardbreaks. + let twice = match self.last_token() { + Some(pp::Token::String(s)) => ";" == s, + Some(pp::Token::Begin(_)) => true, + Some(pp::Token::End) => true, + _ => false, + }; + if twice { + self.hardbreak(); + } + self.hardbreak(); + } + } + if let Some(cmnts) = self.comments() { + cmnts.current += 1; + } + } + + fn next_comment(&mut self) -> Option { + self.comments().as_mut().and_then(|c| c.next()) + } + + fn maybe_print_trailing_comment(&mut self, span: rustc_span::Span, next_pos: Option) { + if let Some(cmnts) = self.comments() { + if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) { + self.print_comment(&cmnt); + } + } + } + + fn print_remaining_comments(&mut self) { + // If there aren't any remaining comments, then we need to manually + // make sure there is a line break at the end. + if self.next_comment().is_none() { + self.hardbreak(); + } + while let Some(ref cmnt) = self.next_comment() { + self.print_comment(cmnt) + } + } + + fn print_literal(&mut self, lit: &ast::Lit) { + self.maybe_print_comment(lit.span.lo()); + self.word(lit.token.to_string()) + } + + fn print_string(&mut self, st: &str, style: ast::StrStyle) { + let st = match style { + ast::StrStyle::Cooked => format!("\"{}\"", st.escape_debug()), + ast::StrStyle::Raw(n) => { + format!("r{delim}\"{string}\"{delim}", delim = "#".repeat(n as usize), string = st) + } + }; + self.word(st) + } + + fn print_symbol(&mut self, sym: Symbol, style: ast::StrStyle) { + self.print_string(sym.as_str(), style); + } + + fn print_inner_attributes(&mut self, attrs: &[ast::Attribute]) -> bool { + self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true) + } + + fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) -> bool { + self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false) + } + + fn print_outer_attributes(&mut self, attrs: &[ast::Attribute]) -> bool { + self.print_either_attributes(attrs, ast::AttrStyle::Outer, false, true) + } + + fn print_inner_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool { + self.print_either_attributes(attrs, ast::AttrStyle::Inner, true, true) + } + + fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool { + self.print_either_attributes(attrs, ast::AttrStyle::Outer, true, true) + } + + fn print_either_attributes( + &mut self, + attrs: &[ast::Attribute], + kind: ast::AttrStyle, + is_inline: bool, + trailing_hardbreak: bool, + ) -> bool { + let mut printed = false; + for attr in attrs { + if attr.style == kind { + self.print_attribute_inline(attr, is_inline); + if is_inline { + self.nbsp(); + } + printed = true; + } + } + if printed && trailing_hardbreak && !is_inline { + self.hardbreak_if_not_bol(); + } + printed + } + + fn print_attribute(&mut self, attr: &ast::Attribute) { + self.print_attribute_inline(attr, false) + } + + fn print_attribute_inline(&mut self, attr: &ast::Attribute, is_inline: bool) { + if !is_inline { + self.hardbreak_if_not_bol(); + } + self.maybe_print_comment(attr.span.lo()); + match attr.kind { + ast::AttrKind::Normal(ref item, _) => { + match attr.style { + ast::AttrStyle::Inner => self.word("#!["), + ast::AttrStyle::Outer => self.word("#["), + } + self.print_attr_item(&item, attr.span); + self.word("]"); + } + ast::AttrKind::DocComment(comment_kind, data) => { + self.word(doc_comment_to_string(comment_kind, attr.style, data)); + self.hardbreak() + } + } + } + + fn print_attr_item(&mut self, item: &ast::AttrItem, span: Span) { + self.ibox(0); + match &item.args { + MacArgs::Delimited(_, delim, tokens) => self.print_mac_common( + Some(MacHeader::Path(&item.path)), + false, + None, + Some(delim.to_token()), + tokens, + true, + span, + ), + MacArgs::Empty => { + self.print_path(&item.path, false, 0); + } + MacArgs::Eq(_, MacArgsEq::Ast(expr)) => { + self.print_path(&item.path, false, 0); + self.space(); + self.word_space("="); + let token_str = self.expr_to_string(expr); + self.word(token_str); + } + MacArgs::Eq(_, MacArgsEq::Hir(lit)) => { + self.print_path(&item.path, false, 0); + self.space(); + self.word_space("="); + let token_str = self.literal_to_string(lit); + self.word(token_str); + } + } + self.end(); + } + + fn print_meta_list_item(&mut self, item: &ast::NestedMetaItem) { + match item { + ast::NestedMetaItem::MetaItem(ref mi) => self.print_meta_item(mi), + ast::NestedMetaItem::Literal(ref lit) => self.print_literal(lit), + } + } + + fn print_meta_item(&mut self, item: &ast::MetaItem) { + self.ibox(INDENT_UNIT); + match item.kind { + ast::MetaItemKind::Word => self.print_path(&item.path, false, 0), + ast::MetaItemKind::NameValue(ref value) => { + self.print_path(&item.path, false, 0); + self.space(); + self.word_space("="); + self.print_literal(value); + } + ast::MetaItemKind::List(ref items) => { + self.print_path(&item.path, false, 0); + self.popen(); + self.commasep(Consistent, &items, |s, i| s.print_meta_list_item(i)); + self.pclose(); + } + } + self.end(); + } + + /// This doesn't deserve to be called "pretty" printing, but it should be + /// meaning-preserving. A quick hack that might help would be to look at the + /// spans embedded in the TTs to decide where to put spaces and newlines. + /// But it'd be better to parse these according to the grammar of the + /// appropriate macro, transcribe back into the grammar we just parsed from, + /// and then pretty-print the resulting AST nodes (so, e.g., we print + /// expression arguments as expressions). It can be done! I think. + fn print_tt(&mut self, tt: &TokenTree, convert_dollar_crate: bool) { + match tt { + TokenTree::Token(token, _) => { + let token_str = self.token_to_string_ext(&token, convert_dollar_crate); + self.word(token_str); + if let token::DocComment(..) = token.kind { + self.hardbreak() + } + } + TokenTree::Delimited(dspan, delim, tts) => { + self.print_mac_common( + None, + false, + None, + Some(*delim), + tts, + convert_dollar_crate, + dspan.entire(), + ); + } + } + } + + fn print_tts(&mut self, tts: &TokenStream, convert_dollar_crate: bool) { + let mut iter = tts.trees().peekable(); + while let Some(tt) = iter.next() { + self.print_tt(tt, convert_dollar_crate); + if let Some(next) = iter.peek() { + if tt_prepend_space(next, tt) { + self.space(); + } + } + } + } + + fn print_mac_common( + &mut self, + header: Option>, + has_bang: bool, + ident: Option, + delim: Option, + tts: &TokenStream, + convert_dollar_crate: bool, + span: Span, + ) { + if delim == Some(Delimiter::Brace) { + self.cbox(INDENT_UNIT); + } + match header { + Some(MacHeader::Path(path)) => self.print_path(path, false, 0), + Some(MacHeader::Keyword(kw)) => self.word(kw), + None => {} + } + if has_bang { + self.word("!"); + } + if let Some(ident) = ident { + self.nbsp(); + self.print_ident(ident); + } + match delim { + Some(Delimiter::Brace) => { + if header.is_some() || has_bang || ident.is_some() { + self.nbsp(); + } + self.word("{"); + if !tts.is_empty() { + self.space(); + } + self.ibox(0); + self.print_tts(tts, convert_dollar_crate); + self.end(); + let empty = tts.is_empty(); + self.bclose(span, empty); + } + Some(delim) => { + let token_str = self.token_kind_to_string(&token::OpenDelim(delim)); + self.word(token_str); + self.ibox(0); + self.print_tts(tts, convert_dollar_crate); + self.end(); + let token_str = self.token_kind_to_string(&token::CloseDelim(delim)); + self.word(token_str); + } + None => { + self.ibox(0); + self.print_tts(tts, convert_dollar_crate); + self.end(); + } + } + } + + fn print_mac_def( + &mut self, + macro_def: &ast::MacroDef, + ident: &Ident, + sp: Span, + print_visibility: impl FnOnce(&mut Self), + ) { + let (kw, has_bang) = if macro_def.macro_rules { + ("macro_rules", true) + } else { + print_visibility(self); + ("macro", false) + }; + self.print_mac_common( + Some(MacHeader::Keyword(kw)), + has_bang, + Some(*ident), + macro_def.body.delim(), + ¯o_def.body.inner_tokens(), + true, + sp, + ); + if macro_def.body.need_semicolon() { + self.word(";"); + } + } + + fn print_path(&mut self, path: &ast::Path, colons_before_params: bool, depth: usize) { + self.maybe_print_comment(path.span.lo()); + + for (i, segment) in path.segments[..path.segments.len() - depth].iter().enumerate() { + if i > 0 { + self.word("::") + } + self.print_path_segment(segment, colons_before_params); + } + } + + fn print_path_segment(&mut self, segment: &ast::PathSegment, colons_before_params: bool) { + if segment.ident.name != kw::PathRoot { + self.print_ident(segment.ident); + if let Some(ref args) = segment.args { + self.print_generic_args(args, colons_before_params); + } + } + } + + fn head>>(&mut self, w: S) { + let w = w.into(); + // Outer-box is consistent. + self.cbox(INDENT_UNIT); + // Head-box is inconsistent. + self.ibox(0); + // Keyword that starts the head. + if !w.is_empty() { + self.word_nbsp(w); + } + } + + fn bopen(&mut self) { + self.word("{"); + self.end(); // Close the head-box. + } + + fn bclose_maybe_open(&mut self, span: rustc_span::Span, empty: bool, close_box: bool) { + let has_comment = self.maybe_print_comment(span.hi()); + if !empty || has_comment { + self.break_offset_if_not_bol(1, -(INDENT_UNIT as isize)); + } + self.word("}"); + if close_box { + self.end(); // Close the outer-box. + } + } + + fn bclose(&mut self, span: rustc_span::Span, empty: bool) { + let close_box = true; + self.bclose_maybe_open(span, empty, close_box) + } + + fn break_offset_if_not_bol(&mut self, n: usize, off: isize) { + if !self.is_beginning_of_line() { + self.break_offset(n, off) + } else if off != 0 { + if let Some(last_token) = self.last_token_still_buffered() { + if last_token.is_hardbreak_tok() { + // We do something pretty sketchy here: tuck the nonzero + // offset-adjustment we were going to deposit along with the + // break into the previous hardbreak. + self.replace_last_token_still_buffered(pp::Printer::hardbreak_tok_offset(off)); + } + } + } + } + + fn nonterminal_to_string(&self, nt: &Nonterminal) -> String { + match *nt { + token::NtExpr(ref e) => self.expr_to_string(e), + token::NtMeta(ref e) => self.attr_item_to_string(e), + token::NtTy(ref e) => self.ty_to_string(e), + token::NtPath(ref e) => self.path_to_string(e), + token::NtItem(ref e) => self.item_to_string(e), + token::NtBlock(ref e) => self.block_to_string(e), + token::NtStmt(ref e) => self.stmt_to_string(e), + token::NtPat(ref e) => self.pat_to_string(e), + token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(e, is_raw).to_string(), + token::NtLifetime(e) => e.to_string(), + token::NtLiteral(ref e) => self.expr_to_string(e), + token::NtVis(ref e) => self.vis_to_string(e), + } + } + + /// Print the token kind precisely, without converting `$crate` into its respective crate name. + fn token_kind_to_string(&self, tok: &TokenKind) -> Cow<'static, str> { + self.token_kind_to_string_ext(tok, None) + } + + fn token_kind_to_string_ext( + &self, + tok: &TokenKind, + convert_dollar_crate: Option, + ) -> Cow<'static, str> { + match *tok { + token::Eq => "=".into(), + token::Lt => "<".into(), + token::Le => "<=".into(), + token::EqEq => "==".into(), + token::Ne => "!=".into(), + token::Ge => ">=".into(), + token::Gt => ">".into(), + token::Not => "!".into(), + token::Tilde => "~".into(), + token::OrOr => "||".into(), + token::AndAnd => "&&".into(), + token::BinOp(op) => binop_to_string(op).into(), + token::BinOpEq(op) => format!("{}=", binop_to_string(op)).into(), + + /* Structural symbols */ + token::At => "@".into(), + token::Dot => ".".into(), + token::DotDot => "..".into(), + token::DotDotDot => "...".into(), + token::DotDotEq => "..=".into(), + token::Comma => ",".into(), + token::Semi => ";".into(), + token::Colon => ":".into(), + token::ModSep => "::".into(), + token::RArrow => "->".into(), + token::LArrow => "<-".into(), + token::FatArrow => "=>".into(), + token::OpenDelim(Delimiter::Parenthesis) => "(".into(), + token::CloseDelim(Delimiter::Parenthesis) => ")".into(), + token::OpenDelim(Delimiter::Bracket) => "[".into(), + token::CloseDelim(Delimiter::Bracket) => "]".into(), + token::OpenDelim(Delimiter::Brace) => "{".into(), + token::CloseDelim(Delimiter::Brace) => "}".into(), + token::OpenDelim(Delimiter::Invisible) | token::CloseDelim(Delimiter::Invisible) => { + "".into() + } + token::Pound => "#".into(), + token::Dollar => "$".into(), + token::Question => "?".into(), + token::SingleQuote => "'".into(), + + /* Literals */ + token::Literal(lit) => literal_to_string(lit).into(), + + /* Name components */ + token::Ident(s, is_raw) => { + IdentPrinter::new(s, is_raw, convert_dollar_crate).to_string().into() + } + token::Lifetime(s) => s.to_string().into(), + + /* Other */ + token::DocComment(comment_kind, attr_style, data) => { + doc_comment_to_string(comment_kind, attr_style, data).into() + } + token::Eof => "".into(), + + token::Interpolated(ref nt) => self.nonterminal_to_string(nt).into(), + } + } + + /// Print the token precisely, without converting `$crate` into its respective crate name. + fn token_to_string(&self, token: &Token) -> Cow<'static, str> { + self.token_to_string_ext(token, false) + } + + fn token_to_string_ext(&self, token: &Token, convert_dollar_crate: bool) -> Cow<'static, str> { + let convert_dollar_crate = convert_dollar_crate.then_some(token.span); + self.token_kind_to_string_ext(&token.kind, convert_dollar_crate) + } + + fn ty_to_string(&self, ty: &ast::Ty) -> String { + Self::to_string(|s| s.print_type(ty)) + } + + fn bounds_to_string(&self, bounds: &[ast::GenericBound]) -> String { + Self::to_string(|s| s.print_type_bounds(bounds)) + } + + fn pat_to_string(&self, pat: &ast::Pat) -> String { + Self::to_string(|s| s.print_pat(pat)) + } + + fn expr_to_string(&self, e: &ast::Expr) -> String { + Self::to_string(|s| s.print_expr(e)) + } + + fn literal_to_string(&self, lit: &ast::Lit) -> String { + Self::to_string(|s| s.print_literal(lit)) + } + + fn tt_to_string(&self, tt: &TokenTree) -> String { + Self::to_string(|s| s.print_tt(tt, false)) + } + + fn tts_to_string(&self, tokens: &TokenStream) -> String { + Self::to_string(|s| s.print_tts(tokens, false)) + } + + fn stmt_to_string(&self, stmt: &ast::Stmt) -> String { + Self::to_string(|s| s.print_stmt(stmt)) + } + + fn item_to_string(&self, i: &ast::Item) -> String { + Self::to_string(|s| s.print_item(i)) + } + + fn assoc_item_to_string(&self, i: &ast::AssocItem) -> String { + Self::to_string(|s| s.print_assoc_item(i)) + } + + fn foreign_item_to_string(&self, i: &ast::ForeignItem) -> String { + Self::to_string(|s| s.print_foreign_item(i)) + } + + fn generic_params_to_string(&self, generic_params: &[ast::GenericParam]) -> String { + Self::to_string(|s| s.print_generic_params(generic_params)) + } + + fn path_to_string(&self, p: &ast::Path) -> String { + Self::to_string(|s| s.print_path(p, false, 0)) + } + + fn path_segment_to_string(&self, p: &ast::PathSegment) -> String { + Self::to_string(|s| s.print_path_segment(p, false)) + } + + fn vis_to_string(&self, v: &ast::Visibility) -> String { + Self::to_string(|s| s.print_visibility(v)) + } + + fn block_to_string(&self, blk: &ast::Block) -> String { + Self::to_string(|s| { + // Containing cbox, will be closed by `print_block` at `}`. + s.cbox(INDENT_UNIT); + // Head-ibox, will be closed by `print_block` after `{`. + s.ibox(0); + s.print_block(blk) + }) + } + + fn meta_list_item_to_string(&self, li: &ast::NestedMetaItem) -> String { + Self::to_string(|s| s.print_meta_list_item(li)) + } + + fn attr_item_to_string(&self, ai: &ast::AttrItem) -> String { + Self::to_string(|s| s.print_attr_item(ai, ai.path.span)) + } + + fn attribute_to_string(&self, attr: &ast::Attribute) -> String { + Self::to_string(|s| s.print_attribute(attr)) + } + + fn param_to_string(&self, arg: &ast::Param) -> String { + Self::to_string(|s| s.print_param(arg, false)) + } + + fn to_string(f: impl FnOnce(&mut State<'_>)) -> String { + let mut printer = State::new(); + f(&mut printer); + printer.s.eof() + } +} + +impl<'a> PrintState<'a> for State<'a> { + fn comments(&mut self) -> &mut Option> { + &mut self.comments + } + + fn print_ident(&mut self, ident: Ident) { + self.word(IdentPrinter::for_ast_ident(ident, ident.is_raw_guess()).to_string()); + self.ann.post(self, AnnNode::Ident(&ident)) + } + + fn print_generic_args(&mut self, args: &ast::GenericArgs, colons_before_params: bool) { + if colons_before_params { + self.word("::") + } + + match *args { + ast::GenericArgs::AngleBracketed(ref data) => { + self.word("<"); + self.commasep(Inconsistent, &data.args, |s, arg| match arg { + ast::AngleBracketedArg::Arg(a) => s.print_generic_arg(a), + ast::AngleBracketedArg::Constraint(c) => s.print_assoc_constraint(c), + }); + self.word(">") + } + + ast::GenericArgs::Parenthesized(ref data) => { + self.word("("); + self.commasep(Inconsistent, &data.inputs, |s, ty| s.print_type(ty)); + self.word(")"); + self.print_fn_ret_ty(&data.output); + } + } + } +} + +impl<'a> State<'a> { + pub fn new() -> State<'a> { + State { s: pp::Printer::new(), comments: None, ann: &NoAnn } + } + + pub(crate) fn commasep_cmnt( + &mut self, + b: Breaks, + elts: &[T], + mut op: F, + mut get_span: G, + ) where + F: FnMut(&mut State<'_>, &T), + G: FnMut(&T) -> rustc_span::Span, + { + self.rbox(0, b); + let len = elts.len(); + let mut i = 0; + for elt in elts { + self.maybe_print_comment(get_span(elt).hi()); + op(self, elt); + i += 1; + if i < len { + self.word(","); + self.maybe_print_trailing_comment(get_span(elt), Some(get_span(&elts[i]).hi())); + self.space_if_not_bol(); + } + } + self.end(); + } + + pub(crate) fn commasep_exprs(&mut self, b: Breaks, exprs: &[P]) { + self.commasep_cmnt(b, exprs, |s, e| s.print_expr(e), |e| e.span) + } + + pub fn print_opt_lifetime(&mut self, lifetime: &Option) { + if let Some(lt) = *lifetime { + self.print_lifetime(lt); + self.nbsp(); + } + } + + pub fn print_assoc_constraint(&mut self, constraint: &ast::AssocConstraint) { + self.print_ident(constraint.ident); + constraint.gen_args.as_ref().map(|args| self.print_generic_args(args, false)); + self.space(); + match &constraint.kind { + ast::AssocConstraintKind::Equality { term } => { + self.word_space("="); + match term { + Term::Ty(ty) => self.print_type(ty), + Term::Const(c) => self.print_expr_anon_const(c, &[]), + } + } + ast::AssocConstraintKind::Bound { bounds } => { + if !bounds.is_empty() { + self.word_nbsp(":"); + self.print_type_bounds(&bounds); + } + } + } + } + + pub fn print_generic_arg(&mut self, generic_arg: &GenericArg) { + match generic_arg { + GenericArg::Lifetime(lt) => self.print_lifetime(*lt), + GenericArg::Type(ty) => self.print_type(ty), + GenericArg::Const(ct) => self.print_expr(&ct.value), + } + } + + pub fn print_type(&mut self, ty: &ast::Ty) { + self.maybe_print_comment(ty.span.lo()); + self.ibox(0); + match ty.kind { + ast::TyKind::Slice(ref ty) => { + self.word("["); + self.print_type(ty); + self.word("]"); + } + ast::TyKind::Ptr(ref mt) => { + self.word("*"); + self.print_mt(mt, true); + } + ast::TyKind::Rptr(ref lifetime, ref mt) => { + self.word("&"); + self.print_opt_lifetime(lifetime); + self.print_mt(mt, false); + } + ast::TyKind::Never => { + self.word("!"); + } + ast::TyKind::Tup(ref elts) => { + self.popen(); + self.commasep(Inconsistent, &elts, |s, ty| s.print_type(ty)); + if elts.len() == 1 { + self.word(","); + } + self.pclose(); + } + ast::TyKind::Paren(ref typ) => { + self.popen(); + self.print_type(typ); + self.pclose(); + } + ast::TyKind::BareFn(ref f) => { + self.print_ty_fn(f.ext, f.unsafety, &f.decl, None, &f.generic_params); + } + ast::TyKind::Path(None, ref path) => { + self.print_path(path, false, 0); + } + ast::TyKind::Path(Some(ref qself), ref path) => self.print_qpath(path, qself, false), + ast::TyKind::TraitObject(ref bounds, syntax) => { + if syntax == ast::TraitObjectSyntax::Dyn { + self.word_nbsp("dyn"); + } + self.print_type_bounds(bounds); + } + ast::TyKind::ImplTrait(_, ref bounds) => { + self.word_nbsp("impl"); + self.print_type_bounds(bounds); + } + ast::TyKind::Array(ref ty, ref length) => { + self.word("["); + self.print_type(ty); + self.word("; "); + self.print_expr(&length.value); + self.word("]"); + } + ast::TyKind::Typeof(ref e) => { + self.word("typeof("); + self.print_expr(&e.value); + self.word(")"); + } + ast::TyKind::Infer => { + self.word("_"); + } + ast::TyKind::Err => { + self.popen(); + self.word("/*ERROR*/"); + self.pclose(); + } + ast::TyKind::ImplicitSelf => { + self.word("Self"); + } + ast::TyKind::MacCall(ref m) => { + self.print_mac(m); + } + ast::TyKind::CVarArgs => { + self.word("..."); + } + } + self.end(); + } + + fn print_trait_ref(&mut self, t: &ast::TraitRef) { + self.print_path(&t.path, false, 0) + } + + fn print_formal_generic_params(&mut self, generic_params: &[ast::GenericParam]) { + if !generic_params.is_empty() { + self.word("for"); + self.print_generic_params(generic_params); + self.nbsp(); + } + } + + fn print_poly_trait_ref(&mut self, t: &ast::PolyTraitRef) { + self.print_formal_generic_params(&t.bound_generic_params); + self.print_trait_ref(&t.trait_ref) + } + + pub(crate) fn print_stmt(&mut self, st: &ast::Stmt) { + self.maybe_print_comment(st.span.lo()); + match st.kind { + ast::StmtKind::Local(ref loc) => { + self.print_outer_attributes(&loc.attrs); + self.space_if_not_bol(); + self.ibox(INDENT_UNIT); + self.word_nbsp("let"); + + self.ibox(INDENT_UNIT); + self.print_local_decl(loc); + self.end(); + if let Some((init, els)) = loc.kind.init_else_opt() { + self.nbsp(); + self.word_space("="); + self.print_expr(init); + if let Some(els) = els { + self.cbox(INDENT_UNIT); + self.ibox(INDENT_UNIT); + self.word(" else "); + self.print_block(els); + } + } + self.word(";"); + self.end(); // `let` ibox + } + ast::StmtKind::Item(ref item) => self.print_item(item), + ast::StmtKind::Expr(ref expr) => { + self.space_if_not_bol(); + self.print_expr_outer_attr_style(expr, false); + if classify::expr_requires_semi_to_be_stmt(expr) { + self.word(";"); + } + } + ast::StmtKind::Semi(ref expr) => { + self.space_if_not_bol(); + self.print_expr_outer_attr_style(expr, false); + self.word(";"); + } + ast::StmtKind::Empty => { + self.space_if_not_bol(); + self.word(";"); + } + ast::StmtKind::MacCall(ref mac) => { + self.space_if_not_bol(); + self.print_outer_attributes(&mac.attrs); + self.print_mac(&mac.mac); + if mac.style == ast::MacStmtStyle::Semicolon { + self.word(";"); + } + } + } + self.maybe_print_trailing_comment(st.span, None) + } + + pub(crate) fn print_block(&mut self, blk: &ast::Block) { + self.print_block_with_attrs(blk, &[]) + } + + pub(crate) fn print_block_unclosed_indent(&mut self, blk: &ast::Block) { + self.print_block_maybe_unclosed(blk, &[], false) + } + + pub(crate) fn print_block_with_attrs(&mut self, blk: &ast::Block, attrs: &[ast::Attribute]) { + self.print_block_maybe_unclosed(blk, attrs, true) + } + + pub(crate) fn print_block_maybe_unclosed( + &mut self, + blk: &ast::Block, + attrs: &[ast::Attribute], + close_box: bool, + ) { + match blk.rules { + BlockCheckMode::Unsafe(..) => self.word_space("unsafe"), + BlockCheckMode::Default => (), + } + self.maybe_print_comment(blk.span.lo()); + self.ann.pre(self, AnnNode::Block(blk)); + self.bopen(); + + let has_attrs = self.print_inner_attributes(attrs); + + for (i, st) in blk.stmts.iter().enumerate() { + match st.kind { + ast::StmtKind::Expr(ref expr) if i == blk.stmts.len() - 1 => { + self.maybe_print_comment(st.span.lo()); + self.space_if_not_bol(); + self.print_expr_outer_attr_style(expr, false); + self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi())); + } + _ => self.print_stmt(st), + } + } + + let empty = !has_attrs && blk.stmts.is_empty(); + self.bclose_maybe_open(blk.span, empty, close_box); + self.ann.post(self, AnnNode::Block(blk)) + } + + /// Print a `let pat = expr` expression. + pub(crate) fn print_let(&mut self, pat: &ast::Pat, expr: &ast::Expr) { + self.word("let "); + self.print_pat(pat); + self.space(); + self.word_space("="); + let npals = || parser::needs_par_as_let_scrutinee(expr.precedence().order()); + self.print_expr_cond_paren(expr, Self::cond_needs_par(expr) || npals()) + } + + pub(crate) fn print_mac(&mut self, m: &ast::MacCall) { + self.print_mac_common( + Some(MacHeader::Path(&m.path)), + true, + None, + m.args.delim(), + &m.args.inner_tokens(), + true, + m.span(), + ); + } + + fn print_inline_asm(&mut self, asm: &ast::InlineAsm) { + enum AsmArg<'a> { + Template(String), + Operand(&'a InlineAsmOperand), + ClobberAbi(Symbol), + Options(InlineAsmOptions), + } + + let mut args = vec![AsmArg::Template(InlineAsmTemplatePiece::to_string(&asm.template))]; + args.extend(asm.operands.iter().map(|(o, _)| AsmArg::Operand(o))); + for (abi, _) in &asm.clobber_abis { + args.push(AsmArg::ClobberAbi(*abi)); + } + if !asm.options.is_empty() { + args.push(AsmArg::Options(asm.options)); + } + + self.popen(); + self.commasep(Consistent, &args, |s, arg| match arg { + AsmArg::Template(template) => s.print_string(&template, ast::StrStyle::Cooked), + AsmArg::Operand(op) => { + let print_reg_or_class = |s: &mut Self, r: &InlineAsmRegOrRegClass| match r { + InlineAsmRegOrRegClass::Reg(r) => s.print_symbol(*r, ast::StrStyle::Cooked), + InlineAsmRegOrRegClass::RegClass(r) => s.word(r.to_string()), + }; + match op { + InlineAsmOperand::In { reg, expr } => { + s.word("in"); + s.popen(); + print_reg_or_class(s, reg); + s.pclose(); + s.space(); + s.print_expr(expr); + } + InlineAsmOperand::Out { reg, late, expr } => { + s.word(if *late { "lateout" } else { "out" }); + s.popen(); + print_reg_or_class(s, reg); + s.pclose(); + s.space(); + match expr { + Some(expr) => s.print_expr(expr), + None => s.word("_"), + } + } + InlineAsmOperand::InOut { reg, late, expr } => { + s.word(if *late { "inlateout" } else { "inout" }); + s.popen(); + print_reg_or_class(s, reg); + s.pclose(); + s.space(); + s.print_expr(expr); + } + InlineAsmOperand::SplitInOut { reg, late, in_expr, out_expr } => { + s.word(if *late { "inlateout" } else { "inout" }); + s.popen(); + print_reg_or_class(s, reg); + s.pclose(); + s.space(); + s.print_expr(in_expr); + s.space(); + s.word_space("=>"); + match out_expr { + Some(out_expr) => s.print_expr(out_expr), + None => s.word("_"), + } + } + InlineAsmOperand::Const { anon_const } => { + s.word("const"); + s.space(); + s.print_expr(&anon_const.value); + } + InlineAsmOperand::Sym { sym } => { + s.word("sym"); + s.space(); + if let Some(qself) = &sym.qself { + s.print_qpath(&sym.path, qself, true); + } else { + s.print_path(&sym.path, true, 0); + } + } + } + } + AsmArg::ClobberAbi(abi) => { + s.word("clobber_abi"); + s.popen(); + s.print_symbol(*abi, ast::StrStyle::Cooked); + s.pclose(); + } + AsmArg::Options(opts) => { + s.word("options"); + s.popen(); + let mut options = vec![]; + if opts.contains(InlineAsmOptions::PURE) { + options.push("pure"); + } + if opts.contains(InlineAsmOptions::NOMEM) { + options.push("nomem"); + } + if opts.contains(InlineAsmOptions::READONLY) { + options.push("readonly"); + } + if opts.contains(InlineAsmOptions::PRESERVES_FLAGS) { + options.push("preserves_flags"); + } + if opts.contains(InlineAsmOptions::NORETURN) { + options.push("noreturn"); + } + if opts.contains(InlineAsmOptions::NOSTACK) { + options.push("nostack"); + } + if opts.contains(InlineAsmOptions::ATT_SYNTAX) { + options.push("att_syntax"); + } + if opts.contains(InlineAsmOptions::RAW) { + options.push("raw"); + } + if opts.contains(InlineAsmOptions::MAY_UNWIND) { + options.push("may_unwind"); + } + s.commasep(Inconsistent, &options, |s, &opt| { + s.word(opt); + }); + s.pclose(); + } + }); + self.pclose(); + } + + pub(crate) fn print_local_decl(&mut self, loc: &ast::Local) { + self.print_pat(&loc.pat); + if let Some(ref ty) = loc.ty { + self.word_space(":"); + self.print_type(ty); + } + } + + pub(crate) fn print_name(&mut self, name: Symbol) { + self.word(name.to_string()); + self.ann.post(self, AnnNode::Name(&name)) + } + + fn print_qpath(&mut self, path: &ast::Path, qself: &ast::QSelf, colons_before_params: bool) { + self.word("<"); + self.print_type(&qself.ty); + if qself.position > 0 { + self.space(); + self.word_space("as"); + let depth = path.segments.len() - qself.position; + self.print_path(path, false, depth); + } + self.word(">"); + for item_segment in &path.segments[qself.position..] { + self.word("::"); + self.print_ident(item_segment.ident); + if let Some(ref args) = item_segment.args { + self.print_generic_args(args, colons_before_params) + } + } + } + + pub(crate) fn print_pat(&mut self, pat: &ast::Pat) { + self.maybe_print_comment(pat.span.lo()); + self.ann.pre(self, AnnNode::Pat(pat)); + /* Pat isn't normalized, but the beauty of it + is that it doesn't matter */ + match pat.kind { + PatKind::Wild => self.word("_"), + PatKind::Ident(binding_mode, ident, ref sub) => { + match binding_mode { + ast::BindingMode::ByRef(mutbl) => { + self.word_nbsp("ref"); + self.print_mutability(mutbl, false); + } + ast::BindingMode::ByValue(ast::Mutability::Not) => {} + ast::BindingMode::ByValue(ast::Mutability::Mut) => { + self.word_nbsp("mut"); + } + } + self.print_ident(ident); + if let Some(ref p) = *sub { + self.space(); + self.word_space("@"); + self.print_pat(p); + } + } + PatKind::TupleStruct(ref qself, ref path, ref elts) => { + if let Some(qself) = qself { + self.print_qpath(path, qself, true); + } else { + self.print_path(path, true, 0); + } + self.popen(); + self.commasep(Inconsistent, &elts, |s, p| s.print_pat(p)); + self.pclose(); + } + PatKind::Or(ref pats) => { + self.strsep("|", true, Inconsistent, &pats, |s, p| s.print_pat(p)); + } + PatKind::Path(None, ref path) => { + self.print_path(path, true, 0); + } + PatKind::Path(Some(ref qself), ref path) => { + self.print_qpath(path, qself, false); + } + PatKind::Struct(ref qself, ref path, ref fields, etc) => { + if let Some(qself) = qself { + self.print_qpath(path, qself, true); + } else { + self.print_path(path, true, 0); + } + self.nbsp(); + self.word("{"); + let empty = fields.is_empty() && !etc; + if !empty { + self.space(); + } + self.commasep_cmnt( + Consistent, + &fields, + |s, f| { + s.cbox(INDENT_UNIT); + if !f.is_shorthand { + s.print_ident(f.ident); + s.word_nbsp(":"); + } + s.print_pat(&f.pat); + s.end(); + }, + |f| f.pat.span, + ); + if etc { + if !fields.is_empty() { + self.word_space(","); + } + self.word(".."); + } + if !empty { + self.space(); + } + self.word("}"); + } + PatKind::Tuple(ref elts) => { + self.popen(); + self.commasep(Inconsistent, &elts, |s, p| s.print_pat(p)); + if elts.len() == 1 { + self.word(","); + } + self.pclose(); + } + PatKind::Box(ref inner) => { + self.word("box "); + self.print_pat(inner); + } + PatKind::Ref(ref inner, mutbl) => { + self.word("&"); + if mutbl == ast::Mutability::Mut { + self.word("mut "); + } + if let PatKind::Ident(ast::BindingMode::ByValue(ast::Mutability::Mut), ..) = + inner.kind + { + self.popen(); + self.print_pat(inner); + self.pclose(); + } else { + self.print_pat(inner); + } + } + PatKind::Lit(ref e) => self.print_expr(&**e), + PatKind::Range(ref begin, ref end, Spanned { node: ref end_kind, .. }) => { + if let Some(e) = begin { + self.print_expr(e); + } + match *end_kind { + RangeEnd::Included(RangeSyntax::DotDotDot) => self.word("..."), + RangeEnd::Included(RangeSyntax::DotDotEq) => self.word("..="), + RangeEnd::Excluded => self.word(".."), + } + if let Some(e) = end { + self.print_expr(e); + } + } + PatKind::Slice(ref elts) => { + self.word("["); + self.commasep(Inconsistent, &elts, |s, p| s.print_pat(p)); + self.word("]"); + } + PatKind::Rest => self.word(".."), + PatKind::Paren(ref inner) => { + self.popen(); + self.print_pat(inner); + self.pclose(); + } + PatKind::MacCall(ref m) => self.print_mac(m), + } + self.ann.post(self, AnnNode::Pat(pat)) + } + + fn print_explicit_self(&mut self, explicit_self: &ast::ExplicitSelf) { + match explicit_self.node { + SelfKind::Value(m) => { + self.print_mutability(m, false); + self.word("self") + } + SelfKind::Region(ref lt, m) => { + self.word("&"); + self.print_opt_lifetime(lt); + self.print_mutability(m, false); + self.word("self") + } + SelfKind::Explicit(ref typ, m) => { + self.print_mutability(m, false); + self.word("self"); + self.word_space(":"); + self.print_type(typ) + } + } + } + + pub(crate) fn print_asyncness(&mut self, asyncness: ast::Async) { + if asyncness.is_async() { + self.word_nbsp("async"); + } + } + + pub fn print_type_bounds(&mut self, bounds: &[ast::GenericBound]) { + let mut first = true; + for bound in bounds { + if first { + first = false; + } else { + self.nbsp(); + self.word_space("+"); + } + + match bound { + GenericBound::Trait(tref, modifier) => { + if modifier == &TraitBoundModifier::Maybe { + self.word("?"); + } + self.print_poly_trait_ref(tref); + } + GenericBound::Outlives(lt) => self.print_lifetime(*lt), + } + } + } + + pub(crate) fn print_lifetime(&mut self, lifetime: ast::Lifetime) { + self.print_name(lifetime.ident.name) + } + + pub(crate) fn print_lifetime_bounds(&mut self, bounds: &ast::GenericBounds) { + for (i, bound) in bounds.iter().enumerate() { + if i != 0 { + self.word(" + "); + } + match bound { + ast::GenericBound::Outlives(lt) => self.print_lifetime(*lt), + _ => panic!(), + } + } + } + + pub(crate) fn print_generic_params(&mut self, generic_params: &[ast::GenericParam]) { + if generic_params.is_empty() { + return; + } + + self.word("<"); + + self.commasep(Inconsistent, &generic_params, |s, param| { + s.print_outer_attributes_inline(¶m.attrs); + + match param.kind { + ast::GenericParamKind::Lifetime => { + let lt = ast::Lifetime { id: param.id, ident: param.ident }; + s.print_lifetime(lt); + if !param.bounds.is_empty() { + s.word_nbsp(":"); + s.print_lifetime_bounds(¶m.bounds) + } + } + ast::GenericParamKind::Type { ref default } => { + s.print_ident(param.ident); + if !param.bounds.is_empty() { + s.word_nbsp(":"); + s.print_type_bounds(¶m.bounds); + } + if let Some(ref default) = default { + s.space(); + s.word_space("="); + s.print_type(default) + } + } + ast::GenericParamKind::Const { ref ty, kw_span: _, ref default } => { + s.word_space("const"); + s.print_ident(param.ident); + s.space(); + s.word_space(":"); + s.print_type(ty); + if !param.bounds.is_empty() { + s.word_nbsp(":"); + s.print_type_bounds(¶m.bounds); + } + if let Some(ref default) = default { + s.space(); + s.word_space("="); + s.print_expr(&default.value); + } + } + } + }); + + self.word(">"); + } + + pub fn print_mutability(&mut self, mutbl: ast::Mutability, print_const: bool) { + match mutbl { + ast::Mutability::Mut => self.word_nbsp("mut"), + ast::Mutability::Not => { + if print_const { + self.word_nbsp("const"); + } + } + } + } + + pub(crate) fn print_mt(&mut self, mt: &ast::MutTy, print_const: bool) { + self.print_mutability(mt.mutbl, print_const); + self.print_type(&mt.ty) + } + + pub(crate) fn print_param(&mut self, input: &ast::Param, is_closure: bool) { + self.ibox(INDENT_UNIT); + + self.print_outer_attributes_inline(&input.attrs); + + match input.ty.kind { + ast::TyKind::Infer if is_closure => self.print_pat(&input.pat), + _ => { + if let Some(eself) = input.to_self() { + self.print_explicit_self(&eself); + } else { + let invalid = if let PatKind::Ident(_, ident, _) = input.pat.kind { + ident.name == kw::Empty + } else { + false + }; + if !invalid { + self.print_pat(&input.pat); + self.word(":"); + self.space(); + } + self.print_type(&input.ty); + } + } + } + self.end(); + } + + pub(crate) fn print_fn_ret_ty(&mut self, fn_ret_ty: &ast::FnRetTy) { + if let ast::FnRetTy::Ty(ty) = fn_ret_ty { + self.space_if_not_bol(); + self.ibox(INDENT_UNIT); + self.word_space("->"); + self.print_type(ty); + self.end(); + self.maybe_print_comment(ty.span.lo()); + } + } + + pub(crate) fn print_ty_fn( + &mut self, + ext: ast::Extern, + unsafety: ast::Unsafe, + decl: &ast::FnDecl, + name: Option, + generic_params: &[ast::GenericParam], + ) { + self.ibox(INDENT_UNIT); + self.print_formal_generic_params(generic_params); + let generics = ast::Generics { + params: Vec::new(), + where_clause: ast::WhereClause { + has_where_token: false, + predicates: Vec::new(), + span: rustc_span::DUMMY_SP, + }, + span: rustc_span::DUMMY_SP, + }; + let header = ast::FnHeader { unsafety, ext, ..ast::FnHeader::default() }; + self.print_fn(decl, header, name, &generics); + self.end(); + } + + pub(crate) fn print_fn_header_info(&mut self, header: ast::FnHeader) { + self.print_constness(header.constness); + self.print_asyncness(header.asyncness); + self.print_unsafety(header.unsafety); + + match header.ext { + ast::Extern::None => {} + ast::Extern::Implicit(_) => { + self.word_nbsp("extern"); + } + ast::Extern::Explicit(abi, _) => { + self.word_nbsp("extern"); + self.print_literal(&abi.as_lit()); + self.nbsp(); + } + } + + self.word("fn") + } + + pub(crate) fn print_unsafety(&mut self, s: ast::Unsafe) { + match s { + ast::Unsafe::No => {} + ast::Unsafe::Yes(_) => self.word_nbsp("unsafe"), + } + } + + pub(crate) fn print_constness(&mut self, s: ast::Const) { + match s { + ast::Const::No => {} + ast::Const::Yes(_) => self.word_nbsp("const"), + } + } + + pub(crate) fn print_is_auto(&mut self, s: ast::IsAuto) { + match s { + ast::IsAuto::Yes => self.word_nbsp("auto"), + ast::IsAuto::No => {} + } + } +} diff --git a/compiler/rustc_ast_pretty/src/pprust/state/delimited.rs b/compiler/rustc_ast_pretty/src/pprust/state/delimited.rs new file mode 100644 index 000000000..fe0640baa --- /dev/null +++ b/compiler/rustc_ast_pretty/src/pprust/state/delimited.rs @@ -0,0 +1,41 @@ +use std::iter::Peekable; +use std::mem; +use std::ops::Deref; + +pub struct Delimited { + is_first: bool, + iter: Peekable, +} + +pub trait IterDelimited: Iterator + Sized { + fn delimited(self) -> Delimited { + Delimited { is_first: true, iter: self.peekable() } + } +} + +impl IterDelimited for I {} + +pub struct IteratorItem { + value: T, + pub is_first: bool, + pub is_last: bool, +} + +impl Iterator for Delimited { + type Item = IteratorItem; + + fn next(&mut self) -> Option { + let value = self.iter.next()?; + let is_first = mem::replace(&mut self.is_first, false); + let is_last = self.iter.peek().is_none(); + Some(IteratorItem { value, is_first, is_last }) + } +} + +impl Deref for IteratorItem { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.value + } +} diff --git a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs new file mode 100644 index 000000000..ead38caee --- /dev/null +++ b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs @@ -0,0 +1,621 @@ +use crate::pp::Breaks::Inconsistent; +use crate::pprust::state::{AnnNode, IterDelimited, PrintState, State, INDENT_UNIT}; + +use rustc_ast::ptr::P; +use rustc_ast::util::parser::{self, AssocOp, Fixity}; +use rustc_ast::{self as ast, BlockCheckMode}; + +impl<'a> State<'a> { + fn print_else(&mut self, els: Option<&ast::Expr>) { + if let Some(_else) = els { + match _else.kind { + // Another `else if` block. + ast::ExprKind::If(ref i, ref then, ref e) => { + self.cbox(INDENT_UNIT - 1); + self.ibox(0); + self.word(" else if "); + self.print_expr_as_cond(i); + self.space(); + self.print_block(then); + self.print_else(e.as_deref()) + } + // Final `else` block. + ast::ExprKind::Block(ref b, _) => { + self.cbox(INDENT_UNIT - 1); + self.ibox(0); + self.word(" else "); + self.print_block(b) + } + // Constraints would be great here! + _ => { + panic!("print_if saw if with weird alternative"); + } + } + } + } + + fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block, elseopt: Option<&ast::Expr>) { + self.head("if"); + self.print_expr_as_cond(test); + self.space(); + self.print_block(blk); + self.print_else(elseopt) + } + + fn print_call_post(&mut self, args: &[P]) { + self.popen(); + self.commasep_exprs(Inconsistent, args); + self.pclose() + } + + fn print_expr_maybe_paren(&mut self, expr: &ast::Expr, prec: i8) { + self.print_expr_cond_paren(expr, expr.precedence().order() < prec) + } + + /// Prints an expr using syntax that's acceptable in a condition position, such as the `cond` in + /// `if cond { ... }`. + fn print_expr_as_cond(&mut self, expr: &ast::Expr) { + self.print_expr_cond_paren(expr, Self::cond_needs_par(expr)) + } + + // Does `expr` need parentheses when printed in a condition position? + // + // These cases need parens due to the parse error observed in #26461: `if return {}` + // parses as the erroneous construct `if (return {})`, not `if (return) {}`. + pub(super) fn cond_needs_par(expr: &ast::Expr) -> bool { + match expr.kind { + ast::ExprKind::Break(..) + | ast::ExprKind::Closure(..) + | ast::ExprKind::Ret(..) + | ast::ExprKind::Yeet(..) => true, + _ => parser::contains_exterior_struct_lit(expr), + } + } + + /// Prints `expr` or `(expr)` when `needs_par` holds. + pub(super) fn print_expr_cond_paren(&mut self, expr: &ast::Expr, needs_par: bool) { + if needs_par { + self.popen(); + } + self.print_expr(expr); + if needs_par { + self.pclose(); + } + } + + fn print_expr_vec(&mut self, exprs: &[P]) { + self.ibox(INDENT_UNIT); + self.word("["); + self.commasep_exprs(Inconsistent, exprs); + self.word("]"); + self.end(); + } + + pub(super) fn print_expr_anon_const( + &mut self, + expr: &ast::AnonConst, + attrs: &[ast::Attribute], + ) { + self.ibox(INDENT_UNIT); + self.word("const"); + self.nbsp(); + if let ast::ExprKind::Block(block, None) = &expr.value.kind { + self.cbox(0); + self.ibox(0); + self.print_block_with_attrs(block, attrs); + } else { + self.print_expr(&expr.value); + } + self.end(); + } + + fn print_expr_repeat(&mut self, element: &ast::Expr, count: &ast::AnonConst) { + self.ibox(INDENT_UNIT); + self.word("["); + self.print_expr(element); + self.word_space(";"); + self.print_expr(&count.value); + self.word("]"); + self.end(); + } + + fn print_expr_struct( + &mut self, + qself: &Option, + path: &ast::Path, + fields: &[ast::ExprField], + rest: &ast::StructRest, + ) { + if let Some(qself) = qself { + self.print_qpath(path, qself, true); + } else { + self.print_path(path, true, 0); + } + self.nbsp(); + self.word("{"); + let has_rest = match rest { + ast::StructRest::Base(_) | ast::StructRest::Rest(_) => true, + ast::StructRest::None => false, + }; + if fields.is_empty() && !has_rest { + self.word("}"); + return; + } + self.cbox(0); + for field in fields.iter().delimited() { + self.maybe_print_comment(field.span.hi()); + self.print_outer_attributes(&field.attrs); + if field.is_first { + self.space_if_not_bol(); + } + if !field.is_shorthand { + self.print_ident(field.ident); + self.word_nbsp(":"); + } + self.print_expr(&field.expr); + if !field.is_last || has_rest { + self.word_space(","); + } else { + self.trailing_comma_or_space(); + } + } + if has_rest { + if fields.is_empty() { + self.space(); + } + self.word(".."); + if let ast::StructRest::Base(expr) = rest { + self.print_expr(expr); + } + self.space(); + } + self.offset(-INDENT_UNIT); + self.end(); + self.word("}"); + } + + fn print_expr_tup(&mut self, exprs: &[P]) { + self.popen(); + self.commasep_exprs(Inconsistent, exprs); + if exprs.len() == 1 { + self.word(","); + } + self.pclose() + } + + fn print_expr_call(&mut self, func: &ast::Expr, args: &[P]) { + let prec = match func.kind { + ast::ExprKind::Field(..) => parser::PREC_FORCE_PAREN, + _ => parser::PREC_POSTFIX, + }; + + self.print_expr_maybe_paren(func, prec); + self.print_call_post(args) + } + + fn print_expr_method_call(&mut self, segment: &ast::PathSegment, args: &[P]) { + let base_args = &args[1..]; + self.print_expr_maybe_paren(&args[0], parser::PREC_POSTFIX); + self.word("."); + self.print_ident(segment.ident); + if let Some(ref args) = segment.args { + self.print_generic_args(args, true); + } + self.print_call_post(base_args) + } + + fn print_expr_binary(&mut self, op: ast::BinOp, lhs: &ast::Expr, rhs: &ast::Expr) { + let assoc_op = AssocOp::from_ast_binop(op.node); + let prec = assoc_op.precedence() as i8; + let fixity = assoc_op.fixity(); + + let (left_prec, right_prec) = match fixity { + Fixity::Left => (prec, prec + 1), + Fixity::Right => (prec + 1, prec), + Fixity::None => (prec + 1, prec + 1), + }; + + let left_prec = match (&lhs.kind, op.node) { + // These cases need parens: `x as i32 < y` has the parser thinking that `i32 < y` is + // the beginning of a path type. It starts trying to parse `x as (i32 < y ...` instead + // of `(x as i32) < ...`. We need to convince it _not_ to do that. + (&ast::ExprKind::Cast { .. }, ast::BinOpKind::Lt | ast::BinOpKind::Shl) => { + parser::PREC_FORCE_PAREN + } + // We are given `(let _ = a) OP b`. + // + // - When `OP <= LAnd` we should print `let _ = a OP b` to avoid redundant parens + // as the parser will interpret this as `(let _ = a) OP b`. + // + // - Otherwise, e.g. when we have `(let a = b) < c` in AST, + // parens are required since the parser would interpret `let a = b < c` as + // `let a = (b < c)`. To achieve this, we force parens. + (&ast::ExprKind::Let { .. }, _) if !parser::needs_par_as_let_scrutinee(prec) => { + parser::PREC_FORCE_PAREN + } + _ => left_prec, + }; + + self.print_expr_maybe_paren(lhs, left_prec); + self.space(); + self.word_space(op.node.to_string()); + self.print_expr_maybe_paren(rhs, right_prec) + } + + fn print_expr_unary(&mut self, op: ast::UnOp, expr: &ast::Expr) { + self.word(ast::UnOp::to_string(op)); + self.print_expr_maybe_paren(expr, parser::PREC_PREFIX) + } + + fn print_expr_addr_of( + &mut self, + kind: ast::BorrowKind, + mutability: ast::Mutability, + expr: &ast::Expr, + ) { + self.word("&"); + match kind { + ast::BorrowKind::Ref => self.print_mutability(mutability, false), + ast::BorrowKind::Raw => { + self.word_nbsp("raw"); + self.print_mutability(mutability, true); + } + } + self.print_expr_maybe_paren(expr, parser::PREC_PREFIX) + } + + pub fn print_expr(&mut self, expr: &ast::Expr) { + self.print_expr_outer_attr_style(expr, true) + } + + pub(super) fn print_expr_outer_attr_style(&mut self, expr: &ast::Expr, is_inline: bool) { + self.maybe_print_comment(expr.span.lo()); + + let attrs = &expr.attrs; + if is_inline { + self.print_outer_attributes_inline(attrs); + } else { + self.print_outer_attributes(attrs); + } + + self.ibox(INDENT_UNIT); + self.ann.pre(self, AnnNode::Expr(expr)); + match expr.kind { + ast::ExprKind::Box(ref expr) => { + self.word_space("box"); + self.print_expr_maybe_paren(expr, parser::PREC_PREFIX); + } + ast::ExprKind::Array(ref exprs) => { + self.print_expr_vec(exprs); + } + ast::ExprKind::ConstBlock(ref anon_const) => { + self.print_expr_anon_const(anon_const, attrs); + } + ast::ExprKind::Repeat(ref element, ref count) => { + self.print_expr_repeat(element, count); + } + ast::ExprKind::Struct(ref se) => { + self.print_expr_struct(&se.qself, &se.path, &se.fields, &se.rest); + } + ast::ExprKind::Tup(ref exprs) => { + self.print_expr_tup(exprs); + } + ast::ExprKind::Call(ref func, ref args) => { + self.print_expr_call(func, &args); + } + ast::ExprKind::MethodCall(ref segment, ref args, _) => { + self.print_expr_method_call(segment, &args); + } + ast::ExprKind::Binary(op, ref lhs, ref rhs) => { + self.print_expr_binary(op, lhs, rhs); + } + ast::ExprKind::Unary(op, ref expr) => { + self.print_expr_unary(op, expr); + } + ast::ExprKind::AddrOf(k, m, ref expr) => { + self.print_expr_addr_of(k, m, expr); + } + ast::ExprKind::Lit(ref lit) => { + self.print_literal(lit); + } + ast::ExprKind::Cast(ref expr, ref ty) => { + let prec = AssocOp::As.precedence() as i8; + self.print_expr_maybe_paren(expr, prec); + self.space(); + self.word_space("as"); + self.print_type(ty); + } + ast::ExprKind::Type(ref expr, ref ty) => { + let prec = AssocOp::Colon.precedence() as i8; + self.print_expr_maybe_paren(expr, prec); + self.word_space(":"); + self.print_type(ty); + } + ast::ExprKind::Let(ref pat, ref scrutinee, _) => { + self.print_let(pat, scrutinee); + } + ast::ExprKind::If(ref test, ref blk, ref elseopt) => { + self.print_if(test, blk, elseopt.as_deref()) + } + ast::ExprKind::While(ref test, ref blk, opt_label) => { + if let Some(label) = opt_label { + self.print_ident(label.ident); + self.word_space(":"); + } + self.cbox(0); + self.ibox(0); + self.word_nbsp("while"); + self.print_expr_as_cond(test); + self.space(); + self.print_block_with_attrs(blk, attrs); + } + ast::ExprKind::ForLoop(ref pat, ref iter, ref blk, opt_label) => { + if let Some(label) = opt_label { + self.print_ident(label.ident); + self.word_space(":"); + } + self.cbox(0); + self.ibox(0); + self.word_nbsp("for"); + self.print_pat(pat); + self.space(); + self.word_space("in"); + self.print_expr_as_cond(iter); + self.space(); + self.print_block_with_attrs(blk, attrs); + } + ast::ExprKind::Loop(ref blk, opt_label) => { + if let Some(label) = opt_label { + self.print_ident(label.ident); + self.word_space(":"); + } + self.cbox(0); + self.ibox(0); + self.word_nbsp("loop"); + self.print_block_with_attrs(blk, attrs); + } + ast::ExprKind::Match(ref expr, ref arms) => { + self.cbox(0); + self.ibox(0); + self.word_nbsp("match"); + self.print_expr_as_cond(expr); + self.space(); + self.bopen(); + self.print_inner_attributes_no_trailing_hardbreak(attrs); + for arm in arms { + self.print_arm(arm); + } + let empty = attrs.is_empty() && arms.is_empty(); + self.bclose(expr.span, empty); + } + ast::ExprKind::Closure( + ref binder, + capture_clause, + asyncness, + movability, + ref decl, + ref body, + _, + ) => { + self.print_closure_binder(binder); + self.print_movability(movability); + self.print_asyncness(asyncness); + self.print_capture_clause(capture_clause); + + self.print_fn_params_and_ret(decl, true); + self.space(); + self.print_expr(body); + self.end(); // need to close a box + + // a box will be closed by print_expr, but we didn't want an overall + // wrapper so we closed the corresponding opening. so create an + // empty box to satisfy the close. + self.ibox(0); + } + ast::ExprKind::Block(ref blk, opt_label) => { + if let Some(label) = opt_label { + self.print_ident(label.ident); + self.word_space(":"); + } + // containing cbox, will be closed by print-block at } + self.cbox(0); + // head-box, will be closed by print-block after { + self.ibox(0); + self.print_block_with_attrs(blk, attrs); + } + ast::ExprKind::Async(capture_clause, _, ref blk) => { + self.word_nbsp("async"); + self.print_capture_clause(capture_clause); + // cbox/ibox in analogy to the `ExprKind::Block` arm above + self.cbox(0); + self.ibox(0); + self.print_block_with_attrs(blk, attrs); + } + ast::ExprKind::Await(ref expr) => { + self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX); + self.word(".await"); + } + ast::ExprKind::Assign(ref lhs, ref rhs, _) => { + let prec = AssocOp::Assign.precedence() as i8; + self.print_expr_maybe_paren(lhs, prec + 1); + self.space(); + self.word_space("="); + self.print_expr_maybe_paren(rhs, prec); + } + ast::ExprKind::AssignOp(op, ref lhs, ref rhs) => { + let prec = AssocOp::Assign.precedence() as i8; + self.print_expr_maybe_paren(lhs, prec + 1); + self.space(); + self.word(op.node.to_string()); + self.word_space("="); + self.print_expr_maybe_paren(rhs, prec); + } + ast::ExprKind::Field(ref expr, ident) => { + self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX); + self.word("."); + self.print_ident(ident); + } + ast::ExprKind::Index(ref expr, ref index) => { + self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX); + self.word("["); + self.print_expr(index); + self.word("]"); + } + ast::ExprKind::Range(ref start, ref end, limits) => { + // Special case for `Range`. `AssocOp` claims that `Range` has higher precedence + // than `Assign`, but `x .. x = x` gives a parse error instead of `x .. (x = x)`. + // Here we use a fake precedence value so that any child with lower precedence than + // a "normal" binop gets parenthesized. (`LOr` is the lowest-precedence binop.) + let fake_prec = AssocOp::LOr.precedence() as i8; + if let Some(ref e) = *start { + self.print_expr_maybe_paren(e, fake_prec); + } + if limits == ast::RangeLimits::HalfOpen { + self.word(".."); + } else { + self.word("..="); + } + if let Some(ref e) = *end { + self.print_expr_maybe_paren(e, fake_prec); + } + } + ast::ExprKind::Underscore => self.word("_"), + ast::ExprKind::Path(None, ref path) => self.print_path(path, true, 0), + ast::ExprKind::Path(Some(ref qself), ref path) => self.print_qpath(path, qself, true), + ast::ExprKind::Break(opt_label, ref opt_expr) => { + self.word("break"); + if let Some(label) = opt_label { + self.space(); + self.print_ident(label.ident); + } + if let Some(ref expr) = *opt_expr { + self.space(); + self.print_expr_maybe_paren(expr, parser::PREC_JUMP); + } + } + ast::ExprKind::Continue(opt_label) => { + self.word("continue"); + if let Some(label) = opt_label { + self.space(); + self.print_ident(label.ident); + } + } + ast::ExprKind::Ret(ref result) => { + self.word("return"); + if let Some(ref expr) = *result { + self.word(" "); + self.print_expr_maybe_paren(expr, parser::PREC_JUMP); + } + } + ast::ExprKind::Yeet(ref result) => { + self.word("do"); + self.word(" "); + self.word("yeet"); + if let Some(ref expr) = *result { + self.word(" "); + self.print_expr_maybe_paren(expr, parser::PREC_JUMP); + } + } + ast::ExprKind::InlineAsm(ref a) => { + self.word("asm!"); + self.print_inline_asm(a); + } + ast::ExprKind::MacCall(ref m) => self.print_mac(m), + ast::ExprKind::Paren(ref e) => { + self.popen(); + self.print_expr(e); + self.pclose(); + } + ast::ExprKind::Yield(ref e) => { + self.word("yield"); + + if let Some(ref expr) = *e { + self.space(); + self.print_expr_maybe_paren(expr, parser::PREC_JUMP); + } + } + ast::ExprKind::Try(ref e) => { + self.print_expr_maybe_paren(e, parser::PREC_POSTFIX); + self.word("?") + } + ast::ExprKind::TryBlock(ref blk) => { + self.cbox(0); + self.ibox(0); + self.word_nbsp("try"); + self.print_block_with_attrs(blk, attrs) + } + ast::ExprKind::Err => { + self.popen(); + self.word("/*ERROR*/"); + self.pclose() + } + } + self.ann.post(self, AnnNode::Expr(expr)); + self.end(); + } + + fn print_arm(&mut self, arm: &ast::Arm) { + // Note, I have no idea why this check is necessary, but here it is. + if arm.attrs.is_empty() { + self.space(); + } + self.cbox(INDENT_UNIT); + self.ibox(0); + self.maybe_print_comment(arm.pat.span.lo()); + self.print_outer_attributes(&arm.attrs); + self.print_pat(&arm.pat); + self.space(); + if let Some(ref e) = arm.guard { + self.word_space("if"); + self.print_expr(e); + self.space(); + } + self.word_space("=>"); + + match arm.body.kind { + ast::ExprKind::Block(ref blk, opt_label) => { + if let Some(label) = opt_label { + self.print_ident(label.ident); + self.word_space(":"); + } + + // The block will close the pattern's ibox. + self.print_block_unclosed_indent(blk); + + // If it is a user-provided unsafe block, print a comma after it. + if let BlockCheckMode::Unsafe(ast::UserProvided) = blk.rules { + self.word(","); + } + } + _ => { + self.end(); // Close the ibox for the pattern. + self.print_expr(&arm.body); + self.word(","); + } + } + self.end(); // Close enclosing cbox. + } + + fn print_closure_binder(&mut self, binder: &ast::ClosureBinder) { + match binder { + ast::ClosureBinder::NotPresent => {} + ast::ClosureBinder::For { generic_params, .. } => { + self.print_formal_generic_params(&generic_params) + } + } + } + + fn print_movability(&mut self, movability: ast::Movability) { + match movability { + ast::Movability::Static => self.word_space("static"), + ast::Movability::Movable => {} + } + } + + fn print_capture_clause(&mut self, capture_clause: ast::CaptureBy) { + match capture_clause { + ast::CaptureBy::Value => self.word_space("move"), + ast::CaptureBy::Ref => {} + } + } +} diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs new file mode 100644 index 000000000..f1caf22f3 --- /dev/null +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -0,0 +1,708 @@ +use crate::pp::Breaks::Inconsistent; +use crate::pprust::state::delimited::IterDelimited; +use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT}; + +use rustc_ast as ast; +use rustc_ast::GenericBound; +use rustc_ast::ModKind; +use rustc_span::symbol::Ident; + +fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String { + format!("{}{}", State::to_string(|s| s.print_visibility(vis)), s) +} + +impl<'a> State<'a> { + fn print_foreign_mod(&mut self, nmod: &ast::ForeignMod, attrs: &[ast::Attribute]) { + self.print_inner_attributes(attrs); + for item in &nmod.items { + self.print_foreign_item(item); + } + } + + pub(crate) fn print_foreign_item(&mut self, item: &ast::ForeignItem) { + let ast::Item { id, span, ident, ref attrs, ref kind, ref vis, tokens: _ } = *item; + self.ann.pre(self, AnnNode::SubItem(id)); + self.hardbreak_if_not_bol(); + self.maybe_print_comment(span.lo()); + self.print_outer_attributes(attrs); + match kind { + ast::ForeignItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => { + self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs); + } + ast::ForeignItemKind::Static(ty, mutbl, body) => { + let def = ast::Defaultness::Final; + self.print_item_const(ident, Some(*mutbl), ty, body.as_deref(), vis, def); + } + ast::ForeignItemKind::TyAlias(box ast::TyAlias { + defaultness, + generics, + where_clauses, + where_predicates_split, + bounds, + ty, + }) => { + self.print_associated_type( + ident, + generics, + *where_clauses, + *where_predicates_split, + bounds, + ty.as_deref(), + vis, + *defaultness, + ); + } + ast::ForeignItemKind::MacCall(m) => { + self.print_mac(m); + if m.args.need_semicolon() { + self.word(";"); + } + } + } + self.ann.post(self, AnnNode::SubItem(id)) + } + + fn print_item_const( + &mut self, + ident: Ident, + mutbl: Option, + ty: &ast::Ty, + body: Option<&ast::Expr>, + vis: &ast::Visibility, + defaultness: ast::Defaultness, + ) { + self.head(""); + self.print_visibility(vis); + self.print_defaultness(defaultness); + let leading = match mutbl { + None => "const", + Some(ast::Mutability::Not) => "static", + Some(ast::Mutability::Mut) => "static mut", + }; + self.word_space(leading); + self.print_ident(ident); + self.word_space(":"); + self.print_type(ty); + if body.is_some() { + self.space(); + } + self.end(); // end the head-ibox + if let Some(body) = body { + self.word_space("="); + self.print_expr(body); + } + self.word(";"); + self.end(); // end the outer cbox + } + + fn print_associated_type( + &mut self, + ident: Ident, + generics: &ast::Generics, + where_clauses: (ast::TyAliasWhereClause, ast::TyAliasWhereClause), + where_predicates_split: usize, + bounds: &ast::GenericBounds, + ty: Option<&ast::Ty>, + vis: &ast::Visibility, + defaultness: ast::Defaultness, + ) { + let (before_predicates, after_predicates) = + generics.where_clause.predicates.split_at(where_predicates_split); + self.head(""); + self.print_visibility(vis); + self.print_defaultness(defaultness); + self.word_space("type"); + self.print_ident(ident); + self.print_generic_params(&generics.params); + if !bounds.is_empty() { + self.word_nbsp(":"); + self.print_type_bounds(bounds); + } + self.print_where_clause_parts(where_clauses.0.0, before_predicates); + if let Some(ty) = ty { + self.space(); + self.word_space("="); + self.print_type(ty); + } + self.print_where_clause_parts(where_clauses.1.0, after_predicates); + self.word(";"); + self.end(); // end inner head-block + self.end(); // end outer head-block + } + + /// Pretty-prints an item. + pub(crate) fn print_item(&mut self, item: &ast::Item) { + self.hardbreak_if_not_bol(); + self.maybe_print_comment(item.span.lo()); + self.print_outer_attributes(&item.attrs); + self.ann.pre(self, AnnNode::Item(item)); + match item.kind { + ast::ItemKind::ExternCrate(orig_name) => { + self.head(visibility_qualified(&item.vis, "extern crate")); + if let Some(orig_name) = orig_name { + self.print_name(orig_name); + self.space(); + self.word("as"); + self.space(); + } + self.print_ident(item.ident); + self.word(";"); + self.end(); // end inner head-block + self.end(); // end outer head-block + } + ast::ItemKind::Use(ref tree) => { + self.print_visibility(&item.vis); + self.word_nbsp("use"); + self.print_use_tree(tree); + self.word(";"); + } + ast::ItemKind::Static(ref ty, mutbl, ref body) => { + let def = ast::Defaultness::Final; + self.print_item_const(item.ident, Some(mutbl), ty, body.as_deref(), &item.vis, def); + } + ast::ItemKind::Const(def, ref ty, ref body) => { + self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, def); + } + ast::ItemKind::Fn(box ast::Fn { defaultness, ref sig, ref generics, ref body }) => { + let body = body.as_deref(); + self.print_fn_full( + sig, + item.ident, + generics, + &item.vis, + defaultness, + body, + &item.attrs, + ); + } + ast::ItemKind::Mod(unsafety, ref mod_kind) => { + self.head(Self::to_string(|s| { + s.print_visibility(&item.vis); + s.print_unsafety(unsafety); + s.word("mod"); + })); + self.print_ident(item.ident); + + match mod_kind { + ModKind::Loaded(items, ..) => { + self.nbsp(); + self.bopen(); + self.print_inner_attributes(&item.attrs); + for item in items { + self.print_item(item); + } + let empty = item.attrs.is_empty() && items.is_empty(); + self.bclose(item.span, empty); + } + ModKind::Unloaded => { + self.word(";"); + self.end(); // end inner head-block + self.end(); // end outer head-block + } + } + } + ast::ItemKind::ForeignMod(ref nmod) => { + self.head(Self::to_string(|s| { + s.print_unsafety(nmod.unsafety); + s.word("extern"); + })); + if let Some(abi) = nmod.abi { + self.print_literal(&abi.as_lit()); + self.nbsp(); + } + self.bopen(); + self.print_foreign_mod(nmod, &item.attrs); + let empty = item.attrs.is_empty() && nmod.items.is_empty(); + self.bclose(item.span, empty); + } + ast::ItemKind::GlobalAsm(ref asm) => { + self.head(visibility_qualified(&item.vis, "global_asm!")); + self.print_inline_asm(asm); + self.end(); + } + ast::ItemKind::TyAlias(box ast::TyAlias { + defaultness, + ref generics, + where_clauses, + where_predicates_split, + ref bounds, + ref ty, + }) => { + let ty = ty.as_deref(); + self.print_associated_type( + item.ident, + generics, + where_clauses, + where_predicates_split, + bounds, + ty, + &item.vis, + defaultness, + ); + } + ast::ItemKind::Enum(ref enum_definition, ref params) => { + self.print_enum_def(enum_definition, params, item.ident, item.span, &item.vis); + } + ast::ItemKind::Struct(ref struct_def, ref generics) => { + self.head(visibility_qualified(&item.vis, "struct")); + self.print_struct(struct_def, generics, item.ident, item.span, true); + } + ast::ItemKind::Union(ref struct_def, ref generics) => { + self.head(visibility_qualified(&item.vis, "union")); + self.print_struct(struct_def, generics, item.ident, item.span, true); + } + ast::ItemKind::Impl(box ast::Impl { + unsafety, + polarity, + defaultness, + constness, + ref generics, + ref of_trait, + ref self_ty, + ref items, + }) => { + self.head(""); + self.print_visibility(&item.vis); + self.print_defaultness(defaultness); + self.print_unsafety(unsafety); + self.word("impl"); + + if generics.params.is_empty() { + self.nbsp(); + } else { + self.print_generic_params(&generics.params); + self.space(); + } + + self.print_constness(constness); + + if let ast::ImplPolarity::Negative(_) = polarity { + self.word("!"); + } + + if let Some(ref t) = *of_trait { + self.print_trait_ref(t); + self.space(); + self.word_space("for"); + } + + self.print_type(self_ty); + self.print_where_clause(&generics.where_clause); + + self.space(); + self.bopen(); + self.print_inner_attributes(&item.attrs); + for impl_item in items { + self.print_assoc_item(impl_item); + } + let empty = item.attrs.is_empty() && items.is_empty(); + self.bclose(item.span, empty); + } + ast::ItemKind::Trait(box ast::Trait { + is_auto, + unsafety, + ref generics, + ref bounds, + ref items, + .. + }) => { + self.head(""); + self.print_visibility(&item.vis); + self.print_unsafety(unsafety); + self.print_is_auto(is_auto); + self.word_nbsp("trait"); + self.print_ident(item.ident); + self.print_generic_params(&generics.params); + let mut real_bounds = Vec::with_capacity(bounds.len()); + for b in bounds.iter() { + if let GenericBound::Trait(ref ptr, ast::TraitBoundModifier::Maybe) = *b { + self.space(); + self.word_space("for ?"); + self.print_trait_ref(&ptr.trait_ref); + } else { + real_bounds.push(b.clone()); + } + } + if !real_bounds.is_empty() { + self.word_nbsp(":"); + self.print_type_bounds(&real_bounds); + } + self.print_where_clause(&generics.where_clause); + self.word(" "); + self.bopen(); + self.print_inner_attributes(&item.attrs); + for trait_item in items { + self.print_assoc_item(trait_item); + } + let empty = item.attrs.is_empty() && items.is_empty(); + self.bclose(item.span, empty); + } + ast::ItemKind::TraitAlias(ref generics, ref bounds) => { + self.head(visibility_qualified(&item.vis, "trait")); + self.print_ident(item.ident); + self.print_generic_params(&generics.params); + let mut real_bounds = Vec::with_capacity(bounds.len()); + // FIXME(durka) this seems to be some quite outdated syntax + for b in bounds.iter() { + if let GenericBound::Trait(ref ptr, ast::TraitBoundModifier::Maybe) = *b { + self.space(); + self.word_space("for ?"); + self.print_trait_ref(&ptr.trait_ref); + } else { + real_bounds.push(b.clone()); + } + } + self.nbsp(); + if !real_bounds.is_empty() { + self.word_nbsp("="); + self.print_type_bounds(&real_bounds); + } + self.print_where_clause(&generics.where_clause); + self.word(";"); + self.end(); // end inner head-block + self.end(); // end outer head-block + } + ast::ItemKind::MacCall(ref mac) => { + self.print_mac(mac); + if mac.args.need_semicolon() { + self.word(";"); + } + } + ast::ItemKind::MacroDef(ref macro_def) => { + self.print_mac_def(macro_def, &item.ident, item.span, |state| { + state.print_visibility(&item.vis) + }); + } + } + self.ann.post(self, AnnNode::Item(item)) + } + + fn print_enum_def( + &mut self, + enum_definition: &ast::EnumDef, + generics: &ast::Generics, + ident: Ident, + span: rustc_span::Span, + visibility: &ast::Visibility, + ) { + self.head(visibility_qualified(visibility, "enum")); + self.print_ident(ident); + self.print_generic_params(&generics.params); + self.print_where_clause(&generics.where_clause); + self.space(); + self.print_variants(&enum_definition.variants, span) + } + + fn print_variants(&mut self, variants: &[ast::Variant], span: rustc_span::Span) { + self.bopen(); + for v in variants { + self.space_if_not_bol(); + self.maybe_print_comment(v.span.lo()); + self.print_outer_attributes(&v.attrs); + self.ibox(0); + self.print_variant(v); + self.word(","); + self.end(); + self.maybe_print_trailing_comment(v.span, None); + } + let empty = variants.is_empty(); + self.bclose(span, empty) + } + + pub(crate) fn print_visibility(&mut self, vis: &ast::Visibility) { + match vis.kind { + ast::VisibilityKind::Public => self.word_nbsp("pub"), + ast::VisibilityKind::Restricted { ref path, .. } => { + let path = Self::to_string(|s| s.print_path(path, false, 0)); + if path == "crate" || path == "self" || path == "super" { + self.word_nbsp(format!("pub({})", path)) + } else { + self.word_nbsp(format!("pub(in {})", path)) + } + } + ast::VisibilityKind::Inherited => {} + } + } + + fn print_defaultness(&mut self, defaultness: ast::Defaultness) { + if let ast::Defaultness::Default(_) = defaultness { + self.word_nbsp("default"); + } + } + + fn print_record_struct_body(&mut self, fields: &[ast::FieldDef], span: rustc_span::Span) { + self.nbsp(); + self.bopen(); + + let empty = fields.is_empty(); + if !empty { + self.hardbreak_if_not_bol(); + + for field in fields { + self.hardbreak_if_not_bol(); + self.maybe_print_comment(field.span.lo()); + self.print_outer_attributes(&field.attrs); + self.print_visibility(&field.vis); + self.print_ident(field.ident.unwrap()); + self.word_nbsp(":"); + self.print_type(&field.ty); + self.word(","); + } + } + + self.bclose(span, empty); + } + + fn print_struct( + &mut self, + struct_def: &ast::VariantData, + generics: &ast::Generics, + ident: Ident, + span: rustc_span::Span, + print_finalizer: bool, + ) { + self.print_ident(ident); + self.print_generic_params(&generics.params); + match struct_def { + ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => { + if let ast::VariantData::Tuple(..) = struct_def { + self.popen(); + self.commasep(Inconsistent, struct_def.fields(), |s, field| { + s.maybe_print_comment(field.span.lo()); + s.print_outer_attributes(&field.attrs); + s.print_visibility(&field.vis); + s.print_type(&field.ty) + }); + self.pclose(); + } + self.print_where_clause(&generics.where_clause); + if print_finalizer { + self.word(";"); + } + self.end(); + self.end(); // Close the outer-box. + } + ast::VariantData::Struct(ref fields, ..) => { + self.print_where_clause(&generics.where_clause); + self.print_record_struct_body(fields, span); + } + } + } + + pub(crate) fn print_variant(&mut self, v: &ast::Variant) { + self.head(""); + self.print_visibility(&v.vis); + let generics = ast::Generics::default(); + self.print_struct(&v.data, &generics, v.ident, v.span, false); + if let Some(ref d) = v.disr_expr { + self.space(); + self.word_space("="); + self.print_expr(&d.value) + } + } + + pub(crate) fn print_assoc_item(&mut self, item: &ast::AssocItem) { + let ast::Item { id, span, ident, ref attrs, ref kind, ref vis, tokens: _ } = *item; + self.ann.pre(self, AnnNode::SubItem(id)); + self.hardbreak_if_not_bol(); + self.maybe_print_comment(span.lo()); + self.print_outer_attributes(attrs); + match kind { + ast::AssocItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => { + self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs); + } + ast::AssocItemKind::Const(def, ty, body) => { + self.print_item_const(ident, None, ty, body.as_deref(), vis, *def); + } + ast::AssocItemKind::TyAlias(box ast::TyAlias { + defaultness, + generics, + where_clauses, + where_predicates_split, + bounds, + ty, + }) => { + self.print_associated_type( + ident, + generics, + *where_clauses, + *where_predicates_split, + bounds, + ty.as_deref(), + vis, + *defaultness, + ); + } + ast::AssocItemKind::MacCall(m) => { + self.print_mac(m); + if m.args.need_semicolon() { + self.word(";"); + } + } + } + self.ann.post(self, AnnNode::SubItem(id)) + } + + fn print_fn_full( + &mut self, + sig: &ast::FnSig, + name: Ident, + generics: &ast::Generics, + vis: &ast::Visibility, + defaultness: ast::Defaultness, + body: Option<&ast::Block>, + attrs: &[ast::Attribute], + ) { + if body.is_some() { + self.head(""); + } + self.print_visibility(vis); + self.print_defaultness(defaultness); + self.print_fn(&sig.decl, sig.header, Some(name), generics); + if let Some(body) = body { + self.nbsp(); + self.print_block_with_attrs(body, attrs); + } else { + self.word(";"); + } + } + + pub(crate) fn print_fn( + &mut self, + decl: &ast::FnDecl, + header: ast::FnHeader, + name: Option, + generics: &ast::Generics, + ) { + self.print_fn_header_info(header); + if let Some(name) = name { + self.nbsp(); + self.print_ident(name); + } + self.print_generic_params(&generics.params); + self.print_fn_params_and_ret(decl, false); + self.print_where_clause(&generics.where_clause) + } + + pub(crate) fn print_fn_params_and_ret(&mut self, decl: &ast::FnDecl, is_closure: bool) { + let (open, close) = if is_closure { ("|", "|") } else { ("(", ")") }; + self.word(open); + self.commasep(Inconsistent, &decl.inputs, |s, param| s.print_param(param, is_closure)); + self.word(close); + self.print_fn_ret_ty(&decl.output) + } + + fn print_where_clause(&mut self, where_clause: &ast::WhereClause) { + self.print_where_clause_parts(where_clause.has_where_token, &where_clause.predicates); + } + + pub(crate) fn print_where_clause_parts( + &mut self, + has_where_token: bool, + predicates: &[ast::WherePredicate], + ) { + if predicates.is_empty() && !has_where_token { + return; + } + + self.space(); + self.word_space("where"); + + for (i, predicate) in predicates.iter().enumerate() { + if i != 0 { + self.word_space(","); + } + + self.print_where_predicate(predicate); + } + } + + pub fn print_where_predicate(&mut self, predicate: &ast::WherePredicate) { + match predicate { + ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate { + bound_generic_params, + bounded_ty, + bounds, + .. + }) => { + self.print_formal_generic_params(bound_generic_params); + self.print_type(bounded_ty); + self.word(":"); + if !bounds.is_empty() { + self.nbsp(); + self.print_type_bounds(bounds); + } + } + ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate { + lifetime, + bounds, + .. + }) => { + self.print_lifetime(*lifetime); + self.word(":"); + if !bounds.is_empty() { + self.nbsp(); + self.print_lifetime_bounds(bounds); + } + } + ast::WherePredicate::EqPredicate(ast::WhereEqPredicate { lhs_ty, rhs_ty, .. }) => { + self.print_type(lhs_ty); + self.space(); + self.word_space("="); + self.print_type(rhs_ty); + } + } + } + + fn print_use_tree(&mut self, tree: &ast::UseTree) { + match tree.kind { + ast::UseTreeKind::Simple(rename, ..) => { + self.print_path(&tree.prefix, false, 0); + if let Some(rename) = rename { + self.nbsp(); + self.word_nbsp("as"); + self.print_ident(rename); + } + } + ast::UseTreeKind::Glob => { + if !tree.prefix.segments.is_empty() { + self.print_path(&tree.prefix, false, 0); + self.word("::"); + } + self.word("*"); + } + ast::UseTreeKind::Nested(ref items) => { + if !tree.prefix.segments.is_empty() { + self.print_path(&tree.prefix, false, 0); + self.word("::"); + } + if items.is_empty() { + self.word("{}"); + } else if items.len() == 1 { + self.print_use_tree(&items[0].0); + } else { + self.cbox(INDENT_UNIT); + self.word("{"); + self.zerobreak(); + self.ibox(0); + for use_tree in items.iter().delimited() { + self.print_use_tree(&use_tree.0); + if !use_tree.is_last { + self.word(","); + if let ast::UseTreeKind::Nested(_) = use_tree.0.kind { + self.hardbreak(); + } else { + self.space(); + } + } + } + self.end(); + self.trailing_comma(); + self.offset(-INDENT_UNIT); + self.word("}"); + self.end(); + } + } + } + } +} diff --git a/compiler/rustc_ast_pretty/src/pprust/tests.rs b/compiler/rustc_ast_pretty/src/pprust/tests.rs new file mode 100644 index 000000000..6c8d42f33 --- /dev/null +++ b/compiler/rustc_ast_pretty/src/pprust/tests.rs @@ -0,0 +1,63 @@ +use super::*; + +use rustc_ast as ast; +use rustc_span::create_default_session_globals_then; +use rustc_span::symbol::Ident; + +fn fun_to_string( + decl: &ast::FnDecl, + header: ast::FnHeader, + name: Ident, + generics: &ast::Generics, +) -> String { + to_string(|s| { + s.head(""); + s.print_fn(decl, header, Some(name), generics); + s.end(); // Close the head box. + s.end(); // Close the outer box. + }) +} + +fn variant_to_string(var: &ast::Variant) -> String { + to_string(|s| s.print_variant(var)) +} + +#[test] +fn test_fun_to_string() { + create_default_session_globals_then(|| { + let abba_ident = Ident::from_str("abba"); + + let decl = + ast::FnDecl { inputs: Vec::new(), output: ast::FnRetTy::Default(rustc_span::DUMMY_SP) }; + let generics = ast::Generics::default(); + assert_eq!( + fun_to_string(&decl, ast::FnHeader::default(), abba_ident, &generics), + "fn abba()" + ); + }) +} + +#[test] +fn test_variant_to_string() { + create_default_session_globals_then(|| { + let ident = Ident::from_str("principal_skinner"); + + let var = ast::Variant { + ident, + vis: ast::Visibility { + span: rustc_span::DUMMY_SP, + kind: ast::VisibilityKind::Inherited, + tokens: None, + }, + attrs: ast::AttrVec::new(), + id: ast::DUMMY_NODE_ID, + data: ast::VariantData::Unit(ast::DUMMY_NODE_ID), + disr_expr: None, + span: rustc_span::DUMMY_SP, + is_placeholder: false, + }; + + let varstr = variant_to_string(&var); + assert_eq!(varstr, "principal_skinner"); + }) +} -- cgit v1.2.3