Skip to content

Commit 8d7d488

Browse files
committed
Lint Abi in ast validation.
1 parent daa4dc9 commit 8d7d488

File tree

6 files changed

+61
-50
lines changed

6 files changed

+61
-50
lines changed

Diff for: 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

Diff for: compiler/rustc_ast_lowering/src/item.rs

+13-24
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
283283
);
284284
let sig = hir::FnSig {
285285
decl,
286-
header: this.lower_fn_header(header, fn_sig_span, id),
286+
header: this.lower_fn_header(header),
287287
span: this.lower_span(fn_sig_span),
288288
};
289289
hir::ItemKind::Fn(sig, generics, body_id)
@@ -295,17 +295,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
295295
}
296296
ModKind::Unloaded => panic!("`mod` items should have been loaded by now"),
297297
},
298-
ItemKind::ForeignMod(ref fm) => {
299-
if fm.abi.is_none() {
300-
self.maybe_lint_missing_abi(span, id, abi::Abi::C { unwind: false });
301-
}
302-
hir::ItemKind::ForeignMod {
303-
abi: fm.abi.map_or(abi::Abi::C { unwind: false }, |abi| self.lower_abi(abi)),
304-
items: self
305-
.arena
306-
.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
307-
}
308-
}
298+
ItemKind::ForeignMod(ref fm) => hir::ItemKind::ForeignMod {
299+
abi: fm.abi.map_or(abi::Abi::FALLBACK, |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+
},
309304
ItemKind::GlobalAsm(ref asm) => {
310305
hir::ItemKind::GlobalAsm(self.lower_inline_asm(span, asm))
311306
}
@@ -811,7 +806,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
811806
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, None)) => {
812807
let names = self.lower_fn_params_to_names(&sig.decl);
813808
let (generics, sig) =
814-
self.lower_method_sig(generics, sig, trait_item_def_id, false, None, i.id);
809+
self.lower_method_sig(generics, sig, trait_item_def_id, false, None);
815810
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)))
816811
}
817812
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, Some(ref body))) => {
@@ -824,7 +819,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
824819
trait_item_def_id,
825820
false,
826821
asyncness.opt_return_id(),
827-
i.id,
828822
);
829823
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)))
830824
}
@@ -901,7 +895,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
901895
impl_item_def_id,
902896
impl_trait_return_allow,
903897
asyncness.opt_return_id(),
904-
i.id,
905898
);
906899

907900
(generics, hir::ImplItemKind::Fn(sig, body_id))
@@ -1296,9 +1289,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
12961289
fn_def_id: LocalDefId,
12971290
impl_trait_return_allow: bool,
12981291
is_async: Option<NodeId>,
1299-
id: NodeId,
13001292
) -> (hir::Generics<'hir>, hir::FnSig<'hir>) {
1301-
let header = self.lower_fn_header(sig.header, sig.span, id);
1293+
let header = self.lower_fn_header(sig.header);
13021294
let (generics, decl) = self.add_in_band_defs(
13031295
generics,
13041296
fn_def_id,
@@ -1315,12 +1307,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
13151307
(generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) })
13161308
}
13171309

1318-
fn lower_fn_header(&mut self, h: FnHeader, span: Span, id: NodeId) -> hir::FnHeader {
1310+
fn lower_fn_header(&mut self, h: FnHeader) -> hir::FnHeader {
13191311
hir::FnHeader {
13201312
unsafety: self.lower_unsafety(h.unsafety),
13211313
asyncness: self.lower_asyncness(h.asyncness),
13221314
constness: self.lower_constness(h.constness),
1323-
abi: self.lower_extern(h.ext, span, id),
1315+
abi: self.lower_extern(h.ext),
13241316
}
13251317
}
13261318

@@ -1331,13 +1323,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
13311323
})
13321324
}
13331325

1334-
pub(super) fn lower_extern(&mut self, ext: Extern, span: Span, id: NodeId) -> abi::Abi {
1326+
pub(super) fn lower_extern(&mut self, ext: Extern) -> abi::Abi {
13351327
match ext {
13361328
Extern::None => abi::Abi::Rust,
1337-
Extern::Implicit => {
1338-
self.maybe_lint_missing_abi(span, id, abi::Abi::C { unwind: false });
1339-
abi::Abi::C { unwind: false }
1340-
}
1329+
Extern::Implicit => abi::Abi::FALLBACK,
13411330
Extern::Explicit(abi) => self.lower_abi(abi),
13421331
}
13431332
}

Diff for: 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, SmallVec};
6867
use std::collections::BTreeMap;
@@ -1360,15 +1359,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13601359
}
13611360
TyKind::BareFn(ref f) => self.with_in_scope_lifetime_defs(&f.generic_params, |this| {
13621361
this.with_anonymous_lifetime_mode(AnonymousLifetimeMode::PassThrough, |this| {
1363-
let span = this.sess.source_map().next_point(t.span.shrink_to_lo());
13641362
hir::TyKind::BareFn(this.arena.alloc(hir::BareFnTy {
13651363
generic_params: this.lower_generic_params(
13661364
&f.generic_params,
13671365
&NodeMap::default(),
13681366
ImplTraitContext::disallowed(),
13691367
),
13701368
unsafety: this.lower_unsafety(f.unsafety),
1371-
abi: this.lower_extern(f.ext, span, t.id),
1369+
abi: this.lower_extern(f.ext),
13721370
decl: this.lower_fn_decl(&f.decl, None, false, None),
13731371
param_names: this.lower_fn_params_to_names(&f.decl),
13741372
}))
@@ -2842,26 +2840,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
28422840
}
28432841
}
28442842
}
2845-
2846-
fn maybe_lint_missing_abi(&mut self, span: Span, id: NodeId, default: Abi) {
2847-
// FIXME(davidtwco): This is a hack to detect macros which produce spans of the
2848-
// call site which do not have a macro backtrace. See #61963.
2849-
let is_macro_callsite = self
2850-
.sess
2851-
.source_map()
2852-
.span_to_snippet(span)
2853-
.map(|snippet| snippet.starts_with("#["))
2854-
.unwrap_or(true);
2855-
if !is_macro_callsite {
2856-
self.resolver.lint_buffer().buffer_lint_with_diagnostic(
2857-
MISSING_ABI,
2858-
id,
2859-
span,
2860-
"extern declarations without an explicit ABI are deprecated",
2861-
BuiltinLintDiagnostics::MissingAbi(span, default),
2862-
)
2863-
}
2864-
}
28652843
}
28662844

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

Diff for: 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" }

Diff for: 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| {

Diff for: 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)