Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7723506

Browse files
committed
Stabilize const_fn_fn_ptr_basics and const_fn_trait_bound
1 parent d137c3a commit 7723506

File tree

9 files changed

+16
-157
lines changed

9 files changed

+16
-157
lines changed

compiler/rustc_const_eval/src/transform/check_consts/check.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
359359
match *ty.kind() {
360360
ty::Ref(_, _, hir::Mutability::Mut) => self.check_op(ops::ty::MutRef(kind)),
361361
ty::Opaque(..) => self.check_op(ops::ty::ImplTrait),
362-
ty::FnPtr(..) => self.check_op(ops::ty::FnPtr(kind)),
363362

364363
ty::Dynamic(preds, _) => {
365364
for pred in preds.iter() {
@@ -395,6 +394,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
395394
| ty::PredicateKind::Projection(_)
396395
| ty::PredicateKind::ConstEvaluatable(..)
397396
| ty::PredicateKind::ConstEquate(..)
397+
| ty::PredicateKind::Trait(..)
398398
| ty::PredicateKind::TypeWellFormedFromEnv(..) => continue,
399399
ty::PredicateKind::ObjectSafe(_) => {
400400
bug!("object safe predicate on function: {:#?}", predicate)
@@ -405,27 +405,6 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
405405
ty::PredicateKind::Subtype(_) | ty::PredicateKind::Coerce(_) => {
406406
bug!("subtype/coerce predicate on function: {:#?}", predicate)
407407
}
408-
ty::PredicateKind::Trait(pred) => {
409-
if Some(pred.def_id()) == tcx.lang_items().sized_trait() {
410-
continue;
411-
}
412-
match pred.self_ty().kind() {
413-
ty::Param(p) => {
414-
let generics = tcx.generics_of(current);
415-
let def = generics.type_param(p, tcx);
416-
let span = tcx.def_span(def.def_id);
417-
418-
// These are part of the function signature, so treat them like
419-
// arguments when determining importance.
420-
let kind = LocalKind::Arg;
421-
422-
self.check_op_spanned(ops::ty::TraitBound(kind), span);
423-
}
424-
// other kinds of bounds are either tautologies
425-
// or cause errors in other passes
426-
_ => continue,
427-
}
428-
}
429408
}
430409
}
431410
match predicates.parent {
@@ -613,7 +592,9 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
613592
),
614593
_,
615594
_,
616-
) => self.check_op(ops::FnPtrCast),
595+
) => {
596+
// Nothing to do here. Function pointer casts are allowed now.
597+
}
617598

618599
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), _, _) => {
619600
// Nothing to check here (`check_local_or_return_ty` ensures no trait objects occur

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -355,31 +355,6 @@ impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
355355
}
356356
}
357357

358-
#[derive(Debug)]
359-
pub struct FnPtrCast;
360-
impl<'tcx> NonConstOp<'tcx> for FnPtrCast {
361-
fn status_in_item(&self, ccx: &ConstCx<'_, 'tcx>) -> Status {
362-
if ccx.const_kind() != hir::ConstContext::ConstFn {
363-
Status::Allowed
364-
} else {
365-
Status::Unstable(sym::const_fn_fn_ptr_basics)
366-
}
367-
}
368-
369-
fn build_error(
370-
&self,
371-
ccx: &ConstCx<'_, 'tcx>,
372-
span: Span,
373-
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
374-
feature_err(
375-
&ccx.tcx.sess.parse_sess,
376-
sym::const_fn_fn_ptr_basics,
377-
span,
378-
&format!("function pointer casts are not allowed in {}s", ccx.const_kind()),
379-
)
380-
}
381-
}
382-
383358
#[derive(Debug)]
384359
pub struct Generator(pub hir::GeneratorKind);
385360
impl<'tcx> NonConstOp<'tcx> for Generator {
@@ -821,40 +796,6 @@ pub mod ty {
821796
}
822797
}
823798

824-
#[derive(Debug)]
825-
pub struct FnPtr(pub mir::LocalKind);
826-
impl<'tcx> NonConstOp<'tcx> for FnPtr {
827-
fn importance(&self) -> DiagnosticImportance {
828-
match self.0 {
829-
mir::LocalKind::Var | mir::LocalKind::Temp => DiagnosticImportance::Secondary,
830-
mir::LocalKind::ReturnPointer | mir::LocalKind::Arg => {
831-
DiagnosticImportance::Primary
832-
}
833-
}
834-
}
835-
836-
fn status_in_item(&self, ccx: &ConstCx<'_, 'tcx>) -> Status {
837-
if ccx.const_kind() != hir::ConstContext::ConstFn {
838-
Status::Allowed
839-
} else {
840-
Status::Unstable(sym::const_fn_fn_ptr_basics)
841-
}
842-
}
843-
844-
fn build_error(
845-
&self,
846-
ccx: &ConstCx<'_, 'tcx>,
847-
span: Span,
848-
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
849-
feature_err(
850-
&ccx.tcx.sess.parse_sess,
851-
sym::const_fn_fn_ptr_basics,
852-
span,
853-
&format!("function pointers cannot appear in {}s", ccx.const_kind()),
854-
)
855-
}
856-
}
857-
858799
#[derive(Debug)]
859800
pub struct ImplTrait;
860801
impl<'tcx> NonConstOp<'tcx> for ImplTrait {
@@ -876,49 +817,6 @@ pub mod ty {
876817
}
877818
}
878819

879-
#[derive(Debug)]
880-
pub struct TraitBound(pub mir::LocalKind);
881-
impl<'tcx> NonConstOp<'tcx> for TraitBound {
882-
fn importance(&self) -> DiagnosticImportance {
883-
match self.0 {
884-
mir::LocalKind::Var | mir::LocalKind::Temp => DiagnosticImportance::Secondary,
885-
mir::LocalKind::ReturnPointer | mir::LocalKind::Arg => {
886-
DiagnosticImportance::Primary
887-
}
888-
}
889-
}
890-
891-
fn status_in_item(&self, ccx: &ConstCx<'_, 'tcx>) -> Status {
892-
if ccx.const_kind() != hir::ConstContext::ConstFn {
893-
Status::Allowed
894-
} else {
895-
Status::Unstable(sym::const_fn_trait_bound)
896-
}
897-
}
898-
899-
fn build_error(
900-
&self,
901-
ccx: &ConstCx<'_, 'tcx>,
902-
span: Span,
903-
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
904-
let mut err = feature_err(
905-
&ccx.tcx.sess.parse_sess,
906-
sym::const_fn_trait_bound,
907-
span,
908-
"trait bounds other than `Sized` on const fn parameters are unstable",
909-
);
910-
911-
match ccx.fn_sig() {
912-
Some(fn_sig) if !fn_sig.span.contains(span) => {
913-
err.span_label(fn_sig.span, "function declared as const here");
914-
}
915-
_ => {}
916-
}
917-
918-
err
919-
}
920-
}
921-
922820
#[derive(Debug)]
923821
pub struct DynTrait(pub mir::LocalKind);
924822
impl<'tcx> NonConstOp<'tcx> for DynTrait {

compiler/rustc_feature/src/accepted.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ declare_features! (
8686
(accepted, conservative_impl_trait, "1.26.0", Some(34511), None),
8787
/// Allows calling constructor functions in `const fn`.
8888
(accepted, const_constructor, "1.40.0", Some(61456), None),
89+
/// Allows using and casting function pointers in a `const fn`.
90+
(accepted, const_fn_fn_ptr_basics, "1.60.0", Some(57563), None),
91+
/// Allows trait bounds in `const fn`.
92+
(accepted, const_fn_trait_bound, "1.60.0", Some(93706), None),
8993
/// Allows calling `transmute` in const fn
9094
(accepted, const_fn_transmute, "1.56.0", Some(53605), None),
9195
/// Allows accessing fields of unions inside `const` functions.

compiler/rustc_feature/src/active.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,6 @@ declare_features! (
338338
(active, const_extern_fn, "1.40.0", Some(64926), None),
339339
/// Allows basic arithmetic on floating point types in a `const fn`.
340340
(active, const_fn_floating_point_arithmetic, "1.48.0", Some(57241), None),
341-
/// Allows using and casting function pointers in a `const fn`.
342-
(active, const_fn_fn_ptr_basics, "1.48.0", Some(57563), None),
343-
/// Allows trait bounds in `const fn`.
344-
(active, const_fn_trait_bound, "1.53.0", Some(93706), None),
345341
/// Allows `for _ in _` loops in const contexts.
346342
(active, const_for, "1.56.0", Some(87575), None),
347343
/// Allows argument and return position `impl Trait` in a `const fn`.

library/alloc/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
#![feature(box_syntax)]
141141
#![feature(cfg_sanitize)]
142142
#![feature(const_deref)]
143-
#![feature(const_fn_trait_bound)]
143+
#![cfg_attr(bootstrap, feature(const_fn_trait_bound))]
144144
#![feature(const_mut_refs)]
145145
#![feature(const_ptr_write)]
146146
#![feature(const_precise_live_drops)]

library/core/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@
158158
#![feature(cfg_target_has_atomic)]
159159
#![feature(cfg_target_has_atomic_equal_alignment)]
160160
#![feature(const_fn_floating_point_arithmetic)]
161-
#![feature(const_fn_fn_ptr_basics)]
162-
#![feature(const_fn_trait_bound)]
161+
#![cfg_attr(bootstrap, feature(const_fn_fn_ptr_basics))]
162+
#![cfg_attr(bootstrap, feature(const_fn_trait_bound))]
163163
#![feature(const_impl_trait)]
164164
#![feature(const_mut_refs)]
165165
#![feature(const_precise_live_drops)]

library/proc_macro/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#![feature(rustc_allow_const_fn_unstable)]
2121
#![feature(nll)]
2222
#![feature(staged_api)]
23-
#![feature(const_fn_trait_bound)]
24-
#![feature(const_fn_fn_ptr_basics)]
23+
#![cfg_attr(bootstrap, feature(const_fn_trait_bound))]
24+
#![cfg_attr(bootstrap, feature(const_fn_fn_ptr_basics))]
2525
#![feature(allow_internal_unstable)]
2626
#![feature(decl_macro)]
2727
#![feature(extern_types)]

library/std/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@
242242
#![feature(char_internals)]
243243
#![feature(concat_bytes)]
244244
#![feature(concat_idents)]
245-
#![feature(const_fn_fn_ptr_basics)]
246-
#![feature(const_fn_trait_bound)]
245+
#![cfg_attr(bootstrap, feature(const_fn_fn_ptr_basics))]
246+
#![cfg_attr(bootstrap, feature(const_fn_trait_bound))]
247247
#![feature(const_format_args)]
248248
#![feature(const_io_structs)]
249249
#![feature(const_ip)]

src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,12 @@ pub fn is_min_const_fn<'a, 'tcx>(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, msrv:
3232
| ty::PredicateKind::Projection(_)
3333
| ty::PredicateKind::ConstEvaluatable(..)
3434
| ty::PredicateKind::ConstEquate(..)
35+
| ty::PredicateKind::Trait(..)
3536
| ty::PredicateKind::TypeWellFormedFromEnv(..) => continue,
3637
ty::PredicateKind::ObjectSafe(_) => panic!("object safe predicate on function: {:#?}", predicate),
3738
ty::PredicateKind::ClosureKind(..) => panic!("closure kind predicate on function: {:#?}", predicate),
3839
ty::PredicateKind::Subtype(_) => panic!("subtype predicate on function: {:#?}", predicate),
3940
ty::PredicateKind::Coerce(_) => panic!("coerce predicate on function: {:#?}", predicate),
40-
ty::PredicateKind::Trait(pred) => {
41-
if Some(pred.def_id()) == tcx.lang_items().sized_trait() {
42-
continue;
43-
}
44-
match pred.self_ty().kind() {
45-
ty::Param(ref p) => {
46-
let generics = tcx.generics_of(current);
47-
let def = generics.type_param(p, tcx);
48-
let span = tcx.def_span(def.def_id);
49-
return Err((
50-
span,
51-
"trait bounds other than `Sized` \
52-
on const fn parameters are unstable"
53-
.into(),
54-
));
55-
},
56-
// other kinds of bounds are either tautologies
57-
// or cause errors in other passes
58-
_ => continue,
59-
}
60-
},
6141
}
6242
}
6343
match predicates.parent {

0 commit comments

Comments
 (0)