Skip to content

Commit 20f28b1

Browse files
committed
Add hard error for explicit ABIs in extern
1 parent 5b882e2 commit 20f28b1

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

compiler/rustc_ast_passes/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ ast_passes_match_arm_with_no_body =
164164
`match` arm with no body
165165
.suggestion = add a body after the pattern
166166
167+
ast_passes_missing_extern_abi = extern declarations without an explicit ABI are disallowed
168+
167169
ast_passes_missing_unsafe_on_extern = extern blocks must be unsafe
168170
169171
ast_passes_module_nonascii = trying to load file for module `{$name}` with non-ascii identifier name

compiler/rustc_ast_passes/src/ast_validation.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ impl<'a> AstValidator<'a> {
677677
self.dcx().emit_err(errors::PatternFnPointer { span });
678678
});
679679
if let Extern::Implicit(extern_span) = bfty.ext {
680-
self.maybe_lint_missing_abi(extern_span, ty.id);
680+
self.maybe_missing_abi(extern_span, ty.id);
681681
}
682682
}
683683
TyKind::TraitObject(bounds, ..) => {
@@ -710,10 +710,12 @@ impl<'a> AstValidator<'a> {
710710
}
711711
}
712712

713-
fn maybe_lint_missing_abi(&mut self, span: Span, id: NodeId) {
713+
fn maybe_missing_abi(&mut self, span: Span, id: NodeId) {
714714
// FIXME(davidtwco): This is a hack to detect macros which produce spans of the
715715
// call site which do not have a macro backtrace. See #61963.
716-
if self
716+
if span.edition().at_least_rust_2027() || self.features.explicit_extern_abis() {
717+
self.dcx().emit_err(errors::MissingExternABI { span });
718+
} else if self
717719
.sess
718720
.source_map()
719721
.span_to_snippet(span)
@@ -976,7 +978,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
976978
}
977979

978980
if abi.is_none() {
979-
this.maybe_lint_missing_abi(*extern_span, item.id);
981+
this.maybe_missing_abi(*extern_span, item.id);
980982
}
981983
visit::walk_item(this, item);
982984
this.extern_mod = old_item;
@@ -1356,7 +1358,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13561358
_,
13571359
) = fk
13581360
{
1359-
self.maybe_lint_missing_abi(*extern_span, id);
1361+
self.maybe_missing_abi(*extern_span, id);
13601362
}
13611363

13621364
// Functions without bodies cannot have patterns.

compiler/rustc_ast_passes/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -824,3 +824,10 @@ pub(crate) struct DuplicatePreciseCapturing {
824824
#[label]
825825
pub bound2: Span,
826826
}
827+
828+
#[derive(Diagnostic)]
829+
#[diag(ast_passes_missing_extern_abi)]
830+
pub(crate) struct MissingExternABI {
831+
#[primary_span]
832+
pub span: Span,
833+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//@ compile-flags: -Z unstable-options --edition 2027
2+
3+
extern fn foo() {}
4+
//~^ ERROR extern declarations without an explicit ABI are disallowed
5+
6+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: extern declarations without an explicit ABI are disallowed
2+
--> $DIR/explicit_extern_abis.rs:3:1
3+
|
4+
LL | extern fn foo() {}
5+
| ^^^^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)