fix: don't drop drop generic args on assoc ty constraints

This commit is contained in:
Caleb Cartwright 2021-08-07 20:24:14 -05:00 committed by Caleb Cartwright
parent fefb5427a2
commit 5d8eb8d79c
2 changed files with 76 additions and 57 deletions

View File

@ -174,34 +174,41 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
SegmentParam::Const(const_) => const_.rewrite(context, shape),
SegmentParam::LifeTime(lt) => lt.rewrite(context, shape),
SegmentParam::Type(ty) => ty.rewrite(context, shape),
SegmentParam::Binding(assoc_ty_constraint) => {
let mut result = match assoc_ty_constraint.kind {
ast::AssocTyConstraintKind::Bound { .. } => {
format!("{}: ", rewrite_ident(context, assoc_ty_constraint.ident))
}
ast::AssocTyConstraintKind::Equality { .. } => {
match context.config.type_punctuation_density() {
TypeDensity::Wide => {
format!("{} = ", rewrite_ident(context, assoc_ty_constraint.ident))
}
TypeDensity::Compressed => {
format!("{}=", rewrite_ident(context, assoc_ty_constraint.ident))
}
}
}
};
let budget = shape.width.checked_sub(result.len())?;
let rewrite = assoc_ty_constraint
.kind
.rewrite(context, Shape::legacy(budget, shape.indent + result.len()))?;
result.push_str(&rewrite);
Some(result)
}
SegmentParam::Binding(atc) => atc.rewrite(context, shape),
}
}
}
impl Rewrite for ast::AssocTyConstraint {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
use ast::AssocTyConstraintKind::{Bound, Equality};
let mut result = String::with_capacity(128);
result.push_str(rewrite_ident(context, self.ident));
if let Some(ref gen_args) = self.gen_args {
let budget = shape.width.checked_sub(result.len())?;
let shape = Shape::legacy(budget, shape.indent + result.len());
let gen_str = rewrite_generic_args(gen_args, context, shape, gen_args.span())?;
result.push_str(&gen_str);
}
let infix = match (&self.kind, context.config.type_punctuation_density()) {
(Bound { .. }, _) => ": ",
(Equality { .. }, TypeDensity::Wide) => " = ",
(Equality { .. }, TypeDensity::Compressed) => "=",
};
result.push_str(infix);
let budget = shape.width.checked_sub(result.len())?;
let shape = Shape::legacy(budget, shape.indent + result.len());
let rewrite = self.kind.rewrite(context, shape)?;
result.push_str(&rewrite);
Some(result)
}
}
impl Rewrite for ast::AssocTyConstraintKind {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
match self {
@ -240,21 +247,9 @@ fn rewrite_segment(
};
if let Some(ref args) = segment.args {
let generics_str = rewrite_generic_args(args, context, shape, mk_sp(*span_lo, span_hi))?;
match **args {
ast::GenericArgs::AngleBracketed(ref data) if !data.args.is_empty() => {
let param_list = data
.args
.iter()
.map(|x| match x {
ast::AngleBracketedArg::Arg(generic_arg) => {
SegmentParam::from_generic_arg(generic_arg)
}
ast::AngleBracketedArg::Constraint(constraint) => {
SegmentParam::Binding(constraint)
}
})
.collect::<Vec<_>>();
// HACK: squeeze out the span between the identifier and the parameters.
// The hack is requried so that we don't remove the separator inside macro calls.
// This does not work in the presence of comment, hoping that people are
@ -270,33 +265,14 @@ fn rewrite_segment(
};
result.push_str(separator);
let generics_str = overflow::rewrite_with_angle_brackets(
context,
"",
param_list.iter(),
shape,
mk_sp(*span_lo, span_hi),
)?;
// Update position of last bracket.
*span_lo = context
.snippet_provider
.span_after(mk_sp(*span_lo, span_hi), "<");
result.push_str(&generics_str)
}
ast::GenericArgs::Parenthesized(ref data) => {
result.push_str(&format_function_type(
data.inputs.iter().map(|x| &**x),
&data.output,
false,
data.span,
context,
shape,
)?);
}
_ => (),
}
result.push_str(&generics_str)
}
Some(result)
@ -489,6 +465,41 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
}
}
fn rewrite_generic_args(
gen_args: &ast::GenericArgs,
context: &RewriteContext<'_>,
shape: Shape,
span: Span,
) -> Option<String> {
match gen_args {
ast::GenericArgs::AngleBracketed(ref data) if !data.args.is_empty() => {
let args = data
.args
.iter()
.map(|x| match x {
ast::AngleBracketedArg::Arg(generic_arg) => {
SegmentParam::from_generic_arg(generic_arg)
}
ast::AngleBracketedArg::Constraint(constraint) => {
SegmentParam::Binding(constraint)
}
})
.collect::<Vec<_>>();
overflow::rewrite_with_angle_brackets(context, "", args.iter(), shape, span)
}
ast::GenericArgs::Parenthesized(ref data) => format_function_type(
data.inputs.iter().map(|x| &**x),
&data.output,
false,
data.span,
context,
shape,
),
_ => Some("".to_owned()),
}
}
fn rewrite_bounded_lifetime(
lt: &ast::Lifetime,
bounds: &[ast::GenericBound],

View File

@ -0,0 +1,8 @@
impl SomeStruct {
fn process<T>(v: T) -> <Self as GAT>::R<T>
where
Self: GAT<R<T> = T>,
{
SomeStruct::do_something(v)
}
}