Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rustfmt 1.x] fix formatting arbitrary extern abi #4089

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.lock

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

6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "rustfmt-nightly"
version = "1.4.12"
version = "1.4.13"
authors = ["Nicholas Cameron <ncameron@mozilla.com>", "The Rustfmt developers"]
description = "Tool to find and fix Rust formatting issues"
repository = "https://github.com/rust-lang/rustfmt"
Expand Down Expand Up @@ -94,7 +94,3 @@ version = "651.0.0"
[dependencies.rustc_span]
package = "rustc-ap-rustc_span"
version = "651.0.0"

[dependencies.rustc_target]
package = "rustc-ap-rustc_target"
version = "651.0.0"
15 changes: 6 additions & 9 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use rustc_ast::ast::{
use rustc_ast::ptr;
use rustc_ast_pretty::pprust;
use rustc_span::{sym, BytePos, ExpnId, Span, Symbol, SyntaxContext};
use rustc_target::spec::abi;
use unicode_width::UnicodeWidthStr;

use crate::comment::{filter_normal_code, CharClasses, FullCodeCharKind, LineClasses};
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 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 tests/target/issue_4086.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(any())]
extern "C++" {}