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

Include constness in impl blocks #4215

Merged
merged 1 commit into from
May 31, 2020
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
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 }
}
}