Skip to content

Commit

Permalink
refactor ABI formatting
Browse files Browse the repository at this point in the history
Here are two paths of behavior this changes:

1. whenever we see an `extern "Rust"` on a function, we don't strip it
from the function (which fixes rust-lang#5701)
2. if `force_explicit_abi` is disabled, we preserve what the user has
written. Previously, rustfmt would change `extern "C"` to `extern `,
which is not something a code formatter should do.
  • Loading branch information
fee1-dead committed Jul 18, 2023
1 parent e0e633e commit 981e9ce
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 23 deletions.
2 changes: 0 additions & 2 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ impl<'a> Item<'a> {
abi: format_extern(
ast::Extern::from_abi(fm.abi, DUMMY_SP),
config.force_explicit_abi(),
true,
),
vis: None,
body: fm
Expand Down Expand Up @@ -338,7 +337,6 @@ impl<'a> FnSig<'a> {
result.push_str(&format_extern(
self.ext,
context.config.force_explicit_abi(),
false,
));
result
}
Expand Down
1 change: 0 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,6 @@ fn rewrite_bare_fn(
result.push_str(&format_extern(
bare_fn.ext,
context.config.force_explicit_abi(),
false,
));

result.push_str("fn");
Expand Down
25 changes: 8 additions & 17 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,14 @@ pub(crate) fn format_mutability(mutability: ast::Mutability) -> &'static str {
}

#[inline]
pub(crate) fn format_extern(
ext: ast::Extern,
explicit_abi: bool,
is_mod: bool,
) -> Cow<'static, str> {
let abi = match ext {
ast::Extern::None => "Rust".to_owned(),
ast::Extern::Implicit(_) => "C".to_owned(),
ast::Extern::Explicit(abi, _) => abi.symbol_unescaped.to_string(),
};

if abi == "Rust" && !is_mod {
Cow::from("")
} else if abi == "C" && !explicit_abi {
Cow::from("extern ")
} else {
Cow::from(format!(r#"extern "{}" "#, abi))
pub(crate) fn format_extern(ext: ast::Extern, force_explicit_abi: bool) -> Cow<'static, str> {
match ext {
ast::Extern::None => Cow::from(""),
ast::Extern::Implicit(_) if force_explicit_abi => Cow::from("extern \"C\" "),
ast::Extern::Implicit(_) => Cow::from("extern "),
ast::Extern::Explicit(abi, _) => {
Cow::from(format!(r#"extern "{}" "#, abi.symbol_unescaped))
}
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/target/extern-rust.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extern "Rust" fn uwu() {}
6 changes: 3 additions & 3 deletions tests/target/extern_not_explicit.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// rustfmt-force_explicit_abi: false

extern {
extern "C" {
fn some_fn() -> ();
}

extern fn sup() {}
extern "C" fn sup() {}

type funky_func = extern fn(
type funky_func = extern "C" fn(
unsafe extern "rust-call" fn(
*const JSJitInfo,
*mut JSContext,
Expand Down

0 comments on commit 981e9ce

Please sign in to comment.