Skip to content

Improve DelimArgs pretty-printing #140312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use thin_vec::{ThinVec, thin_vec};
pub use crate::format::*;
use crate::ptr::P;
use crate::token::{self, CommentKind, Delimiter};
use crate::tokenstream::{DelimSpan, LazyAttrTokenStream, TokenStream};
use crate::tokenstream::{DelimSpan, LazyAttrTokenStream, Spacing, TokenStream};
use crate::util::parser::{ExprPrecedence, Fixity};

/// A "Label" is an identifier of some point in sources,
Expand Down Expand Up @@ -1930,6 +1930,10 @@ impl AttrArgs {
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
pub struct DelimArgs {
pub dspan: DelimSpan,
// The spacing after the open delimiter. Used when pretty-printing to
// decide whether to put a space after the opening delim and a space before
// the closing delim.
pub open_spacing: Spacing,
pub delim: Delimiter, // Note: `Delimiter::Invisible` never occurs
pub tokens: TokenStream,
}
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,12 @@ impl MetaItemKind {
fn from_attr_args(args: &AttrArgs) -> Option<MetaItemKind> {
match args {
AttrArgs::Empty => Some(MetaItemKind::Word),
AttrArgs::Delimited(DelimArgs { dspan: _, delim: Delimiter::Parenthesis, tokens }) => {
MetaItemKind::list_from_tokens(tokens.clone()).map(MetaItemKind::List)
}
AttrArgs::Delimited(DelimArgs {
dspan: _,
open_spacing: _,
delim: Delimiter::Parenthesis,
tokens,
}) => MetaItemKind::list_from_tokens(tokens.clone()).map(MetaItemKind::List),
AttrArgs::Delimited(..) => None,
AttrArgs::Eq { expr, .. } => match expr.kind {
ExprKind::Lit(token_lit) => {
Expand Down Expand Up @@ -679,6 +682,7 @@ pub fn mk_attr_nested_word(
let path = Path::from_ident(outer_ident);
let attr_args = AttrArgs::Delimited(DelimArgs {
dspan: DelimSpan::from_single(span),
open_spacing: Spacing::Joint,
delim: Delimiter::Parenthesis,
tokens: inner_tokens,
});
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ fn visit_attr_args<T: MutVisitor>(vis: &mut T, args: &mut AttrArgs) {

// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
fn visit_delim_args<T: MutVisitor>(vis: &mut T, args: &mut DelimArgs) {
let DelimArgs { dspan, delim: _, tokens } = args;
let DelimArgs { dspan, open_spacing: _, delim: _, tokens } = args;
visit_tts(vis, tokens);
visit_delim_span(vis, dspan);
}
Expand Down
51 changes: 33 additions & 18 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,15 +629,17 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
ast::Safety::Default | ast::Safety::Safe(_) => {}
}
match &item.args {
AttrArgs::Delimited(DelimArgs { dspan: _, delim, tokens }) => self.print_mac_common(
Some(MacHeader::Path(&item.path)),
false,
None,
*delim,
tokens,
true,
span,
),
AttrArgs::Delimited(DelimArgs { dspan: _, open_spacing, delim, tokens }) => self
.print_mac_common(
Some(MacHeader::Path(&item.path)),
false,
None,
*delim,
*open_spacing,
tokens,
true,
span,
),
AttrArgs::Empty => {
self.print_path(&item.path, false, 0);
}
Expand Down Expand Up @@ -679,6 +681,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
false,
None,
*delim,
spacing.open,
Copy link
Contributor

@petrochenkov petrochenkov Apr 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes in use a::{b, c} and Foo<{true}> are caused by this line, and are related to token stream printing, not to AST printing.
So they do not need adding the open_spacing: Spacing field to AST, token streams already have the relevant information.

I guess for AST DelimArgs it would also be good enough to just use Spacing::Alone for { and Spacing::Joint for (/[ instead of adding the field.

tts,
convert_dollar_crate,
dspan.entire(),
Expand Down Expand Up @@ -735,6 +738,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
has_bang: bool,
ident: Option<Ident>,
delim: Delimiter,
open_spacing: Spacing,
tts: &TokenStream,
convert_dollar_crate: bool,
span: Span,
Expand All @@ -760,16 +764,25 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.nbsp();
}
self.word("{");
if !tts.is_empty() {

// Respect `Alone` and print a space, unless the list is empty.
let open_space = open_spacing == Spacing::Alone && !tts.is_empty();
if open_space {
self.space();
}
self.ibox(0);
self.print_tts(tts, convert_dollar_crate);
self.end();
let empty = tts.is_empty();
self.bclose(span, empty);

// Use `spacing.open` for the spacing *before* the closing
// delim. Because spacing on delimiters is lost when going
// through proc macros, and otherwise we can end up with ugly
// cases like `{ x}`. Symmetry is better.
self.bclose(span, !open_space);
}
delim => {
// `open_spacing` is ignored. We never print spaces after
// non-brace opening delims or before non-brace closing delims.
let token_str = self.token_kind_to_string(&delim.as_open_token_kind());
self.word(token_str);
self.ibox(0);
Expand Down Expand Up @@ -799,6 +812,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
has_bang,
Some(*ident),
macro_def.body.delim,
macro_def.body.open_spacing,
&macro_def.body.tokens,
true,
sp,
Expand Down Expand Up @@ -845,9 +859,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.end(); // Close the head-box.
}

fn bclose_maybe_open(&mut self, span: rustc_span::Span, empty: bool, close_box: bool) {
fn bclose_maybe_open(&mut self, span: rustc_span::Span, no_space: bool, close_box: bool) {
let has_comment = self.maybe_print_comment(span.hi());
if !empty || has_comment {
if !no_space || has_comment {
self.break_offset_if_not_bol(1, -INDENT_UNIT);
}
self.word("}");
Expand All @@ -856,9 +870,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
}
}

fn bclose(&mut self, span: rustc_span::Span, empty: bool) {
fn bclose(&mut self, span: rustc_span::Span, no_space: bool) {
let close_box = true;
self.bclose_maybe_open(span, empty, close_box)
self.bclose_maybe_open(span, no_space, close_box)
}

fn break_offset_if_not_bol(&mut self, n: usize, off: isize) {
Expand Down Expand Up @@ -1420,8 +1434,8 @@ impl<'a> State<'a> {
}
}

let empty = !has_attrs && blk.stmts.is_empty();
self.bclose_maybe_open(blk.span, empty, close_box);
let no_space = !has_attrs && blk.stmts.is_empty();
self.bclose_maybe_open(blk.span, no_space, close_box);
self.ann.post(self, AnnNode::Block(blk))
}

Expand Down Expand Up @@ -1468,6 +1482,7 @@ impl<'a> State<'a> {
true,
None,
m.args.delim,
m.args.open_spacing,
&m.args.tokens,
true,
m.span(),
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_builtin_macros/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod context;

use rustc_ast::ptr::P;
use rustc_ast::token::Delimiter;
use rustc_ast::tokenstream::{DelimSpan, TokenStream};
use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenStream};
use rustc_ast::{DelimArgs, Expr, ExprKind, MacCall, Path, PathSegment, UnOp, token};
use rustc_ast_pretty::pprust;
use rustc_errors::PResult;
Expand Down Expand Up @@ -59,6 +59,7 @@ pub(crate) fn expand_assert<'cx>(
path: panic_path(),
args: P(DelimArgs {
dspan: DelimSpan::from_single(call_site_span),
open_spacing: Spacing::Joint,
delim: Delimiter::Parenthesis,
tokens,
}),
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_builtin_macros/src/assert/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_ast::ptr::P;
use rustc_ast::token::{self, Delimiter, IdentIsRaw};
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenStream, TokenTree};
use rustc_ast::{
BinOpKind, BorrowKind, DUMMY_NODE_ID, DelimArgs, Expr, ExprKind, ItemKind, MacCall, MethodCall,
Mutability, Path, PathSegment, Stmt, StructRest, UnOp, UseTree, UseTreeKind,
Expand Down Expand Up @@ -180,6 +180,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
path: panic_path,
args: P(DelimArgs {
dspan: DelimSpan::from_single(self.span),
open_spacing: Spacing::Joint,
delim: Delimiter::Parenthesis,
tokens: initial.into_iter().chain(captures).collect::<TokenStream>(),
}),
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_builtin_macros/src/autodiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,10 @@ mod llvm_enzyme {
Spacing::Joint,
)];
let never_arg = ast::DelimArgs {
dspan: ast::tokenstream::DelimSpan::from_single(span),
dspan: DelimSpan::from_single(span),
open_spacing: Spacing::Joint,
delim: ast::token::Delimiter::Parenthesis,
tokens: ast::tokenstream::TokenStream::from_iter(ts2),
tokens: TokenStream::from_iter(ts2),
};
let inline_item = ast::AttrItem {
unsafety: ast::Safety::Default,
Expand Down Expand Up @@ -395,6 +396,7 @@ mod llvm_enzyme {
// Now update for d_fn
rustc_ad_attr.item.args = rustc_ast::AttrArgs::Delimited(rustc_ast::DelimArgs {
dspan: DelimSpan::dummy(),
open_spacing: Spacing::Joint,
delim: rustc_ast::token::Delimiter::Parenthesis,
tokens: ts,
});
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_builtin_macros/src/edition_panic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_ast::ptr::P;
use rustc_ast::token::Delimiter;
use rustc_ast::tokenstream::{DelimSpan, TokenStream};
use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenStream};
use rustc_ast::*;
use rustc_expand::base::*;
use rustc_span::edition::Edition;
Expand Down Expand Up @@ -60,6 +60,7 @@ fn expand<'cx>(
},
args: P(DelimArgs {
dspan: DelimSpan::from_single(sp),
open_spacing: Spacing::Joint,
delim: Delimiter::Parenthesis,
tokens: tts,
}),
Expand Down
15 changes: 9 additions & 6 deletions compiler/rustc_expand/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustc_ast::ptr::P;
use rustc_ast::util::literal;
use rustc_ast::{
self as ast, AnonConst, AttrVec, BlockCheckMode, Expr, LocalKind, MatchKind, PatKind, UnOp,
attr, token,
attr, token, tokenstream,
};
use rustc_span::source_map::Spanned;
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
Expand Down Expand Up @@ -54,14 +54,16 @@ impl<'a> ExtCtxt<'a> {
pub fn macro_call(
&self,
span: Span,
open_spacing: tokenstream::Spacing,
path: ast::Path,
delim: ast::token::Delimiter,
tokens: ast::tokenstream::TokenStream,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: these could be fully imported, I don't see any naming conflicts in this file.

delim: token::Delimiter,
tokens: tokenstream::TokenStream,
) -> P<ast::MacCall> {
P(ast::MacCall {
path,
args: P(ast::DelimArgs {
dspan: ast::tokenstream::DelimSpan { open: span, close: span },
dspan: tokenstream::DelimSpan { open: span, close: span },
open_spacing,
delim,
tokens,
}),
Expand Down Expand Up @@ -476,12 +478,13 @@ impl<'a> ExtCtxt<'a> {
span,
self.macro_call(
span,
tokenstream::Spacing::Joint,
self.path_global(
span,
[sym::std, sym::unreachable].map(|s| Ident::new(s, span)).to_vec(),
),
ast::token::Delimiter::Parenthesis,
ast::tokenstream::TokenStream::default(),
token::Delimiter::Parenthesis,
tokenstream::TokenStream::default(),
),
)
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_expand/src/placeholders.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc_ast::mut_visit::*;
use rustc_ast::ptr::P;
use rustc_ast::token::Delimiter;
use rustc_ast::tokenstream::Spacing;
use rustc_ast::visit::AssocCtxt;
use rustc_ast::{self as ast, Safety};
use rustc_data_structures::fx::FxHashMap;
Expand All @@ -20,6 +21,7 @@ pub(crate) fn placeholder(
path: ast::Path { span: DUMMY_SP, segments: ThinVec::new(), tokens: None },
args: P(ast::DelimArgs {
dspan: ast::tokenstream::DelimSpan::dummy(),
open_spacing: Spacing::Joint,
delim: Delimiter::Parenthesis,
tokens: ast::tokenstream::TokenStream::new(Vec::new()),
}),
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,13 @@ impl<'a> State<'a> {
};

match &item.args {
hir::AttrArgs::Delimited(DelimArgs { dspan: _, delim, tokens }) => self
hir::AttrArgs::Delimited(DelimArgs { dspan: _, open_spacing, delim, tokens }) => self
.print_mac_common(
Some(MacHeader::Path(&path)),
false,
None,
*delim,
*open_spacing,
&tokens,
true,
span,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ pub fn parse_cfg_attr(
<https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute>";

match cfg_attr.get_normal_item().args {
ast::AttrArgs::Delimited(ast::DelimArgs { dspan, delim, ref tokens })
ast::AttrArgs::Delimited(ast::DelimArgs { dspan, open_spacing: _, delim, ref tokens })
if !tokens.is_empty() =>
{
crate::validate_attr::check_cfg_attr_bad_delim(psess, dspan, delim);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ast::token::IdentIsRaw;
use rustc_ast::ast::*;
use rustc_ast::ptr::P;
use rustc_ast::token::{self, Delimiter, InvisibleOrigin, MetaVarKind, TokenKind};
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenStream, TokenTree};
use rustc_ast::util::case::Case;
use rustc_ast::{self as ast};
use rustc_ast_pretty::pprust;
Expand Down Expand Up @@ -2198,7 +2198,7 @@ impl<'a> Parser<'a> {
let arrow = TokenTree::token_alone(token::FatArrow, pspan.between(bspan)); // `=>`
let tokens = TokenStream::new(vec![params, arrow, body]);
let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
P(DelimArgs { dspan, delim: Delimiter::Brace, tokens })
P(DelimArgs { dspan, open_spacing: Spacing::Alone, delim: Delimiter::Brace, tokens })
} else {
self.unexpected_any()?
};
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1464,10 +1464,11 @@ impl<'a> Parser<'a> {
|| self.check(exp!(OpenBrace));

delimited.then(|| {
let TokenTree::Delimited(dspan, _, delim, tokens) = self.parse_token_tree() else {
let TokenTree::Delimited(dspan, spacing, delim, tokens) = self.parse_token_tree()
else {
unreachable!()
};
DelimArgs { dspan, delim, tokens }
DelimArgs { dspan, open_spacing: spacing.open, delim, tokens }
})
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/validate_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn parse_meta<'a>(psess: &'a ParseSess, attr: &Attribute) -> PResult<'a, Met
path: item.path.clone(),
kind: match &item.args {
AttrArgs::Empty => MetaItemKind::Word,
AttrArgs::Delimited(DelimArgs { dspan, delim, tokens }) => {
AttrArgs::Delimited(DelimArgs { dspan, open_spacing: _, delim, tokens }) => {
check_meta_bad_delim(psess, *dspan, *delim);
let nmis =
parse_in(psess, tokens.clone(), "meta list", |p| p.parse_meta_seq_top())?;
Expand Down
Loading
Loading