Skip to content

Commit

Permalink
Do not format generic consts
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Nov 2, 2024
1 parent 00ed73c commit 16394e9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ pub struct WhereClause {
pub span: Span,
}

impl WhereClause {
pub fn is_empty(&self) -> bool {
!self.has_where_token && self.predicates.is_empty()
}
}

impl Default for WhereClause {
fn default() -> WhereClause {
WhereClause { has_where_token: false, predicates: ThinVec::new(), span: DUMMY_SP }
Expand Down
27 changes: 21 additions & 6 deletions src/tools/rustfmt/src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,7 @@ pub(crate) struct StaticParts<'a> {
safety: ast::Safety,
vis: &'a ast::Visibility,
ident: symbol::Ident,
generics: Option<&'a ast::Generics>,
ty: &'a ast::Ty,
mutability: ast::Mutability,
expr_opt: Option<&'a ptr::P<ast::Expr>>,
Expand All @@ -2018,15 +2019,18 @@ pub(crate) struct StaticParts<'a> {

impl<'a> StaticParts<'a> {
pub(crate) fn from_item(item: &'a ast::Item) -> Self {
let (defaultness, prefix, safety, ty, mutability, expr) = match &item.kind {
ast::ItemKind::Static(s) => (None, "static", s.safety, &s.ty, s.mutability, &s.expr),
let (defaultness, prefix, safety, ty, mutability, expr, generics) = match &item.kind {
ast::ItemKind::Static(s) => {
(None, "static", s.safety, &s.ty, s.mutability, &s.expr, None)
}
ast::ItemKind::Const(c) => (
Some(c.defaultness),
"const",
ast::Safety::Default,
&c.ty,
ast::Mutability::Not,
&c.expr,
Some(&c.generics),
),
_ => unreachable!(),
};
Expand All @@ -2035,6 +2039,7 @@ impl<'a> StaticParts<'a> {
safety,
vis: &item.vis,
ident: item.ident,
generics,
ty,
mutability,
expr_opt: expr.as_ref(),
Expand All @@ -2044,15 +2049,16 @@ impl<'a> StaticParts<'a> {
}

pub(crate) fn from_trait_item(ti: &'a ast::AssocItem) -> Self {
let (defaultness, ty, expr_opt) = match &ti.kind {
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr),
let (defaultness, ty, expr_opt, generics) = match &ti.kind {
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr, Some(&c.generics)),
_ => unreachable!(),
};
StaticParts {
prefix: "const",
safety: ast::Safety::Default,
vis: &ti.vis,
ident: ti.ident,
generics,
ty,
mutability: ast::Mutability::Not,
expr_opt: expr_opt.as_ref(),
Expand All @@ -2062,15 +2068,16 @@ impl<'a> StaticParts<'a> {
}

pub(crate) fn from_impl_item(ii: &'a ast::AssocItem) -> Self {
let (defaultness, ty, expr) = match &ii.kind {
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr),
let (defaultness, ty, expr, generics) = match &ii.kind {
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr, Some(&c.generics)),
_ => unreachable!(),
};
StaticParts {
prefix: "const",
safety: ast::Safety::Default,
vis: &ii.vis,
ident: ii.ident,
generics,
ty,
mutability: ast::Mutability::Not,
expr_opt: expr.as_ref(),
Expand All @@ -2085,6 +2092,14 @@ fn rewrite_static(
static_parts: &StaticParts<'_>,
offset: Indent,
) -> Option<String> {
// For now, if this static (or const) has generics, then bail.
if static_parts
.generics
.is_some_and(|g| !g.params.is_empty() || !g.where_clause.is_empty())
{
return None;
}

let colon = colon_spaces(context.config);
let mut prefix = format!(
"{}{}{}{} {}{}{}",
Expand Down
25 changes: 25 additions & 0 deletions src/tools/rustfmt/tests/target/const-generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Make sure we don't mess up the formatting of generic consts

#![feature(generic_const_items)]

const GENERIC<N, const M: usize>: i32 = 0;

const WHERECLAUSE: i32 = 0
where
i32:;

trait Foo {
const GENERIC<N, const M: usize>: i32;

const WHERECLAUSE: i32
where
i32:;
}

impl Foo for () {
const GENERIC<N, const M: usize>: i32 = 0;

const WHERECLAUSE: i32 = 0
where
i32:;
}

0 comments on commit 16394e9

Please sign in to comment.