Skip to content

Commit

Permalink
fix formatting arbitrary extern abi (#4088)
Browse files Browse the repository at this point in the history
* fix: arbitrary abi formatting

* deps: remove unneeded rustc-ap-rustc_target
  • Loading branch information
calebcartwright authored Mar 31, 2020
1 parent dee282d commit 4d22687
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 14 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions rustfmt-core/rustfmt-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ version = "642.0.0"
package = "rustc-ap-rustc_span"
version = "642.0.0"

[dependencies.rustc_target]
package = "rustc-ap-rustc_target"
version = "642.0.0"

[dependencies.syntax]
package = "rustc-ap-syntax"
version = "642.0.0"
Expand Down
15 changes: 6 additions & 9 deletions rustfmt-core/rustfmt-lib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::borrow::Cow;

use rustc_ast_pretty::pprust;
use rustc_span::{sym, BytePos, ExpnId, Span, Symbol, SyntaxContext};
use rustc_target::spec::abi;
use syntax::ast::{
self, Attribute, CrateSugar, MetaItem, MetaItemKind, NestedMetaItem, NodeId, Path, Visibility,
VisibilityKind,
Expand Down Expand Up @@ -140,19 +139,17 @@ pub(crate) fn format_extern(
is_mod: bool,
) -> Cow<'static, str> {
let abi = match ext {
ast::Extern::None => abi::Abi::Rust,
ast::Extern::Implicit => abi::Abi::C,
ast::Extern::Explicit(abi) => {
abi::lookup(&abi.symbol_unescaped.as_str()).unwrap_or(abi::Abi::Rust)
}
ast::Extern::None => "Rust".to_owned(),
ast::Extern::Implicit => "C".to_owned(),
ast::Extern::Explicit(abi) => abi.symbol_unescaped.to_string(),
};

if abi == abi::Abi::Rust && !is_mod {
if abi == "Rust" && !is_mod {
Cow::from("")
} else if abi == abi::Abi::C && !explicit_abi {
} else if abi == "C" && !explicit_abi {
Cow::from("extern ")
} else {
Cow::from(format!("extern {} ", abi))
Cow::from(format!(r#"extern "{}" "#, abi))
}
}

Expand Down
2 changes: 2 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/source/issue_4086.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(any())]
extern "C++" {}
2 changes: 2 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/issue_4086.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(any())]
extern "C++" {}

0 comments on commit 4d22687

Please sign in to comment.