From e3e04af071aa4ec253bbe5338c5e89c4cb7cbcc9 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Tue, 14 Jan 2020 23:36:24 -0600 Subject: [PATCH] refactor: update ABI/Extern handling Updates to account for changes in https://github.com/rust-lang/rust/pull/66271 --- rustfmt-core/rustfmt-lib/src/items.rs | 24 +++++++++++++++--------- rustfmt-core/rustfmt-lib/src/types.rs | 6 +++--- rustfmt-core/rustfmt-lib/src/utils.rs | 10 ++++++++++ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/rustfmt-core/rustfmt-lib/src/items.rs b/rustfmt-core/rustfmt-lib/src/items.rs index c4dfe803679..547aab67e1c 100644 --- a/rustfmt-core/rustfmt-lib/src/items.rs +++ b/rustfmt-core/rustfmt-lib/src/items.rs @@ -195,7 +195,7 @@ impl<'a> Item<'a> { fn from_foreign_mod(fm: &'a ast::ForeignMod, span: Span, config: &Config) -> Item<'a> { Item { keyword: "", - abi: format_abi(fm.abi, config.force_explicit_abi(), true), + abi: format_extern(ast::Extern::from_abi(fm.abi), config.force_explicit_abi(), true), vis: None, body: fm .items @@ -219,7 +219,8 @@ enum BodyElement<'a> { pub(crate) struct FnSig<'a> { decl: &'a ast::FnDecl, generics: &'a ast::Generics, - abi: abi::Abi, + ext: ast::Extern, + // abi: abi::Abi, is_async: Cow<'a, ast::IsAsync>, constness: ast::Constness, defaultness: ast::Defaultness, @@ -236,7 +237,7 @@ impl<'a> FnSig<'a> { FnSig { decl, generics, - abi: abi::Abi::Rust, + ext: ast::Extern::None, is_async: Cow::Owned(ast::IsAsync::NotAsync), constness: ast::Constness::NotConst, defaultness: ast::Defaultness::Final, @@ -246,15 +247,20 @@ impl<'a> FnSig<'a> { } pub(crate) fn from_method_sig( - method_sig: &'a ast::MethodSig, + method_sig: &'a ast::FnSig, generics: &'a ast::Generics, ) -> FnSig<'a> { + // let abi = match method_sig.header.ext { + // ast::Extern::None => abi::Abi::Rust, + // ast::Extern::Implicit => abi::Abi::C, + // ast::Extern::Explicit(abi) => self.lower_abi(abi), + // }; FnSig { unsafety: method_sig.header.unsafety, is_async: Cow::Borrowed(&method_sig.header.asyncness.node), constness: method_sig.header.constness.node, defaultness: ast::Defaultness::Final, - abi: method_sig.header.abi, + ext: method_sig.header.ext, decl: &*method_sig.decl, generics, visibility: DEFAULT_VISIBILITY, @@ -271,7 +277,7 @@ impl<'a> FnSig<'a> { visit::FnKind::ItemFn(_, fn_header, visibility, _) => FnSig { decl, generics, - abi: fn_header.abi, + ext: fn_header.ext, constness: fn_header.constness.node, is_async: Cow::Borrowed(&fn_header.asyncness.node), defaultness, @@ -296,8 +302,8 @@ impl<'a> FnSig<'a> { result.push_str(format_constness(self.constness)); result.push_str(format_async(&self.is_async)); result.push_str(format_unsafety(self.unsafety)); - result.push_str(&format_abi( - self.abi, + result.push_str(&format_extern( + self.ext, context.config.force_explicit_abi(), false, )); @@ -383,7 +389,7 @@ impl<'a> FmtVisitor<'a> { &mut self, indent: Indent, ident: ast::Ident, - sig: &ast::MethodSig, + sig: &ast::FnSig, generics: &ast::Generics, span: Span, ) -> Option { diff --git a/rustfmt-core/rustfmt-lib/src/types.rs b/rustfmt-core/rustfmt-lib/src/types.rs index 8dd8f269d79..071f7c7e380 100644 --- a/rustfmt-core/rustfmt-lib/src/types.rs +++ b/rustfmt-core/rustfmt-lib/src/types.rs @@ -18,7 +18,7 @@ use crate::shape::Shape; use crate::source_map::SpanUtils; use crate::spanned::Spanned; use crate::utils::{ - colon_spaces, extra_offset, first_line_width, format_abi, format_mutability, + colon_spaces, extra_offset, first_line_width, format_extern, format_mutability, last_line_extendable, last_line_width, mk_sp, rewrite_ident, }; @@ -772,8 +772,8 @@ fn rewrite_bare_fn( result.push_str(crate::utils::format_unsafety(bare_fn.unsafety)); - result.push_str(&format_abi( - bare_fn.abi, + result.push_str(&format_extern( + bare_fn.ext, context.config.force_explicit_abi(), false, )); diff --git a/rustfmt-core/rustfmt-lib/src/utils.rs b/rustfmt-core/rustfmt-lib/src/utils.rs index 205bac1dce9..9bf1aa850af 100644 --- a/rustfmt-core/rustfmt-lib/src/utils.rs +++ b/rustfmt-core/rustfmt-lib/src/utils.rs @@ -132,6 +132,16 @@ 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> { + match (ext, explicit_abi, is_mod) { + (ast::Extern::None, _, false) => Cow::from(""), + (ast::Extern::Implicit, false, _) => Cow::from("extern "), + (ast::Extern::Explicit(abi), _, _) => Cow::from(format!("extern {} ", abi.symbol_unescaped.as_str())), + (_, _, _) => unreachable!(), + } +} + #[inline] pub(crate) fn format_abi(abi: abi::Abi, explicit_abi: bool, is_mod: bool) -> Cow<'static, str> { if abi == abi::Abi::Rust && !is_mod {