Skip to content

Commit c3304c8

Browse files
committed
stabilize extended_varargs_abi_support
1 parent 5ab6924 commit c3304c8

File tree

18 files changed

+73
-113
lines changed

18 files changed

+73
-113
lines changed

compiler/rustc_abi/src/extern_abi.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::hash::{Hash, Hasher};
66
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd};
77
#[cfg(feature = "nightly")]
88
use rustc_macros::{Decodable, Encodable};
9+
use rustc_span::{Symbol, sym};
910

1011
use crate::AbiFromStrErr;
1112

@@ -226,6 +227,12 @@ impl StableOrd for ExternAbi {
226227
#[cfg(feature = "nightly")]
227228
rustc_error_messages::into_diag_arg_using_display!(ExternAbi);
228229

230+
pub enum CVariadicStatus {
231+
NotSupported,
232+
Stable,
233+
Unstable { feature: Symbol },
234+
}
235+
229236
impl ExternAbi {
230237
/// An ABI "like Rust"
231238
///
@@ -238,23 +245,30 @@ impl ExternAbi {
238245
matches!(self, Rust | RustCall | RustCold)
239246
}
240247

241-
pub fn supports_varargs(self) -> bool {
248+
/// Returns whether the ABI supports C variadics.
249+
pub fn supports_c_variadic(self) -> CVariadicStatus {
242250
// * C and Cdecl obviously support varargs.
243251
// * C can be based on Aapcs, SysV64 or Win64, so they must support varargs.
244252
// * EfiApi is based on Win64 or C, so it also supports it.
253+
// * System automatically falls back to C when used with variadics, therefore supports it.
245254
//
246255
// * Stdcall does not, because it would be impossible for the callee to clean
247256
// up the arguments. (callee doesn't know how many arguments are there)
248257
// * Same for Fastcall, Vectorcall and Thiscall.
249258
// * Other calling conventions are related to hardware or the compiler itself.
259+
//
260+
// All of the supported ones must have a test in `tests/codegen/cffi/c-variadic-ffi.rs`.
250261
match self {
251262
Self::C { .. }
252263
| Self::Cdecl { .. }
253264
| Self::Aapcs { .. }
254265
| Self::Win64 { .. }
255266
| Self::SysV64 { .. }
256-
| Self::EfiApi => true,
257-
_ => false,
267+
| Self::EfiApi => CVariadicStatus::Stable,
268+
Self::System { .. } => {
269+
CVariadicStatus::Unstable { feature: sym::extern_system_varargs }
270+
}
271+
_ => CVariadicStatus::NotSupported,
258272
}
259273
}
260274
}

compiler/rustc_abi/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ mod tests;
6363

6464
pub use callconv::{Heterogeneous, HomogeneousAggregate, Reg, RegKind};
6565
pub use canon_abi::{ArmCall, CanonAbi, InterruptKind, X86Call};
66-
pub use extern_abi::{ExternAbi, all_names};
66+
pub use extern_abi::{CVariadicStatus, ExternAbi, all_names};
6767
#[cfg(feature = "nightly")]
6868
pub use layout::{FIRST_VARIANT, FieldIdx, Layout, TyAbiInterface, TyAndLayout, VariantIdx};
6969
pub use layout::{LayoutCalculator, LayoutCalculatorError};

compiler/rustc_feature/src/accepted.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ declare_features! (
203203
(accepted, expr_fragment_specifier_2024, "1.83.0", Some(123742)),
204204
/// Allows arbitrary expressions in key-value attributes at parse time.
205205
(accepted, extended_key_value_attributes, "1.54.0", Some(78835)),
206+
/// Allows using `aapcs`, `efiapi`, `sysv64` and `win64` as calling conventions
207+
/// for functions with varargs.
208+
(accepted, extended_varargs_abi_support, "CURRENT_RUSTC_VERSION", Some(100189)),
206209
/// Allows resolving absolute paths as paths from other crates.
207210
(accepted, extern_absolute_paths, "1.30.0", Some(44660)),
208211
/// Allows `extern crate foo as bar;`. This puts `bar` into extern prelude.

compiler/rustc_feature/src/unstable.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,6 @@ declare_features! (
492492
(incomplete, explicit_tail_calls, "1.72.0", Some(112788)),
493493
/// Allows using `#[export_stable]` which indicates that an item is exportable.
494494
(incomplete, export_stable, "1.88.0", Some(139939)),
495-
/// Allows using `aapcs`, `efiapi`, `sysv64` and `win64` as calling conventions
496-
/// for functions with varargs.
497-
(unstable, extended_varargs_abi_support, "1.65.0", Some(100189)),
498495
/// Allows using `system` as a calling convention with varargs.
499496
(unstable, extern_system_varargs, "1.86.0", Some(136946)),
500497
/// Allows defining `extern type`s.

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
978978
tcx.ensure_ok().fn_sig(def_id);
979979
let item = tcx.hir_foreign_item(item);
980980
let hir::ForeignItemKind::Fn(sig, ..) = item.kind else { bug!() };
981-
require_c_abi_if_c_variadic(tcx, sig.decl, abi, item.span);
981+
check_c_variadic_abi(tcx, sig.decl, abi, item.span);
982982
}
983983
DefKind::Static { .. } => {
984984
tcx.ensure_ok().codegen_fn_attrs(def_id);

compiler/rustc_hir_analysis/src/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ use tracing::debug;
9898

9999
use self::compare_impl_item::collect_return_position_impl_trait_in_trait_tys;
100100
use self::region::region_scope_tree;
101-
use crate::{errors, require_c_abi_if_c_variadic};
101+
use crate::{check_c_variadic_abi, errors};
102102

103103
/// Adds query implementations to the [Providers] vtable, see [`rustc_middle::query`]
104104
pub(super) fn provide(providers: &mut Providers) {

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ use rustc_trait_selection::traits::{self, FulfillmentError};
5252
use tracing::{debug, instrument};
5353

5454
use crate::check::check_abi;
55+
use crate::check_c_variadic_abi;
5556
use crate::errors::{AmbiguousLifetimeBound, BadReturnTypeNotation};
5657
use crate::hir_ty_lowering::errors::{GenericsArgsErrExtend, prohibit_assoc_item_constraint};
5758
use crate::hir_ty_lowering::generics::{check_generic_arg_count, lower_generic_args};
5859
use crate::middle::resolve_bound_vars as rbv;
59-
use crate::require_c_abi_if_c_variadic;
6060

6161
/// A path segment that is semantically allowed to have generic arguments.
6262
#[derive(Debug)]
@@ -2412,7 +2412,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24122412
Ty::new_tup_from_iter(tcx, fields.iter().map(|t| self.lower_ty(t)))
24132413
}
24142414
hir::TyKind::FnPtr(bf) => {
2415-
require_c_abi_if_c_variadic(tcx, bf.decl, bf.abi, hir_ty.span);
2415+
check_c_variadic_abi(tcx, bf.decl, bf.abi, hir_ty.span);
24162416

24172417
Ty::new_fn_ptr(
24182418
tcx,

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ mod outlives;
9090
mod variance;
9191

9292
pub use errors::NoVariantNamed;
93-
use rustc_abi::ExternAbi;
93+
use rustc_abi::{CVariadicStatus, ExternAbi};
9494
use rustc_hir::def::DefKind;
9595
use rustc_hir::lints::DelayedLint;
9696
use rustc_hir::{self as hir};
@@ -99,7 +99,6 @@ use rustc_middle::mir::interpret::GlobalId;
9999
use rustc_middle::query::Providers;
100100
use rustc_middle::ty::{self, Const, Ty, TyCtxt};
101101
use rustc_session::parse::feature_err;
102-
use rustc_span::symbol::sym;
103102
use rustc_span::{ErrorGuaranteed, Span};
104103
use rustc_trait_selection::traits;
105104

@@ -108,46 +107,34 @@ use crate::hir_ty_lowering::{FeedConstTy, HirTyLowerer};
108107

109108
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
110109

111-
fn require_c_abi_if_c_variadic(
112-
tcx: TyCtxt<'_>,
113-
decl: &hir::FnDecl<'_>,
114-
abi: ExternAbi,
115-
span: Span,
116-
) {
117-
// ABIs which can stably use varargs
118-
if !decl.c_variadic || matches!(abi, ExternAbi::C { .. } | ExternAbi::Cdecl { .. }) {
110+
fn check_c_variadic_abi(tcx: TyCtxt<'_>, decl: &hir::FnDecl<'_>, abi: ExternAbi, span: Span) {
111+
if !decl.c_variadic {
112+
// Not even a variadic function.
119113
return;
120114
}
121115

122-
// ABIs with feature-gated stability
123-
let extended_abi_support = tcx.features().extended_varargs_abi_support();
124-
let extern_system_varargs = tcx.features().extern_system_varargs();
125-
126-
// If the feature gate has been enabled, we can stop here
127-
if extern_system_varargs && let ExternAbi::System { .. } = abi {
128-
return;
129-
};
130-
if extended_abi_support && abi.supports_varargs() {
131-
return;
132-
};
133-
134-
// Looks like we need to pick an error to emit.
135-
// Is there any feature which we could have enabled to make this work?
136-
let unstable_explain =
137-
format!("C-variadic functions with the {abi} calling convention are unstable");
138-
match abi {
139-
ExternAbi::System { .. } => {
140-
feature_err(&tcx.sess, sym::extern_system_varargs, span, unstable_explain)
116+
match abi.supports_c_variadic() {
117+
CVariadicStatus::Stable => {}
118+
CVariadicStatus::NotSupported => {
119+
tcx.dcx()
120+
.create_err(errors::VariadicFunctionCompatibleConvention {
121+
span,
122+
convention: &format!("{abi}"),
123+
})
124+
.emit();
141125
}
142-
abi if abi.supports_varargs() => {
143-
feature_err(&tcx.sess, sym::extended_varargs_abi_support, span, unstable_explain)
126+
CVariadicStatus::Unstable { feature } => {
127+
if !tcx.features().enabled(feature) {
128+
feature_err(
129+
&tcx.sess,
130+
feature,
131+
span,
132+
format!("C-variadic functions with the {abi} calling convention are unstable"),
133+
)
134+
.emit();
135+
}
144136
}
145-
_ => tcx.dcx().create_err(errors::VariadicFunctionCompatibleConvention {
146-
span,
147-
convention: &format!("{abi}"),
148-
}),
149137
}
150-
.emit();
151138
}
152139

153140
/// Adds query implementations to the [Providers] vtable, see [`rustc_middle::query`]

library/std/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@
291291
#![feature(doc_masked)]
292292
#![feature(doc_notable_trait)]
293293
#![feature(dropck_eyepatch)]
294-
#![feature(extended_varargs_abi_support)]
295294
#![feature(f16)]
296295
#![feature(f128)]
297296
#![feature(ffi_const)]

src/doc/unstable-book/src/language-features/extended-varargs-abi-support.md

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)