Skip to content

Commit 16394e9

Browse files
Do not format generic consts
1 parent 00ed73c commit 16394e9

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

compiler/rustc_ast/src/ast.rs

+6
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,12 @@ pub struct WhereClause {
414414
pub span: Span,
415415
}
416416

417+
impl WhereClause {
418+
pub fn is_empty(&self) -> bool {
419+
!self.has_where_token && self.predicates.is_empty()
420+
}
421+
}
422+
417423
impl Default for WhereClause {
418424
fn default() -> WhereClause {
419425
WhereClause { has_where_token: false, predicates: ThinVec::new(), span: DUMMY_SP }

src/tools/rustfmt/src/items.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,7 @@ pub(crate) struct StaticParts<'a> {
20092009
safety: ast::Safety,
20102010
vis: &'a ast::Visibility,
20112011
ident: symbol::Ident,
2012+
generics: Option<&'a ast::Generics>,
20122013
ty: &'a ast::Ty,
20132014
mutability: ast::Mutability,
20142015
expr_opt: Option<&'a ptr::P<ast::Expr>>,
@@ -2018,15 +2019,18 @@ pub(crate) struct StaticParts<'a> {
20182019

20192020
impl<'a> StaticParts<'a> {
20202021
pub(crate) fn from_item(item: &'a ast::Item) -> Self {
2021-
let (defaultness, prefix, safety, ty, mutability, expr) = match &item.kind {
2022-
ast::ItemKind::Static(s) => (None, "static", s.safety, &s.ty, s.mutability, &s.expr),
2022+
let (defaultness, prefix, safety, ty, mutability, expr, generics) = match &item.kind {
2023+
ast::ItemKind::Static(s) => {
2024+
(None, "static", s.safety, &s.ty, s.mutability, &s.expr, None)
2025+
}
20232026
ast::ItemKind::Const(c) => (
20242027
Some(c.defaultness),
20252028
"const",
20262029
ast::Safety::Default,
20272030
&c.ty,
20282031
ast::Mutability::Not,
20292032
&c.expr,
2033+
Some(&c.generics),
20302034
),
20312035
_ => unreachable!(),
20322036
};
@@ -2035,6 +2039,7 @@ impl<'a> StaticParts<'a> {
20352039
safety,
20362040
vis: &item.vis,
20372041
ident: item.ident,
2042+
generics,
20382043
ty,
20392044
mutability,
20402045
expr_opt: expr.as_ref(),
@@ -2044,15 +2049,16 @@ impl<'a> StaticParts<'a> {
20442049
}
20452050

20462051
pub(crate) fn from_trait_item(ti: &'a ast::AssocItem) -> Self {
2047-
let (defaultness, ty, expr_opt) = match &ti.kind {
2048-
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr),
2052+
let (defaultness, ty, expr_opt, generics) = match &ti.kind {
2053+
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr, Some(&c.generics)),
20492054
_ => unreachable!(),
20502055
};
20512056
StaticParts {
20522057
prefix: "const",
20532058
safety: ast::Safety::Default,
20542059
vis: &ti.vis,
20552060
ident: ti.ident,
2061+
generics,
20562062
ty,
20572063
mutability: ast::Mutability::Not,
20582064
expr_opt: expr_opt.as_ref(),
@@ -2062,15 +2068,16 @@ impl<'a> StaticParts<'a> {
20622068
}
20632069

20642070
pub(crate) fn from_impl_item(ii: &'a ast::AssocItem) -> Self {
2065-
let (defaultness, ty, expr) = match &ii.kind {
2066-
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr),
2071+
let (defaultness, ty, expr, generics) = match &ii.kind {
2072+
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr, Some(&c.generics)),
20672073
_ => unreachable!(),
20682074
};
20692075
StaticParts {
20702076
prefix: "const",
20712077
safety: ast::Safety::Default,
20722078
vis: &ii.vis,
20732079
ident: ii.ident,
2080+
generics,
20742081
ty,
20752082
mutability: ast::Mutability::Not,
20762083
expr_opt: expr.as_ref(),
@@ -2085,6 +2092,14 @@ fn rewrite_static(
20852092
static_parts: &StaticParts<'_>,
20862093
offset: Indent,
20872094
) -> Option<String> {
2095+
// For now, if this static (or const) has generics, then bail.
2096+
if static_parts
2097+
.generics
2098+
.is_some_and(|g| !g.params.is_empty() || !g.where_clause.is_empty())
2099+
{
2100+
return None;
2101+
}
2102+
20882103
let colon = colon_spaces(context.config);
20892104
let mut prefix = format!(
20902105
"{}{}{}{} {}{}{}",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Make sure we don't mess up the formatting of generic consts
2+
3+
#![feature(generic_const_items)]
4+
5+
const GENERIC<N, const M: usize>: i32 = 0;
6+
7+
const WHERECLAUSE: i32 = 0
8+
where
9+
i32:;
10+
11+
trait Foo {
12+
const GENERIC<N, const M: usize>: i32;
13+
14+
const WHERECLAUSE: i32
15+
where
16+
i32:;
17+
}
18+
19+
impl Foo for () {
20+
const GENERIC<N, const M: usize>: i32 = 0;
21+
22+
const WHERECLAUSE: i32 = 0
23+
where
24+
i32:;
25+
}

0 commit comments

Comments
 (0)