Skip to content

Commit 30cdc2b

Browse files
ytmimicalebcartwright
authored andcommitted
format safety keywords on static items
This includes both `ast::StaticItem` and `ast::StaticForeignItem`. `safety` was added to both `ast::StaticItem` and `ast::SaticForeignItem` in #124482.
1 parent 3ffd7d4 commit 30cdc2b

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/items.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,7 @@ pub(crate) fn rewrite_struct_field(
19151915

19161916
pub(crate) struct StaticParts<'a> {
19171917
prefix: &'a str,
1918+
safety: ast::Safety,
19181919
vis: &'a ast::Visibility,
19191920
ident: symbol::Ident,
19201921
ty: &'a ast::Ty,
@@ -1926,11 +1927,12 @@ pub(crate) struct StaticParts<'a> {
19261927

19271928
impl<'a> StaticParts<'a> {
19281929
pub(crate) fn from_item(item: &'a ast::Item) -> Self {
1929-
let (defaultness, prefix, ty, mutability, expr) = match &item.kind {
1930-
ast::ItemKind::Static(s) => (None, "static", &s.ty, s.mutability, &s.expr),
1930+
let (defaultness, prefix, safety, ty, mutability, expr) = match &item.kind {
1931+
ast::ItemKind::Static(s) => (None, "static", s.safety, &s.ty, s.mutability, &s.expr),
19311932
ast::ItemKind::Const(c) => (
19321933
Some(c.defaultness),
19331934
"const",
1935+
ast::Safety::Default,
19341936
&c.ty,
19351937
ast::Mutability::Not,
19361938
&c.expr,
@@ -1939,6 +1941,7 @@ impl<'a> StaticParts<'a> {
19391941
};
19401942
StaticParts {
19411943
prefix,
1944+
safety,
19421945
vis: &item.vis,
19431946
ident: item.ident,
19441947
ty,
@@ -1956,6 +1959,7 @@ impl<'a> StaticParts<'a> {
19561959
};
19571960
StaticParts {
19581961
prefix: "const",
1962+
safety: ast::Safety::Default,
19591963
vis: &ti.vis,
19601964
ident: ti.ident,
19611965
ty,
@@ -1973,6 +1977,7 @@ impl<'a> StaticParts<'a> {
19731977
};
19741978
StaticParts {
19751979
prefix: "const",
1980+
safety: ast::Safety::Default,
19761981
vis: &ii.vis,
19771982
ident: ii.ident,
19781983
ty,
@@ -1989,11 +1994,13 @@ fn rewrite_static(
19891994
static_parts: &StaticParts<'_>,
19901995
offset: Indent,
19911996
) -> Option<String> {
1997+
println!("rewriting static");
19921998
let colon = colon_spaces(context.config);
19931999
let mut prefix = format!(
1994-
"{}{}{} {}{}{}",
2000+
"{}{}{}{} {}{}{}",
19952001
format_visibility(context, static_parts.vis),
19962002
static_parts.defaultness.map_or("", format_defaultness),
2003+
format_safety(static_parts.safety),
19972004
static_parts.prefix,
19982005
format_mutability(static_parts.mutability),
19992006
rewrite_ident(context, static_parts.ident),
@@ -3338,10 +3345,12 @@ impl Rewrite for ast::ForeignItem {
33383345
// FIXME(#21): we're dropping potential comments in between the
33393346
// function kw here.
33403347
let vis = format_visibility(context, &self.vis);
3348+
let safety = format_safety(static_foreign_item.safety);
33413349
let mut_str = format_mutability(static_foreign_item.mutability);
33423350
let prefix = format!(
3343-
"{}static {}{}:",
3351+
"{}{}static {}{}:",
33443352
vis,
3353+
safety,
33453354
mut_str,
33463355
rewrite_ident(context, self.ident)
33473356
);

tests/target/unsafe_extern_blocks.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// See tracking issue for unsafe_extern_blocks
2+
// https://github.com/rust-lang/rust/issues/123743
3+
4+
#![feature(unsafe_extern_blocks)]
5+
6+
safe static TEST1: i32;
7+
8+
unsafe extern "C" {
9+
safe static TEST2: i32;
10+
unsafe static TEST3: i32;
11+
static TEST4: i32;
12+
13+
pub safe static TEST5: i32;
14+
pub unsafe static TEST6: i32;
15+
pub static TEST7: i32;
16+
17+
safe fn test1(i: i32);
18+
}

0 commit comments

Comments
 (0)