Skip to content

Commit 2d85fe3

Browse files
authored
Unrolled build for rust-lang#131657
Rollup merge of rust-lang#131657 - compiler-errors:rustfmt-modifiers, r=ytmimi Rustfmt `for<'a> async` correctly In rust-lang#127054, we decided to move the trait bound modifier for `async for<'a> Fn()` to `for<'a> async Fn()`. This wasn't adjusted in rustfmt, so this PR implements that. It also requires consolidating the bound formatting into the `Rewrite` impl for `PolyTraitRef`. Fixes rust-lang#131649
2 parents 7342830 + dca646a commit 2d85fe3

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

src/tools/rustfmt/src/types.rs

+27-23
Original file line numberDiff line numberDiff line change
@@ -613,26 +613,8 @@ impl Rewrite for ast::GenericBound {
613613
ast::GenericBound::Trait(ref poly_trait_ref) => {
614614
let snippet = context.snippet(self.span());
615615
let has_paren = snippet.starts_with('(') && snippet.ends_with(')');
616-
let ast::TraitBoundModifiers {
617-
constness,
618-
asyncness,
619-
polarity,
620-
} = poly_trait_ref.modifiers;
621-
let mut constness = constness.as_str().to_string();
622-
if !constness.is_empty() {
623-
constness.push(' ');
624-
}
625-
let mut asyncness = asyncness.as_str().to_string();
626-
if !asyncness.is_empty() {
627-
asyncness.push(' ');
628-
}
629-
let polarity = polarity.as_str();
630-
let shape = shape
631-
.offset_left(constness.len() + polarity.len())
632-
.max_width_error(shape.width, self.span())?;
633616
poly_trait_ref
634617
.rewrite_result(context, shape)
635-
.map(|s| format!("{constness}{asyncness}{polarity}{s}"))
636618
.map(|s| if has_paren { format!("({})", s) } else { s })
637619
}
638620
ast::GenericBound::Use(ref args, span) => {
@@ -756,19 +738,41 @@ impl Rewrite for ast::PolyTraitRef {
756738
}
757739

758740
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
759-
if let Some(lifetime_str) = rewrite_bound_params(context, shape, &self.bound_generic_params)
741+
let (binder, shape) = if let Some(lifetime_str) =
742+
rewrite_bound_params(context, shape, &self.bound_generic_params)
760743
{
761744
// 6 is "for<> ".len()
762745
let extra_offset = lifetime_str.len() + 6;
763746
let shape = shape
764747
.offset_left(extra_offset)
765748
.max_width_error(shape.width, self.span)?;
766-
let path_str = self.trait_ref.rewrite_result(context, shape)?;
767-
768-
Ok(format!("for<{lifetime_str}> {path_str}"))
749+
(format!("for<{lifetime_str}> "), shape)
769750
} else {
770-
self.trait_ref.rewrite_result(context, shape)
751+
(String::new(), shape)
752+
};
753+
754+
let ast::TraitBoundModifiers {
755+
constness,
756+
asyncness,
757+
polarity,
758+
} = self.modifiers;
759+
let mut constness = constness.as_str().to_string();
760+
if !constness.is_empty() {
761+
constness.push(' ');
771762
}
763+
let mut asyncness = asyncness.as_str().to_string();
764+
if !asyncness.is_empty() {
765+
asyncness.push(' ');
766+
}
767+
let polarity = polarity.as_str();
768+
let shape = shape
769+
.offset_left(constness.len() + polarity.len())
770+
.max_width_error(shape.width, self.span)?;
771+
772+
let path_str = self.trait_ref.rewrite_result(context, shape)?;
773+
Ok(format!(
774+
"{binder}{constness}{asyncness}{polarity}{path_str}"
775+
))
772776
}
773777
}
774778

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
// rustfmt-edition: 2018
22

33
fn foo() -> impl async Fn() {}
4+
5+
fn bar() -> impl for<'a> async Fn(&'a ()) {}

0 commit comments

Comments
 (0)