refactor ABI formatting (#5845)

fixes 5701

Whenever we see an `extern "Rust"` on a function, we don't strip it from the function.

If there's any future desire to have rustfmt remove an explicit "Rust" ABI, as it historically did prior to this change, then we can consider updating the rustfmt config surface to support that scenario
This commit is contained in:
fee1-dead 2023-08-14 20:19:23 +08:00 committed by GitHub
parent b069aac44d
commit 4b01e62943
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 20 deletions

View File

@ -248,7 +248,6 @@ fn from_foreign_mod(fm: &'a ast::ForeignMod, span: Span, config: &Config) -> Ite
abi: format_extern(
ast::Extern::from_abi(fm.abi, DUMMY_SP),
config.force_explicit_abi(),
true,
),
vis: None,
body: fm
@ -336,7 +335,6 @@ fn to_str(&self, context: &RewriteContext<'_>) -> String {
result.push_str(&format_extern(
self.ext,
context.config.force_explicit_abi(),
false,
));
result
}

View File

@ -892,7 +892,6 @@ fn rewrite_bare_fn(
result.push_str(&format_extern(
bare_fn.ext,
context.config.force_explicit_abi(),
false,
));
result.push_str("fn");

View File

@ -131,23 +131,18 @@ 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, explicit_abi: bool) -> Cow<'static, str> {
match ext {
ast::Extern::None => Cow::from(""),
ast::Extern::Implicit(_) if explicit_abi => Cow::from("extern \"C\" "),
ast::Extern::Implicit(_) => Cow::from("extern "),
// turn `extern "C"` into `extern` when `explicit_abi` is set to false
ast::Extern::Explicit(abi, _) if abi.symbol_unescaped == sym::C && !explicit_abi => {
Cow::from("extern ")
}
ast::Extern::Explicit(abi, _) => {
Cow::from(format!(r#"extern "{}" "#, abi.symbol_unescaped))
}
}
}

View File

@ -0,0 +1 @@
extern "Rust" fn uwu() {}