Skip to content

Commit 1f1d212

Browse files
committed
Lint Abi in ast validation.
1 parent 7256855 commit 1f1d212

File tree

6 files changed

+50
-50
lines changed

6 files changed

+50
-50
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3631,6 +3631,7 @@ dependencies = [
36313631
"rustc_parse",
36323632
"rustc_session",
36333633
"rustc_span",
3634+
"rustc_target",
36343635
"tracing",
36353636
]
36363637

compiler/rustc_ast_lowering/src/item.rs

+13-24
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
306306
);
307307
let sig = hir::FnSig {
308308
decl,
309-
header: this.lower_fn_header(header, fn_sig_span, id),
309+
header: this.lower_fn_header(header),
310310
span: fn_sig_span,
311311
};
312312
hir::ItemKind::Fn(sig, generics, body_id)
@@ -318,17 +318,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
318318
}
319319
ModKind::Unloaded => panic!("`mod` items should have been loaded by now"),
320320
},
321-
ItemKind::ForeignMod(ref fm) => {
322-
if fm.abi.is_none() {
323-
self.maybe_lint_missing_abi(span, id, abi::Abi::C { unwind: false });
324-
}
325-
hir::ItemKind::ForeignMod {
326-
abi: fm.abi.map_or(abi::Abi::C { unwind: false }, |abi| self.lower_abi(abi)),
327-
items: self
328-
.arena
329-
.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
330-
}
331-
}
321+
ItemKind::ForeignMod(ref fm) => hir::ItemKind::ForeignMod {
322+
abi: fm.abi.map_or(abi::Abi::FALLBACK, |abi| self.lower_abi(abi)),
323+
items: self
324+
.arena
325+
.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
326+
},
332327
ItemKind::GlobalAsm(ref asm) => {
333328
hir::ItemKind::GlobalAsm(self.lower_inline_asm(span, asm))
334329
}
@@ -833,7 +828,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
833828
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, None)) => {
834829
let names = self.lower_fn_params_to_names(&sig.decl);
835830
let (generics, sig) =
836-
self.lower_method_sig(generics, sig, trait_item_def_id, false, None, i.id);
831+
self.lower_method_sig(generics, sig, trait_item_def_id, false, None);
837832
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)))
838833
}
839834
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, Some(ref body))) => {
@@ -846,7 +841,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
846841
trait_item_def_id,
847842
false,
848843
asyncness.opt_return_id(),
849-
i.id,
850844
);
851845
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)))
852846
}
@@ -911,7 +905,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
911905
impl_item_def_id,
912906
impl_trait_return_allow,
913907
asyncness.opt_return_id(),
914-
i.id,
915908
);
916909

917910
(generics, hir::ImplItemKind::Fn(sig, body_id))
@@ -1305,9 +1298,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
13051298
fn_def_id: LocalDefId,
13061299
impl_trait_return_allow: bool,
13071300
is_async: Option<NodeId>,
1308-
id: NodeId,
13091301
) -> (hir::Generics<'hir>, hir::FnSig<'hir>) {
1310-
let header = self.lower_fn_header(sig.header, sig.span, id);
1302+
let header = self.lower_fn_header(sig.header);
13111303
let (generics, decl) = self.add_in_band_defs(
13121304
generics,
13131305
fn_def_id,
@@ -1324,12 +1316,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
13241316
(generics, hir::FnSig { header, decl, span: sig.span })
13251317
}
13261318

1327-
fn lower_fn_header(&mut self, h: FnHeader, span: Span, id: NodeId) -> hir::FnHeader {
1319+
fn lower_fn_header(&mut self, h: FnHeader) -> hir::FnHeader {
13281320
hir::FnHeader {
13291321
unsafety: self.lower_unsafety(h.unsafety),
13301322
asyncness: self.lower_asyncness(h.asyncness),
13311323
constness: self.lower_constness(h.constness),
1332-
abi: self.lower_extern(h.ext, span, id),
1324+
abi: self.lower_extern(h.ext),
13331325
}
13341326
}
13351327

@@ -1340,13 +1332,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
13401332
})
13411333
}
13421334

1343-
pub(super) fn lower_extern(&mut self, ext: Extern, span: Span, id: NodeId) -> abi::Abi {
1335+
pub(super) fn lower_extern(&mut self, ext: Extern) -> abi::Abi {
13441336
match ext {
13451337
Extern::None => abi::Abi::Rust,
1346-
Extern::Implicit => {
1347-
self.maybe_lint_missing_abi(span, id, abi::Abi::C { unwind: false });
1348-
abi::Abi::C { unwind: false }
1349-
}
1338+
Extern::Implicit => abi::Abi::FALLBACK,
13501339
Extern::Explicit(abi) => self.lower_abi(abi),
13511340
}
13521341
}

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, 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, 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;
@@ -1287,15 +1286,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12871286
}
12881287
TyKind::BareFn(ref f) => self.with_in_scope_lifetime_defs(&f.generic_params, |this| {
12891288
this.with_anonymous_lifetime_mode(AnonymousLifetimeMode::PassThrough, |this| {
1290-
let span = this.sess.source_map().next_point(t.span.shrink_to_lo());
12911289
hir::TyKind::BareFn(this.arena.alloc(hir::BareFnTy {
12921290
generic_params: this.lower_generic_params(
12931291
&f.generic_params,
12941292
&NodeMap::default(),
12951293
ImplTraitContext::disallowed(),
12961294
),
12971295
unsafety: this.lower_unsafety(f.unsafety),
1298-
abi: this.lower_extern(f.ext, span, t.id),
1296+
abi: this.lower_extern(f.ext),
12991297
decl: this.lower_fn_decl(&f.decl, None, false, None),
13001298
param_names: this.lower_fn_params_to_names(&f.decl),
13011299
}))
@@ -2763,26 +2761,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
27632761
}
27642762
}
27652763
}
2766-
2767-
fn maybe_lint_missing_abi(&mut self, span: Span, id: NodeId, default: Abi) {
2768-
// FIXME(davidtwco): This is a hack to detect macros which produce spans of the
2769-
// call site which do not have a macro backtrace. See #61963.
2770-
let is_macro_callsite = self
2771-
.sess
2772-
.source_map()
2773-
.span_to_snippet(span)
2774-
.map(|snippet| snippet.starts_with("#["))
2775-
.unwrap_or(true);
2776-
if !is_macro_callsite {
2777-
self.resolver.lint_buffer().buffer_lint_with_diagnostic(
2778-
MISSING_ABI,
2779-
id,
2780-
span,
2781-
"extern declarations without an explicit ABI are deprecated",
2782-
BuiltinLintDiagnostics::MissingAbi(span, default),
2783-
)
2784-
}
2785-
}
27862764
}
27872765

27882766
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
@@ -15,4 +15,5 @@ rustc_feature = { path = "../rustc_feature" }
1515
rustc_parse = { path = "../rustc_parse" }
1616
rustc_session = { path = "../rustc_session" }
1717
rustc_span = { path = "../rustc_span" }
18+
rustc_target = { path = "../rustc_target" }
1819
rustc_ast = { path = "../rustc_ast" }

compiler/rustc_ast_passes/src/ast_validation.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ 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::symbol::{kw, sym, Ident};
2222
use rustc_span::Span;
23+
use rustc_target::spec::abi;
2324
use std::mem;
2425
use std::ops::DerefMut;
2526

@@ -877,6 +878,26 @@ impl<'a> AstValidator<'a> {
877878
_ => {}
878879
}
879880
}
881+
882+
fn maybe_lint_missing_abi(&mut self, span: Span, id: NodeId) {
883+
// FIXME(davidtwco): This is a hack to detect macros which produce spans of the
884+
// call site which do not have a macro backtrace. See #61963.
885+
let is_macro_callsite = self
886+
.session
887+
.source_map()
888+
.span_to_snippet(span)
889+
.map(|snippet| snippet.starts_with("#["))
890+
.unwrap_or(true);
891+
if !is_macro_callsite {
892+
self.lint_buffer.buffer_lint_with_diagnostic(
893+
MISSING_ABI,
894+
id,
895+
span,
896+
"extern declarations without an explicit ABI are deprecated",
897+
BuiltinLintDiagnostics::MissingAbi(span, abi::Abi::FALLBACK),
898+
)
899+
}
900+
}
880901
}
881902

882903
/// Checks that generic parameters are in the correct order,
@@ -1111,7 +1132,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11111132
self.error_item_without_body(item.span, "function", msg, " { <body> }");
11121133
}
11131134
}
1114-
ItemKind::ForeignMod(ForeignMod { unsafety, .. }) => {
1135+
ItemKind::ForeignMod(ForeignMod { abi, unsafety, .. }) => {
11151136
let old_item = mem::replace(&mut self.extern_mod, Some(item));
11161137
self.invalid_visibility(
11171138
&item.vis,
@@ -1120,6 +1141,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11201141
if let Unsafe::Yes(span) = unsafety {
11211142
self.err_handler().span_err(span, "extern block cannot be declared unsafe");
11221143
}
1144+
if abi.is_none() {
1145+
self.maybe_lint_missing_abi(item.span, item.id);
1146+
}
11231147
visit::walk_item(self, item);
11241148
self.extern_mod = old_item;
11251149
return; // Avoid visiting again.
@@ -1459,6 +1483,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14591483
.emit();
14601484
}
14611485

1486+
if let Some(FnHeader { ext: Extern::Implicit, .. }) = fk.header() {
1487+
self.maybe_lint_missing_abi(span, id);
1488+
}
1489+
14621490
// Functions without bodies cannot have patterns.
14631491
if let FnKind::Fn(ctxt, _, sig, _, None) = fk {
14641492
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)