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`````
This commit is contained in:
commit
13cdfae599
@ -414,6 +414,12 @@ pub struct WhereClause {
|
|||||||
pub span: Span,
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl WhereClause {
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
!self.has_where_token && self.predicates.is_empty()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Default for WhereClause {
|
impl Default for WhereClause {
|
||||||
fn default() -> WhereClause {
|
fn default() -> WhereClause {
|
||||||
WhereClause { has_where_token: false, predicates: ThinVec::new(), span: DUMMY_SP }
|
WhereClause { has_where_token: false, predicates: ThinVec::new(), span: DUMMY_SP }
|
||||||
|
@ -2009,6 +2009,7 @@ pub(crate) struct StaticParts<'a> {
|
|||||||
safety: ast::Safety,
|
safety: ast::Safety,
|
||||||
vis: &'a ast::Visibility,
|
vis: &'a ast::Visibility,
|
||||||
ident: symbol::Ident,
|
ident: symbol::Ident,
|
||||||
|
generics: Option<&'a ast::Generics>,
|
||||||
ty: &'a ast::Ty,
|
ty: &'a ast::Ty,
|
||||||
mutability: ast::Mutability,
|
mutability: ast::Mutability,
|
||||||
expr_opt: Option<&'a ptr::P<ast::Expr>>,
|
expr_opt: Option<&'a ptr::P<ast::Expr>>,
|
||||||
@ -2018,8 +2019,10 @@ pub(crate) struct StaticParts<'a> {
|
|||||||
|
|
||||||
impl<'a> StaticParts<'a> {
|
impl<'a> StaticParts<'a> {
|
||||||
pub(crate) fn from_item(item: &'a ast::Item) -> Self {
|
pub(crate) fn from_item(item: &'a ast::Item) -> Self {
|
||||||
let (defaultness, prefix, safety, ty, mutability, expr) = match &item.kind {
|
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),
|
ast::ItemKind::Static(s) => {
|
||||||
|
(None, "static", s.safety, &s.ty, s.mutability, &s.expr, None)
|
||||||
|
}
|
||||||
ast::ItemKind::Const(c) => (
|
ast::ItemKind::Const(c) => (
|
||||||
Some(c.defaultness),
|
Some(c.defaultness),
|
||||||
"const",
|
"const",
|
||||||
@ -2027,6 +2030,7 @@ pub(crate) fn from_item(item: &'a ast::Item) -> Self {
|
|||||||
&c.ty,
|
&c.ty,
|
||||||
ast::Mutability::Not,
|
ast::Mutability::Not,
|
||||||
&c.expr,
|
&c.expr,
|
||||||
|
Some(&c.generics),
|
||||||
),
|
),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
@ -2035,6 +2039,7 @@ pub(crate) fn from_item(item: &'a ast::Item) -> Self {
|
|||||||
safety,
|
safety,
|
||||||
vis: &item.vis,
|
vis: &item.vis,
|
||||||
ident: item.ident,
|
ident: item.ident,
|
||||||
|
generics,
|
||||||
ty,
|
ty,
|
||||||
mutability,
|
mutability,
|
||||||
expr_opt: expr.as_ref(),
|
expr_opt: expr.as_ref(),
|
||||||
@ -2044,8 +2049,8 @@ pub(crate) fn from_item(item: &'a ast::Item) -> Self {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn from_trait_item(ti: &'a ast::AssocItem) -> Self {
|
pub(crate) fn from_trait_item(ti: &'a ast::AssocItem) -> Self {
|
||||||
let (defaultness, ty, expr_opt) = match &ti.kind {
|
let (defaultness, ty, expr_opt, generics) = match &ti.kind {
|
||||||
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr),
|
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr, Some(&c.generics)),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
StaticParts {
|
StaticParts {
|
||||||
@ -2053,6 +2058,7 @@ pub(crate) fn from_trait_item(ti: &'a ast::AssocItem) -> Self {
|
|||||||
safety: ast::Safety::Default,
|
safety: ast::Safety::Default,
|
||||||
vis: &ti.vis,
|
vis: &ti.vis,
|
||||||
ident: ti.ident,
|
ident: ti.ident,
|
||||||
|
generics,
|
||||||
ty,
|
ty,
|
||||||
mutability: ast::Mutability::Not,
|
mutability: ast::Mutability::Not,
|
||||||
expr_opt: expr_opt.as_ref(),
|
expr_opt: expr_opt.as_ref(),
|
||||||
@ -2062,8 +2068,8 @@ pub(crate) fn from_trait_item(ti: &'a ast::AssocItem) -> Self {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn from_impl_item(ii: &'a ast::AssocItem) -> Self {
|
pub(crate) fn from_impl_item(ii: &'a ast::AssocItem) -> Self {
|
||||||
let (defaultness, ty, expr) = match &ii.kind {
|
let (defaultness, ty, expr, generics) = match &ii.kind {
|
||||||
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr),
|
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr, Some(&c.generics)),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
StaticParts {
|
StaticParts {
|
||||||
@ -2071,6 +2077,7 @@ pub(crate) fn from_impl_item(ii: &'a ast::AssocItem) -> Self {
|
|||||||
safety: ast::Safety::Default,
|
safety: ast::Safety::Default,
|
||||||
vis: &ii.vis,
|
vis: &ii.vis,
|
||||||
ident: ii.ident,
|
ident: ii.ident,
|
||||||
|
generics,
|
||||||
ty,
|
ty,
|
||||||
mutability: ast::Mutability::Not,
|
mutability: ast::Mutability::Not,
|
||||||
expr_opt: expr.as_ref(),
|
expr_opt: expr.as_ref(),
|
||||||
@ -2085,6 +2092,14 @@ fn rewrite_static(
|
|||||||
static_parts: &StaticParts<'_>,
|
static_parts: &StaticParts<'_>,
|
||||||
offset: Indent,
|
offset: Indent,
|
||||||
) -> Option<String> {
|
) -> 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 colon = colon_spaces(context.config);
|
||||||
let mut prefix = format!(
|
let mut prefix = format!(
|
||||||
"{}{}{}{} {}{}{}",
|
"{}{}{}{} {}{}{}",
|
||||||
|
25
src/tools/rustfmt/tests/target/const-generics.rs
Normal file
25
src/tools/rustfmt/tests/target/const-generics.rs
Normal file
@ -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:;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user