Skip to content

Commit 01cfa9a

Browse files
committed
Add hard error for extern without explicit ABI
1 parent ee53c26 commit 01cfa9a

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

Diff for: compiler/rustc_ast_passes/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ ast_passes_extern_types_cannot = `type`s inside `extern` blocks cannot have {$de
7979
.suggestion = remove the {$remove_descr}
8080
.label = `extern` block begins here
8181
82+
ast_passes_extern_without_abi = `extern` declarations without an explicit ABI are disallowed
83+
.suggestion = specify an ABI
84+
.help = prior to Rust 2024, a default ABI was inferred
85+
8286
ast_passes_feature_on_non_nightly = `#![feature]` may not be used on the {$channel} release channel
8387
.suggestion = remove the attribute
8488
.stable_since = the feature `{$name}` has been stable since `{$since}` and no longer requires an attribute to enable

Diff for: compiler/rustc_ast_passes/src/ast_validation.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ impl<'a> AstValidator<'a> {
684684
self.dcx().emit_err(errors::PatternFnPointer { span });
685685
});
686686
if let Extern::Implicit(extern_span) = bfty.ext {
687-
self.maybe_lint_missing_abi(extern_span, ty.id);
687+
self.handle_missing_abi(extern_span, ty.id);
688688
}
689689
}
690690
TyKind::TraitObject(bounds, ..) => {
@@ -717,10 +717,12 @@ impl<'a> AstValidator<'a> {
717717
}
718718
}
719719

720-
fn maybe_lint_missing_abi(&mut self, span: Span, id: NodeId) {
720+
fn handle_missing_abi(&mut self, span: Span, id: NodeId) {
721721
// FIXME(davidtwco): This is a hack to detect macros which produce spans of the
722722
// call site which do not have a macro backtrace. See #61963.
723-
if self
723+
if span.edition().at_least_edition_future() && self.features.explicit_extern_abis() {
724+
self.dcx().emit_err(errors::MissingAbi { span });
725+
} else if self
724726
.sess
725727
.source_map()
726728
.span_to_snippet(span)
@@ -996,7 +998,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
996998
}
997999

9981000
if abi.is_none() {
999-
self.maybe_lint_missing_abi(*extern_span, item.id);
1001+
self.handle_missing_abi(*extern_span, item.id);
10001002
}
10011003
self.with_in_extern_mod(*safety, |this| {
10021004
visit::walk_item(this, item);
@@ -1370,7 +1372,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13701372
},
13711373
) = fk
13721374
{
1373-
self.maybe_lint_missing_abi(*extern_span, id);
1375+
self.handle_missing_abi(*extern_span, id);
13741376
}
13751377

13761378
// Functions without bodies cannot have patterns.

Diff for: compiler/rustc_ast_passes/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,12 @@ pub(crate) struct DuplicatePreciseCapturing {
823823
#[label]
824824
pub bound2: Span,
825825
}
826+
827+
#[derive(Diagnostic)]
828+
#[diag(ast_passes_extern_without_abi)]
829+
#[help]
830+
pub(crate) struct MissingAbi {
831+
#[primary_span]
832+
#[suggestion(code = "extern \"<abi>\"", applicability = "has-placeholders")]
833+
pub span: Span,
834+
}

Diff for: compiler/rustc_lint/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ lint_expectation = this lint expectation is unfulfilled
271271
lint_extern_crate_not_idiomatic = `extern crate` is not idiomatic in the new edition
272272
.suggestion = convert it to a `use`
273273
274-
lint_extern_without_abi = extern declarations without an explicit ABI are deprecated
274+
lint_extern_without_abi = `extern` declarations without an explicit ABI are deprecated
275275
.label = ABI should be specified here
276276
.suggestion = explicitly specify the {$default_abi} ABI
277277

0 commit comments

Comments
 (0)