Skip to content

Commit b27ccbc

Browse files
committed
Auto merge of #87114 - cjgillot:abilint, r=estebank
Lint missing Abi in ast validation instead of lowering.
2 parents cc9bb15 + 8d7d488 commit b27ccbc

File tree

6 files changed

+61
-50
lines changed

6 files changed

+61
-50
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3556,6 +3556,7 @@ dependencies = [
35563556
"rustc_parse",
35573557
"rustc_session",
35583558
"rustc_span",
3559+
"rustc_target",
35593560
"tracing",
35603561
]
35613562

compiler/rustc_ast_lowering/src/item.rs

+13-24
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
279279
);
280280
let sig = hir::FnSig {
281281
decl,
282-
header: this.lower_fn_header(header, fn_sig_span, id),
282+
header: this.lower_fn_header(header),
283283
span: this.lower_span(fn_sig_span),
284284
};
285285
hir::ItemKind::Fn(sig, generics, body_id)
@@ -291,17 +291,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
291291
}
292292
ModKind::Unloaded => panic!("`mod` items should have been loaded by now"),
293293
},
294-
ItemKind::ForeignMod(ref fm) => {
295-
if fm.abi.is_none() {
296-
self.maybe_lint_missing_abi(span, id, abi::Abi::C { unwind: false });
297-
}
298-
hir::ItemKind::ForeignMod {
299-
abi: fm.abi.map_or(abi::Abi::C { unwind: false }, |abi| self.lower_abi(abi)),
300-
items: self
301-
.arena
302-
.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
303-
}
304-
}
294+
ItemKind::ForeignMod(ref fm) => hir::ItemKind::ForeignMod {
295+
abi: fm.abi.map_or(abi::Abi::FALLBACK, |abi| self.lower_abi(abi)),
296+
items: self
297+
.arena
298+
.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
299+
},
305300
ItemKind::GlobalAsm(ref asm) => {
306301
hir::ItemKind::GlobalAsm(self.lower_inline_asm(span, asm))
307302
}
@@ -807,7 +802,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
807802
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, None)) => {
808803
let names = self.lower_fn_params_to_names(&sig.decl);
809804
let (generics, sig) =
810-
self.lower_method_sig(generics, sig, trait_item_def_id, false, None, i.id);
805+
self.lower_method_sig(generics, sig, trait_item_def_id, false, None);
811806
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)))
812807
}
813808
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, Some(ref body))) => {
@@ -820,7 +815,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
820815
trait_item_def_id,
821816
false,
822817
asyncness.opt_return_id(),
823-
i.id,
824818
);
825819
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)))
826820
}
@@ -897,7 +891,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
897891
impl_item_def_id,
898892
impl_trait_return_allow,
899893
asyncness.opt_return_id(),
900-
i.id,
901894
);
902895

903896
(generics, hir::ImplItemKind::Fn(sig, body_id))
@@ -1292,9 +1285,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
12921285
fn_def_id: LocalDefId,
12931286
impl_trait_return_allow: bool,
12941287
is_async: Option<NodeId>,
1295-
id: NodeId,
12961288
) -> (hir::Generics<'hir>, hir::FnSig<'hir>) {
1297-
let header = self.lower_fn_header(sig.header, sig.span, id);
1289+
let header = self.lower_fn_header(sig.header);
12981290
let (generics, decl) = self.add_in_band_defs(
12991291
generics,
13001292
fn_def_id,
@@ -1311,12 +1303,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
13111303
(generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) })
13121304
}
13131305

1314-
fn lower_fn_header(&mut self, h: FnHeader, span: Span, id: NodeId) -> hir::FnHeader {
1306+
fn lower_fn_header(&mut self, h: FnHeader) -> hir::FnHeader {
13151307
hir::FnHeader {
13161308
unsafety: self.lower_unsafety(h.unsafety),
13171309
asyncness: self.lower_asyncness(h.asyncness),
13181310
constness: self.lower_constness(h.constness),
1319-
abi: self.lower_extern(h.ext, span, id),
1311+
abi: self.lower_extern(h.ext),
13201312
}
13211313
}
13221314

@@ -1327,13 +1319,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
13271319
})
13281320
}
13291321

1330-
pub(super) fn lower_extern(&mut self, ext: Extern, span: Span, id: NodeId) -> abi::Abi {
1322+
pub(super) fn lower_extern(&mut self, ext: Extern) -> abi::Abi {
13311323
match ext {
13321324
Extern::None => abi::Abi::Rust,
1333-
Extern::Implicit => {
1334-
self.maybe_lint_missing_abi(span, id, abi::Abi::C { unwind: false });
1335-
abi::Abi::C { unwind: false }
1336-
}
1325+
Extern::Implicit => abi::Abi::FALLBACK,
13371326
Extern::Explicit(abi) => self.lower_abi(abi),
13381327
}
13391328
}

compiler/rustc_ast_lowering/src/lib.rs

+2-24
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
5353
use rustc_hir::intravisit;
5454
use rustc_hir::{ConstArg, GenericArg, InferKind, ParamName};
5555
use rustc_index::vec::{Idx, IndexVec};
56-
use rustc_session::lint::builtin::{BARE_TRAIT_OBJECTS, MISSING_ABI};
56+
use rustc_session::lint::builtin::BARE_TRAIT_OBJECTS;
5757
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
5858
use rustc_session::utils::{FlattenNonterminals, NtToTokenstream};
5959
use rustc_session::Session;
@@ -62,7 +62,6 @@ use rustc_span::hygiene::ExpnId;
6262
use rustc_span::source_map::{respan, CachingSourceMapView, DesugaringKind};
6363
use rustc_span::symbol::{kw, sym, Ident, Symbol};
6464
use rustc_span::{Span, DUMMY_SP};
65-
use rustc_target::spec::abi::Abi;
6665

6766
use smallvec::SmallVec;
6867
use std::collections::BTreeMap;
@@ -1361,15 +1360,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13611360
}
13621361
TyKind::BareFn(ref f) => self.with_in_scope_lifetime_defs(&f.generic_params, |this| {
13631362
this.with_anonymous_lifetime_mode(AnonymousLifetimeMode::PassThrough, |this| {
1364-
let span = this.sess.source_map().next_point(t.span.shrink_to_lo());
13651363
hir::TyKind::BareFn(this.arena.alloc(hir::BareFnTy {
13661364
generic_params: this.lower_generic_params(
13671365
&f.generic_params,
13681366
&NodeMap::default(),
13691367
ImplTraitContext::disallowed(),
13701368
),
13711369
unsafety: this.lower_unsafety(f.unsafety),
1372-
abi: this.lower_extern(f.ext, span, t.id),
1370+
abi: this.lower_extern(f.ext),
13731371
decl: this.lower_fn_decl(&f.decl, None, false, None),
13741372
param_names: this.lower_fn_params_to_names(&f.decl),
13751373
}))
@@ -2749,26 +2747,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
27492747
}
27502748
}
27512749
}
2752-
2753-
fn maybe_lint_missing_abi(&mut self, span: Span, id: NodeId, default: Abi) {
2754-
// FIXME(davidtwco): This is a hack to detect macros which produce spans of the
2755-
// call site which do not have a macro backtrace. See #61963.
2756-
let is_macro_callsite = self
2757-
.sess
2758-
.source_map()
2759-
.span_to_snippet(span)
2760-
.map(|snippet| snippet.starts_with("#["))
2761-
.unwrap_or(true);
2762-
if !is_macro_callsite {
2763-
self.resolver.lint_buffer().buffer_lint_with_diagnostic(
2764-
MISSING_ABI,
2765-
id,
2766-
span,
2767-
"extern declarations without an explicit ABI are deprecated",
2768-
BuiltinLintDiagnostics::MissingAbi(span, default),
2769-
)
2770-
}
2771-
}
27722750
}
27732751

27742752
fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body<'_>>) -> Vec<hir::BodyId> {

compiler/rustc_ast_passes/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ rustc_feature = { path = "../rustc_feature" }
1414
rustc_parse = { path = "../rustc_parse" }
1515
rustc_session = { path = "../rustc_session" }
1616
rustc_span = { path = "../rustc_span" }
17+
rustc_target = { path = "../rustc_target" }
1718
rustc_ast = { path = "../rustc_ast" }

compiler/rustc_ast_passes/src/ast_validation.rs

+41-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ use rustc_ast_pretty::pprust;
1515
use rustc_data_structures::fx::FxHashMap;
1616
use rustc_errors::{error_code, pluralize, struct_span_err, Applicability};
1717
use rustc_parse::validate_attr;
18-
use rustc_session::lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY;
18+
use rustc_session::lint::builtin::{MISSING_ABI, PATTERNS_IN_FNS_WITHOUT_BODY};
1919
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
2020
use rustc_session::Session;
2121
use rustc_span::source_map::Spanned;
2222
use rustc_span::symbol::{kw, sym, Ident};
2323
use rustc_span::Span;
24+
use rustc_target::spec::abi;
2425
use std::mem;
2526
use std::ops::DerefMut;
2627

@@ -844,6 +845,10 @@ impl<'a> AstValidator<'a> {
844845
.emit();
845846
});
846847
self.check_late_bound_lifetime_defs(&bfty.generic_params);
848+
if let Extern::Implicit = bfty.ext {
849+
let sig_span = self.session.source_map().next_point(ty.span.shrink_to_lo());
850+
self.maybe_lint_missing_abi(sig_span, ty.id);
851+
}
847852
}
848853
TyKind::TraitObject(ref bounds, ..) => {
849854
let mut any_lifetime_bounds = false;
@@ -894,6 +899,26 @@ impl<'a> AstValidator<'a> {
894899
_ => {}
895900
}
896901
}
902+
903+
fn maybe_lint_missing_abi(&mut self, span: Span, id: NodeId) {
904+
// FIXME(davidtwco): This is a hack to detect macros which produce spans of the
905+
// call site which do not have a macro backtrace. See #61963.
906+
let is_macro_callsite = self
907+
.session
908+
.source_map()
909+
.span_to_snippet(span)
910+
.map(|snippet| snippet.starts_with("#["))
911+
.unwrap_or(true);
912+
if !is_macro_callsite {
913+
self.lint_buffer.buffer_lint_with_diagnostic(
914+
MISSING_ABI,
915+
id,
916+
span,
917+
"extern declarations without an explicit ABI are deprecated",
918+
BuiltinLintDiagnostics::MissingAbi(span, abi::Abi::FALLBACK),
919+
)
920+
}
921+
}
897922
}
898923

899924
/// Checks that generic parameters are in the correct order,
@@ -1178,7 +1203,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11781203
walk_list!(self, visit_attribute, &item.attrs);
11791204
return; // Avoid visiting again.
11801205
}
1181-
ItemKind::ForeignMod(ForeignMod { unsafety, .. }) => {
1206+
ItemKind::ForeignMod(ForeignMod { abi, unsafety, .. }) => {
11821207
let old_item = mem::replace(&mut self.extern_mod, Some(item));
11831208
self.invalid_visibility(
11841209
&item.vis,
@@ -1187,6 +1212,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11871212
if let Unsafe::Yes(span) = unsafety {
11881213
self.err_handler().span_err(span, "extern block cannot be declared unsafe");
11891214
}
1215+
if abi.is_none() {
1216+
self.maybe_lint_missing_abi(item.span, item.id);
1217+
}
11901218
visit::walk_item(self, item);
11911219
self.extern_mod = old_item;
11921220
return; // Avoid visiting again.
@@ -1526,6 +1554,17 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15261554
.emit();
15271555
}
15281556

1557+
if let FnKind::Fn(
1558+
_,
1559+
_,
1560+
FnSig { span: sig_span, header: FnHeader { ext: Extern::Implicit, .. }, .. },
1561+
_,
1562+
_,
1563+
) = fk
1564+
{
1565+
self.maybe_lint_missing_abi(*sig_span, id);
1566+
}
1567+
15291568
// Functions without bodies cannot have patterns.
15301569
if let FnKind::Fn(ctxt, _, sig, _, None) = fk {
15311570
Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| {

compiler/rustc_target/src/spec/abi.rs

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ pub fn all_names() -> Vec<&'static str> {
8787
}
8888

8989
impl Abi {
90+
/// Default ABI chosen for `extern fn` declarations without an explicit ABI.
91+
pub const FALLBACK: Abi = Abi::C { unwind: false };
92+
9093
#[inline]
9194
pub fn index(self) -> usize {
9295
// N.B., this ordering MUST match the AbiDatas array above.

0 commit comments

Comments
 (0)