about summary refs log tree commit diff
path: root/src/tools
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-11-03 12:08:55 +0100
committerGitHub <noreply@github.com>2024-11-03 12:08:55 +0100
commit13cdfae599cd0e6b84e8aab34bd5d4286032b7ca (patch)
treeea8bd2c2767c12f8135e2abfa11ae0b90d608274 /src/tools
parent7d7f2b5e2f79473d6f972fceae0f51fa55468d29 (diff)
parent16394e9776e1be1abc61a9b26baf73070aa7c37b (diff)
downloadrust-13cdfae599cd0e6b84e8aab34bd5d4286032b7ca.tar.gz
rust-13cdfae599cd0e6b84e8aab34bd5d4286032b7ca.zip
Rollup merge of #132540 - compiler-errors:gc, r=calebcartwright
Do not format generic consts

We introduced **nightly support** for generic const items in #113522, but formatting of consts was not modified. Making them format *correctly* is hard, so let's just bail formatting them so we don't accidentally strip their generics and where clauses. This is essentially no-op formatting for generic const items.

r? `````@calebcartwright````` or `````@ytmimi`````
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/rustfmt/src/items.rs27
-rw-r--r--src/tools/rustfmt/tests/target/const-generics.rs25
2 files changed, 46 insertions, 6 deletions
diff --git a/src/tools/rustfmt/src/items.rs b/src/tools/rustfmt/src/items.rs
index fc043a697e0..327a547b295 100644
--- a/src/tools/rustfmt/src/items.rs
+++ b/src/tools/rustfmt/src/items.rs
@@ -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>>,
@@ -2018,8 +2019,10 @@ 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",
@@ -2027,6 +2030,7 @@ impl<'a> StaticParts<'a> {
                 &c.ty,
                 ast::Mutability::Not,
                 &c.expr,
+                Some(&c.generics),
             ),
             _ => unreachable!(),
         };
@@ -2035,6 +2039,7 @@ impl<'a> StaticParts<'a> {
             safety,
             vis: &item.vis,
             ident: item.ident,
+            generics,
             ty,
             mutability,
             expr_opt: expr.as_ref(),
@@ -2044,8 +2049,8 @@ 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 {
@@ -2053,6 +2058,7 @@ impl<'a> StaticParts<'a> {
             safety: ast::Safety::Default,
             vis: &ti.vis,
             ident: ti.ident,
+            generics,
             ty,
             mutability: ast::Mutability::Not,
             expr_opt: expr_opt.as_ref(),
@@ -2062,8 +2068,8 @@ 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 {
@@ -2071,6 +2077,7 @@ impl<'a> StaticParts<'a> {
             safety: ast::Safety::Default,
             vis: &ii.vis,
             ident: ii.ident,
+            generics,
             ty,
             mutability: ast::Mutability::Not,
             expr_opt: expr.as_ref(),
@@ -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!(
         "{}{}{}{} {}{}{}",
diff --git a/src/tools/rustfmt/tests/target/const-generics.rs b/src/tools/rustfmt/tests/target/const-generics.rs
new file mode 100644
index 00000000000..94f76643664
--- /dev/null
+++ b/src/tools/rustfmt/tests/target/const-generics.rs
@@ -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:;
+}