Skip to content

Commit 14dff25

Browse files
Merge #10812
10812: feat: Format `Fn` traits using parentheses r=jonas-schievink a=jonas-schievink Fixes #8157 bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents cf2ddb9 + 9f4c26e commit 14dff25

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

crates/hir_def/src/path.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ pub struct GenericArgs {
135135
pub has_self_type: bool,
136136
/// Associated type bindings like in `Iterator<Item = T>`.
137137
pub bindings: Vec<AssociatedTypeBinding>,
138+
/// Whether these generic args were desugared from `Trait(Arg) -> Output`
139+
/// parenthesis notation typically used for the `Fn` traits.
140+
pub desugared_from_fn: bool,
138141
}
139142

140143
/// An associated type binding like in `Iterator<Item = T>`.
@@ -269,7 +272,12 @@ impl GenericArgs {
269272
}
270273

271274
pub(crate) fn empty() -> GenericArgs {
272-
GenericArgs { args: Vec::new(), has_self_type: false, bindings: Vec::new() }
275+
GenericArgs {
276+
args: Vec::new(),
277+
has_self_type: false,
278+
bindings: Vec::new(),
279+
desugared_from_fn: false,
280+
}
273281
}
274282
}
275283

crates/hir_def/src/path/lower.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub(super) fn lower_generic_args(
193193
if args.is_empty() && bindings.is_empty() {
194194
return None;
195195
}
196-
Some(GenericArgs { args, has_self_type: false, bindings })
196+
Some(GenericArgs { args, has_self_type: false, bindings, desugared_from_fn: false })
197197
}
198198

199199
/// Collect `GenericArgs` from the parts of a fn-like path, i.e. `Fn(X, Y)
@@ -229,5 +229,5 @@ fn lower_generic_args_from_fn_path(
229229
bounds: Vec::new(),
230230
});
231231
}
232-
Some(GenericArgs { args, has_self_type: false, bindings })
232+
Some(GenericArgs { args, has_self_type: false, bindings, desugared_from_fn: true })
233233
}

crates/hir_ty/src/display.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,16 @@ impl HirDisplay for Path {
11621162
if let Some(generic_args) = segment.args_and_bindings {
11631163
// We should be in type context, so format as `Foo<Bar>` instead of `Foo::<Bar>`.
11641164
// Do we actually format expressions?
1165+
if generic_args.desugared_from_fn {
1166+
// First argument will be a tuple, which already includes the parentheses.
1167+
generic_args.args[0].hir_fmt(f)?;
1168+
if let Some(ret) = &generic_args.bindings[0].type_ref {
1169+
write!(f, " -> ")?;
1170+
ret.hir_fmt(f)?;
1171+
}
1172+
return Ok(());
1173+
}
1174+
11651175
write!(f, "<")?;
11661176
let mut first = true;
11671177
for arg in &generic_args.args {

0 commit comments

Comments
 (0)