Skip to content
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

Fix const param defaults being yeeted out of code #4819

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
12 changes: 11 additions & 1 deletion src/formatting/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,13 +605,23 @@ impl Rewrite for ast::GenericParam {
if let ast::GenericParamKind::Const {
ref ty,
kw_span: _,
default: _,
default,
} = &self.kind
{
result.push_str("const ");
result.push_str(rewrite_ident(context, self.ident));
result.push_str(": ");
result.push_str(&ty.rewrite(context, shape)?);
if let Some(default) = default {
let eq_str = match context.config.type_punctuation_density() {
TypeDensity::Compressed => "=",
TypeDensity::Wide => " = ",
};
result.push_str(eq_str);
let budget = shape.width.checked_sub(result.len())?;
let rewrite = default.rewrite(context, Shape::legacy(budget, shape.indent))?;
result.push_str(&rewrite);
}
} else {
result.push_str(rewrite_ident(context, self.ident));
}
Expand Down
10 changes: 10 additions & 0 deletions tests/source/issue-4816/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![feature(const_generics_defaults)]
struct Foo<const N: usize = 1, const N2: usize = 2>;
struct Bar<const N: usize, const N2: usize = { N +
1 }>;
struct Lots<const N1BlahFooUwU: usize = { 10 + 28 + 1872 / 10 * 3 },const N2SecondParamOhmyyy: usize = { N1BlahFooUwU / 2 + 10 * 2 },>;
struct NamesRHard<const N: usize = { 1 + 1 + 1 + 1 + 1 + 1 }>;
struct FooBar<
const LessThan100ButClose: usize = {1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1}
>;
struct FooBarrrrrrrr<const N: usize = {13478234326456456444323871+ 1+ 1+ 1+ 1+ 1+ 1+ 1+ 1+ 1+ 1+ 1+ 1+ 1+1+1+1 + 1},>;
35 changes: 35 additions & 0 deletions tests/target/issue-4816/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#![feature(const_generics_defaults)]
struct Foo<const N: usize = 1, const N2: usize = 2>;
struct Bar<const N: usize, const N2: usize = { N + 1 }>;
struct Lots<
const N1BlahFooUwU: usize = { 10 + 28 + 1872 / 10 * 3 },
const N2SecondParamOhmyyy: usize = { N1BlahFooUwU / 2 + 10 * 2 },
>;
struct NamesRHard<const N: usize = { 1 + 1 + 1 + 1 + 1 + 1 }>;
struct FooBar<
const LessThan100ButClose: usize = {
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
},
>;
struct FooBarrrrrrrr<
const N: usize = {
13478234326456456444323871
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1
},
>;