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

1.x: Backport fixes around deleting pieces of extern functions #4562

Merged
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
47 changes: 24 additions & 23 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl Rewrite for ast::Local {
// FIXME convert to using rewrite style rather than visitor
// FIXME format modules in this style
#[allow(dead_code)]
#[derive(Debug)]
struct Item<'a> {
unsafety: ast::Unsafe,
abi: Cow<'static, str>,
Expand Down Expand Up @@ -153,6 +154,7 @@ impl<'a> Item<'a> {
}
}

#[derive(Debug)]
enum BodyElement<'a> {
// Stmt(&'a ast::Stmt),
// Field(&'a ast::Field),
Expand All @@ -174,26 +176,10 @@ pub(crate) struct FnSig<'a> {
}

impl<'a> FnSig<'a> {
pub(crate) fn new(
decl: &'a ast::FnDecl,
generics: &'a ast::Generics,
vis: ast::Visibility,
) -> FnSig<'a> {
FnSig {
decl,
generics,
ext: ast::Extern::None,
is_async: Cow::Owned(ast::Async::No),
constness: ast::Const::No,
defaultness: ast::Defaultness::Final,
unsafety: ast::Unsafe::No,
visibility: vis,
}
}

pub(crate) fn from_method_sig(
method_sig: &'a ast::FnSig,
generics: &'a ast::Generics,
visibility: ast::Visibility,
) -> FnSig<'a> {
FnSig {
unsafety: method_sig.header.unsafety,
Expand All @@ -203,7 +189,7 @@ impl<'a> FnSig<'a> {
ext: method_sig.header.ext,
decl: &*method_sig.decl,
generics,
visibility: DEFAULT_VISIBILITY,
visibility,
}
}

Expand All @@ -216,9 +202,8 @@ impl<'a> FnSig<'a> {
match *fn_kind {
visit::FnKind::Fn(fn_ctxt, _, fn_sig, vis, _) => match fn_ctxt {
visit::FnCtxt::Assoc(..) => {
let mut fn_sig = FnSig::from_method_sig(fn_sig, generics);
let mut fn_sig = FnSig::from_method_sig(fn_sig, generics, vis.clone());
fn_sig.defaultness = defaultness;
fn_sig.visibility = vis.clone();
fn_sig
}
_ => FnSig {
Expand Down Expand Up @@ -347,7 +332,7 @@ impl<'a> FmtVisitor<'a> {
&context,
indent,
ident,
&FnSig::from_method_sig(sig, generics),
&FnSig::from_method_sig(sig, generics, DEFAULT_VISIBILITY),
span,
FnBraceStyle::None,
)?;
Expand Down Expand Up @@ -3058,11 +3043,27 @@ impl Rewrite for ast::ForeignItem {
let span = mk_sp(self.span.lo(), self.span.hi() - BytePos(1));

let item_str = match self.kind {
ast::ForeignItemKind::Fn(_, ref fn_sig, ref generics, _) => rewrite_fn_base(
ast::ForeignItemKind::Fn(defaultness, ref fn_sig, ref generics, Some(ref body)) => {
let mut visitor = FmtVisitor::from_context(context);
visitor.block_indent = shape.indent;
visitor.last_pos = self.span.lo();
let inner_attrs = inner_attributes(&self.attrs);
let fn_ctxt = visit::FnCtxt::Foreign;
visitor.visit_fn(
visit::FnKind::Fn(fn_ctxt, self.ident, &fn_sig, &self.vis, Some(body)),
generics,
&fn_sig.decl,
self.span,
defaultness,
Some(&inner_attrs),
);
Some(visitor.buffer.to_owned())
}
ast::ForeignItemKind::Fn(_, ref fn_sig, ref generics, None) => rewrite_fn_base(
context,
shape.indent,
self.ident,
&FnSig::new(&fn_sig.decl, generics, self.vis.clone()),
&FnSig::from_method_sig(&fn_sig, generics, self.vis.clone()),
span,
FnBraceStyle::None,
)
Expand Down
2 changes: 1 addition & 1 deletion src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {

// Note that this only gets called for function definitions. Required methods
// on traits do not get handled here.
fn visit_fn(
pub(crate) fn visit_fn(
&mut self,
fk: visit::FnKind<'_>,
generics: &ast::Generics,
Expand Down
14 changes: 14 additions & 0 deletions tests/source/extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ libc::c_long;
, mode3: *const c_char,
file: *mut FILE)
-> *mut FILE;


const fn foo(

) ->
*mut Bar;
unsafe fn foo(

) -> *
mut
Bar;

pub(super) const fn foo() -> *mut Bar;
pub(crate) unsafe fn foo() -> *mut Bar;
}

extern {
Expand Down
6 changes: 6 additions & 0 deletions tests/target/extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ extern "C" {
mode3: *const c_char,
file: *mut FILE,
) -> *mut FILE;

const fn foo() -> *mut Bar;
unsafe fn foo() -> *mut Bar;

pub(super) const fn foo() -> *mut Bar;
pub(crate) unsafe fn foo() -> *mut Bar;
}

extern "C" {}
5 changes: 5 additions & 0 deletions tests/target/issue-4313.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern "C" {
fn f() {
fn g() {}
}
}