From 20aacb839eb1150813b705f515a23c44dfb139e6 Mon Sep 17 00:00:00 2001 From: Ayaz Hafiz Date: Sat, 30 May 2020 10:12:06 -0700 Subject: [PATCH] Include constness in impl blocks Closes #4084 --- rustfmt-core/rustfmt-lib/src/formatting/items.rs | 2 ++ rustfmt-core/rustfmt-lib/src/formatting/utils.rs | 8 ++++++++ rustfmt-core/rustfmt-lib/tests/source/impls.rs | 8 ++++++++ rustfmt-core/rustfmt-lib/tests/target/impls.rs | 8 ++++++++ 4 files changed, 26 insertions(+) diff --git a/rustfmt-core/rustfmt-lib/src/formatting/items.rs b/rustfmt-core/rustfmt-lib/src/formatting/items.rs index 9382d1ccd9e..5a6156ca2b8 100644 --- a/rustfmt-core/rustfmt-lib/src/formatting/items.rs +++ b/rustfmt-core/rustfmt-lib/src/formatting/items.rs @@ -905,6 +905,7 @@ fn format_impl_ref_and_type( unsafety, polarity, defaultness, + constness, ref generics, of_trait: ref trait_ref, ref self_ty, @@ -920,6 +921,7 @@ fn format_impl_ref_and_type( let shape = Shape::indented(offset + last_line_width(&result), context.config); let generics_str = rewrite_generics(context, "impl", generics, shape)?; result.push_str(&generics_str); + result.push_str(format_constness_right(constness)); let polarity_str = match polarity { ast::ImplPolarity::Negative(_) => "!", diff --git a/rustfmt-core/rustfmt-lib/src/formatting/utils.rs b/rustfmt-core/rustfmt-lib/src/formatting/utils.rs index 4c9bef7cb8a..0d38c8436c3 100644 --- a/rustfmt-core/rustfmt-lib/src/formatting/utils.rs +++ b/rustfmt-core/rustfmt-lib/src/formatting/utils.rs @@ -102,6 +102,14 @@ pub(crate) fn format_constness(constness: ast::Const) -> &'static str { } } +#[inline] +pub(crate) fn format_constness_right(constness: ast::Const) -> &'static str { + match constness { + ast::Const::Yes(..) => " const", + ast::Const::No => "", + } +} + #[inline] pub(crate) fn format_defaultness(defaultness: ast::Defaultness) -> &'static str { match defaultness { diff --git a/rustfmt-core/rustfmt-lib/tests/source/impls.rs b/rustfmt-core/rustfmt-lib/tests/source/impls.rs index fde7ad6d017..fb8701989fa 100644 --- a/rustfmt-core/rustfmt-lib/tests/source/impls.rs +++ b/rustfmt-core/rustfmt-lib/tests/source/impls.rs @@ -160,3 +160,11 @@ impl<'a, 'b, 'c> SomeThing for (&'a mut SomethingLong, &'b mut Someth // #2746 impl<'seq1, 'seq2, 'body, 'scope, Channel> Adc12< Dual, MasterRunningDma<'seq1, 'body, 'scope, Channel>, SlaveRunningDma<'seq2, 'body, 'scope>, > where Channel: DmaChannel, {} + +// #4084 +impl const std::default::Default for Struct { + #[inline] + fn default() -> Self { + Self { f: 12.5 } + } +} diff --git a/rustfmt-core/rustfmt-lib/tests/target/impls.rs b/rustfmt-core/rustfmt-lib/tests/target/impls.rs index 0777a7ed249..bf63f924a33 100644 --- a/rustfmt-core/rustfmt-lib/tests/target/impls.rs +++ b/rustfmt-core/rustfmt-lib/tests/target/impls.rs @@ -234,3 +234,11 @@ where Channel: DmaChannel, { } + +// #4084 +impl const std::default::Default for Struct { + #[inline] + fn default() -> Self { + Self { f: 12.5 } + } +}