Skip to content

Commit

Permalink
Preserve visibility of fn items without body
Browse files Browse the repository at this point in the history
  • Loading branch information
upsuper committed Jun 3, 2021
1 parent 77df640 commit 1f7122a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ enum BodyElement<'a> {
}

/// Represents a fn's signature.
#[derive(Debug)]
pub(crate) struct FnSig<'a> {
decl: &'a ast::FnDecl,
generics: &'a ast::Generics,
Expand Down Expand Up @@ -323,6 +324,7 @@ impl<'a> FmtVisitor<'a> {
ident: Ident,
sig: &ast::FnSig,
generics: &ast::Generics,
vis: &ast::Visibility,
span: Span,
) -> Option<String> {
// Drop semicolon or it will be interpreted as comment.
Expand All @@ -333,7 +335,7 @@ impl<'a> FmtVisitor<'a> {
&context,
indent,
ident,
&FnSig::from_method_sig(sig, generics, DEFAULT_VISIBILITY),
&FnSig::from_method_sig(sig, generics, vis.clone()),
span,
FnBraceStyle::None,
)?;
Expand Down
15 changes: 8 additions & 7 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
item.ident,
&fn_signature,
generics,
&item.vis,
item.span,
);
self.push_rewrite(item.span, rewrite);
Expand Down Expand Up @@ -639,13 +640,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
ast::AssocItemKind::Const(..) => self.visit_static(&StaticParts::from_trait_item(ti)),
ast::AssocItemKind::Fn(ref fn_kind) => {
let ast::FnKind(defaultness, ref sig, ref generics, ref block) = **fn_kind;
let vis = ast::Visibility {
kind: ast::VisibilityKind::Inherited,
span: DUMMY_SP,
tokens: None,
};
if let Some(ref body) = block {
let inner_attrs = inner_attributes(&ti.attrs);
let vis = ast::Visibility {
kind: ast::VisibilityKind::Inherited,
span: DUMMY_SP,
tokens: None,
};
let fn_ctxt = visit::FnCtxt::Assoc(visit::AssocCtxt::Trait);
self.visit_fn(
visit::FnKind::Fn(fn_ctxt, ti.ident, sig, &vis, Some(body)),
Expand All @@ -658,7 +659,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
} else {
let indent = self.block_indent;
let rewrite =
self.rewrite_required_fn(indent, ti.ident, sig, generics, ti.span);
self.rewrite_required_fn(indent, ti.ident, sig, generics, &vis, ti.span);
self.push_rewrite(ti.span, rewrite);
}
}
Expand Down Expand Up @@ -708,7 +709,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
} else {
let indent = self.block_indent;
let rewrite =
self.rewrite_required_fn(indent, ii.ident, sig, generics, ii.span);
self.rewrite_required_fn(indent, ii.ident, sig, generics, &ii.vis, ii.span);
self.push_rewrite(ii.span, rewrite);
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/target/fn-without-body.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Tests fns without function body

pub fn foo(a: AAA, b: BBB) -> RetType;

pub(crate) fn foo(a: AAA, b: BBB) -> RetType;

impl Foo {
pub fn foo(a: AAA);
}

0 comments on commit 1f7122a

Please sign in to comment.