Skip to content

Commit 97349b8

Browse files
committed
const_items_unit_type_default: rustfmt support
1 parent 76a4044 commit 97349b8

3 files changed

Lines changed: 83 additions & 24 deletions

File tree

src/tools/rustfmt/src/items.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,7 +2005,7 @@ pub(crate) struct StaticParts<'a> {
20052005
vis: &'a ast::Visibility,
20062006
ident: symbol::Ident,
20072007
generics: Option<&'a ast::Generics>,
2008-
ty: &'a ast::Ty,
2008+
ty: Option<&'a ast::Ty>,
20092009
mutability: ast::Mutability,
20102010
expr_opt: Option<&'a ast::Expr>,
20112011
defaultness: Option<ast::Defaultness>,
@@ -2021,7 +2021,7 @@ impl<'a> StaticParts<'a> {
20212021
"static",
20222022
s.safety,
20232023
s.ident,
2024-
&s.ty,
2024+
Some(&*s.ty),
20252025
s.mutability,
20262026
s.expr.as_deref(),
20272027
None,
@@ -2031,7 +2031,7 @@ impl<'a> StaticParts<'a> {
20312031
"const",
20322032
ast::Safety::Default,
20332033
c.ident,
2034-
&c.ty,
2034+
c.ty.as_non_default(),
20352035
ast::Mutability::Not,
20362036
c.rhs.as_ref().map(|rhs| rhs.expr()),
20372037
Some(&c.generics),
@@ -2056,7 +2056,7 @@ impl<'a> StaticParts<'a> {
20562056
let (defaultness, ty, expr_opt, generics) = match &ti.kind {
20572057
ast::AssocItemKind::Const(c) => (
20582058
c.defaultness,
2059-
&c.ty,
2059+
c.ty.as_non_default(),
20602060
c.rhs.as_ref().map(|rhs| rhs.expr()),
20612061
Some(&c.generics),
20622062
),
@@ -2080,7 +2080,7 @@ impl<'a> StaticParts<'a> {
20802080
let (defaultness, ty, expr_opt, generics) = match &ii.kind {
20812081
ast::AssocItemKind::Const(c) => (
20822082
c.defaultness,
2083-
&c.ty,
2083+
c.ty.as_non_default(),
20842084
c.rhs.as_ref().map(|rhs| rhs.expr()),
20852085
Some(&c.generics),
20862086
),
@@ -2114,35 +2114,38 @@ fn rewrite_static(
21142114
return None;
21152115
}
21162116

2117-
let colon = colon_spaces(context.config);
21182117
let mut prefix = format!(
2119-
"{}{}{}{} {}{}{}",
2118+
"{}{}{}{} {}{}",
21202119
format_visibility(context, static_parts.vis),
21212120
static_parts.defaultness.map_or("", format_defaultness),
21222121
format_safety(static_parts.safety),
21232122
static_parts.prefix,
21242123
format_mutability(static_parts.mutability),
21252124
rewrite_ident(context, static_parts.ident),
2126-
colon,
21272125
);
2128-
// 2 = " =".len()
2129-
let ty_shape =
2130-
Shape::indented(offset.block_only(), context.config).offset_left(prefix.len() + 2)?;
2131-
let ty_str = match static_parts.ty.rewrite(context, ty_shape) {
2132-
Some(ty_str) => ty_str,
2133-
None => {
2134-
if prefix.ends_with(' ') {
2135-
prefix.pop();
2126+
let ty_str = match static_parts.ty {
2127+
Some(ty) => {
2128+
prefix.push_str(colon_spaces(context.config));
2129+
let ty_shape = Shape::indented(offset.block_only(), context.config)
2130+
.offset_left(prefix.len() + const { " =".len() })?;
2131+
match ty.rewrite(context, ty_shape) {
2132+
Some(ty_str) => ty_str,
2133+
None => {
2134+
if prefix.ends_with(' ') {
2135+
prefix.pop();
2136+
}
2137+
let nested_indent = offset.block_indent(context.config);
2138+
let nested_shape = Shape::indented(nested_indent, context.config);
2139+
let ty_str = ty.rewrite(context, nested_shape)?;
2140+
format!(
2141+
"{}{}",
2142+
nested_indent.to_string_with_newline(context.config),
2143+
ty_str
2144+
)
2145+
}
21362146
}
2137-
let nested_indent = offset.block_indent(context.config);
2138-
let nested_shape = Shape::indented(nested_indent, context.config);
2139-
let ty_str = static_parts.ty.rewrite(context, nested_shape)?;
2140-
format!(
2141-
"{}{}",
2142-
nested_indent.to_string_with_newline(context.config),
2143-
ty_str
2144-
)
21452147
}
2148+
None => "".to_string(),
21462149
};
21472150

21482151
if let Some(expr) = static_parts.expr_opt {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![feature(const_items_unit_type_default)]
2+
3+
const FOO = {
4+
5+
6+
assert!(true)
7+
};
8+
9+
#[cfg(false)] const BAR = assert!(false);
10+
11+
12+
#[cfg(false)]
13+
// foo
14+
const
15+
// foo 2
16+
BAZ
17+
// baz
18+
=
19+
// baz 2
20+
21+
{
22+
// bar
23+
assert!(false)
24+
// baz
25+
} ;
26+
27+
28+
#[expect(unused)]
29+
const _ = { let a = 1; assert!(true); } ;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![feature(const_items_unit_type_default)]
2+
3+
const FOO = { assert!(true) };
4+
5+
#[cfg(false)]
6+
const BAR = assert!(false);
7+
8+
#[cfg(false)]
9+
// foo
10+
const
11+
// foo 2
12+
BAZ
13+
// baz
14+
=
15+
// baz 2
16+
17+
{
18+
// bar
19+
assert!(false)
20+
// baz
21+
} ;
22+
23+
#[expect(unused)]
24+
const _ = {
25+
let a = 1;
26+
assert!(true);
27+
};

0 commit comments

Comments
 (0)