Skip to content

Commit 2bf340c

Browse files
compiler: plug unsupported ABI leakage from the AST
We modify rustc_ast_lowering to prevent all unsupported ABIs from leaking through the HIR without being checked for target support. Previously ad-hoc checking on various HIR items required making sure we check every HIR item which could contain an `extern "{abi}"` string. This is a losing proposition compared to gating the lowering itself. As a consequence, unsupported ABI strings will now hard-error instead of triggering the FCW `unsupported_fn_ptr_calling_conventions`. This FCW was upgraded to warn in dependencies in Rust 1.87 which was released on 2025 May 17, and it is now 2025 June, so it has become active within a stable Rust version. As we already had errored on these ABIs in most other positions, and have warned for fn ptrs, this breakage has had reasonable foreshadowing. However, this does cause errors for usages of `extern "{abi}"` that were theoretically writeable within source but could not actually be applied in any useful way by Rust programmers without either warning or error. For instance, trait declarations without impls were never checked. These are the exact kinds of leakages that this new approach prevents. A deprecation cycle is not useful for these marginal cases as upon impl, even default impls within traits, different HIR objects would be used. Details of our HIR analysis meant that those objects did get checked.
1 parent f315e61 commit 2bf340c

File tree

1 file changed

+15
-4
lines changed
  • compiler/rustc_ast_lowering/src

1 file changed

+15
-4
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_abi::ExternAbi;
22
use rustc_ast::ptr::P;
33
use rustc_ast::visit::AssocCtxt;
44
use rustc_ast::*;
5-
use rustc_errors::ErrorGuaranteed;
5+
use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
66
use rustc_hir::def::{DefKind, PerNS, Res};
77
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
88
use rustc_hir::{self as hir, HirId, LifetimeSource, PredicateOrigin};
@@ -1617,9 +1617,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
16171617
self.error_on_invalid_abi(abi_str);
16181618
ExternAbi::Rust
16191619
});
1620-
let sess = self.tcx.sess;
1621-
let features = self.tcx.features();
1622-
gate_unstable_abi(sess, features, span, extern_abi);
1620+
let tcx = self.tcx;
1621+
1622+
// we can't do codegen for unsupported ABIs, so error now so we won't get farther
1623+
if !tcx.sess.target.is_abi_supported(extern_abi) {
1624+
struct_span_code_err!(
1625+
tcx.dcx(),
1626+
span,
1627+
E0570,
1628+
"`{extern_abi}` is not a supported ABI for the current target",
1629+
)
1630+
.emit();
1631+
}
1632+
// stability gate even things that are already errored on
1633+
gate_unstable_abi(tcx.sess, tcx.features(), span, extern_abi);
16231634
extern_abi
16241635
}
16251636

0 commit comments

Comments
 (0)