Skip to content

Commit

Permalink
Include constness in impl blocks (#4215)
Browse files Browse the repository at this point in the history
Closes #4084
  • Loading branch information
ayazhafiz authored May 31, 2020
1 parent 6a861fb commit c6c9c7f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rustfmt-core/rustfmt-lib/src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@ fn format_impl_ref_and_type(
unsafety,
polarity,
defaultness,
constness,
ref generics,
of_trait: ref trait_ref,
ref self_ty,
Expand All @@ -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(_) => "!",
Expand Down
8 changes: 8 additions & 0 deletions rustfmt-core/rustfmt-lib/src/formatting/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/source/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,11 @@ impl<'a, 'b, 'c> SomeThing<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 }
}
}
8 changes: 8 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,11 @@ where
Channel: DmaChannel,
{
}

// #4084
impl const std::default::Default for Struct {
#[inline]
fn default() -> Self {
Self { f: 12.5 }
}
}

0 comments on commit c6c9c7f

Please sign in to comment.