Skip to content

Sync subtree #5487

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

Merged
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2022-06-21"
channel = "nightly-2022-08-06"
components = ["rustc-dev"]
33 changes: 27 additions & 6 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::overflow::OverflowableItem;
use crate::rewrite::{Rewrite, RewriteContext};
use crate::shape::Shape;
use crate::source_map::SpanUtils;
use crate::types::rewrite_lifetime_param;
use crate::utils::{last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt};

// This module is pretty messy because of the rules around closures and blocks:
Expand All @@ -24,6 +25,7 @@ use crate::utils::{last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt};
// can change whether it is treated as an expression or statement.

pub(crate) fn rewrite_closure(
binder: &ast::ClosureBinder,
capture: ast::CaptureBy,
is_async: &ast::Async,
movability: ast::Movability,
Expand All @@ -36,7 +38,7 @@ pub(crate) fn rewrite_closure(
debug!("rewrite_closure {:?}", body);

let (prefix, extra_offset) = rewrite_closure_fn_decl(
capture, is_async, movability, fn_decl, body, span, context, shape,
binder, capture, is_async, movability, fn_decl, body, span, context, shape,
)?;
// 1 = space between `|...|` and body.
let body_shape = shape.offset_left(extra_offset)?;
Expand Down Expand Up @@ -227,6 +229,7 @@ fn rewrite_closure_block(

// Return type is (prefix, extra_offset)
fn rewrite_closure_fn_decl(
binder: &ast::ClosureBinder,
capture: ast::CaptureBy,
asyncness: &ast::Async,
movability: ast::Movability,
Expand All @@ -236,6 +239,17 @@ fn rewrite_closure_fn_decl(
context: &RewriteContext<'_>,
shape: Shape,
) -> Option<(String, usize)> {
let binder = match binder {
ast::ClosureBinder::For { generic_params, .. } if generic_params.is_empty() => {
"for<> ".to_owned()
}
ast::ClosureBinder::For { generic_params, .. } => {
let lifetime_str = rewrite_lifetime_param(context, shape, generic_params)?;
format!("for<{lifetime_str}> ")
}
ast::ClosureBinder::NotPresent => "".to_owned(),
};

let immovable = if movability == ast::Movability::Static {
"static "
} else {
Expand All @@ -250,7 +264,7 @@ fn rewrite_closure_fn_decl(
// 4 = "|| {".len(), which is overconservative when the closure consists of
// a single expression.
let nested_shape = shape
.shrink_left(immovable.len() + is_async.len() + mover.len())?
.shrink_left(binder.len() + immovable.len() + is_async.len() + mover.len())?
.sub_width(4)?;

// 1 = |
Expand Down Expand Up @@ -288,7 +302,7 @@ fn rewrite_closure_fn_decl(
.tactic(tactic)
.preserve_newline(true);
let list_str = write_list(&item_vec, &fmt)?;
let mut prefix = format!("{}{}{}|{}|", immovable, is_async, mover, list_str);
let mut prefix = format!("{}{}{}{}|{}|", binder, immovable, is_async, mover, list_str);

if !ret_str.is_empty() {
if prefix.contains('\n') {
Expand All @@ -312,8 +326,15 @@ pub(crate) fn rewrite_last_closure(
expr: &ast::Expr,
shape: Shape,
) -> Option<String> {
if let ast::ExprKind::Closure(capture, ref is_async, movability, ref fn_decl, ref body, _) =
expr.kind
if let ast::ExprKind::Closure(
ref binder,
capture,
ref is_async,
movability,
ref fn_decl,
ref body,
_,
) = expr.kind
{
let body = match body.kind {
ast::ExprKind::Block(ref block, _)
Expand All @@ -326,7 +347,7 @@ pub(crate) fn rewrite_last_closure(
_ => body,
};
let (prefix, extra_offset) = rewrite_closure_fn_decl(
capture, is_async, movability, fn_decl, body, expr.span, context, shape,
binder, capture, is_async, movability, fn_decl, body, expr.span, context, shape,
)?;
// If the closure goes multi line before its body, do not overflow the closure.
if prefix.contains('\n') {
Expand Down
4 changes: 3 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ pub use crate::config::options::*;
#[macro_use]
pub(crate) mod config_type;
#[macro_use]
#[allow(unreachable_pub)]
pub(crate) mod options;

pub(crate) mod file_lines;
#[allow(unreachable_pub)]
pub(crate) mod lists;
pub(crate) mod macro_names;

Expand Down Expand Up @@ -420,7 +422,7 @@ mod test {
use rustfmt_config_proc_macro::config_type;

#[config_type]
pub enum PartiallyUnstableOption {
pub(crate) enum PartiallyUnstableOption {
V1,
V2,
#[unstable_variant]
Expand Down
16 changes: 11 additions & 5 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,17 @@ pub(crate) fn format_expr(
Some("yield".to_string())
}
}
ast::ExprKind::Closure(capture, ref is_async, movability, ref fn_decl, ref body, _) => {
closures::rewrite_closure(
capture, is_async, movability, fn_decl, body, expr.span, context, shape,
)
}
ast::ExprKind::Closure(
ref binder,
capture,
ref is_async,
movability,
ref fn_decl,
ref body,
_,
) => closures::rewrite_closure(
binder, capture, is_async, movability, fn_decl, body, expr.span, context, shape,
),
ast::ExprKind::Try(..)
| ast::ExprKind::Field(..)
| ast::ExprKind::MethodCall(..)
Expand Down
2 changes: 1 addition & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl<'a> Item<'a> {
Item {
unsafety: fm.unsafety,
abi: format_extern(
ast::Extern::from_abi(fm.abi),
ast::Extern::from_abi(fm.abi, DUMMY_SP),
config.force_explicit_abi(),
true,
),
Expand Down
98 changes: 61 additions & 37 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::collections::HashMap;
use std::panic::{catch_unwind, AssertUnwindSafe};

use rustc_ast::token::{BinOpToken, Delimiter, Token, TokenKind};
use rustc_ast::tokenstream::{Cursor, Spacing, TokenStream, TokenTree};
use rustc_ast::tokenstream::{Cursor, TokenStream, TokenTree};
use rustc_ast::{ast, ptr};
use rustc_ast_pretty::pprust;
use rustc_span::{
Expand Down Expand Up @@ -683,7 +683,7 @@ struct MacroArgParser {

fn last_tok(tt: &TokenTree) -> Token {
match *tt {
TokenTree::Token(ref t) => t.clone(),
TokenTree::Token(ref t, _) => t.clone(),
TokenTree::Delimited(delim_span, delim, _) => Token {
kind: TokenKind::CloseDelim(delim),
span: delim_span.close,
Expand Down Expand Up @@ -738,10 +738,13 @@ impl MacroArgParser {

fn add_meta_variable(&mut self, iter: &mut Cursor) -> Option<()> {
match iter.next() {
Some(TokenTree::Token(Token {
kind: TokenKind::Ident(name, _),
..
})) => {
Some(TokenTree::Token(
Token {
kind: TokenKind::Ident(name, _),
..
},
_,
)) => {
self.result.push(ParsedMacroArg {
kind: MacroArgKind::MetaVariable(name, self.buf.clone()),
});
Expand Down Expand Up @@ -778,21 +781,30 @@ impl MacroArgParser {
}

match tok {
TokenTree::Token(Token {
kind: TokenKind::BinOp(BinOpToken::Plus),
..
})
| TokenTree::Token(Token {
kind: TokenKind::Question,
..
})
| TokenTree::Token(Token {
kind: TokenKind::BinOp(BinOpToken::Star),
..
}) => {
TokenTree::Token(
Token {
kind: TokenKind::BinOp(BinOpToken::Plus),
..
},
_,
)
| TokenTree::Token(
Token {
kind: TokenKind::Question,
..
},
_,
)
| TokenTree::Token(
Token {
kind: TokenKind::BinOp(BinOpToken::Star),
..
},
_,
) => {
break;
}
TokenTree::Token(ref t) => {
TokenTree::Token(ref t, _) => {
buffer.push_str(&pprust::token_to_string(t));
}
_ => return None,
Expand Down Expand Up @@ -860,10 +872,13 @@ impl MacroArgParser {

while let Some(tok) = iter.next() {
match tok {
TokenTree::Token(Token {
kind: TokenKind::Dollar,
span,
}) => {
TokenTree::Token(
Token {
kind: TokenKind::Dollar,
span,
},
_,
) => {
// We always want to add a separator before meta variables.
if !self.buf.is_empty() {
self.add_separator();
Expand All @@ -876,13 +891,16 @@ impl MacroArgParser {
span,
};
}
TokenTree::Token(Token {
kind: TokenKind::Colon,
..
}) if self.is_meta_var => {
TokenTree::Token(
Token {
kind: TokenKind::Colon,
..
},
_,
) if self.is_meta_var => {
self.add_meta_variable(&mut iter)?;
}
TokenTree::Token(ref t) => self.update_buffer(t),
TokenTree::Token(ref t, _) => self.update_buffer(t),
TokenTree::Delimited(_delimited_span, delimited, ref tts) => {
if !self.buf.is_empty() {
if next_space(&self.last_tok.kind) == SpaceState::Always {
Expand Down Expand Up @@ -1124,12 +1142,15 @@ impl MacroParser {
TokenTree::Token(..) => return None,
TokenTree::Delimited(delimited_span, d, _) => (delimited_span.open.lo(), d),
};
let args = TokenStream::new(vec![(tok, Spacing::Joint)]);
let args = TokenStream::new(vec![tok]);
match self.toks.next()? {
TokenTree::Token(Token {
kind: TokenKind::FatArrow,
..
}) => {}
TokenTree::Token(
Token {
kind: TokenKind::FatArrow,
..
},
_,
) => {}
_ => return None,
}
let (mut hi, body, whole_body) = match self.toks.next()? {
Expand All @@ -1148,10 +1169,13 @@ impl MacroParser {
)
}
};
if let Some(TokenTree::Token(Token {
kind: TokenKind::Semi,
span,
})) = self.toks.look_ahead(0)
if let Some(TokenTree::Token(
Token {
kind: TokenKind::Semi,
span,
},
_,
)) = self.toks.look_ahead(0)
{
hi = span.hi();
self.toks.next();
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ pub(crate) fn can_be_overflowed_type(
}

/// Returns `None` if there is no `LifetimeDef` in the given generic parameters.
fn rewrite_lifetime_param(
pub(crate) fn rewrite_lifetime_param(
context: &RewriteContext<'_>,
shape: Shape,
generic_params: &[ast::GenericParam],
Expand Down
6 changes: 3 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ pub(crate) fn format_extern(
) -> Cow<'static, str> {
let abi = match ext {
ast::Extern::None => "Rust".to_owned(),
ast::Extern::Implicit => "C".to_owned(),
ast::Extern::Explicit(abi) => abi.symbol_unescaped.to_string(),
ast::Extern::Implicit(_) => "C".to_owned(),
ast::Extern::Explicit(abi, _) => abi.symbol_unescaped.to_string(),
};

if abi == "Rust" && !is_mod {
Expand Down Expand Up @@ -480,7 +480,7 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
| ast::ExprKind::Binary(_, _, ref expr)
| ast::ExprKind::Index(_, ref expr)
| ast::ExprKind::Unary(_, ref expr)
| ast::ExprKind::Closure(_, _, _, _, ref expr, _)
| ast::ExprKind::Closure(_, _, _, _, _, ref expr, _)
| ast::ExprKind::Try(ref expr)
| ast::ExprKind::Yield(Some(ref expr)) => is_block_expr(context, expr, repr),
// This can only be a string lit
Expand Down
10 changes: 10 additions & 0 deletions tests/source/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ fn main() {
"--emit=dep-info"
} else { a }
});

for<> || -> () {};
for< >|| -> () {};
for<
> || -> () {};

for< 'a
,'b,
'c > |_: &'a (), _: &'b (), _: &'c ()| -> () {};

}

fn issue311() {
Expand Down
6 changes: 6 additions & 0 deletions tests/target/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ fn main() {
a
}
});

for<> || -> () {};
for<> || -> () {};
for<> || -> () {};

for<'a, 'b, 'c> |_: &'a (), _: &'b (), _: &'c ()| -> () {};
}

fn issue311() {
Expand Down