Skip to content

Commit 4b01e62

Browse files
authored
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
1 parent b069aac commit 4b01e62

File tree

4 files changed

+13
-20
lines changed

4 files changed

+13
-20
lines changed

Diff for: src/items.rs

-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ impl<'a> Item<'a> {
248248
abi: format_extern(
249249
ast::Extern::from_abi(fm.abi, DUMMY_SP),
250250
config.force_explicit_abi(),
251-
true,
252251
),
253252
vis: None,
254253
body: fm
@@ -336,7 +335,6 @@ impl<'a> FnSig<'a> {
336335
result.push_str(&format_extern(
337336
self.ext,
338337
context.config.force_explicit_abi(),
339-
false,
340338
));
341339
result
342340
}

Diff for: src/types.rs

-1
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,6 @@ fn rewrite_bare_fn(
892892
result.push_str(&format_extern(
893893
bare_fn.ext,
894894
context.config.force_explicit_abi(),
895-
false,
896895
));
897896

898897
result.push_str("fn");

Diff for: src/utils.rs

+12-17
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,18 @@ pub(crate) fn format_mutability(mutability: ast::Mutability) -> &'static str {
131131
}
132132

133133
#[inline]
134-
pub(crate) fn format_extern(
135-
ext: ast::Extern,
136-
explicit_abi: bool,
137-
is_mod: bool,
138-
) -> Cow<'static, str> {
139-
let abi = match ext {
140-
ast::Extern::None => "Rust".to_owned(),
141-
ast::Extern::Implicit(_) => "C".to_owned(),
142-
ast::Extern::Explicit(abi, _) => abi.symbol_unescaped.to_string(),
143-
};
144-
145-
if abi == "Rust" && !is_mod {
146-
Cow::from("")
147-
} else if abi == "C" && !explicit_abi {
148-
Cow::from("extern ")
149-
} else {
150-
Cow::from(format!(r#"extern "{abi}" "#))
134+
pub(crate) fn format_extern(ext: ast::Extern, explicit_abi: bool) -> Cow<'static, str> {
135+
match ext {
136+
ast::Extern::None => Cow::from(""),
137+
ast::Extern::Implicit(_) if explicit_abi => Cow::from("extern \"C\" "),
138+
ast::Extern::Implicit(_) => Cow::from("extern "),
139+
// turn `extern "C"` into `extern` when `explicit_abi` is set to false
140+
ast::Extern::Explicit(abi, _) if abi.symbol_unescaped == sym::C && !explicit_abi => {
141+
Cow::from("extern ")
142+
}
143+
ast::Extern::Explicit(abi, _) => {
144+
Cow::from(format!(r#"extern "{}" "#, abi.symbol_unescaped))
145+
}
151146
}
152147
}
153148

Diff for: tests/target/extern-rust.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extern "Rust" fn uwu() {}

0 commit comments

Comments
 (0)