Skip to content

Commit a61c641

Browse files
committed
stabilize const_extern_fn
1 parent 6cf068d commit a61c641

20 files changed

+50
-112
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,9 @@ struct PostExpansionVisitor<'a> {
7575

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

81-
if let ast::Const::Yes(_) = constness {
82-
match symbol_unescaped {
83-
// Stable
84-
sym::Rust | sym::C => {}
85-
abi => gate!(
86-
&self,
87-
const_extern_fn,
88-
span,
89-
format!("`{}` as a `const fn` ABI is unstable", abi)
90-
),
91-
}
92-
}
93-
9481
match abi::is_enabled(self.features, span, symbol_unescaped.as_str()) {
9582
Ok(()) => (),
9683
Err(abi::AbiDisabled::Unstable { feature, explain }) => {
@@ -110,9 +97,9 @@ impl<'a> PostExpansionVisitor<'a> {
11097
}
11198
}
11299

113-
fn check_extern(&self, ext: ast::Extern, constness: ast::Const) {
100+
fn check_extern(&self, ext: ast::Extern) {
114101
if let ast::Extern::Explicit(abi, _) = ext {
115-
self.check_abi(abi, constness);
102+
self.check_abi(abi);
116103
}
117104
}
118105

@@ -239,7 +226,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
239226
match &i.kind {
240227
ast::ItemKind::ForeignMod(foreign_module) => {
241228
if let Some(abi) = foreign_module.abi {
242-
self.check_abi(abi, ast::Const::No);
229+
self.check_abi(abi);
243230
}
244231
}
245232

@@ -341,7 +328,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
341328
match &ty.kind {
342329
ast::TyKind::BareFn(bare_fn_ty) => {
343330
// Function pointers cannot be `const`
344-
self.check_extern(bare_fn_ty.ext, ast::Const::No);
331+
self.check_extern(bare_fn_ty.ext);
345332
self.check_late_bound_lifetime_defs(&bare_fn_ty.generic_params);
346333
}
347334
ast::TyKind::Never => {
@@ -446,7 +433,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
446433
fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
447434
if let Some(header) = fn_kind.header() {
448435
// Stability of const fn methods are covered in `visit_assoc_item` below.
449-
self.check_extern(header.ext, header.constness);
436+
self.check_extern(header.ext);
450437
}
451438

452439
if let FnKind::Closure(ast::ClosureBinder::For { generic_params, .. }, ..) = fn_kind {

compiler/rustc_feature/src/accepted.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ declare_features! (
115115
(accepted, conservative_impl_trait, "1.26.0", Some(34511)),
116116
/// Allows calling constructor functions in `const fn`.
117117
(accepted, const_constructor, "1.40.0", Some(61456)),
118+
/// Allows the definition of `const extern fn` and `const unsafe extern fn`.
119+
(accepted, const_extern_fn, "CURRENT_RUSTC_VERSION", Some(64926)),
118120
/// Allows basic arithmetic on floating point types in a `const fn`.
119121
(accepted, const_fn_floating_point_arithmetic, "CURRENT_RUSTC_VERSION", Some(57241)),
120122
/// Allows using and casting function pointers in a `const fn`.

compiler/rustc_feature/src/unstable.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,6 @@ declare_features! (
401401
(unstable, const_async_blocks, "1.53.0", Some(85368)),
402402
/// Allows `const || {}` closures in const contexts.
403403
(incomplete, const_closures, "1.68.0", Some(106003)),
404-
/// Allows the definition of `const extern fn` and `const unsafe extern fn`.
405-
(unstable, const_extern_fn, "1.40.0", Some(64926)),
406404
/// Allows `for _ in _` loops in const contexts.
407405
(unstable, const_for, "1.56.0", Some(87575)),
408406
/// Allows using `&mut` in constant functions.

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,9 +607,6 @@ impl<'a> Parser<'a> {
607607
let decl = self.parse_fn_decl(|_| false, AllowPlus::No, recover_return_sign)?;
608608
let whole_span = lo.to(self.prev_token.span);
609609
if let ast::Const::Yes(span) = constness {
610-
// If we ever start to allow `const fn()`, then update
611-
// feature gating for `#![feature(const_extern_fn)]` to
612-
// cover it.
613610
self.dcx().emit_err(FnPointerCannotBeConst { span: whole_span, qualifier: span });
614611
}
615612
if let Some(ast::CoroutineKind::Async { span, .. }) = coroutine_kind {

tests/ui/consts/const-eval/unwind-abort.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(const_extern_fn)]
2-
31
const extern "C" fn foo() {
42
panic!() //~ ERROR evaluation of constant value failed
53
}

tests/ui/consts/const-eval/unwind-abort.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
error[E0080]: evaluation of constant value failed
2-
--> $DIR/unwind-abort.rs:4:5
2+
--> $DIR/unwind-abort.rs:2:5
33
|
44
LL | panic!()
5-
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/unwind-abort.rs:4:5
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/unwind-abort.rs:2:5
66
|
77
note: inside `foo`
8-
--> $DIR/unwind-abort.rs:4:5
8+
--> $DIR/unwind-abort.rs:2:5
99
|
1010
LL | panic!()
1111
| ^^^^^^^^
1212
note: inside `_`
13-
--> $DIR/unwind-abort.rs:7:15
13+
--> $DIR/unwind-abort.rs:5:15
1414
|
1515
LL | const _: () = foo();
1616
| ^^^^^

tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(const_extern_fn)]
2-
31
extern "C" {
42
fn regular_in_block();
53
}

tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error[E0015]: cannot call non-const fn `regular_in_block` in constant functions
2-
--> $DIR/const-extern-fn-call-extern-fn.rs:9:9
2+
--> $DIR/const-extern-fn-call-extern-fn.rs:7:9
33
|
44
LL | regular_in_block();
55
| ^^^^^^^^^^^^^^^^^^
66
|
77
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
88

99
error[E0015]: cannot call non-const fn `regular` in constant functions
10-
--> $DIR/const-extern-fn-call-extern-fn.rs:18:9
10+
--> $DIR/const-extern-fn-call-extern-fn.rs:16:9
1111
|
1212
LL | regular();
1313
| ^^^^^^^^^
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#![feature(const_extern_fn)]
2-
3-
const extern "C" fn ptr_cast(val: *const u8) { val as usize; }
4-
//~^ ERROR pointers cannot be cast to integers
5-
1+
const extern "C" fn ptr_cast(val: *const u8) {
2+
val as usize;
3+
//~^ ERROR pointers cannot be cast to integers
4+
}
65

76
fn main() {}

tests/ui/consts/const-extern-fn/const-extern-fn-min-const-fn.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: pointers cannot be cast to integers during const eval
2-
--> $DIR/const-extern-fn-min-const-fn.rs:3:48
2+
--> $DIR/const-extern-fn-min-const-fn.rs:2:5
33
|
4-
LL | const extern "C" fn ptr_cast(val: *const u8) { val as usize; }
5-
| ^^^^^^^^^^^^
4+
LL | val as usize;
5+
| ^^^^^^^^^^^^
66
|
77
= note: at compile-time, pointers do not have an integer value
88
= note: avoiding this restriction via `transmute`, `union`, or raw pointers leads to compile-time undefined behavior

tests/ui/consts/const-extern-fn/const-extern-fn-requires-unsafe.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(const_extern_fn)]
2-
31
const unsafe extern "C" fn foo() -> usize {
42
5
53
}

tests/ui/consts/const-extern-fn/const-extern-fn-requires-unsafe.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error[E0133]: call to unsafe function `foo` is unsafe and requires unsafe function or block
2-
--> $DIR/const-extern-fn-requires-unsafe.rs:10:5
2+
--> $DIR/const-extern-fn-requires-unsafe.rs:8:5
33
|
44
LL | foo();
55
| ^^^^^ call to unsafe function
66
|
77
= note: consult the function's documentation for information on how to avoid undefined behavior
88

99
error[E0133]: call to unsafe function `foo` is unsafe and requires unsafe function or block
10-
--> $DIR/const-extern-fn-requires-unsafe.rs:8:17
10+
--> $DIR/const-extern-fn-requires-unsafe.rs:6:17
1111
|
1212
LL | let a: [u8; foo()];
1313
| ^^^^^ call to unsafe function

tests/ui/consts/const-extern-fn/const-extern-fn.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@ run-pass
2-
#![feature(const_extern_fn)]
32

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

4948
unsize(&[0, 1, 2]);
50-
unsafe { closure(); }
51-
unsafe { use_float(); }
49+
unsafe {
50+
closure();
51+
}
52+
unsafe {
53+
use_float();
54+
}
5255
}

tests/ui/consts/const-extern-fn/feature-gate-const_extern_fn.rs

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

tests/ui/consts/const-extern-fn/feature-gate-const_extern_fn.stderr

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

tests/ui/consts/miri_unleashed/abi-mismatch.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Checks that we report ABI mismatches for "const extern fn"
22
//@ compile-flags: -Z unleash-the-miri-inside-of-you
33

4-
#![feature(const_extern_fn)]
5-
64
const extern "C" fn c_fn() {}
75

86
const fn call_rust_fn(my_fn: extern "Rust" fn()) {

tests/ui/consts/miri_unleashed/abi-mismatch.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
error[E0080]: could not evaluate static initializer
2-
--> $DIR/abi-mismatch.rs:9:5
2+
--> $DIR/abi-mismatch.rs:7:5
33
|
44
LL | my_fn();
55
| ^^^^^^^ calling a function with calling convention C using calling convention Rust
66
|
77
note: inside `call_rust_fn`
8-
--> $DIR/abi-mismatch.rs:9:5
8+
--> $DIR/abi-mismatch.rs:7:5
99
|
1010
LL | my_fn();
1111
| ^^^^^^^
1212
note: inside `VAL`
13-
--> $DIR/abi-mismatch.rs:15:18
13+
--> $DIR/abi-mismatch.rs:13:18
1414
|
1515
LL | static VAL: () = call_rust_fn(unsafe { std::mem::transmute(c_fn as extern "C" fn()) });
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1717

1818
warning: skipping const checks
1919
|
2020
help: skipping check that does not even have a feature gate
21-
--> $DIR/abi-mismatch.rs:9:5
21+
--> $DIR/abi-mismatch.rs:7:5
2222
|
2323
LL | my_fn();
2424
| ^^^^^^^

tests/ui/consts/unwind-abort.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//@ check-pass
22

3-
#![feature(const_extern_fn)]
4-
53
// We don't unwind in const-eval anyways.
64
const extern "C" fn foo() {
75
panic!()

tests/ui/parser/fn-header-semantic-fail.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
//@ edition:2018
44

5-
#![feature(const_extern_fn)]
6-
75
fn main() {
86
async fn ff1() {} // OK.
97
unsafe fn ff2() {} // OK.

0 commit comments

Comments
 (0)