Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ()) {}
Loading