diff --git a/compiler/rustc_const_eval/src/check_consts/check.rs b/compiler/rustc_const_eval/src/check_consts/check.rs index 3ded81b90ffc1..4232c241ad48c 100644 --- a/compiler/rustc_const_eval/src/check_consts/check.rs +++ b/compiler/rustc_const_eval/src/check_consts/check.rs @@ -574,10 +574,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { Rvalue::UnaryOp(_, operand) => { let ty = operand.ty(self.body, self.tcx); - if is_int_bool_or_char(ty) { - // Int, bool, and char operations are fine. - } else if ty.is_floating_point() { - self.check_op(ops::FloatingPointOp); + if is_int_bool_float_or_char(ty) { + // Int, bool, float, and char operations are fine. } else { span_bug!(self.span, "non-primitive type in `Rvalue::UnaryOp`: {:?}", ty); } @@ -587,8 +585,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { let lhs_ty = lhs.ty(self.body, self.tcx); let rhs_ty = rhs.ty(self.body, self.tcx); - if is_int_bool_or_char(lhs_ty) && is_int_bool_or_char(rhs_ty) { - // Int, bool, and char operations are fine. + if is_int_bool_float_or_char(lhs_ty) && is_int_bool_float_or_char(rhs_ty) { + // Int, bool, float, and char operations are fine. } else if lhs_ty.is_fn_ptr() || lhs_ty.is_unsafe_ptr() { assert!(matches!( op, @@ -602,8 +600,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { )); self.check_op(ops::RawPtrComparison); - } else if lhs_ty.is_floating_point() || rhs_ty.is_floating_point() { - self.check_op(ops::FloatingPointOp); } else { span_bug!( self.span, @@ -1008,8 +1004,8 @@ fn place_as_reborrow<'tcx>( } } -fn is_int_bool_or_char(ty: Ty<'_>) -> bool { - ty.is_bool() || ty.is_integral() || ty.is_char() +fn is_int_bool_float_or_char(ty: Ty<'_>) -> bool { + ty.is_bool() || ty.is_integral() || ty.is_char() || ty.is_floating_point() } fn emit_unstable_in_stable_error(ccx: &ConstCx<'_, '_>, span: Span, gate: Symbol) { diff --git a/compiler/rustc_const_eval/src/check_consts/ops.rs b/compiler/rustc_const_eval/src/check_consts/ops.rs index f47a2ec8f7598..49bacdb20feb1 100644 --- a/compiler/rustc_const_eval/src/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/check_consts/ops.rs @@ -55,28 +55,6 @@ pub trait NonConstOp<'tcx>: std::fmt::Debug { fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx>; } -#[derive(Debug)] -pub struct FloatingPointOp; -impl<'tcx> NonConstOp<'tcx> for FloatingPointOp { - fn status_in_item(&self, ccx: &ConstCx<'_, 'tcx>) -> Status { - if ccx.const_kind() == hir::ConstContext::ConstFn { - Status::Unstable(sym::const_fn_floating_point_arithmetic) - } else { - Status::Allowed - } - } - - #[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable - fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> { - feature_err( - &ccx.tcx.sess, - sym::const_fn_floating_point_arithmetic, - span, - format!("floating point arithmetic is not allowed in {}s", ccx.const_kind()), - ) - } -} - /// A function call where the callee is a pointer. #[derive(Debug)] pub struct FnCallIndirect; @@ -440,22 +418,12 @@ impl<'tcx> NonConstOp<'tcx> for CellBorrow { DiagImportance::Secondary } fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> Diag<'tcx> { - // FIXME: Maybe a more elegant solution to this if else case - if let hir::ConstContext::Static(_) = ccx.const_kind() { - ccx.dcx().create_err(errors::InteriorMutableDataRefer { - span, - opt_help: Some(()), - kind: ccx.const_kind(), - teach: ccx.tcx.sess.teach(E0492).then_some(()), - }) - } else { - ccx.dcx().create_err(errors::InteriorMutableDataRefer { - span, - opt_help: None, - kind: ccx.const_kind(), - teach: ccx.tcx.sess.teach(E0492).then_some(()), - }) - } + ccx.dcx().create_err(errors::InteriorMutableDataRefer { + span, + opt_help: matches!(ccx.const_kind(), hir::ConstContext::Static(_)).then_some(()), + kind: ccx.const_kind(), + teach: ccx.tcx.sess.teach(E0492).then_some(()), + }) } } diff --git a/compiler/rustc_feature/src/accepted.rs b/compiler/rustc_feature/src/accepted.rs index 9684fe105107e..d329b8600ae84 100644 --- a/compiler/rustc_feature/src/accepted.rs +++ b/compiler/rustc_feature/src/accepted.rs @@ -113,6 +113,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 basic arithmetic on floating point types in a `const fn`. + (accepted, const_fn_floating_point_arithmetic, "CURRENT_RUSTC_VERSION", Some(57241)), /// Allows using and casting function pointers in a `const fn`. (accepted, const_fn_fn_ptr_basics, "1.61.0", Some(57563)), /// Allows trait bounds in `const fn`. diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs index 03210085a12c9..2541726ee160a 100644 --- a/compiler/rustc_feature/src/unstable.rs +++ b/compiler/rustc_feature/src/unstable.rs @@ -402,8 +402,6 @@ declare_features! ( (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 basic arithmetic on floating point types in a `const fn`. - (unstable, const_fn_floating_point_arithmetic, "1.48.0", Some(57241)), /// Allows `for _ in _` loops in const contexts. (unstable, const_for, "1.56.0", Some(87575)), /// Allows using `&mut` in constant functions. diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 0917631e04500..a1d1600731ea7 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -192,6 +192,7 @@ // // Language features: // tidy-alphabetical-start +#![cfg_attr(bootstrap, feature(const_fn_floating_point_arithmetic))] #![feature(abi_unadjusted)] #![feature(adt_const_params)] #![feature(allow_internal_unsafe)] @@ -201,7 +202,6 @@ #![feature(cfg_sanitize)] #![feature(cfg_target_has_atomic)] #![feature(cfg_target_has_atomic_equal_alignment)] -#![feature(const_fn_floating_point_arithmetic)] #![feature(const_for)] #![feature(const_mut_refs)] #![feature(const_precise_live_drops)] diff --git a/tests/ui/consts/const_fn_floating_point_arithmetic.gated.stderr b/tests/ui/consts/const_fn_floating_point_arithmetic.gated.stderr deleted file mode 100644 index e1b8154a287e0..0000000000000 --- a/tests/ui/consts/const_fn_floating_point_arithmetic.gated.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error: fatal error triggered by #[rustc_error] - --> $DIR/const_fn_floating_point_arithmetic.rs:20:1 - | -LL | fn main() {} - | ^^^^^^^^^ - -error: aborting due to 1 previous error - diff --git a/tests/ui/consts/const_fn_floating_point_arithmetic.rs b/tests/ui/consts/const_fn_floating_point_arithmetic.rs deleted file mode 100644 index b0d0bc6b9f4a9..0000000000000 --- a/tests/ui/consts/const_fn_floating_point_arithmetic.rs +++ /dev/null @@ -1,20 +0,0 @@ -// gate-test-const_fn_floating_point_arithmetic - -//@ revisions: stock gated - -#![feature(rustc_attrs)] -#![cfg_attr(gated, feature(const_fn_floating_point_arithmetic))] - -const fn add(f: f32) -> f32 { f + 2.0 } -//[stock]~^ floating point arithmetic -const fn sub(f: f32) -> f32 { 2.0 - f } -//[stock]~^ floating point arithmetic -const fn mul(f: f32, g: f32) -> f32 { f * g } -//[stock]~^ floating point arithmetic -const fn div(f: f32, g: f32) -> f32 { f / g } -//[stock]~^ floating point arithmetic -const fn neg(f: f32) -> f32 { -f } -//[stock]~^ floating point arithmetic - -#[rustc_error] -fn main() {} //[gated]~ fatal error triggered by #[rustc_error] diff --git a/tests/ui/consts/const_fn_floating_point_arithmetic.stock.stderr b/tests/ui/consts/const_fn_floating_point_arithmetic.stock.stderr deleted file mode 100644 index b5b94786ebb91..0000000000000 --- a/tests/ui/consts/const_fn_floating_point_arithmetic.stock.stderr +++ /dev/null @@ -1,53 +0,0 @@ -error[E0658]: floating point arithmetic is not allowed in constant functions - --> $DIR/const_fn_floating_point_arithmetic.rs:8:31 - | -LL | const fn add(f: f32) -> f32 { f + 2.0 } - | ^^^^^^^ - | - = note: see issue #57241 for more information - = help: add `#![feature(const_fn_floating_point_arithmetic)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - -error[E0658]: floating point arithmetic is not allowed in constant functions - --> $DIR/const_fn_floating_point_arithmetic.rs:10:31 - | -LL | const fn sub(f: f32) -> f32 { 2.0 - f } - | ^^^^^^^ - | - = note: see issue #57241 for more information - = help: add `#![feature(const_fn_floating_point_arithmetic)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - -error[E0658]: floating point arithmetic is not allowed in constant functions - --> $DIR/const_fn_floating_point_arithmetic.rs:12:39 - | -LL | const fn mul(f: f32, g: f32) -> f32 { f * g } - | ^^^^^ - | - = note: see issue #57241 for more information - = help: add `#![feature(const_fn_floating_point_arithmetic)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - -error[E0658]: floating point arithmetic is not allowed in constant functions - --> $DIR/const_fn_floating_point_arithmetic.rs:14:39 - | -LL | const fn div(f: f32, g: f32) -> f32 { f / g } - | ^^^^^ - | - = note: see issue #57241 for more information - = help: add `#![feature(const_fn_floating_point_arithmetic)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - -error[E0658]: floating point arithmetic is not allowed in constant functions - --> $DIR/const_fn_floating_point_arithmetic.rs:16:31 - | -LL | const fn neg(f: f32) -> f32 { -f } - | ^^ - | - = note: see issue #57241 for more information - = help: add `#![feature(const_fn_floating_point_arithmetic)]` to the crate attributes to enable - = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date - -error: aborting due to 5 previous errors - -For more information about this error, try `rustc --explain E0658`. diff --git a/tests/ui/consts/const_let_eq_float.rs b/tests/ui/consts/const_let_eq_float.rs index 30d839cdc2a8c..c9ca6b8b7ea75 100644 --- a/tests/ui/consts/const_let_eq_float.rs +++ b/tests/ui/consts/const_let_eq_float.rs @@ -1,7 +1,5 @@ //@ run-pass -#![feature(const_fn_floating_point_arithmetic)] - struct Foo(T); struct Bar { x: T } struct W(f32);