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

reuse rewrite_static when formatting foreign static items #6268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 24 additions & 22 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,27 @@ impl<'a> StaticParts<'a> {
}
}

pub(crate) fn from_foreign_item(item: &'a ast::ForeignItem) -> Self {
let (defaultness, prefix, safety, ty, mutability, expr) = match &item.kind {
ast::ForeignItemKind::Static(s) => {
(None, "static", s.safety, &s.ty, s.mutability, &s.expr)
}
_ => unreachable!(),
};

StaticParts {
prefix,
safety,
vis: &item.vis,
ident: item.ident,
ty,
mutability,
expr_opt: expr.as_ref(),
defaultness,
span: item.span,
}
}

pub(crate) fn from_trait_item(ti: &'a ast::AssocItem) -> Self {
let (defaultness, ty, expr_opt) = match &ti.kind {
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr),
Expand Down Expand Up @@ -3399,28 +3420,9 @@ impl Rewrite for ast::ForeignItem {
.map(|(s, _, _)| format!("{};", s))
}
}
ast::ForeignItemKind::Static(ref static_foreign_item) => {
// FIXME(#21): we're dropping potential comments in between the
// function kw here.
Comment on lines -3403 to -3404
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be honest I wasn't sure if this FIXME comment was misplaced or not. Would it be better to move this to the ast::ForeignItemKind::Fn match arm above?

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 {}{}:",
vis,
safety,
mut_str,
rewrite_ident(context, self.ident)
);
// 1 = ;
rewrite_assign_rhs(
context,
prefix,
&static_foreign_item.ty,
&RhsAssignKind::Ty,
shape.sub_width(1)?,
)
.map(|s| s + ";")
ast::ForeignItemKind::Static(_) => {
let static_parts = StaticParts::from_foreign_item(&self);
rewrite_static(context, &static_parts, shape.indent)
}
ast::ForeignItemKind::TyAlias(ref ty_alias) => {
let (kind, span) = (&ItemVisitorKind::ForeignItem(self), self.span);
Expand Down
3 changes: 3 additions & 0 deletions tests/target/issue_6267.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extern "C" {
static TEST: i32 = 42;
}
Loading