Skip to content

Commit ebfa3e3

Browse files
committed
stabilize const_fn_floating_point_arithmetic
1 parent a32d4a0 commit ebfa3e3

27 files changed

+92
-257
lines changed

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -575,10 +575,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
575575

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

591-
if is_int_bool_or_char(lhs_ty) && is_int_bool_or_char(rhs_ty) {
592-
// Int, bool, and char operations are fine.
589+
if is_int_bool_float_or_char(lhs_ty) && is_int_bool_float_or_char(rhs_ty) {
590+
// Int, bool, float, and char operations are fine.
593591
} else if lhs_ty.is_fn_ptr() || lhs_ty.is_unsafe_ptr() {
594592
assert_matches!(
595593
op,
@@ -603,8 +601,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
603601
);
604602

605603
self.check_op(ops::RawPtrComparison);
606-
} else if lhs_ty.is_floating_point() || rhs_ty.is_floating_point() {
607-
self.check_op(ops::FloatingPointOp);
608604
} else {
609605
span_bug!(
610606
self.span,
@@ -1009,8 +1005,8 @@ fn place_as_reborrow<'tcx>(
10091005
}
10101006
}
10111007

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

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

compiler/rustc_const_eval/src/check_consts/ops.rs

Lines changed: 6 additions & 38 deletions
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: true,
448-
kind: ccx.const_kind(),
449-
teach: ccx.tcx.sess.teach(E0492),
450-
})
451-
} else {
452-
ccx.dcx().create_err(errors::InteriorMutableDataRefer {
453-
span,
454-
opt_help: false,
455-
kind: ccx.const_kind(),
456-
teach: ccx.tcx.sess.teach(E0492),
457-
})
458-
}
421+
ccx.dcx().create_err(errors::InteriorMutableDataRefer {
422+
span,
423+
opt_help: matches!(ccx.const_kind(), hir::ConstContext::Static(_)),
424+
kind: ccx.const_kind(),
425+
teach: ccx.tcx.sess.teach(E0492),
426+
})
459427
}
460428
}
461429

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 basic arithmetic on floating point types in a `const fn`.
119+
(accepted, const_fn_floating_point_arithmetic, "CURRENT_RUSTC_VERSION", Some(57241)),
118120
/// Allows using and casting function pointers in a `const fn`.
119121
(accepted, const_fn_fn_ptr_basics, "1.61.0", Some(57563)),
120122
/// Allows trait bounds in `const fn`.

compiler/rustc_feature/src/unstable.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,6 @@ declare_features! (
400400
(incomplete, const_closures, "1.68.0", Some(106003)),
401401
/// Allows the definition of `const extern fn` and `const unsafe extern fn`.
402402
(unstable, const_extern_fn, "1.40.0", Some(64926)),
403-
/// Allows basic arithmetic on floating point types in a `const fn`.
404-
(unstable, const_fn_floating_point_arithmetic, "1.48.0", Some(57241)),
405403
/// Allows `for _ in _` loops in const contexts.
406404
(unstable, const_for, "1.56.0", Some(87575)),
407405
/// Allows using `&mut` in constant functions.

library/core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@
194194
// Language features:
195195
// tidy-alphabetical-start
196196
#![cfg_attr(bootstrap, feature(asm_const))]
197+
#![cfg_attr(bootstrap, feature(const_fn_floating_point_arithmetic))]
197198
#![cfg_attr(bootstrap, feature(min_exhaustive_patterns))]
198199
#![feature(abi_unadjusted)]
199200
#![feature(adt_const_params)]
@@ -203,7 +204,6 @@
203204
#![feature(cfg_sanitize)]
204205
#![feature(cfg_target_has_atomic)]
205206
#![feature(cfg_target_has_atomic_equal_alignment)]
206-
#![feature(const_fn_floating_point_arithmetic)]
207207
#![feature(const_for)]
208208
#![feature(const_mut_refs)]
209209
#![feature(const_precise_live_drops)]

src/tools/clippy/tests/ui/floating_point_abs.fixed

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(const_fn_floating_point_arithmetic)]
21
#![warn(clippy::suboptimal_flops)]
32

43
/// Allow suboptimal ops in constant context

src/tools/clippy/tests/ui/floating_point_abs.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(const_fn_floating_point_arithmetic)]
21
#![warn(clippy::suboptimal_flops)]
32

43
/// Allow suboptimal ops in constant context

src/tools/clippy/tests/ui/floating_point_abs.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: manual implementation of `abs` method
2-
--> tests/ui/floating_point_abs.rs:15:5
2+
--> tests/ui/floating_point_abs.rs:14:5
33
|
44
LL | if num >= 0.0 { num } else { -num }
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.abs()`
@@ -8,43 +8,43 @@ LL | if num >= 0.0 { num } else { -num }
88
= help: to override `-D warnings` add `#[allow(clippy::suboptimal_flops)]`
99

1010
error: manual implementation of `abs` method
11-
--> tests/ui/floating_point_abs.rs:19:5
11+
--> tests/ui/floating_point_abs.rs:18:5
1212
|
1313
LL | if 0.0 < num { num } else { -num }
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.abs()`
1515

1616
error: manual implementation of `abs` method
17-
--> tests/ui/floating_point_abs.rs:23:5
17+
--> tests/ui/floating_point_abs.rs:22:5
1818
|
1919
LL | if a.a > 0.0 { a.a } else { -a.a }
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `a.a.abs()`
2121

2222
error: manual implementation of `abs` method
23-
--> tests/ui/floating_point_abs.rs:27:5
23+
--> tests/ui/floating_point_abs.rs:26:5
2424
|
2525
LL | if 0.0 >= num { -num } else { num }
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.abs()`
2727

2828
error: manual implementation of `abs` method
29-
--> tests/ui/floating_point_abs.rs:31:5
29+
--> tests/ui/floating_point_abs.rs:30:5
3030
|
3131
LL | if a.a < 0.0 { -a.a } else { a.a }
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `a.a.abs()`
3333

3434
error: manual implementation of negation of `abs` method
35-
--> tests/ui/floating_point_abs.rs:35:5
35+
--> tests/ui/floating_point_abs.rs:34:5
3636
|
3737
LL | if num < 0.0 { num } else { -num }
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `-num.abs()`
3939

4040
error: manual implementation of negation of `abs` method
41-
--> tests/ui/floating_point_abs.rs:39:5
41+
--> tests/ui/floating_point_abs.rs:38:5
4242
|
4343
LL | if 0.0 >= num { num } else { -num }
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `-num.abs()`
4545

4646
error: manual implementation of negation of `abs` method
47-
--> tests/ui/floating_point_abs.rs:44:12
47+
--> tests/ui/floating_point_abs.rs:43:12
4848
|
4949
LL | a: if a.a >= 0.0 { -a.a } else { a.a },
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `-a.a.abs()`

src/tools/clippy/tests/ui/floating_point_mul_add.fixed

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(const_fn_floating_point_arithmetic)]
21
#![warn(clippy::suboptimal_flops)]
32

43
/// Allow suboptimal_ops in constant context

src/tools/clippy/tests/ui/floating_point_mul_add.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(const_fn_floating_point_arithmetic)]
21
#![warn(clippy::suboptimal_flops)]
32

43
/// Allow suboptimal_ops in constant context

0 commit comments

Comments
 (0)