Skip to content

Commit a7bdfe2

Browse files
authored
Rollup merge of rust-lang#132357 - m-ou-se:explicit-abi, r=compiler-errors
Improve missing_abi lint This is for the migration lint for rust-lang/rfcs#3722 It is not yet marked as an edition migration lint, because `Edition2027` doesn't exist yet. The lint now includes a machine applicable suggestion: ``` warning: extern declarations without an explicit ABI are deprecated --> src/main.rs:3:1 | 3 | extern fn a() {} | ^^^^^^ help: explicitly specify the C ABI: `extern "C"` | ```
2 parents 268f259 + a433ea2 commit a7bdfe2

File tree

10 files changed

+17
-19
lines changed

10 files changed

+17
-19
lines changed

Diff for: compiler/rustc_ast/src/ast.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2810,6 +2810,8 @@ pub struct ModSpans {
28102810
/// E.g., `extern { .. }` or `extern "C" { .. }`.
28112811
#[derive(Clone, Encodable, Decodable, Debug)]
28122812
pub struct ForeignMod {
2813+
/// Span of the `extern` keyword.
2814+
pub extern_span: Span,
28132815
/// `unsafe` keyword accepted syntactically for macro DSLs, but not
28142816
/// semantically by Rust.
28152817
pub safety: Safety,

Diff for: compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ pub fn walk_ty<T: MutVisitor>(vis: &mut T, ty: &mut P<Ty>) {
525525
}
526526

527527
fn walk_foreign_mod<T: MutVisitor>(vis: &mut T, foreign_mod: &mut ForeignMod) {
528-
let ForeignMod { safety, abi: _, items } = foreign_mod;
528+
let ForeignMod { extern_span: _, safety, abi: _, items } = foreign_mod;
529529
visit_safety(vis, safety);
530530
items.flat_map_in_place(|item| vis.flat_map_foreign_item(item));
531531
}

Diff for: compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ impl WalkItemKind for ItemKind {
366366
}
367367
ModKind::Unloaded => {}
368368
},
369-
ItemKind::ForeignMod(ForeignMod { safety: _, abi: _, items }) => {
369+
ItemKind::ForeignMod(ForeignMod { extern_span: _, safety: _, abi: _, items }) => {
370370
walk_list!(visitor, visit_foreign_item, items);
371371
}
372372
ItemKind::GlobalAsm(asm) => try_visit!(visitor.visit_inline_asm(asm)),

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,8 @@ impl<'a> AstValidator<'a> {
677677
Self::check_decl_no_pat(&bfty.decl, |span, _, _| {
678678
self.dcx().emit_err(errors::PatternFnPointer { span });
679679
});
680-
if let Extern::Implicit(_) = bfty.ext {
681-
let sig_span = self.sess.source_map().next_point(ty.span.shrink_to_lo());
682-
self.maybe_lint_missing_abi(sig_span, ty.id);
680+
if let Extern::Implicit(extern_span) = bfty.ext {
681+
self.maybe_lint_missing_abi(extern_span, ty.id);
683682
}
684683
}
685684
TyKind::TraitObject(bounds, ..) => {
@@ -953,7 +952,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
953952
walk_list!(self, visit_attribute, &item.attrs);
954953
return; // Avoid visiting again.
955954
}
956-
ItemKind::ForeignMod(ForeignMod { abi, safety, .. }) => {
955+
ItemKind::ForeignMod(ForeignMod { extern_span, abi, safety, .. }) => {
957956
self.with_in_extern_mod(*safety, |this| {
958957
let old_item = mem::replace(&mut this.extern_mod, Some(item.span));
959958
this.visibility_not_permitted(
@@ -977,7 +976,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
977976
}
978977

979978
if abi.is_none() {
980-
this.maybe_lint_missing_abi(item.span, item.id);
979+
this.maybe_lint_missing_abi(*extern_span, item.id);
981980
}
982981
visit::walk_item(this, item);
983982
this.extern_mod = old_item;
@@ -1350,13 +1349,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13501349
if let FnKind::Fn(
13511350
_,
13521351
_,
1353-
FnSig { span: sig_span, header: FnHeader { ext: Extern::Implicit(_), .. }, .. },
1352+
FnSig { header: FnHeader { ext: Extern::Implicit(extern_span), .. }, .. },
13541353
_,
13551354
_,
13561355
_,
13571356
) = fk
13581357
{
1359-
self.maybe_lint_missing_abi(*sig_span, id);
1358+
self.maybe_lint_missing_abi(*extern_span, id);
13601359
}
13611360

13621361
// Functions without bodies cannot have patterns.

Diff for: compiler/rustc_lint/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ lint_extern_crate_not_idiomatic = `extern crate` is not idiomatic in the new edi
268268
269269
lint_extern_without_abi = extern declarations without an explicit ABI are deprecated
270270
.label = ABI should be specified here
271-
.help = the default ABI is {$default_abi}
271+
.suggestion = explicitly specify the {$default_abi} ABI
272272
273273
lint_for_loops_over_fallibles =
274274
for loop over {$article} `{$ref_prefix}{$ty}`. This is more readably written as an `if let` statement

Diff for: compiler/rustc_lint/src/lints.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2738,11 +2738,9 @@ pub(crate) struct PatternsInFnsWithoutBodySub {
27382738

27392739
#[derive(LintDiagnostic)]
27402740
#[diag(lint_extern_without_abi)]
2741-
#[help]
27422741
pub(crate) struct MissingAbi {
2743-
#[label]
2742+
#[suggestion(code = "extern \"{default_abi}\"", applicability = "machine-applicable")]
27442743
pub span: Span,
2745-
27462744
pub default_abi: &'static str,
27472745
}
27482746

Diff for: compiler/rustc_parse/src/parser/item.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,7 @@ impl<'a> Parser<'a> {
11941194
attrs: &mut AttrVec,
11951195
mut safety: Safety,
11961196
) -> PResult<'a, ItemInfo> {
1197+
let extern_span = self.prev_token.uninterpolated_span();
11971198
let abi = self.parse_abi(); // ABI?
11981199
// FIXME: This recovery should be tested better.
11991200
if safety == Safety::Default
@@ -1205,6 +1206,7 @@ impl<'a> Parser<'a> {
12051206
let _ = self.eat_keyword(kw::Unsafe);
12061207
}
12071208
let module = ast::ForeignMod {
1209+
extern_span,
12081210
safety,
12091211
abi,
12101212
items: self.parse_item_list(attrs, |p| p.parse_foreign_item(ForceCollect::No))?,

Diff for: tests/ui/lint/cli-lint-override.forbid_warn.stderr

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ error: extern declarations without an explicit ABI are deprecated
22
--> $DIR/cli-lint-override.rs:12:1
33
|
44
LL | extern fn foo() {}
5-
| ^^^^^^^^^^^^^^^ ABI should be specified here
5+
| ^^^^^^ help: explicitly specify the C ABI: `extern "C"`
66
|
7-
= help: the default ABI is C
87
= note: requested on the command line with `-F missing-abi`
98

109
error: aborting due to 1 previous error

Diff for: tests/ui/lint/cli-lint-override.force_warn_deny.stderr

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ warning: extern declarations without an explicit ABI are deprecated
22
--> $DIR/cli-lint-override.rs:12:1
33
|
44
LL | extern fn foo() {}
5-
| ^^^^^^^^^^^^^^^ ABI should be specified here
5+
| ^^^^^^ help: explicitly specify the C ABI: `extern "C"`
66
|
7-
= help: the default ABI is C
87
= note: requested on the command line with `--force-warn missing-abi`
98

109
warning: 1 warning emitted

Diff for: tests/ui/lint/cli-lint-override.warn_deny.stderr

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ error: extern declarations without an explicit ABI are deprecated
22
--> $DIR/cli-lint-override.rs:12:1
33
|
44
LL | extern fn foo() {}
5-
| ^^^^^^^^^^^^^^^ ABI should be specified here
5+
| ^^^^^^ help: explicitly specify the C ABI: `extern "C"`
66
|
7-
= help: the default ABI is C
87
= note: requested on the command line with `-D missing-abi`
98

109
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)