Skip to content

Commit e265baf

Browse files
authored
Unrolled build for rust-lang#128596
Rollup merge of rust-lang#128596 - RalfJung:const_fn_floating_point_arithmetic, r=nnethercote stabilize const_fn_floating_point_arithmetic Part of rust-lang#128288 Fixes rust-lang#57241 The existing test `tests/ui/consts/const_let_eq_float.rs` ([link](https://github.com/RalfJung/rust/blob/const_fn_floating_point_arithmetic/tests/ui/consts/const_let_eq_float.rs)) covers the basics, and also Miri has extensive tests covering the interpreter's float machinery. Also, that machinery can already be used on stable inside `const`/`static` initializers, just not inside `const fn`. This was explicitly called out in rust-lang/rfcs#3514 so in a sense t-lang just recently already FCP'd this, but let's hear from them whether they want another FCP for the stabilization here or whether that was covered by the FCP for the RFC. Cc ``@rust-lang/lang`` ### Open items - [x] Update the Reference: rust-lang/reference#1566
2 parents 697d953 + ebfa3e3 commit e265baf

27 files changed

+92
-257
lines changed

compiler/rustc_const_eval/src/check_consts/check.rs

+6-10
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

+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: 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

+2
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

-2
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@
193193
// Language features:
194194
// tidy-alphabetical-start
195195
#![cfg_attr(bootstrap, feature(asm_const))]
196+
#![cfg_attr(bootstrap, feature(const_fn_floating_point_arithmetic))]
196197
#![cfg_attr(bootstrap, feature(min_exhaustive_patterns))]
197198
#![feature(abi_unadjusted)]
198199
#![feature(adt_const_params)]
@@ -202,7 +203,6 @@
202203
#![feature(cfg_sanitize)]
203204
#![feature(cfg_target_has_atomic)]
204205
#![feature(cfg_target_has_atomic_equal_alignment)]
205-
#![feature(const_fn_floating_point_arithmetic)]
206206
#![feature(const_for)]
207207
#![feature(const_mut_refs)]
208208
#![feature(const_precise_live_drops)]

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

-1
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

-1
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

+8-8
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

-1
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

-1
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.stderr

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: multiply and add expressions can be calculated more efficiently and accurately
2-
--> tests/ui/floating_point_mul_add.rs:20:13
2+
--> tests/ui/floating_point_mul_add.rs:19:13
33
|
44
LL | let _ = a * b + c;
55
| ^^^^^^^^^ help: consider using: `a.mul_add(b, c)`
@@ -8,73 +8,73 @@ LL | let _ = a * b + c;
88
= help: to override `-D warnings` add `#[allow(clippy::suboptimal_flops)]`
99

1010
error: multiply and add expressions can be calculated more efficiently and accurately
11-
--> tests/ui/floating_point_mul_add.rs:21:13
11+
--> tests/ui/floating_point_mul_add.rs:20:13
1212
|
1313
LL | let _ = a * b - c;
1414
| ^^^^^^^^^ help: consider using: `a.mul_add(b, -c)`
1515

1616
error: multiply and add expressions can be calculated more efficiently and accurately
17-
--> tests/ui/floating_point_mul_add.rs:22:13
17+
--> tests/ui/floating_point_mul_add.rs:21:13
1818
|
1919
LL | let _ = c + a * b;
2020
| ^^^^^^^^^ help: consider using: `a.mul_add(b, c)`
2121

2222
error: multiply and add expressions can be calculated more efficiently and accurately
23-
--> tests/ui/floating_point_mul_add.rs:23:13
23+
--> tests/ui/floating_point_mul_add.rs:22:13
2424
|
2525
LL | let _ = c - a * b;
2626
| ^^^^^^^^^ help: consider using: `a.mul_add(-b, c)`
2727

2828
error: multiply and add expressions can be calculated more efficiently and accurately
29-
--> tests/ui/floating_point_mul_add.rs:24:13
29+
--> tests/ui/floating_point_mul_add.rs:23:13
3030
|
3131
LL | let _ = a + 2.0 * 4.0;
3232
| ^^^^^^^^^^^^^ help: consider using: `2.0f64.mul_add(4.0, a)`
3333

3434
error: multiply and add expressions can be calculated more efficiently and accurately
35-
--> tests/ui/floating_point_mul_add.rs:25:13
35+
--> tests/ui/floating_point_mul_add.rs:24:13
3636
|
3737
LL | let _ = a + 2. * 4.;
3838
| ^^^^^^^^^^^ help: consider using: `2.0f64.mul_add(4., a)`
3939

4040
error: multiply and add expressions can be calculated more efficiently and accurately
41-
--> tests/ui/floating_point_mul_add.rs:27:13
41+
--> tests/ui/floating_point_mul_add.rs:26:13
4242
|
4343
LL | let _ = (a * b) + c;
4444
| ^^^^^^^^^^^ help: consider using: `a.mul_add(b, c)`
4545

4646
error: multiply and add expressions can be calculated more efficiently and accurately
47-
--> tests/ui/floating_point_mul_add.rs:28:13
47+
--> tests/ui/floating_point_mul_add.rs:27:13
4848
|
4949
LL | let _ = c + (a * b);
5050
| ^^^^^^^^^^^ help: consider using: `a.mul_add(b, c)`
5151

5252
error: multiply and add expressions can be calculated more efficiently and accurately
53-
--> tests/ui/floating_point_mul_add.rs:29:13
53+
--> tests/ui/floating_point_mul_add.rs:28:13
5454
|
5555
LL | let _ = a * b * c + d;
5656
| ^^^^^^^^^^^^^ help: consider using: `(a * b).mul_add(c, d)`
5757

5858
error: multiply and add expressions can be calculated more efficiently and accurately
59-
--> tests/ui/floating_point_mul_add.rs:31:13
59+
--> tests/ui/floating_point_mul_add.rs:30:13
6060
|
6161
LL | let _ = a.mul_add(b, c) * a.mul_add(b, c) + a.mul_add(b, c) + c;
6262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `a.mul_add(b, c).mul_add(a.mul_add(b, c), a.mul_add(b, c))`
6363

6464
error: multiply and add expressions can be calculated more efficiently and accurately
65-
--> tests/ui/floating_point_mul_add.rs:32:13
65+
--> tests/ui/floating_point_mul_add.rs:31:13
6666
|
6767
LL | let _ = 1234.567_f64 * 45.67834_f64 + 0.0004_f64;
6868
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1234.567_f64.mul_add(45.67834_f64, 0.0004_f64)`
6969

7070
error: multiply and add expressions can be calculated more efficiently and accurately
71-
--> tests/ui/floating_point_mul_add.rs:34:13
71+
--> tests/ui/floating_point_mul_add.rs:33:13
7272
|
7373
LL | let _ = (a * a + b).sqrt();
7474
| ^^^^^^^^^^^ help: consider using: `a.mul_add(a, b)`
7575

7676
error: multiply and add expressions can be calculated more efficiently and accurately
77-
--> tests/ui/floating_point_mul_add.rs:37:13
77+
--> tests/ui/floating_point_mul_add.rs:36:13
7878
|
7979
LL | let _ = a - (b * u as f64);
8080
| ^^^^^^^^^^^^^^^^^^ help: consider using: `b.mul_add(-(u as f64), a)`

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

-1
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_flops in constant context

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

-1
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_flops in constant context

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: conversion to radians can be done more accurately
2-
--> tests/ui/floating_point_rad.rs:11:13
2+
--> tests/ui/floating_point_rad.rs:10:13
33
|
44
LL | let _ = degrees as f64 * std::f64::consts::PI / 180.0;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(degrees as f64).to_radians()`
@@ -8,43 +8,43 @@ LL | let _ = degrees as f64 * std::f64::consts::PI / 180.0;
88
= help: to override `-D warnings` add `#[allow(clippy::suboptimal_flops)]`
99

1010
error: conversion to degrees can be done more accurately
11-
--> tests/ui/floating_point_rad.rs:12:13
11+
--> tests/ui/floating_point_rad.rs:11:13
1212
|
1313
LL | let _ = degrees as f64 * 180.0 / std::f64::consts::PI;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(degrees as f64).to_degrees()`
1515

1616
error: conversion to degrees can be done more accurately
17-
--> tests/ui/floating_point_rad.rs:17:13
17+
--> tests/ui/floating_point_rad.rs:16:13
1818
|
1919
LL | let _ = x * 180f32 / std::f32::consts::PI;
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.to_degrees()`
2121

2222
error: conversion to degrees can be done more accurately
23-
--> tests/ui/floating_point_rad.rs:18:13
23+
--> tests/ui/floating_point_rad.rs:17:13
2424
|
2525
LL | let _ = 90. * 180f64 / std::f64::consts::PI;
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.0_f64.to_degrees()`
2727

2828
error: conversion to degrees can be done more accurately
29-
--> tests/ui/floating_point_rad.rs:19:13
29+
--> tests/ui/floating_point_rad.rs:18:13
3030
|
3131
LL | let _ = 90.5 * 180f64 / std::f64::consts::PI;
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.5_f64.to_degrees()`
3333

3434
error: conversion to radians can be done more accurately
35-
--> tests/ui/floating_point_rad.rs:20:13
35+
--> tests/ui/floating_point_rad.rs:19:13
3636
|
3737
LL | let _ = x * std::f32::consts::PI / 180f32;
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.to_radians()`
3939

4040
error: conversion to radians can be done more accurately
41-
--> tests/ui/floating_point_rad.rs:21:13
41+
--> tests/ui/floating_point_rad.rs:20:13
4242
|
4343
LL | let _ = 90. * std::f32::consts::PI / 180f32;
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.0_f64.to_radians()`
4545

4646
error: conversion to radians can be done more accurately
47-
--> tests/ui/floating_point_rad.rs:22:13
47+
--> tests/ui/floating_point_rad.rs:21:13
4848
|
4949
LL | let _ = 90.5 * std::f32::consts::PI / 180f32;
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `90.5_f64.to_radians()`

tests/ui/consts/const-extern-fn/const-extern-fn-min-const-fn.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#![feature(const_extern_fn)]
22

3-
const extern "C" fn unsize(x: &[u8; 3]) -> &[u8] { x }
4-
const unsafe extern "C" fn closure() -> fn() { || {} }
5-
const unsafe extern "C" fn use_float() { 1.0 + 1.0; }
6-
//~^ ERROR floating point arithmetic
73
const extern "C" fn ptr_cast(val: *const u8) { val as usize; }
84
//~^ ERROR pointers cannot be cast to integers
95

0 commit comments

Comments
 (0)