about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEllen <supbscripter@gmail.com>2021-04-28 20:44:40 +0100
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>2021-07-27 19:50:11 -0500
commit0b21ea2161923a87f65932b6abb5e7a94003ec3a (patch)
treed68e69691f7edabe5ca139c2ff4ae286f3e4dc3b
parente7fa07036fa3e87f536fa3bedd3daad51dfeaf16 (diff)
downloadrust-0b21ea2161923a87f65932b6abb5e7a94003ec3a.tar.gz
rust-0b21ea2161923a87f65932b6abb5e7a94003ec3a.zip
Unyeet const param defaults
-rw-r--r--src/types.rs12
-rw-r--r--tests/source/issue-4816/lib.rs10
-rw-r--r--tests/target/issue-4816/lib.rs35
3 files changed, 56 insertions, 1 deletions
diff --git a/src/types.rs b/src/types.rs
index c6f89c31065..1d3f4669fcd 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -571,13 +571,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));
         }
diff --git a/tests/source/issue-4816/lib.rs b/tests/source/issue-4816/lib.rs
new file mode 100644
index 00000000000..43d540c4a5d
--- /dev/null
+++ b/tests/source/issue-4816/lib.rs
@@ -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},>;
diff --git a/tests/target/issue-4816/lib.rs b/tests/target/issue-4816/lib.rs
new file mode 100644
index 00000000000..246e775e1fe
--- /dev/null
+++ b/tests/target/issue-4816/lib.rs
@@ -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
+    },
+>;