Skip to content

Commit e3e04af

Browse files
refactor: update ABI/Extern handling
Updates to account for changes in rust-lang/rust#66271
1 parent 53cb6fb commit e3e04af

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

rustfmt-core/rustfmt-lib/src/items.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<'a> Item<'a> {
195195
fn from_foreign_mod(fm: &'a ast::ForeignMod, span: Span, config: &Config) -> Item<'a> {
196196
Item {
197197
keyword: "",
198-
abi: format_abi(fm.abi, config.force_explicit_abi(), true),
198+
abi: format_extern(ast::Extern::from_abi(fm.abi), config.force_explicit_abi(), true),
199199
vis: None,
200200
body: fm
201201
.items
@@ -219,7 +219,8 @@ enum BodyElement<'a> {
219219
pub(crate) struct FnSig<'a> {
220220
decl: &'a ast::FnDecl,
221221
generics: &'a ast::Generics,
222-
abi: abi::Abi,
222+
ext: ast::Extern,
223+
// abi: abi::Abi,
223224
is_async: Cow<'a, ast::IsAsync>,
224225
constness: ast::Constness,
225226
defaultness: ast::Defaultness,
@@ -236,7 +237,7 @@ impl<'a> FnSig<'a> {
236237
FnSig {
237238
decl,
238239
generics,
239-
abi: abi::Abi::Rust,
240+
ext: ast::Extern::None,
240241
is_async: Cow::Owned(ast::IsAsync::NotAsync),
241242
constness: ast::Constness::NotConst,
242243
defaultness: ast::Defaultness::Final,
@@ -246,15 +247,20 @@ impl<'a> FnSig<'a> {
246247
}
247248

248249
pub(crate) fn from_method_sig(
249-
method_sig: &'a ast::MethodSig,
250+
method_sig: &'a ast::FnSig,
250251
generics: &'a ast::Generics,
251252
) -> FnSig<'a> {
253+
// let abi = match method_sig.header.ext {
254+
// ast::Extern::None => abi::Abi::Rust,
255+
// ast::Extern::Implicit => abi::Abi::C,
256+
// ast::Extern::Explicit(abi) => self.lower_abi(abi),
257+
// };
252258
FnSig {
253259
unsafety: method_sig.header.unsafety,
254260
is_async: Cow::Borrowed(&method_sig.header.asyncness.node),
255261
constness: method_sig.header.constness.node,
256262
defaultness: ast::Defaultness::Final,
257-
abi: method_sig.header.abi,
263+
ext: method_sig.header.ext,
258264
decl: &*method_sig.decl,
259265
generics,
260266
visibility: DEFAULT_VISIBILITY,
@@ -271,7 +277,7 @@ impl<'a> FnSig<'a> {
271277
visit::FnKind::ItemFn(_, fn_header, visibility, _) => FnSig {
272278
decl,
273279
generics,
274-
abi: fn_header.abi,
280+
ext: fn_header.ext,
275281
constness: fn_header.constness.node,
276282
is_async: Cow::Borrowed(&fn_header.asyncness.node),
277283
defaultness,
@@ -296,8 +302,8 @@ impl<'a> FnSig<'a> {
296302
result.push_str(format_constness(self.constness));
297303
result.push_str(format_async(&self.is_async));
298304
result.push_str(format_unsafety(self.unsafety));
299-
result.push_str(&format_abi(
300-
self.abi,
305+
result.push_str(&format_extern(
306+
self.ext,
301307
context.config.force_explicit_abi(),
302308
false,
303309
));
@@ -383,7 +389,7 @@ impl<'a> FmtVisitor<'a> {
383389
&mut self,
384390
indent: Indent,
385391
ident: ast::Ident,
386-
sig: &ast::MethodSig,
392+
sig: &ast::FnSig,
387393
generics: &ast::Generics,
388394
span: Span,
389395
) -> Option<String> {

rustfmt-core/rustfmt-lib/src/types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::shape::Shape;
1818
use crate::source_map::SpanUtils;
1919
use crate::spanned::Spanned;
2020
use crate::utils::{
21-
colon_spaces, extra_offset, first_line_width, format_abi, format_mutability,
21+
colon_spaces, extra_offset, first_line_width, format_extern, format_mutability,
2222
last_line_extendable, last_line_width, mk_sp, rewrite_ident,
2323
};
2424

@@ -772,8 +772,8 @@ fn rewrite_bare_fn(
772772

773773
result.push_str(crate::utils::format_unsafety(bare_fn.unsafety));
774774

775-
result.push_str(&format_abi(
776-
bare_fn.abi,
775+
result.push_str(&format_extern(
776+
bare_fn.ext,
777777
context.config.force_explicit_abi(),
778778
false,
779779
));

rustfmt-core/rustfmt-lib/src/utils.rs

+10
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ pub(crate) fn format_mutability(mutability: ast::Mutability) -> &'static str {
132132
}
133133
}
134134

135+
#[inline]
136+
pub(crate) fn format_extern(ext: ast::Extern, explicit_abi: bool, is_mod: bool) -> Cow<'static, str> {
137+
match (ext, explicit_abi, is_mod) {
138+
(ast::Extern::None, _, false) => Cow::from(""),
139+
(ast::Extern::Implicit, false, _) => Cow::from("extern "),
140+
(ast::Extern::Explicit(abi), _, _) => Cow::from(format!("extern {} ", abi.symbol_unescaped.as_str())),
141+
(_, _, _) => unreachable!(),
142+
}
143+
}
144+
135145
#[inline]
136146
pub(crate) fn format_abi(abi: abi::Abi, explicit_abi: bool, is_mod: bool) -> Cow<'static, str> {
137147
if abi == abi::Abi::Rust && !is_mod {

0 commit comments

Comments
 (0)