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

don't convert implict ABI when doing so could fail to preserve semantics #4293

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
18 changes: 15 additions & 3 deletions src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,19 @@ struct Item<'a> {
}

impl<'a> Item<'a> {
fn from_foreign_mod(fm: &'a ast::ForeignMod, span: Span, config: &Config) -> Item<'a> {
fn from_foreign_mod(
fm: &'a ast::ForeignMod,
span: Span,
config: &Config,
attrs: &[ast::Attribute],
) -> Item<'a> {
Item {
keyword: "",
abi: format_extern(
ast::Extern::from_abi(fm.abi),
config.force_explicit_abi(),
true,
Some(attrs),
),
vis: None,
body: fm
Expand Down Expand Up @@ -308,6 +314,7 @@ impl<'a> FnSig<'a> {
self.ext,
context.config.force_explicit_abi(),
false,
None,
));
result
}
Expand Down Expand Up @@ -352,8 +359,13 @@ impl<'a> FmtVisitor<'a> {
}
}

pub(crate) fn format_foreign_mod(&mut self, fm: &ast::ForeignMod, span: Span) {
let item = Item::from_foreign_mod(fm, span, self.config);
pub(crate) fn format_foreign_mod(
&mut self,
fm: &ast::ForeignMod,
span: Span,
attrs: &[ast::Attribute],
) {
let item = Item::from_foreign_mod(fm, span, self.config, attrs);
self.format_item(&item);
}

Expand Down
1 change: 1 addition & 0 deletions src/formatting/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ fn rewrite_bare_fn(
bare_fn.ext,
context.config.force_explicit_abi(),
false,
None,
));

result.push_str("fn");
Expand Down
33 changes: 21 additions & 12 deletions src/formatting/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,28 @@ pub(crate) fn format_extern(
ext: ast::Extern,
explicit_abi: bool,
is_mod: bool,
attrs: Option<&[ast::Attribute]>,
) -> Cow<'static, str> {
let abi = match ext {
ast::Extern::None => "Rust".to_owned(),
ast::Extern::Implicit => "C".to_owned(),
ast::Extern::Explicit(abi) => abi.symbol_unescaped.to_string(),
};

if abi == "Rust" && !is_mod {
Cow::from("")
} else if abi == "C" && !explicit_abi {
Cow::from("extern ")
} else {
Cow::from(format!(r#"extern "{}" "#, abi))
let format_explicit_abi = |abi: &str| Cow::from(format!(r#"extern "{}" "#, abi));
let explicit_conversion_preserves_semantics =
|| !is_mod || (is_mod && attrs.map_or(true, |a| a.is_empty()));

match ext {
ast::Extern::None if !is_mod => Cow::from(""),
ast::Extern::Explicit(ast::StrLit {
symbol_unescaped, ..
}) if !is_mod && symbol_unescaped == rustc_span::sym::rust => Cow::from(""),
ast::Extern::Implicit if !explicit_abi || !explicit_conversion_preserves_semantics() => {
Cow::from("extern ")
}
ast::Extern::Explicit(ast::StrLit {
symbol_unescaped, ..
}) if !explicit_abi && symbol_unescaped == rustc_span::sym::C => Cow::from("extern "),
ast::Extern::None => format_explicit_abi("Rust"),
ast::Extern::Implicit => format_explicit_abi("C"),
ast::Extern::Explicit(ast::StrLit {
symbol_unescaped, ..
}) => format_explicit_abi(&symbol_unescaped.to_string()),
topecongiro marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/formatting/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
}
ast::ItemKind::ForeignMod(ref foreign_mod) => {
self.format_missing_with_indent(source!(self, item.span).lo());
self.format_foreign_mod(foreign_mod, item.span);
self.format_foreign_mod(foreign_mod, item.span, item.attrs());
}
ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => {
self.visit_static(&StaticParts::from_item(item));
Expand Down
17 changes: 17 additions & 0 deletions tests/source/extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,20 @@ libc::c_long;
extern {

}

// #2908 - https://github.com/rust-lang/rustfmt/issues/2908
#[wasm_bindgen(module = "child_process", version = "*")]
extern {
#[wasm_bindgen(js_name = execSync)]
fn exec_sync(cmd: &str) -> Buffer;

fn foo() -> Bar;
}

// Users that have an existing explicit ABI would need to convert to implicit
// manually, as rustfmt will be conservative and not attempt to convert explicit
// to implicit in the wasm case.
#[wasm_bindgen]
extern "C" {
fn foo() -> Bar;
}
Comment on lines +76 to +82
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I believe that an explicit C ABI became common practice with wasm_bindgen at least in part because of rustfmt 1.x behavior. However, I don't think we should attempt to help those users convert back to an implicit ABI as it seems to risky/error prone IMO; with this change, rustfmt 2.0 by default would permit users to do what they want and will no longer force Explicit ABIs.

17 changes: 17 additions & 0 deletions tests/target/extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,20 @@ extern "C" {
}

extern "C" {}

// #2908 - https://github.com/rust-lang/rustfmt/issues/2908
#[wasm_bindgen(module = "child_process", version = "*")]
extern {
#[wasm_bindgen(js_name = execSync)]
fn exec_sync(cmd: &str) -> Buffer;

fn foo() -> Bar;
}

// Users that have an existing explicit ABI would need to convert to implicit
// manually, as rustfmt will be conservative and not attempt to convert explicit
// to implicit in the wasm case.
#[wasm_bindgen]
extern "C" {
fn foo() -> Bar;
}