Skip to content

Commit dbf4151

Browse files
fix: don't drop drop generic args on assoc ty constraints
1 parent fefb542 commit dbf4151

File tree

2 files changed

+77
-56
lines changed

2 files changed

+77
-56
lines changed

src/types.rs

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -174,31 +174,37 @@ impl<'a> Rewrite for SegmentParam<'a> {
174174
SegmentParam::Const(const_) => const_.rewrite(context, shape),
175175
SegmentParam::LifeTime(lt) => lt.rewrite(context, shape),
176176
SegmentParam::Type(ty) => ty.rewrite(context, shape),
177-
SegmentParam::Binding(assoc_ty_constraint) => {
178-
let mut result = match assoc_ty_constraint.kind {
179-
ast::AssocTyConstraintKind::Bound { .. } => {
180-
format!("{}: ", rewrite_ident(context, assoc_ty_constraint.ident))
181-
}
182-
ast::AssocTyConstraintKind::Equality { .. } => {
183-
match context.config.type_punctuation_density() {
184-
TypeDensity::Wide => {
185-
format!("{} = ", rewrite_ident(context, assoc_ty_constraint.ident))
186-
}
187-
TypeDensity::Compressed => {
188-
format!("{}=", rewrite_ident(context, assoc_ty_constraint.ident))
189-
}
190-
}
191-
}
192-
};
177+
SegmentParam::Binding(atc) => atc.rewrite(context, shape),
178+
}
179+
}
180+
}
193181

194-
let budget = shape.width.checked_sub(result.len())?;
195-
let rewrite = assoc_ty_constraint
196-
.kind
197-
.rewrite(context, Shape::legacy(budget, shape.indent + result.len()))?;
198-
result.push_str(&rewrite);
199-
Some(result)
200-
}
182+
impl Rewrite for ast::AssocTyConstraint {
183+
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
184+
use ast::AssocTyConstraintKind::{Bound, Equality};
185+
186+
let mut result = String::with_capacity(128);
187+
result.push_str(rewrite_ident(context, self.ident));
188+
189+
if let Some(ref gen_args) = self.gen_args {
190+
let budget = shape.width.checked_sub(result.len())?;
191+
let shape = Shape::legacy(budget, shape.indent + result.len());
192+
result.push_str(&gen_args.rewrite(context, shape)?);
201193
}
194+
195+
let infix = match (&self.kind, context.config.type_punctuation_density()) {
196+
(Bound { .. }, _) => ": ",
197+
(Equality { .. }, TypeDensity::Wide) => " = ",
198+
(Equality { .. }, TypeDensity::Compressed) => "=",
199+
};
200+
result.push_str(infix);
201+
202+
let budget = shape.width.checked_sub(result.len())?;
203+
let shape = Shape::legacy(budget, shape.indent + result.len());
204+
let rewrite = self.kind.rewrite(context, shape)?;
205+
result.push_str(&rewrite);
206+
207+
Some(result)
202208
}
203209
}
204210

@@ -240,21 +246,9 @@ fn rewrite_segment(
240246
};
241247

242248
if let Some(ref args) = segment.args {
249+
let generics_str = args.rewrite(context, shape)?;
243250
match **args {
244251
ast::GenericArgs::AngleBracketed(ref data) if !data.args.is_empty() => {
245-
let param_list = data
246-
.args
247-
.iter()
248-
.map(|x| match x {
249-
ast::AngleBracketedArg::Arg(generic_arg) => {
250-
SegmentParam::from_generic_arg(generic_arg)
251-
}
252-
ast::AngleBracketedArg::Constraint(constraint) => {
253-
SegmentParam::Binding(constraint)
254-
}
255-
})
256-
.collect::<Vec<_>>();
257-
258252
// HACK: squeeze out the span between the identifier and the parameters.
259253
// The hack is requried so that we don't remove the separator inside macro calls.
260254
// This does not work in the presence of comment, hoping that people are
@@ -270,33 +264,14 @@ fn rewrite_segment(
270264
};
271265
result.push_str(separator);
272266

273-
let generics_str = overflow::rewrite_with_angle_brackets(
274-
context,
275-
"",
276-
param_list.iter(),
277-
shape,
278-
mk_sp(*span_lo, span_hi),
279-
)?;
280-
281267
// Update position of last bracket.
282268
*span_lo = context
283269
.snippet_provider
284270
.span_after(mk_sp(*span_lo, span_hi), "<");
285-
286-
result.push_str(&generics_str)
287-
}
288-
ast::GenericArgs::Parenthesized(ref data) => {
289-
result.push_str(&format_function_type(
290-
data.inputs.iter().map(|x| &**x),
291-
&data.output,
292-
false,
293-
data.span,
294-
context,
295-
shape,
296-
)?);
297271
}
298272
_ => (),
299273
}
274+
result.push_str(&generics_str)
300275
}
301276

302277
Some(result)
@@ -489,6 +464,44 @@ impl Rewrite for ast::GenericArg {
489464
}
490465
}
491466

467+
impl Rewrite for ast::GenericArgs {
468+
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
469+
match *self {
470+
ast::GenericArgs::AngleBracketed(ref data) if !data.args.is_empty() => {
471+
let param_list = data
472+
.args
473+
.iter()
474+
.map(|x| match x {
475+
ast::AngleBracketedArg::Arg(generic_arg) => {
476+
SegmentParam::from_generic_arg(generic_arg)
477+
}
478+
ast::AngleBracketedArg::Constraint(constraint) => {
479+
SegmentParam::Binding(constraint)
480+
}
481+
})
482+
.collect::<Vec<_>>();
483+
484+
overflow::rewrite_with_angle_brackets(
485+
context,
486+
"",
487+
param_list.iter(),
488+
shape,
489+
self.span(),
490+
)
491+
}
492+
ast::GenericArgs::Parenthesized(ref data) => format_function_type(
493+
data.inputs.iter().map(|x| &**x),
494+
&data.output,
495+
false,
496+
data.span,
497+
context,
498+
shape,
499+
),
500+
_ => Some("".to_owned()),
501+
}
502+
}
503+
}
504+
492505
fn rewrite_bounded_lifetime(
493506
lt: &ast::Lifetime,
494507
bounds: &[ast::GenericBound],

tests/target/issue_4943.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
impl SomeStruct {
2+
fn process<T>(v: T) -> <Self as GAT>::R<T>
3+
where
4+
Self: GAT<R<T> = T>,
5+
{
6+
SomeStruct::do_something(v)
7+
}
8+
}

0 commit comments

Comments
 (0)