Rollup merge of #77122 - ecstatic-morse:const-fn-arithmetic, r=RalfJung,oli-obk
Add `#![feature(const_fn_floating_point_arithmetic)]` cc #76618 This is a template for splitting up `const_fn` into granular feature gates. I think this will make it easier, both for us and for users, to track stabilization of each individual feature. We don't *have* to do this, however. We could also keep stabilizing things out from under `const_fn`. cc @rust-lang/wg-const-eval r? @oli-obk
This commit is contained in:
@@ -112,6 +112,30 @@ impl NonConstOp for Abort {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FloatingPointOp;
|
||||
impl NonConstOp for FloatingPointOp {
|
||||
const STOPS_CONST_CHECKING: bool = true;
|
||||
|
||||
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
|
||||
if ccx.const_kind() == hir::ConstContext::ConstFn {
|
||||
Status::Unstable(sym::const_fn_floating_point_arithmetic)
|
||||
} else {
|
||||
Status::Allowed
|
||||
}
|
||||
}
|
||||
|
||||
fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) {
|
||||
feature_err(
|
||||
&ccx.tcx.sess.parse_sess,
|
||||
sym::const_fn_floating_point_arithmetic,
|
||||
span,
|
||||
&format!("floating point arithmetic is not allowed in {}s", ccx.const_kind()),
|
||||
)
|
||||
.emit();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NonPrimitiveOp;
|
||||
impl NonConstOp for NonPrimitiveOp {
|
||||
|
||||
@@ -540,8 +540,12 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
|
||||
|
||||
Rvalue::UnaryOp(_, ref operand) => {
|
||||
let ty = operand.ty(self.body, self.tcx);
|
||||
if !(ty.is_integral() || ty.is_bool()) {
|
||||
self.check_op(ops::NonPrimitiveOp)
|
||||
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);
|
||||
} else {
|
||||
span_bug!(self.span, "non-primitive type in `Rvalue::UnaryOp`: {:?}", ty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -550,7 +554,9 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
|
||||
let lhs_ty = lhs.ty(self.body, self.tcx);
|
||||
let rhs_ty = rhs.ty(self.body, self.tcx);
|
||||
|
||||
if let ty::RawPtr(_) | ty::FnPtr(..) = lhs_ty.kind() {
|
||||
if is_int_bool_or_char(lhs_ty) && is_int_bool_or_char(rhs_ty) {
|
||||
// Int, bool, and char operations are fine.
|
||||
} else if lhs_ty.is_fn_ptr() || lhs_ty.is_unsafe_ptr() {
|
||||
assert_eq!(lhs_ty, rhs_ty);
|
||||
assert!(
|
||||
op == BinOp::Eq
|
||||
@@ -563,12 +569,15 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
|
||||
);
|
||||
|
||||
self.check_op(ops::RawPtrComparison);
|
||||
}
|
||||
|
||||
if !(lhs_ty.is_integral() || lhs_ty.is_bool() || lhs_ty.is_char())
|
||||
|| !(rhs_ty.is_integral() || rhs_ty.is_bool() || rhs_ty.is_char())
|
||||
{
|
||||
self.check_op(ops::NonPrimitiveOp)
|
||||
} else if lhs_ty.is_floating_point() || rhs_ty.is_floating_point() {
|
||||
self.check_op(ops::FloatingPointOp);
|
||||
} else {
|
||||
span_bug!(
|
||||
self.span,
|
||||
"non-primitive type in `Rvalue::BinaryOp`: {:?} ⚬ {:?}",
|
||||
lhs_ty,
|
||||
rhs_ty
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -867,3 +876,7 @@ fn place_as_reborrow(
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn is_int_bool_or_char(ty: Ty<'_>) -> bool {
|
||||
ty.is_bool() || ty.is_integral() || ty.is_char()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user