Skip to content

Commit 0d2521a

Browse files
Add const_fn_floating_point_arithmetic
1 parent 5bfeee5 commit 0d2521a

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

compiler/rustc_feature/src/active.rs

+3
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,9 @@ declare_features! (
584584
/// Allows non trivial generic constants which have to be manually propageted upwards.
585585
(active, const_evaluatable_checked, "1.48.0", Some(76560), None),
586586

587+
/// Allows basic arithmetic on floating point types in a `const fn`.
588+
(active, const_fn_floating_point_arithmetic, "1.48.0", Some(57563), None),
589+
587590
// -------------------------------------------------------------------------
588591
// feature-group-end: actual feature gates
589592
// -------------------------------------------------------------------------

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

+24
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,30 @@ impl NonConstOp for Abort {
112112
}
113113
}
114114

115+
#[derive(Debug)]
116+
pub struct FloatingPointOp;
117+
impl NonConstOp for FloatingPointOp {
118+
const STOPS_CONST_CHECKING: bool = true;
119+
120+
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
121+
if ccx.const_kind() == hir::ConstContext::ConstFn {
122+
Status::Unstable(sym::const_fn_floating_point_arithmetic)
123+
} else {
124+
Status::Allowed
125+
}
126+
}
127+
128+
fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) {
129+
feature_err(
130+
&ccx.tcx.sess.parse_sess,
131+
sym::const_fn_floating_point_arithmetic,
132+
span,
133+
&format!("floating point arithmetic is not allowed in {}s", ccx.const_kind()),
134+
)
135+
.emit();
136+
}
137+
}
138+
115139
#[derive(Debug)]
116140
pub struct NonPrimitiveOp;
117141
impl NonConstOp for NonPrimitiveOp {

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ symbols! {
351351
const_evaluatable_checked,
352352
const_extern_fn,
353353
const_fn,
354+
const_fn_floating_point_arithmetic,
354355
const_fn_transmute,
355356
const_fn_union,
356357
const_generics,

0 commit comments

Comments
 (0)