Skip to content

Commit 6352034

Browse files
committed
const_items_unit_type_default: rustfmt support
1 parent d2e1ddc commit 6352034

File tree

3 files changed

+92
-23
lines changed

3 files changed

+92
-23
lines changed

src/tools/rustfmt/src/items.rs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,7 +2005,7 @@ pub(crate) struct StaticPartsLhs<'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
defaultness: Option<ast::Defaultness>,
20112011
}
@@ -2026,7 +2026,7 @@ impl<'a> StaticParts<'a> {
20262026
vis: &item.vis,
20272027
ident: s.ident,
20282028
generics: None,
2029-
ty: &s.ty,
2029+
ty: Some(&s.ty),
20302030
mutability: s.mutability,
20312031
defaultness: None,
20322032
}),
@@ -2040,7 +2040,10 @@ impl<'a> StaticParts<'a> {
20402040
vis: &item.vis,
20412041
ident: c.ident,
20422042
generics: Some(&c.generics),
2043-
ty: &c.ty,
2043+
ty: match &c.ty {
2044+
ast::FnRetTy::Default(_) => None,
2045+
ast::FnRetTy::Ty(ty) => Some(ty),
2046+
},
20442047
mutability: ast::Mutability::Not,
20452048
defaultness: Some(c.defaultness),
20462049
}),
@@ -2073,7 +2076,10 @@ impl<'a> StaticParts<'a> {
20732076
vis: &ti.vis,
20742077
ident,
20752078
generics,
2076-
ty,
2079+
ty: match ty {
2080+
ast::FnRetTy::Default(_) => None,
2081+
ast::FnRetTy::Ty(ty) => Some(ty),
2082+
},
20772083
mutability: ast::Mutability::Not,
20782084
defaultness: Some(defaultness),
20792085
}),
@@ -2099,7 +2105,10 @@ impl<'a> StaticParts<'a> {
20992105
vis: &ii.vis,
21002106
ident,
21012107
generics,
2102-
ty,
2108+
ty: match ty {
2109+
ast::FnRetTy::Default(_) => None,
2110+
ast::FnRetTy::Ty(ty) => Some(&ty),
2111+
},
21032112
mutability: ast::Mutability::Not,
21042113
defaultness: Some(defaultness),
21052114
}),
@@ -2130,34 +2139,38 @@ fn rewrite_static_lhs(
21302139
return None;
21312140
}
21322141

2133-
let colon = colon_spaces(context.config);
21342142
let mut prefix = format!(
2135-
"{}{}{}{} {}{}{}",
2143+
"{}{}{}{} {}{}",
21362144
format_visibility(context, vis),
21372145
defaultness.map_or("", format_defaultness),
21382146
format_safety(safety),
21392147
prefix,
21402148
format_mutability(mutability),
21412149
rewrite_ident(context, ident),
2142-
colon,
21432150
);
2144-
let ty_shape = Shape::indented(offset.block_only(), context.config)
2145-
.offset_left(prefix.len() + const { " =".len() })?;
2146-
let ty_str = match ty.rewrite(context, ty_shape) {
2147-
Some(ty_str) => ty_str,
2148-
None => {
2149-
if prefix.ends_with(' ') {
2150-
prefix.pop();
2151+
let ty_str = match ty {
2152+
Some(ty) => {
2153+
prefix.push_str(colon_spaces(context.config));
2154+
let ty_shape = Shape::indented(offset.block_only(), context.config)
2155+
.offset_left(prefix.len() + const { " =".len() })?;
2156+
match ty.rewrite(context, ty_shape) {
2157+
Some(ty_str) => ty_str,
2158+
None => {
2159+
if prefix.ends_with(' ') {
2160+
prefix.pop();
2161+
}
2162+
let nested_indent = offset.block_indent(context.config);
2163+
let nested_shape = Shape::indented(nested_indent, context.config);
2164+
let ty_str = ty.rewrite(context, nested_shape)?;
2165+
format!(
2166+
"{}{}",
2167+
nested_indent.to_string_with_newline(context.config),
2168+
ty_str
2169+
)
2170+
}
21512171
}
2152-
let nested_indent = offset.block_indent(context.config);
2153-
let nested_shape = Shape::indented(nested_indent, context.config);
2154-
let ty_str = ty.rewrite(context, nested_shape)?;
2155-
format!(
2156-
"{}{}",
2157-
nested_indent.to_string_with_newline(context.config),
2158-
ty_str
2159-
)
21602172
}
2173+
None => "".to_string(),
21612174
};
21622175
Some([prefix, ty_str])
21632176
}
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)