Skip to content

Commit 89192cd

Browse files
authored
Do not remove fn headers (e.g., async) on extern fn items (#4291)
2 parents 144c9ec + 07fa1ca commit 89192cd

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

src/formatting/items.rs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ impl Rewrite for ast::Local {
185185
// FIXME convert to using rewrite style rather than visitor
186186
// FIXME format modules in this style
187187
#[allow(dead_code)]
188+
#[derive(Debug)]
188189
struct Item<'a> {
189190
keyword: &'static str,
190191
abi: Cow<'static, str>,
@@ -213,6 +214,7 @@ impl<'a> Item<'a> {
213214
}
214215
}
215216

217+
#[derive(Debug)]
216218
enum BodyElement<'a> {
217219
// Stmt(&'a ast::Stmt),
218220
// Field(&'a ast::Field),
@@ -234,26 +236,10 @@ pub(crate) struct FnSig<'a> {
234236
}
235237

236238
impl<'a> FnSig<'a> {
237-
pub(crate) fn new(
238-
decl: &'a ast::FnDecl,
239-
generics: &'a ast::Generics,
240-
vis: ast::Visibility,
241-
) -> FnSig<'a> {
242-
FnSig {
243-
decl,
244-
generics,
245-
ext: ast::Extern::None,
246-
is_async: Cow::Owned(ast::Async::No),
247-
constness: ast::Const::No,
248-
defaultness: ast::Defaultness::Final,
249-
unsafety: ast::Unsafe::No,
250-
visibility: vis,
251-
}
252-
}
253-
254239
pub(crate) fn from_method_sig(
255240
method_sig: &'a ast::FnSig,
256241
generics: &'a ast::Generics,
242+
visibility: ast::Visibility,
257243
) -> FnSig<'a> {
258244
FnSig {
259245
unsafety: method_sig.header.unsafety,
@@ -263,7 +249,7 @@ impl<'a> FnSig<'a> {
263249
ext: method_sig.header.ext,
264250
decl: &*method_sig.decl,
265251
generics,
266-
visibility: DEFAULT_VISIBILITY,
252+
visibility,
267253
}
268254
}
269255

@@ -276,9 +262,8 @@ impl<'a> FnSig<'a> {
276262
match *fn_kind {
277263
visit::FnKind::Fn(fn_ctxt, _, fn_sig, vis, _) => match fn_ctxt {
278264
visit::FnCtxt::Assoc(..) => {
279-
let mut fn_sig = FnSig::from_method_sig(fn_sig, generics);
265+
let mut fn_sig = FnSig::from_method_sig(fn_sig, generics, vis.clone());
280266
fn_sig.defaultness = defaultness;
281-
fn_sig.visibility = vis.clone();
282267
fn_sig
283268
}
284269
_ => FnSig {
@@ -409,7 +394,7 @@ impl<'a> FmtVisitor<'a> {
409394
&context,
410395
indent,
411396
ident,
412-
&FnSig::from_method_sig(sig, generics),
397+
&FnSig::from_method_sig(sig, generics, DEFAULT_VISIBILITY),
413398
span,
414399
FnBraceStyle::None,
415400
)?;
@@ -3137,7 +3122,7 @@ impl Rewrite for ast::ForeignItem {
31373122
context,
31383123
shape.indent,
31393124
self.ident,
3140-
&FnSig::new(&fn_sig.decl, generics, self.vis.clone()),
3125+
&FnSig::from_method_sig(&fn_sig, generics, self.vis.clone()),
31413126
span,
31423127
FnBraceStyle::None,
31433128
)

tests/source/extern.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ libc::c_long;
5858
, mode3: *const c_char,
5959
file: *mut FILE)
6060
-> *mut FILE;
61+
62+
63+
async fn foo(
64+
65+
) -> *mut
66+
Bar;
67+
const fn foo(
68+
69+
) ->
70+
*mut Bar;
71+
unsafe fn foo(
72+
73+
) -> *
74+
mut
75+
Bar;
76+
77+
pub async fn foo() -> *mut Bar;
78+
pub(super) const fn foo() -> *mut Bar;
79+
pub(crate) unsafe fn foo() -> *mut Bar;
6180
}
6281

6382
extern {

tests/target/extern.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ extern "C" {
7373
mode3: *const c_char,
7474
file: *mut FILE,
7575
) -> *mut FILE;
76+
77+
async fn foo() -> *mut Bar;
78+
const fn foo() -> *mut Bar;
79+
unsafe fn foo() -> *mut Bar;
80+
81+
pub async fn foo() -> *mut Bar;
82+
pub(super) const fn foo() -> *mut Bar;
83+
pub(crate) unsafe fn foo() -> *mut Bar;
7684
}
7785

7886
extern "C" {}

0 commit comments

Comments
 (0)