Skip to content

Commit e041c20

Browse files
Merge pull request rust-lang#5487 from calebcartwright/subtree-sync-2022-08-06
Sync subtree
2 parents 662702e + c78ef92 commit e041c20

File tree

10 files changed

+124
-55
lines changed

10 files changed

+124
-55
lines changed

rust-toolchain

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "nightly-2022-06-21"
2+
channel = "nightly-2022-08-06"
33
components = ["rustc-dev"]

src/closures.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::overflow::OverflowableItem;
1111
use crate::rewrite::{Rewrite, RewriteContext};
1212
use crate::shape::Shape;
1313
use crate::source_map::SpanUtils;
14+
use crate::types::rewrite_lifetime_param;
1415
use crate::utils::{last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt};
1516

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

2627
pub(crate) fn rewrite_closure(
28+
binder: &ast::ClosureBinder,
2729
capture: ast::CaptureBy,
2830
is_async: &ast::Async,
2931
movability: ast::Movability,
@@ -36,7 +38,7 @@ pub(crate) fn rewrite_closure(
3638
debug!("rewrite_closure {:?}", body);
3739

3840
let (prefix, extra_offset) = rewrite_closure_fn_decl(
39-
capture, is_async, movability, fn_decl, body, span, context, shape,
41+
binder, capture, is_async, movability, fn_decl, body, span, context, shape,
4042
)?;
4143
// 1 = space between `|...|` and body.
4244
let body_shape = shape.offset_left(extra_offset)?;
@@ -227,6 +229,7 @@ fn rewrite_closure_block(
227229

228230
// Return type is (prefix, extra_offset)
229231
fn rewrite_closure_fn_decl(
232+
binder: &ast::ClosureBinder,
230233
capture: ast::CaptureBy,
231234
asyncness: &ast::Async,
232235
movability: ast::Movability,
@@ -236,6 +239,17 @@ fn rewrite_closure_fn_decl(
236239
context: &RewriteContext<'_>,
237240
shape: Shape,
238241
) -> Option<(String, usize)> {
242+
let binder = match binder {
243+
ast::ClosureBinder::For { generic_params, .. } if generic_params.is_empty() => {
244+
"for<> ".to_owned()
245+
}
246+
ast::ClosureBinder::For { generic_params, .. } => {
247+
let lifetime_str = rewrite_lifetime_param(context, shape, generic_params)?;
248+
format!("for<{lifetime_str}> ")
249+
}
250+
ast::ClosureBinder::NotPresent => "".to_owned(),
251+
};
252+
239253
let immovable = if movability == ast::Movability::Static {
240254
"static "
241255
} else {
@@ -250,7 +264,7 @@ fn rewrite_closure_fn_decl(
250264
// 4 = "|| {".len(), which is overconservative when the closure consists of
251265
// a single expression.
252266
let nested_shape = shape
253-
.shrink_left(immovable.len() + is_async.len() + mover.len())?
267+
.shrink_left(binder.len() + immovable.len() + is_async.len() + mover.len())?
254268
.sub_width(4)?;
255269

256270
// 1 = |
@@ -288,7 +302,7 @@ fn rewrite_closure_fn_decl(
288302
.tactic(tactic)
289303
.preserve_newline(true);
290304
let list_str = write_list(&item_vec, &fmt)?;
291-
let mut prefix = format!("{}{}{}|{}|", immovable, is_async, mover, list_str);
305+
let mut prefix = format!("{}{}{}{}|{}|", binder, immovable, is_async, mover, list_str);
292306

293307
if !ret_str.is_empty() {
294308
if prefix.contains('\n') {
@@ -312,8 +326,15 @@ pub(crate) fn rewrite_last_closure(
312326
expr: &ast::Expr,
313327
shape: Shape,
314328
) -> Option<String> {
315-
if let ast::ExprKind::Closure(capture, ref is_async, movability, ref fn_decl, ref body, _) =
316-
expr.kind
329+
if let ast::ExprKind::Closure(
330+
ref binder,
331+
capture,
332+
ref is_async,
333+
movability,
334+
ref fn_decl,
335+
ref body,
336+
_,
337+
) = expr.kind
317338
{
318339
let body = match body.kind {
319340
ast::ExprKind::Block(ref block, _)
@@ -326,7 +347,7 @@ pub(crate) fn rewrite_last_closure(
326347
_ => body,
327348
};
328349
let (prefix, extra_offset) = rewrite_closure_fn_decl(
329-
capture, is_async, movability, fn_decl, body, expr.span, context, shape,
350+
binder, capture, is_async, movability, fn_decl, body, expr.span, context, shape,
330351
)?;
331352
// If the closure goes multi line before its body, do not overflow the closure.
332353
if prefix.contains('\n') {

src/config/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ pub use crate::config::options::*;
2020
#[macro_use]
2121
pub(crate) mod config_type;
2222
#[macro_use]
23+
#[allow(unreachable_pub)]
2324
pub(crate) mod options;
2425

2526
pub(crate) mod file_lines;
27+
#[allow(unreachable_pub)]
2628
pub(crate) mod lists;
2729
pub(crate) mod macro_names;
2830

@@ -420,7 +422,7 @@ mod test {
420422
use rustfmt_config_proc_macro::config_type;
421423

422424
#[config_type]
423-
pub enum PartiallyUnstableOption {
425+
pub(crate) enum PartiallyUnstableOption {
424426
V1,
425427
V2,
426428
#[unstable_variant]

src/expr.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,17 @@ pub(crate) fn format_expr(
203203
Some("yield".to_string())
204204
}
205205
}
206-
ast::ExprKind::Closure(capture, ref is_async, movability, ref fn_decl, ref body, _) => {
207-
closures::rewrite_closure(
208-
capture, is_async, movability, fn_decl, body, expr.span, context, shape,
209-
)
210-
}
206+
ast::ExprKind::Closure(
207+
ref binder,
208+
capture,
209+
ref is_async,
210+
movability,
211+
ref fn_decl,
212+
ref body,
213+
_,
214+
) => closures::rewrite_closure(
215+
binder, capture, is_async, movability, fn_decl, body, expr.span, context, shape,
216+
),
211217
ast::ExprKind::Try(..)
212218
| ast::ExprKind::Field(..)
213219
| ast::ExprKind::MethodCall(..)

src/items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl<'a> Item<'a> {
148148
Item {
149149
unsafety: fm.unsafety,
150150
abi: format_extern(
151-
ast::Extern::from_abi(fm.abi),
151+
ast::Extern::from_abi(fm.abi, DUMMY_SP),
152152
config.force_explicit_abi(),
153153
true,
154154
),

src/macros.rs

+61-37
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::collections::HashMap;
1313
use std::panic::{catch_unwind, AssertUnwindSafe};
1414

1515
use rustc_ast::token::{BinOpToken, Delimiter, Token, TokenKind};
16-
use rustc_ast::tokenstream::{Cursor, Spacing, TokenStream, TokenTree};
16+
use rustc_ast::tokenstream::{Cursor, TokenStream, TokenTree};
1717
use rustc_ast::{ast, ptr};
1818
use rustc_ast_pretty::pprust;
1919
use rustc_span::{
@@ -683,7 +683,7 @@ struct MacroArgParser {
683683

684684
fn last_tok(tt: &TokenTree) -> Token {
685685
match *tt {
686-
TokenTree::Token(ref t) => t.clone(),
686+
TokenTree::Token(ref t, _) => t.clone(),
687687
TokenTree::Delimited(delim_span, delim, _) => Token {
688688
kind: TokenKind::CloseDelim(delim),
689689
span: delim_span.close,
@@ -738,10 +738,13 @@ impl MacroArgParser {
738738

739739
fn add_meta_variable(&mut self, iter: &mut Cursor) -> Option<()> {
740740
match iter.next() {
741-
Some(TokenTree::Token(Token {
742-
kind: TokenKind::Ident(name, _),
743-
..
744-
})) => {
741+
Some(TokenTree::Token(
742+
Token {
743+
kind: TokenKind::Ident(name, _),
744+
..
745+
},
746+
_,
747+
)) => {
745748
self.result.push(ParsedMacroArg {
746749
kind: MacroArgKind::MetaVariable(name, self.buf.clone()),
747750
});
@@ -778,21 +781,30 @@ impl MacroArgParser {
778781
}
779782

780783
match tok {
781-
TokenTree::Token(Token {
782-
kind: TokenKind::BinOp(BinOpToken::Plus),
783-
..
784-
})
785-
| TokenTree::Token(Token {
786-
kind: TokenKind::Question,
787-
..
788-
})
789-
| TokenTree::Token(Token {
790-
kind: TokenKind::BinOp(BinOpToken::Star),
791-
..
792-
}) => {
784+
TokenTree::Token(
785+
Token {
786+
kind: TokenKind::BinOp(BinOpToken::Plus),
787+
..
788+
},
789+
_,
790+
)
791+
| TokenTree::Token(
792+
Token {
793+
kind: TokenKind::Question,
794+
..
795+
},
796+
_,
797+
)
798+
| TokenTree::Token(
799+
Token {
800+
kind: TokenKind::BinOp(BinOpToken::Star),
801+
..
802+
},
803+
_,
804+
) => {
793805
break;
794806
}
795-
TokenTree::Token(ref t) => {
807+
TokenTree::Token(ref t, _) => {
796808
buffer.push_str(&pprust::token_to_string(t));
797809
}
798810
_ => return None,
@@ -860,10 +872,13 @@ impl MacroArgParser {
860872

861873
while let Some(tok) = iter.next() {
862874
match tok {
863-
TokenTree::Token(Token {
864-
kind: TokenKind::Dollar,
865-
span,
866-
}) => {
875+
TokenTree::Token(
876+
Token {
877+
kind: TokenKind::Dollar,
878+
span,
879+
},
880+
_,
881+
) => {
867882
// We always want to add a separator before meta variables.
868883
if !self.buf.is_empty() {
869884
self.add_separator();
@@ -876,13 +891,16 @@ impl MacroArgParser {
876891
span,
877892
};
878893
}
879-
TokenTree::Token(Token {
880-
kind: TokenKind::Colon,
881-
..
882-
}) if self.is_meta_var => {
894+
TokenTree::Token(
895+
Token {
896+
kind: TokenKind::Colon,
897+
..
898+
},
899+
_,
900+
) if self.is_meta_var => {
883901
self.add_meta_variable(&mut iter)?;
884902
}
885-
TokenTree::Token(ref t) => self.update_buffer(t),
903+
TokenTree::Token(ref t, _) => self.update_buffer(t),
886904
TokenTree::Delimited(_delimited_span, delimited, ref tts) => {
887905
if !self.buf.is_empty() {
888906
if next_space(&self.last_tok.kind) == SpaceState::Always {
@@ -1124,12 +1142,15 @@ impl MacroParser {
11241142
TokenTree::Token(..) => return None,
11251143
TokenTree::Delimited(delimited_span, d, _) => (delimited_span.open.lo(), d),
11261144
};
1127-
let args = TokenStream::new(vec![(tok, Spacing::Joint)]);
1145+
let args = TokenStream::new(vec![tok]);
11281146
match self.toks.next()? {
1129-
TokenTree::Token(Token {
1130-
kind: TokenKind::FatArrow,
1131-
..
1132-
}) => {}
1147+
TokenTree::Token(
1148+
Token {
1149+
kind: TokenKind::FatArrow,
1150+
..
1151+
},
1152+
_,
1153+
) => {}
11331154
_ => return None,
11341155
}
11351156
let (mut hi, body, whole_body) = match self.toks.next()? {
@@ -1148,10 +1169,13 @@ impl MacroParser {
11481169
)
11491170
}
11501171
};
1151-
if let Some(TokenTree::Token(Token {
1152-
kind: TokenKind::Semi,
1153-
span,
1154-
})) = self.toks.look_ahead(0)
1172+
if let Some(TokenTree::Token(
1173+
Token {
1174+
kind: TokenKind::Semi,
1175+
span,
1176+
},
1177+
_,
1178+
)) = self.toks.look_ahead(0)
11551179
{
11561180
hi = span.hi();
11571181
self.toks.next();

src/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ pub(crate) fn can_be_overflowed_type(
10671067
}
10681068

10691069
/// Returns `None` if there is no `LifetimeDef` in the given generic parameters.
1070-
fn rewrite_lifetime_param(
1070+
pub(crate) fn rewrite_lifetime_param(
10711071
context: &RewriteContext<'_>,
10721072
shape: Shape,
10731073
generic_params: &[ast::GenericParam],

src/utils.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ pub(crate) fn format_extern(
138138
) -> Cow<'static, str> {
139139
let abi = match ext {
140140
ast::Extern::None => "Rust".to_owned(),
141-
ast::Extern::Implicit => "C".to_owned(),
142-
ast::Extern::Explicit(abi) => abi.symbol_unescaped.to_string(),
141+
ast::Extern::Implicit(_) => "C".to_owned(),
142+
ast::Extern::Explicit(abi, _) => abi.symbol_unescaped.to_string(),
143143
};
144144

145145
if abi == "Rust" && !is_mod {
@@ -480,7 +480,7 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
480480
| ast::ExprKind::Binary(_, _, ref expr)
481481
| ast::ExprKind::Index(_, ref expr)
482482
| ast::ExprKind::Unary(_, ref expr)
483-
| ast::ExprKind::Closure(_, _, _, _, ref expr, _)
483+
| ast::ExprKind::Closure(_, _, _, _, _, ref expr, _)
484484
| ast::ExprKind::Try(ref expr)
485485
| ast::ExprKind::Yield(Some(ref expr)) => is_block_expr(context, expr, repr),
486486
// This can only be a string lit

tests/source/closure.rs

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ fn main() {
5151
"--emit=dep-info"
5252
} else { a }
5353
});
54+
55+
for<> || -> () {};
56+
for< >|| -> () {};
57+
for<
58+
> || -> () {};
59+
60+
for< 'a
61+
,'b,
62+
'c > |_: &'a (), _: &'b (), _: &'c ()| -> () {};
63+
5464
}
5565

5666
fn issue311() {

tests/target/closure.rs

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ fn main() {
7171
a
7272
}
7373
});
74+
75+
for<> || -> () {};
76+
for<> || -> () {};
77+
for<> || -> () {};
78+
79+
for<'a, 'b, 'c> |_: &'a (), _: &'b (), _: &'c ()| -> () {};
7480
}
7581

7682
fn issue311() {

0 commit comments

Comments
 (0)