summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_ast_pretty/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:35 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:35 +0000
commitd1b2d29528b7794b41e66fc2136e395a02f8529b (patch)
treea4a17504b260206dec3cf55b2dca82929a348ac2 /compiler/rustc_ast_pretty/src
parentReleasing progress-linux version 1.72.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.tar.xz
rustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.zip
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_ast_pretty/src')
-rw-r--r--compiler/rustc_ast_pretty/src/pprust/state.rs17
-rw-r--r--compiler/rustc_ast_pretty/src/pprust/state/expr.rs10
-rw-r--r--compiler/rustc_ast_pretty/src/pprust/state/item.rs35
3 files changed, 45 insertions, 17 deletions
diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs
index 59239b49e..58ce73047 100644
--- a/compiler/rustc_ast_pretty/src/pprust/state.rs
+++ b/compiler/rustc_ast_pretty/src/pprust/state.rs
@@ -150,6 +150,8 @@ pub fn print_crate<'a>(
/// 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 {
+ // No space after these tokens, e.g. `x.y`, `$e`
+ // (The carets point to `prev`.) ^ ^
if matches!(token.kind, token::Dot | token::Dollar) {
return false;
}
@@ -158,10 +160,19 @@ fn tt_prepend_space(tt: &TokenTree, prev: &TokenTree) -> bool {
}
}
match tt {
+ // No space before these tokens, e.g. `foo,`, `println!`, `x.y`
+ // (The carets point to `token`.) ^ ^ ^
+ //
+ // FIXME: having `Not` here works well for macro invocations like
+ // `println!()`, but is bad when `!` means "logical not" or "the never
+ // type", where the lack of space causes ugliness like this:
+ // `Fn() ->!`, `x =! y`, `if! x { f(); }`.
TokenTree::Token(token, _) => !matches!(token.kind, token::Comma | token::Not | token::Dot),
+ // No space before parentheses if preceded by these tokens, e.g. `foo(...)`
TokenTree::Delimited(_, Delimiter::Parenthesis, _) => {
!matches!(prev, TokenTree::Token(Token { kind: token::Ident(..), .. }, _))
}
+ // No space before brackets if preceded by these tokens, e.g. `#[...]`
TokenTree::Delimited(_, Delimiter::Bracket, _) => {
!matches!(prev, TokenTree::Token(Token { kind: token::Pound, .. }, _))
}
@@ -476,7 +487,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
Some(MacHeader::Path(&item.path)),
false,
None,
- delim.to_token(),
+ *delim,
tokens,
true,
span,
@@ -640,7 +651,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
Some(MacHeader::Keyword(kw)),
has_bang,
Some(*ident),
- macro_def.body.delim.to_token(),
+ macro_def.body.delim,
&macro_def.body.tokens.clone(),
true,
sp,
@@ -1240,7 +1251,7 @@ impl<'a> State<'a> {
Some(MacHeader::Path(&m.path)),
true,
None,
- m.args.delim.to_token(),
+ m.args.delim,
&m.args.tokens.clone(),
true,
m.span(),
diff --git a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs
index 609920180..39741a039 100644
--- a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs
+++ b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs
@@ -477,7 +477,7 @@ impl<'a> State<'a> {
self.word(".");
self.print_ident(*ident);
}
- ast::ExprKind::Index(expr, index) => {
+ ast::ExprKind::Index(expr, index, _) => {
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
self.word("[");
self.print_expr(index);
@@ -697,15 +697,15 @@ pub fn reconstruct_format_args_template_string(pieces: &[FormatArgsPiece]) -> St
write!(template, "{n}").unwrap();
if p.format_options != Default::default() || p.format_trait != FormatTrait::Display
{
- template.push_str(":");
+ template.push(':');
}
if let Some(fill) = p.format_options.fill {
template.push(fill);
}
match p.format_options.alignment {
- Some(FormatAlignment::Left) => template.push_str("<"),
- Some(FormatAlignment::Right) => template.push_str(">"),
- Some(FormatAlignment::Center) => template.push_str("^"),
+ Some(FormatAlignment::Left) => template.push('<'),
+ Some(FormatAlignment::Right) => template.push('>'),
+ Some(FormatAlignment::Center) => template.push('^'),
None => {}
}
match p.format_options.sign {
diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs
index 5c01b7ea7..d27a44f12 100644
--- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs
+++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs
@@ -30,10 +30,15 @@ impl<'a> State<'a> {
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::Static(ty, mutbl, body) => self.print_item_const(
+ ident,
+ Some(*mutbl),
+ &ast::Generics::default(),
+ ty,
+ body.as_deref(),
+ vis,
+ ast::Defaultness::Final,
+ ),
ast::ForeignItemKind::TyAlias(box ast::TyAlias {
defaultness,
generics,
@@ -67,6 +72,7 @@ impl<'a> State<'a> {
&mut self,
ident: Ident,
mutbl: Option<ast::Mutability>,
+ generics: &ast::Generics,
ty: &ast::Ty,
body: Option<&ast::Expr>,
vis: &ast::Visibility,
@@ -82,6 +88,7 @@ impl<'a> State<'a> {
};
self.word_space(leading);
self.print_ident(ident);
+ self.print_generic_params(&generics.params);
self.word_space(":");
self.print_type(ty);
if body.is_some() {
@@ -92,6 +99,7 @@ impl<'a> State<'a> {
self.word_space("=");
self.print_expr(body);
}
+ self.print_where_clause(&generics.where_clause);
self.word(";");
self.end(); // end the outer cbox
}
@@ -158,20 +166,21 @@ impl<'a> State<'a> {
self.word(";");
}
ast::ItemKind::Static(box StaticItem { ty, mutability: mutbl, expr: body }) => {
- let def = ast::Defaultness::Final;
self.print_item_const(
item.ident,
Some(*mutbl),
+ &ast::Generics::default(),
ty,
body.as_deref(),
&item.vis,
- def,
+ ast::Defaultness::Final,
);
}
- ast::ItemKind::Const(box ast::ConstItem { defaultness, ty, expr }) => {
+ ast::ItemKind::Const(box ast::ConstItem { defaultness, generics, ty, expr }) => {
self.print_item_const(
item.ident,
None,
+ generics,
ty,
expr.as_deref(),
&item.vis,
@@ -515,8 +524,16 @@ impl<'a> State<'a> {
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(box ast::ConstItem { defaultness, ty, expr }) => {
- self.print_item_const(ident, None, ty, expr.as_deref(), vis, *defaultness);
+ ast::AssocItemKind::Const(box ast::ConstItem { defaultness, generics, ty, expr }) => {
+ self.print_item_const(
+ ident,
+ None,
+ generics,
+ ty,
+ expr.as_deref(),
+ vis,
+ *defaultness,
+ );
}
ast::AssocItemKind::Type(box ast::TyAlias {
defaultness,