Skip to content

Commit

Permalink
stabilize const_extern_fn
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Sep 14, 2024
1 parent 02b1be1 commit 027a557
Show file tree
Hide file tree
Showing 26 changed files with 86 additions and 133 deletions.
25 changes: 6 additions & 19 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,9 @@ struct PostExpansionVisitor<'a> {

impl<'a> PostExpansionVisitor<'a> {
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
fn check_abi(&self, abi: ast::StrLit, constness: ast::Const) {
fn check_abi(&self, abi: ast::StrLit) {
let ast::StrLit { symbol_unescaped, span, .. } = abi;

if let ast::Const::Yes(_) = constness {
match symbol_unescaped {
// Stable
sym::Rust | sym::C => {}
abi => gate!(
&self,
const_extern_fn,
span,
format!("`{}` as a `const fn` ABI is unstable", abi)
),
}
}

match abi::is_enabled(self.features, span, symbol_unescaped.as_str()) {
Ok(()) => (),
Err(abi::AbiDisabled::Unstable { feature, explain }) => {
Expand All @@ -110,9 +97,9 @@ impl<'a> PostExpansionVisitor<'a> {
}
}

fn check_extern(&self, ext: ast::Extern, constness: ast::Const) {
fn check_extern(&self, ext: ast::Extern) {
if let ast::Extern::Explicit(abi, _) = ext {
self.check_abi(abi, constness);
self.check_abi(abi);
}
}

Expand Down Expand Up @@ -239,7 +226,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
match &i.kind {
ast::ItemKind::ForeignMod(foreign_module) => {
if let Some(abi) = foreign_module.abi {
self.check_abi(abi, ast::Const::No);
self.check_abi(abi);
}
}

Expand Down Expand Up @@ -341,7 +328,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
match &ty.kind {
ast::TyKind::BareFn(bare_fn_ty) => {
// Function pointers cannot be `const`
self.check_extern(bare_fn_ty.ext, ast::Const::No);
self.check_extern(bare_fn_ty.ext);
self.check_late_bound_lifetime_defs(&bare_fn_ty.generic_params);
}
ast::TyKind::Never => {
Expand Down Expand Up @@ -446,7 +433,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
if let Some(header) = fn_kind.header() {
// Stability of const fn methods are covered in `visit_assoc_item` below.
self.check_extern(header.ext, header.constness);
self.check_extern(header.ext);
}

if let FnKind::Closure(ast::ClosureBinder::For { generic_params, .. }, ..) = fn_kind {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/accepted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ declare_features! (
(accepted, conservative_impl_trait, "1.26.0", Some(34511)),
/// Allows calling constructor functions in `const fn`.
(accepted, const_constructor, "1.40.0", Some(61456)),
/// Allows the definition of `const extern fn` and `const unsafe extern fn`.
(accepted, const_extern_fn, "CURRENT_RUSTC_VERSION", Some(64926)),
/// Allows basic arithmetic on floating point types in a `const fn`.
(accepted, const_fn_floating_point_arithmetic, "1.82.0", Some(57241)),
/// Allows using and casting function pointers in a `const fn`.
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,6 @@ declare_features! (
(unstable, const_async_blocks, "1.53.0", Some(85368)),
/// Allows `const || {}` closures in const contexts.
(incomplete, const_closures, "1.68.0", Some(106003)),
/// Allows the definition of `const extern fn` and `const unsafe extern fn`.
(unstable, const_extern_fn, "1.40.0", Some(64926)),
/// Allows `for _ in _` loops in const contexts.
(unstable, const_for, "1.56.0", Some(87575)),
/// Allows using `&mut` in constant functions.
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_parse/src/parser/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,6 @@ impl<'a> Parser<'a> {
let decl = self.parse_fn_decl(|_| false, AllowPlus::No, recover_return_sign)?;
let whole_span = lo.to(self.prev_token.span);
if let ast::Const::Yes(span) = constness {
// If we ever start to allow `const fn()`, then update
// feature gating for `#![feature(const_extern_fn)]` to
// cover it.
self.dcx().emit_err(FnPointerCannotBeConst { span: whole_span, qualifier: span });
}
if let Some(ast::CoroutineKind::Async { span, .. }) = coroutine_kind {
Expand Down
3 changes: 2 additions & 1 deletion src/tools/clippy/clippy_config/src/msrvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ macro_rules! msrv_aliases {

// names may refer to stabilized feature flags or library items
msrv_aliases! {
1,83,0 { CONST_EXTERN_FN }
1,83,0 { CONST_FLOAT_BITS_CONV }
1,81,0 { LINT_REASONS_STABILIZATION }
1,80,0 { BOX_INTO_ITER}
Expand All @@ -27,7 +28,7 @@ msrv_aliases! {
1,68,0 { PATH_MAIN_SEPARATOR_STR }
1,65,0 { LET_ELSE, POINTER_CAST_CONSTNESS }
1,63,0 { CLONE_INTO }
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE, CONST_EXTERN_FN }
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE, CONST_EXTERN_C_FN }
1,59,0 { THREAD_LOCAL_CONST_INIT }
1,58,0 { FORMAT_ARGS_CAPTURE, PATTERN_TRAIT_CHAR_ARRAY, CONST_RAW_PTR_DEREF }
1,56,0 { CONST_FN_UNION }
Expand Down
10 changes: 4 additions & 6 deletions src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
.iter()
.any(|param| matches!(param.kind, GenericParamKind::Const { .. }));

if already_const(header)
|| has_const_generic_params
|| !could_be_const_with_abi(cx, &self.msrv, header.abi)
if already_const(header) || has_const_generic_params || !could_be_const_with_abi(&self.msrv, header.abi)
{
return;
}
Expand Down Expand Up @@ -183,13 +181,13 @@ fn already_const(header: hir::FnHeader) -> bool {
header.constness == Constness::Const
}

fn could_be_const_with_abi(cx: &LateContext<'_>, msrv: &Msrv, abi: Abi) -> bool {
fn could_be_const_with_abi(msrv: &Msrv, abi: Abi) -> bool {
match abi {
Abi::Rust => true,
// `const extern "C"` was stabilized after 1.62.0
Abi::C { unwind: false } => msrv.meets(msrvs::CONST_EXTERN_FN),
Abi::C { unwind: false } => msrv.meets(msrvs::CONST_EXTERN_C_FN),
// Rest ABIs are still unstable and need the `const_extern_fn` feature enabled.
_ => cx.tcx.features().const_extern_fn,
_ => msrv.meets(msrvs::CONST_EXTERN_FN),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,6 @@ mod msrv {
extern "C" fn c() {}
}

mod with_extern {
extern "C-unwind" fn c_unwind() {}
extern "system" fn system() {}
extern "system-unwind" fn system_unwind() {}
}

mod with_ty_alias {
type Foo = impl std::fmt::Debug;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![warn(clippy::missing_const_for_fn)]
#![allow(unsupported_calling_conventions)]
#![feature(const_extern_fn)]

const extern "C-unwind" fn c_unwind() {}
//~^ ERROR: this could be a `const fn`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![warn(clippy::missing_const_for_fn)]
#![allow(unsupported_calling_conventions)]
#![feature(const_extern_fn)]

extern "C-unwind" fn c_unwind() {}
//~^ ERROR: this could be a `const fn`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:5:1
--> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:4:1
|
LL | extern "C-unwind" fn c_unwind() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -12,7 +12,7 @@ LL | const extern "C-unwind" fn c_unwind() {}
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:7:1
--> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:6:1
|
LL | extern "system" fn system() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -23,7 +23,7 @@ LL | const extern "system" fn system() {}
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:9:1
--> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:8:1
|
LL | extern "system-unwind" fn system_unwind() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -34,7 +34,7 @@ LL | const extern "system-unwind" fn system_unwind() {}
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:11:1
--> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:10:1
|
LL | pub extern "stdcall" fn std_call() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -45,7 +45,7 @@ LL | pub const extern "stdcall" fn std_call() {}
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:13:1
--> tests/ui/missing_const_for_fn/could_be_const_with_const_extern_fn.rs:12:1
|
LL | pub extern "stdcall-unwind" fn std_call_unwind() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/consts/const-eval/unwind-abort.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(const_extern_fn)]

const extern "C" fn foo() {
panic!() //~ ERROR evaluation of constant value failed
}
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/consts/const-eval/unwind-abort.stderr
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
error[E0080]: evaluation of constant value failed
--> $DIR/unwind-abort.rs:4:5
--> $DIR/unwind-abort.rs:2:5
|
LL | panic!()
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/unwind-abort.rs:4:5
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/unwind-abort.rs:2:5
|
note: inside `foo`
--> $DIR/unwind-abort.rs:4:5
--> $DIR/unwind-abort.rs:2:5
|
LL | panic!()
| ^^^^^^^^
note: inside `_`
--> $DIR/unwind-abort.rs:7:15
--> $DIR/unwind-abort.rs:5:15
|
LL | const _: () = foo();
| ^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(const_extern_fn)]

extern "C" {
fn regular_in_block();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0015]: cannot call non-const fn `regular_in_block` in constant functions
--> $DIR/const-extern-fn-call-extern-fn.rs:9:9
--> $DIR/const-extern-fn-call-extern-fn.rs:7:9
|
LL | regular_in_block();
| ^^^^^^^^^^^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error[E0015]: cannot call non-const fn `regular` in constant functions
--> $DIR/const-extern-fn-call-extern-fn.rs:18:9
--> $DIR/const-extern-fn-call-extern-fn.rs:16:9
|
LL | regular();
| ^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![feature(const_extern_fn)]

const extern "C" fn ptr_cast(val: *const u8) { val as usize; }
//~^ ERROR pointers cannot be cast to integers

const extern "C" fn ptr_cast(val: *const u8) {
val as usize;
//~^ ERROR pointers cannot be cast to integers
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: pointers cannot be cast to integers during const eval
--> $DIR/const-extern-fn-min-const-fn.rs:3:48
--> $DIR/const-extern-fn-min-const-fn.rs:2:5
|
LL | const extern "C" fn ptr_cast(val: *const u8) { val as usize; }
| ^^^^^^^^^^^^
LL | val as usize;
| ^^^^^^^^^^^^
|
= note: at compile-time, pointers do not have an integer value
= note: avoiding this restriction via `transmute`, `union`, or raw pointers leads to compile-time undefined behavior
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#![feature(const_extern_fn)]

const unsafe extern "C" fn foo() -> usize {
5
}

const unsafe extern "C-unwind" fn bar() -> usize {
5
}

fn main() {
let a: [u8; foo()];
//~^ call to unsafe function `foo` is unsafe and requires unsafe function or block
foo();
//~^ ERROR call to unsafe function `foo` is unsafe and requires unsafe function or block
let b: [u8; bar()];
//~^ call to unsafe function `bar` is unsafe and requires unsafe function or block
bar();
//~^ ERROR call to unsafe function `bar` is unsafe and requires unsafe function or block
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
error[E0133]: call to unsafe function `foo` is unsafe and requires unsafe function or block
--> $DIR/const-extern-fn-requires-unsafe.rs:10:5
--> $DIR/const-extern-fn-requires-unsafe.rs:12:5
|
LL | foo();
| ^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior

error[E0133]: call to unsafe function `bar` is unsafe and requires unsafe function or block
--> $DIR/const-extern-fn-requires-unsafe.rs:16:5
|
LL | bar();
| ^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior

error[E0133]: call to unsafe function `foo` is unsafe and requires unsafe function or block
--> $DIR/const-extern-fn-requires-unsafe.rs:8:17
--> $DIR/const-extern-fn-requires-unsafe.rs:10:17
|
LL | let a: [u8; foo()];
| ^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior

error: aborting due to 2 previous errors
error[E0133]: call to unsafe function `bar` is unsafe and requires unsafe function or block
--> $DIR/const-extern-fn-requires-unsafe.rs:14:17
|
LL | let b: [u8; bar()];
| ^^^^^ call to unsafe function
|
= note: consult the function's documentation for information on how to avoid undefined behavior

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0133`.
9 changes: 6 additions & 3 deletions tests/ui/consts/const-extern-fn/const-extern-fn.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@ run-pass
#![feature(const_extern_fn)]

const extern "C" fn foo1(val: u8) -> u8 {
val + 1
Expand Down Expand Up @@ -47,6 +46,10 @@ fn main() {
let _bar2_cast: unsafe extern "C" fn(bool) -> bool = bar2;

unsize(&[0, 1, 2]);
unsafe { closure(); }
unsafe { use_float(); }
unsafe {
closure();
}
unsafe {
use_float();
}
}
13 changes: 0 additions & 13 deletions tests/ui/consts/const-extern-fn/feature-gate-const_extern_fn.rs

This file was deleted.

This file was deleted.

Loading

0 comments on commit 027a557

Please sign in to comment.