Skip to content

Commit 5d8eb8d

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

File tree

2 files changed

+75
-56
lines changed

2 files changed

+75
-56
lines changed

src/types.rs

+67-56
Original file line numberDiff line numberDiff line change
@@ -174,31 +174,38 @@ 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+
let gen_str = rewrite_generic_args(gen_args, context, shape, gen_args.span())?;
193+
result.push_str(&gen_str);
201194
}
195+
196+
let infix = match (&self.kind, context.config.type_punctuation_density()) {
197+
(Bound { .. }, _) => ": ",
198+
(Equality { .. }, TypeDensity::Wide) => " = ",
199+
(Equality { .. }, TypeDensity::Compressed) => "=",
200+
};
201+
result.push_str(infix);
202+
203+
let budget = shape.width.checked_sub(result.len())?;
204+
let shape = Shape::legacy(budget, shape.indent + result.len());
205+
let rewrite = self.kind.rewrite(context, shape)?;
206+
result.push_str(&rewrite);
207+
208+
Some(result)
202209
}
203210
}
204211

@@ -240,21 +247,9 @@ fn rewrite_segment(
240247
};
241248

242249
if let Some(ref args) = segment.args {
250+
let generics_str = rewrite_generic_args(args, context, shape, mk_sp(*span_lo, span_hi))?;
243251
match **args {
244252
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-
258253
// HACK: squeeze out the span between the identifier and the parameters.
259254
// The hack is requried so that we don't remove the separator inside macro calls.
260255
// This does not work in the presence of comment, hoping that people are
@@ -270,33 +265,14 @@ fn rewrite_segment(
270265
};
271266
result.push_str(separator);
272267

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-
281268
// Update position of last bracket.
282269
*span_lo = context
283270
.snippet_provider
284271
.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-
)?);
297272
}
298273
_ => (),
299274
}
275+
result.push_str(&generics_str)
300276
}
301277

302278
Some(result)
@@ -489,6 +465,41 @@ impl Rewrite for ast::GenericArg {
489465
}
490466
}
491467

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

tests/target/issue_4943.rs

+8
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)