Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format safety keywords on static items #6204

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,7 @@ pub(crate) fn rewrite_struct_field(

pub(crate) struct StaticParts<'a> {
prefix: &'a str,
safety: ast::Safety,
vis: &'a ast::Visibility,
ident: symbol::Ident,
ty: &'a ast::Ty,
Expand All @@ -1926,11 +1927,12 @@ pub(crate) struct StaticParts<'a> {

impl<'a> StaticParts<'a> {
pub(crate) fn from_item(item: &'a ast::Item) -> Self {
let (defaultness, prefix, ty, mutability, expr) = match &item.kind {
ast::ItemKind::Static(s) => (None, "static", &s.ty, s.mutability, &s.expr),
let (defaultness, prefix, safety, ty, mutability, expr) = match &item.kind {
ast::ItemKind::Static(s) => (None, "static", s.safety, &s.ty, s.mutability, &s.expr),
ast::ItemKind::Const(c) => (
Some(c.defaultness),
"const",
ast::Safety::Default,
&c.ty,
ast::Mutability::Not,
&c.expr,
Expand All @@ -1939,6 +1941,7 @@ impl<'a> StaticParts<'a> {
};
StaticParts {
prefix,
safety,
vis: &item.vis,
ident: item.ident,
ty,
Expand All @@ -1956,6 +1959,7 @@ impl<'a> StaticParts<'a> {
};
StaticParts {
prefix: "const",
safety: ast::Safety::Default,
vis: &ti.vis,
ident: ti.ident,
ty,
Expand All @@ -1973,6 +1977,7 @@ impl<'a> StaticParts<'a> {
};
StaticParts {
prefix: "const",
safety: ast::Safety::Default,
vis: &ii.vis,
ident: ii.ident,
ty,
Expand All @@ -1989,11 +1994,13 @@ fn rewrite_static(
static_parts: &StaticParts<'_>,
offset: Indent,
) -> Option<String> {
println!("rewriting static");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You left in a debug print, it seems.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am surprised that this did not make any of the tests fail...?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am surprised that this did not make any of the tests fail...?

same (#6210 (comment))

we'll do a postmortem of sorts to review and hopefully identify some preventative measures to prevent a recurrence

let colon = colon_spaces(context.config);
let mut prefix = format!(
"{}{}{} {}{}{}",
"{}{}{}{} {}{}{}",
format_visibility(context, static_parts.vis),
static_parts.defaultness.map_or("", format_defaultness),
format_safety(static_parts.safety),
static_parts.prefix,
format_mutability(static_parts.mutability),
rewrite_ident(context, static_parts.ident),
Expand Down Expand Up @@ -3338,10 +3345,12 @@ impl Rewrite for ast::ForeignItem {
// FIXME(#21): we're dropping potential comments in between the
// function kw here.
let vis = format_visibility(context, &self.vis);
let safety = format_safety(static_foreign_item.safety);
let mut_str = format_mutability(static_foreign_item.mutability);
let prefix = format!(
"{}static {}{}:",
"{}{}static {}{}:",
vis,
safety,
mut_str,
rewrite_ident(context, self.ident)
);
Expand Down
18 changes: 18 additions & 0 deletions tests/target/unsafe_extern_blocks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// See tracking issue for unsafe_extern_blocks
// https://github.com/rust-lang/rust/issues/123743

#![feature(unsafe_extern_blocks)]

safe static TEST1: i32;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that formatting the safety here is necessary per this comment https://github.com/rust-lang/rust/pull/124482/files#r1608953719. For now it parses, so I don't think there's an issue, though I'm happy to revert the changes to StaticParts if we don't think this is necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep it, since it doesn't hurt.


unsafe extern "C" {
safe static TEST2: i32;
unsafe static TEST3: i32;
static TEST4: i32;

pub safe static TEST5: i32;
pub unsafe static TEST6: i32;
pub static TEST7: i32;

safe fn test1(i: i32);
}
Loading