From 981e9ce658a1e2964c622970cd9f8a4e8f1fc46a Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Tue, 18 Jul 2023 18:07:54 +0000 Subject: [PATCH] refactor ABI formatting 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 #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. --- src/items.rs | 2 -- src/types.rs | 1 - src/utils.rs | 25 ++++++++----------------- tests/target/extern-rust.rs | 1 + tests/target/extern_not_explicit.rs | 6 +++--- 5 files changed, 12 insertions(+), 23 deletions(-) create mode 100644 tests/target/extern-rust.rs diff --git a/src/items.rs b/src/items.rs index d5bc38303e0..faf4a6b0076 100644 --- a/src/items.rs +++ b/src/items.rs @@ -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 @@ -338,7 +337,6 @@ impl<'a> FnSig<'a> { result.push_str(&format_extern( self.ext, context.config.force_explicit_abi(), - false, )); result } diff --git a/src/types.rs b/src/types.rs index 18a08f17ba0..54404bee2dd 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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"); diff --git a/src/utils.rs b/src/utils.rs index b8a44d4bade..b01af81276c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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)) + } } } diff --git a/tests/target/extern-rust.rs b/tests/target/extern-rust.rs new file mode 100644 index 00000000000..32824c91203 --- /dev/null +++ b/tests/target/extern-rust.rs @@ -0,0 +1 @@ +extern "Rust" fn uwu() {} diff --git a/tests/target/extern_not_explicit.rs b/tests/target/extern_not_explicit.rs index b55b64d05b3..1f800cf1460 100644 --- a/tests/target/extern_not_explicit.rs +++ b/tests/target/extern_not_explicit.rs @@ -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,