Skip to content

Commit e474908

Browse files
committed
stabilize const_fn_floating_point_arithmetic
1 parent eefd2ea commit e474908

File tree

9 files changed

+15
-134
lines changed

9 files changed

+15
-134
lines changed

compiler/rustc_const_eval/src/check_consts/check.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
574574

575575
Rvalue::UnaryOp(_, operand) => {
576576
let ty = operand.ty(self.body, self.tcx);
577-
if is_int_bool_or_char(ty) {
578-
// Int, bool, and char operations are fine.
579-
} else if ty.is_floating_point() {
580-
self.check_op(ops::FloatingPointOp);
577+
if is_int_bool_float_or_char(ty) {
578+
// Int, bool, float, and char operations are fine.
581579
} else {
582580
span_bug!(self.span, "non-primitive type in `Rvalue::UnaryOp`: {:?}", ty);
583581
}
@@ -587,8 +585,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
587585
let lhs_ty = lhs.ty(self.body, self.tcx);
588586
let rhs_ty = rhs.ty(self.body, self.tcx);
589587

590-
if is_int_bool_or_char(lhs_ty) && is_int_bool_or_char(rhs_ty) {
591-
// Int, bool, and char operations are fine.
588+
if is_int_bool_float_or_char(lhs_ty) && is_int_bool_float_or_char(rhs_ty) {
589+
// Int, bool, float, and char operations are fine.
592590
} else if lhs_ty.is_fn_ptr() || lhs_ty.is_unsafe_ptr() {
593591
assert!(matches!(
594592
op,
@@ -602,8 +600,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
602600
));
603601

604602
self.check_op(ops::RawPtrComparison);
605-
} else if lhs_ty.is_floating_point() || rhs_ty.is_floating_point() {
606-
self.check_op(ops::FloatingPointOp);
607603
} else {
608604
span_bug!(
609605
self.span,
@@ -1008,8 +1004,8 @@ fn place_as_reborrow<'tcx>(
10081004
}
10091005
}
10101006

1011-
fn is_int_bool_or_char(ty: Ty<'_>) -> bool {
1012-
ty.is_bool() || ty.is_integral() || ty.is_char()
1007+
fn is_int_bool_float_or_char(ty: Ty<'_>) -> bool {
1008+
ty.is_bool() || ty.is_integral() || ty.is_char() || ty.is_floating_point()
10131009
}
10141010

10151011
fn emit_unstable_in_stable_error(ccx: &ConstCx<'_, '_>, span: Span, gate: Symbol) {

compiler/rustc_const_eval/src/check_consts/ops.rs

+6-38
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,6 @@ pub trait NonConstOp<'tcx>: std::fmt::Debug {
5555
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx>;
5656
}
5757

58-
#[derive(Debug)]
59-
pub struct FloatingPointOp;
60-
impl<'tcx> NonConstOp<'tcx> for FloatingPointOp {
61-
fn status_in_item(&self, ccx: &ConstCx<'_, 'tcx>) -> Status {
62-
if ccx.const_kind() == hir::ConstContext::ConstFn {
63-
Status::Unstable(sym::const_fn_floating_point_arithmetic)
64-
} else {
65-
Status::Allowed
66-
}
67-
}
68-
69-
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
70-
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
71-
feature_err(
72-
&ccx.tcx.sess,
73-
sym::const_fn_floating_point_arithmetic,
74-
span,
75-
format!("floating point arithmetic is not allowed in {}s", ccx.const_kind()),
76-
)
77-
}
78-
}
79-
8058
/// A function call where the callee is a pointer.
8159
#[derive(Debug)]
8260
pub struct FnCallIndirect;
@@ -440,22 +418,12 @@ impl<'tcx> NonConstOp<'tcx> for CellBorrow {
440418
DiagImportance::Secondary
441419
}
442420
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> {
443-
// FIXME: Maybe a more elegant solution to this if else case
444-
if let hir::ConstContext::Static(_) = ccx.const_kind() {
445-
ccx.dcx().create_err(errors::InteriorMutableDataRefer {
446-
span,
447-
opt_help: Some(()),
448-
kind: ccx.const_kind(),
449-
teach: ccx.tcx.sess.teach(E0492).then_some(()),
450-
})
451-
} else {
452-
ccx.dcx().create_err(errors::InteriorMutableDataRefer {
453-
span,
454-
opt_help: None,
455-
kind: ccx.const_kind(),
456-
teach: ccx.tcx.sess.teach(E0492).then_some(()),
457-
})
458-
}
421+
ccx.dcx().create_err(errors::InteriorMutableDataRefer {
422+
span,
423+
opt_help: matches!(ccx.const_kind(), hir::ConstContext::Static(_)).then_some(()),
424+
kind: ccx.const_kind(),
425+
teach: ccx.tcx.sess.teach(E0492).then_some(()),
426+
})
459427
}
460428
}
461429

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ declare_features! (
113113
(accepted, conservative_impl_trait, "1.26.0", Some(34511)),
114114
/// Allows calling constructor functions in `const fn`.
115115
(accepted, const_constructor, "1.40.0", Some(61456)),
116+
/// Allows basic arithmetic on floating point types in a `const fn`.
117+
(accepted, const_fn_floating_point_arithmetic, "CURRENT_RUSTC_VERSION", Some(57241)),
116118
/// Allows using and casting function pointers in a `const fn`.
117119
(accepted, const_fn_fn_ptr_basics, "1.61.0", Some(57563)),
118120
/// Allows trait bounds in `const fn`.

compiler/rustc_feature/src/unstable.rs

-2
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,6 @@ declare_features! (
402402
(incomplete, const_closures, "1.68.0", Some(106003)),
403403
/// Allows the definition of `const extern fn` and `const unsafe extern fn`.
404404
(unstable, const_extern_fn, "1.40.0", Some(64926)),
405-
/// Allows basic arithmetic on floating point types in a `const fn`.
406-
(unstable, const_fn_floating_point_arithmetic, "1.48.0", Some(57241)),
407405
/// Allows `for _ in _` loops in const contexts.
408406
(unstable, const_for, "1.56.0", Some(87575)),
409407
/// Allows using `&mut` in constant functions.

library/core/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
//
193193
// Language features:
194194
// tidy-alphabetical-start
195+
#![cfg_attr(bootstrap, feature(const_fn_floating_point_arithmetic))]
195196
#![feature(abi_unadjusted)]
196197
#![feature(adt_const_params)]
197198
#![feature(allow_internal_unsafe)]
@@ -201,7 +202,6 @@
201202
#![feature(cfg_sanitize)]
202203
#![feature(cfg_target_has_atomic)]
203204
#![feature(cfg_target_has_atomic_equal_alignment)]
204-
#![feature(const_fn_floating_point_arithmetic)]
205205
#![feature(const_for)]
206206
#![feature(const_mut_refs)]
207207
#![feature(const_precise_live_drops)]

tests/ui/consts/const_fn_floating_point_arithmetic.gated.stderr

-8
This file was deleted.

tests/ui/consts/const_fn_floating_point_arithmetic.rs

-20
This file was deleted.

tests/ui/consts/const_fn_floating_point_arithmetic.stock.stderr

-53
This file was deleted.

tests/ui/consts/const_let_eq_float.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//@ run-pass
22

3-
#![feature(const_fn_floating_point_arithmetic)]
4-
53
struct Foo<T>(T);
64
struct Bar<T> { x: T }
75
struct W(f32);

0 commit comments

Comments
 (0)