Skip to content

don't drop drop generic args on assoc ty constraints #4947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 67 additions & 56 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,31 +174,38 @@ 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());
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)
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -489,6 +465,41 @@ impl Rewrite for ast::GenericArg {
}
}

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