Skip to content

Commit 94a9765

Browse files
authored
Rollup merge of rust-lang#87114 - cjgillot:abilint, r=estebank
Lint missing Abi in ast validation instead of lowering.
2 parents f3f8e75 + 1f1d212 commit 94a9765

File tree

6 files changed

+50
-50
lines changed

6 files changed

+50
-50
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3560,6 +3560,7 @@ dependencies = [
35603560
"rustc_parse",
35613561
"rustc_session",
35623562
"rustc_span",
3563+
"rustc_target",
35633564
"tracing",
35643565
]
35653566

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
300300
);
301301
let sig = hir::FnSig {
302302
decl,
303-
header: this.lower_fn_header(header, fn_sig_span, id),
303+
header: this.lower_fn_header(header),
304304
span: fn_sig_span,
305305
};
306306
hir::ItemKind::Fn(sig, generics, body_id)
@@ -312,17 +312,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
312312
}
313313
ModKind::Unloaded => panic!("`mod` items should have been loaded by now"),
314314
},
315-
ItemKind::ForeignMod(ref fm) => {
316-
if fm.abi.is_none() {
317-
self.maybe_lint_missing_abi(span, id, abi::Abi::C { unwind: false });
318-
}
319-
hir::ItemKind::ForeignMod {
320-
abi: fm.abi.map_or(abi::Abi::C { unwind: false }, |abi| self.lower_abi(abi)),
321-
items: self
322-
.arena
323-
.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
324-
}
325-
}
315+
ItemKind::ForeignMod(ref fm) => hir::ItemKind::ForeignMod {
316+
abi: fm.abi.map_or(abi::Abi::FALLBACK, |abi| self.lower_abi(abi)),
317+
items: self
318+
.arena
319+
.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
320+
},
326321
ItemKind::GlobalAsm(ref asm) => {
327322
hir::ItemKind::GlobalAsm(self.lower_inline_asm(span, asm))
328323
}
@@ -816,7 +811,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
816811
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, None)) => {
817812
let names = self.lower_fn_params_to_names(&sig.decl);
818813
let (generics, sig) =
819-
self.lower_method_sig(generics, sig, trait_item_def_id, false, None, i.id);
814+
self.lower_method_sig(generics, sig, trait_item_def_id, false, None);
820815
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)))
821816
}
822817
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, Some(ref body))) => {
@@ -829,7 +824,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
829824
trait_item_def_id,
830825
false,
831826
asyncness.opt_return_id(),
832-
i.id,
833827
);
834828
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)))
835829
}
@@ -894,7 +888,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
894888
impl_item_def_id,
895889
impl_trait_return_allow,
896890
asyncness.opt_return_id(),
897-
i.id,
898891
);
899892

900893
(generics, hir::ImplItemKind::Fn(sig, body_id))
@@ -1287,9 +1280,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
12871280
fn_def_id: LocalDefId,
12881281
impl_trait_return_allow: bool,
12891282
is_async: Option<NodeId>,
1290-
id: NodeId,
12911283
) -> (hir::Generics<'hir>, hir::FnSig<'hir>) {
1292-
let header = self.lower_fn_header(sig.header, sig.span, id);
1284+
let header = self.lower_fn_header(sig.header);
12931285
let (generics, decl) = self.add_in_band_defs(
12941286
generics,
12951287
fn_def_id,
@@ -1306,12 +1298,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
13061298
(generics, hir::FnSig { header, decl, span: sig.span })
13071299
}
13081300

1309-
fn lower_fn_header(&mut self, h: FnHeader, span: Span, id: NodeId) -> hir::FnHeader {
1301+
fn lower_fn_header(&mut self, h: FnHeader) -> hir::FnHeader {
13101302
hir::FnHeader {
13111303
unsafety: self.lower_unsafety(h.unsafety),
13121304
asyncness: self.lower_asyncness(h.asyncness),
13131305
constness: self.lower_constness(h.constness),
1314-
abi: self.lower_extern(h.ext, span, id),
1306+
abi: self.lower_extern(h.ext),
13151307
}
13161308
}
13171309

@@ -1322,13 +1314,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
13221314
})
13231315
}
13241316

1325-
pub(super) fn lower_extern(&mut self, ext: Extern, span: Span, id: NodeId) -> abi::Abi {
1317+
pub(super) fn lower_extern(&mut self, ext: Extern) -> abi::Abi {
13261318
match ext {
13271319
Extern::None => abi::Abi::Rust,
1328-
Extern::Implicit => {
1329-
self.maybe_lint_missing_abi(span, id, abi::Abi::C { unwind: false });
1330-
abi::Abi::C { unwind: false }
1331-
}
1320+
Extern::Implicit => abi::Abi::FALLBACK,
13321321
Extern::Explicit(abi) => self.lower_abi(abi),
13331322
}
13341323
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 24 deletions
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
}))
@@ -2826,26 +2824,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
28262824
}
28272825
}
28282826
}
2829-
2830-
fn maybe_lint_missing_abi(&mut self, span: Span, id: NodeId, default: Abi) {
2831-
// FIXME(davidtwco): This is a hack to detect macros which produce spans of the
2832-
// call site which do not have a macro backtrace. See #61963.
2833-
let is_macro_callsite = self
2834-
.sess
2835-
.source_map()
2836-
.span_to_snippet(span)
2837-
.map(|snippet| snippet.starts_with("#["))
2838-
.unwrap_or(true);
2839-
if !is_macro_callsite {
2840-
self.resolver.lint_buffer().buffer_lint_with_diagnostic(
2841-
MISSING_ABI,
2842-
id,
2843-
span,
2844-
"extern declarations without an explicit ABI are deprecated",
2845-
BuiltinLintDiagnostics::MissingAbi(span, default),
2846-
)
2847-
}
2848-
}
28492827
}
28502828

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

compiler/rustc_ast_passes/Cargo.toml

Lines changed: 1 addition & 0 deletions
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

Lines changed: 30 additions & 2 deletions
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

Lines changed: 3 additions & 0 deletions
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)