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 7cccef8 commit b5ea5c2
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 122 deletions.
3 changes: 2 additions & 1 deletion 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 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
6 changes: 0 additions & 6 deletions tests/ui/missing_const_for_fn/cant_be_const.rs
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
14 changes: 14 additions & 0 deletions tests/ui/missing_const_for_fn/could_be_const.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![warn(clippy::missing_const_for_fn)]
#![allow(incomplete_features, clippy::let_and_return, clippy::missing_transmute_annotations)]
#![allow(unsupported_calling_conventions)]
#![feature(const_mut_refs)]
#![feature(const_trait_impl)]

Expand Down Expand Up @@ -204,3 +205,16 @@ mod with_ty_alias {
// in this test.
const fn alias_ty_is_projection(bar: <() as FooTrait>::Foo) {}
}

mod extern_fn {
const extern "C-unwind" fn c_unwind() {}
//~^ ERROR: this could be a `const fn`
const extern "system" fn system() {}
//~^ ERROR: this could be a `const fn`
const extern "system-unwind" fn system_unwind() {}
//~^ ERROR: this could be a `const fn`
pub const extern "stdcall" fn std_call() {}
//~^ ERROR: this could be a `const fn`
pub const extern "stdcall-unwind" fn std_call_unwind() {}
//~^ ERROR: this could be a `const fn`
}
14 changes: 14 additions & 0 deletions tests/ui/missing_const_for_fn/could_be_const.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![warn(clippy::missing_const_for_fn)]
#![allow(incomplete_features, clippy::let_and_return, clippy::missing_transmute_annotations)]
#![allow(unsupported_calling_conventions)]
#![feature(const_mut_refs)]
#![feature(const_trait_impl)]

Expand Down Expand Up @@ -204,3 +205,16 @@ mod with_ty_alias {
// in this test.
fn alias_ty_is_projection(bar: <() as FooTrait>::Foo) {}
}

mod extern_fn {
extern "C-unwind" fn c_unwind() {}
//~^ ERROR: this could be a `const fn`
extern "system" fn system() {}
//~^ ERROR: this could be a `const fn`
extern "system-unwind" fn system_unwind() {}
//~^ ERROR: this could be a `const fn`
pub extern "stdcall" fn std_call() {}
//~^ ERROR: this could be a `const fn`
pub extern "stdcall-unwind" fn std_call_unwind() {}
//~^ ERROR: this could be a `const fn`
}
99 changes: 77 additions & 22 deletions tests/ui/missing_const_for_fn/could_be_const.stderr
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.rs:14:5
--> tests/ui/missing_const_for_fn/could_be_const.rs:15:5
|
LL | / pub fn new() -> Self {
LL | |
Expand All @@ -16,7 +16,7 @@ LL | pub const fn new() -> Self {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:20:5
--> tests/ui/missing_const_for_fn/could_be_const.rs:21:5
|
LL | / fn const_generic_params<'a, T, const N: usize>(&self, b: &'a [T; N]) -> &'a [T; N] {
LL | |
Expand All @@ -30,7 +30,7 @@ LL | const fn const_generic_params<'a, T, const N: usize>(&self, b: &'a [T;
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:27:1
--> tests/ui/missing_const_for_fn/could_be_const.rs:28:1
|
LL | / fn one() -> i32 {
LL | |
Expand All @@ -44,7 +44,7 @@ LL | const fn one() -> i32 {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:33:1
--> tests/ui/missing_const_for_fn/could_be_const.rs:34:1
|
LL | / fn two() -> i32 {
LL | |
Expand All @@ -59,7 +59,7 @@ LL | const fn two() -> i32 {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:40:1
--> tests/ui/missing_const_for_fn/could_be_const.rs:41:1
|
LL | / fn string() -> String {
LL | |
Expand All @@ -73,7 +73,7 @@ LL | const fn string() -> String {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:46:1
--> tests/ui/missing_const_for_fn/could_be_const.rs:47:1
|
LL | / unsafe fn four() -> i32 {
LL | |
Expand All @@ -87,7 +87,7 @@ LL | const unsafe fn four() -> i32 {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:52:1
--> tests/ui/missing_const_for_fn/could_be_const.rs:53:1
|
LL | / fn generic<T>(t: T) -> T {
LL | |
Expand All @@ -101,7 +101,7 @@ LL | const fn generic<T>(t: T) -> T {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:61:1
--> tests/ui/missing_const_for_fn/could_be_const.rs:62:1
|
LL | / fn generic_arr<T: Copy>(t: [T; 1]) -> T {
LL | |
Expand All @@ -115,7 +115,7 @@ LL | const fn generic_arr<T: Copy>(t: [T; 1]) -> T {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:75:9
--> tests/ui/missing_const_for_fn/could_be_const.rs:76:9
|
LL | / pub fn b(self, a: &A) -> B {
LL | |
Expand All @@ -129,7 +129,7 @@ LL | pub const fn b(self, a: &A) -> B {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:85:5
--> tests/ui/missing_const_for_fn/could_be_const.rs:86:5
|
LL | / fn const_fn_stabilized_before_msrv(byte: u8) {
LL | |
Expand All @@ -143,7 +143,7 @@ LL | const fn const_fn_stabilized_before_msrv(byte: u8) {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:97:1
--> tests/ui/missing_const_for_fn/could_be_const.rs:98:1
|
LL | / fn msrv_1_46() -> i32 {
LL | |
Expand All @@ -157,7 +157,7 @@ LL | const fn msrv_1_46() -> i32 {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:117:1
--> tests/ui/missing_const_for_fn/could_be_const.rs:118:1
|
LL | fn d(this: D) {}
| ^^^^^^^^^^^^^^^^
Expand All @@ -168,7 +168,7 @@ LL | const fn d(this: D) {}
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:125:9
--> tests/ui/missing_const_for_fn/could_be_const.rs:126:9
|
LL | / fn deref_ptr_can_be_const(self) -> usize {
LL | |
Expand All @@ -182,7 +182,7 @@ LL | const fn deref_ptr_can_be_const(self) -> usize {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:130:9
--> tests/ui/missing_const_for_fn/could_be_const.rs:131:9
|
LL | / fn deref_copied_val(self) -> usize {
LL | |
Expand All @@ -196,7 +196,7 @@ LL | const fn deref_copied_val(self) -> usize {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:141:5
--> tests/ui/missing_const_for_fn/could_be_const.rs:142:5
|
LL | / fn union_access_can_be_const() {
LL | |
Expand All @@ -211,7 +211,7 @@ LL | const fn union_access_can_be_const() {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:149:9
--> tests/ui/missing_const_for_fn/could_be_const.rs:150:9
|
LL | extern "C" fn c() {}
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -222,7 +222,7 @@ LL | const extern "C" fn c() {}
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:153:9
--> tests/ui/missing_const_for_fn/could_be_const.rs:154:9
|
LL | extern fn implicit_c() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -233,7 +233,7 @@ LL | const extern fn implicit_c() {}
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:170:9
--> tests/ui/missing_const_for_fn/could_be_const.rs:171:9
|
LL | / pub fn new(strings: Vec<String>) -> Self {
LL | | Self { strings }
Expand All @@ -246,7 +246,7 @@ LL | pub const fn new(strings: Vec<String>) -> Self {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:175:9
--> tests/ui/missing_const_for_fn/could_be_const.rs:176:9
|
LL | / pub fn empty() -> Self {
LL | | Self { strings: Vec::new() }
Expand All @@ -259,7 +259,7 @@ LL | pub const fn empty() -> Self {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:186:9
--> tests/ui/missing_const_for_fn/could_be_const.rs:187:9
|
LL | / pub fn new(text: String) -> Self {
LL | | let vec = Vec::new();
Expand All @@ -273,7 +273,7 @@ LL | pub const fn new(text: String) -> Self {
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:205:5
--> tests/ui/missing_const_for_fn/could_be_const.rs:206:5
|
LL | fn alias_ty_is_projection(bar: <() as FooTrait>::Foo) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -283,5 +283,60 @@ help: make the function `const`
LL | const fn alias_ty_is_projection(bar: <() as FooTrait>::Foo) {}
| +++++

error: aborting due to 21 previous errors
error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:210:5
|
LL | extern "C-unwind" fn c_unwind() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: make the function `const`
|
LL | const extern "C-unwind" fn c_unwind() {}
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:212:5
|
LL | extern "system" fn system() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: make the function `const`
|
LL | const extern "system" fn system() {}
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:214:5
|
LL | extern "system-unwind" fn system_unwind() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: make the function `const`
|
LL | const extern "system-unwind" fn system_unwind() {}
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:216:5
|
LL | pub extern "stdcall" fn std_call() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: make the function `const`
|
LL | pub const extern "stdcall" fn std_call() {}
| +++++

error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:218:5
|
LL | pub extern "stdcall-unwind" fn std_call_unwind() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: make the function `const`
|
LL | pub const extern "stdcall-unwind" fn std_call_unwind() {}
| +++++

error: aborting due to 26 previous errors

This file was deleted.

This file was deleted.

Loading

0 comments on commit b5ea5c2

Please sign in to comment.