Skip to content

Commit

Permalink
fix: don't drop drop generic args on assoc ty constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright committed Aug 8, 2021
1 parent fefb542 commit dbf4151
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 56 deletions.
125 changes: 69 additions & 56 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,31 +174,37 @@ impl<'a> Rewrite for SegmentParam<'a> {
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))
}
}
}
};
SegmentParam::Binding(atc) => atc.rewrite(context, shape),
}
}
}

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)
}
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());
result.push_str(&gen_args.rewrite(context, shape)?);
}

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)
}
}

Expand Down Expand Up @@ -240,21 +246,9 @@ fn rewrite_segment(
};

if let Some(ref args) = segment.args {
let generics_str = args.rewrite(context, shape)?;
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
Expand All @@ -270,33 +264,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)
Expand Down Expand Up @@ -489,6 +464,44 @@ impl Rewrite for ast::GenericArg {
}
}

impl Rewrite for ast::GenericArgs {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
match *self {
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<_>>();

overflow::rewrite_with_angle_brackets(
context,
"",
param_list.iter(),
shape,
self.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],
Expand Down
8 changes: 8 additions & 0 deletions tests/target/issue_4943.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit dbf4151

Please sign in to comment.