Skip to content

Commit

Permalink
Rollup merge of rust-lang#131657 - compiler-errors:rustfmt-modifiers,…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
matthiaskrgr authored Oct 16, 2024
2 parents b6a085a + dca646a commit 950fb62
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/tools/rustfmt/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,26 +613,8 @@ impl Rewrite for ast::GenericBound {
ast::GenericBound::Trait(ref poly_trait_ref) => {
let snippet = context.snippet(self.span());
let has_paren = snippet.starts_with('(') && snippet.ends_with(')');
let ast::TraitBoundModifiers {
constness,
asyncness,
polarity,
} = poly_trait_ref.modifiers;
let mut constness = constness.as_str().to_string();
if !constness.is_empty() {
constness.push(' ');
}
let mut asyncness = asyncness.as_str().to_string();
if !asyncness.is_empty() {
asyncness.push(' ');
}
let polarity = polarity.as_str();
let shape = shape
.offset_left(constness.len() + polarity.len())
.max_width_error(shape.width, self.span())?;
poly_trait_ref
.rewrite_result(context, shape)
.map(|s| format!("{constness}{asyncness}{polarity}{s}"))
.map(|s| if has_paren { format!("({})", s) } else { s })
}
ast::GenericBound::Use(ref args, span) => {
Expand Down Expand Up @@ -756,19 +738,41 @@ impl Rewrite for ast::PolyTraitRef {
}

fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
if let Some(lifetime_str) = rewrite_bound_params(context, shape, &self.bound_generic_params)
let (binder, shape) = if let Some(lifetime_str) =
rewrite_bound_params(context, shape, &self.bound_generic_params)
{
// 6 is "for<> ".len()
let extra_offset = lifetime_str.len() + 6;
let shape = shape
.offset_left(extra_offset)
.max_width_error(shape.width, self.span)?;
let path_str = self.trait_ref.rewrite_result(context, shape)?;

Ok(format!("for<{lifetime_str}> {path_str}"))
(format!("for<{lifetime_str}> "), shape)
} else {
self.trait_ref.rewrite_result(context, shape)
(String::new(), shape)
};

let ast::TraitBoundModifiers {
constness,
asyncness,
polarity,
} = self.modifiers;
let mut constness = constness.as_str().to_string();
if !constness.is_empty() {
constness.push(' ');
}
let mut asyncness = asyncness.as_str().to_string();
if !asyncness.is_empty() {
asyncness.push(' ');
}
let polarity = polarity.as_str();
let shape = shape
.offset_left(constness.len() + polarity.len())
.max_width_error(shape.width, self.span)?;

let path_str = self.trait_ref.rewrite_result(context, shape)?;
Ok(format!(
"{binder}{constness}{asyncness}{polarity}{path_str}"
))
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/tools/rustfmt/tests/target/asyncness.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// rustfmt-edition: 2018

fn foo() -> impl async Fn() {}

fn bar() -> impl for<'a> async Fn(&'a ()) {}

0 comments on commit 950fb62

Please sign in to comment.