1
Fork 0

Auto merge of #85078 - RalfJung:const_fn_unsize, r=oli-obk

stabilize const_fn_unsize

I will post a stabilization report and ask for FCP in https://github.com/rust-lang/rust/issues/64992.
This PR is for the implementation side of stabilization.

r? `@oli-obk`
Fixes https://github.com/rust-lang/rust/issues/64992
This commit is contained in:
bors 2021-05-22 17:38:15 +00:00
commit f98bd7eeca
14 changed files with 51 additions and 123 deletions

View file

@ -541,30 +541,6 @@ impl NonConstOp for UnionAccess {
}
}
/// See [#64992].
///
/// [#64992]: https://github.com/rust-lang/rust/issues/64992
#[derive(Debug)]
pub struct UnsizingCast;
impl NonConstOp for UnsizingCast {
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
if ccx.const_kind() != hir::ConstContext::ConstFn {
Status::Allowed
} else {
Status::Unstable(sym::const_fn_unsize)
}
}
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
feature_err(
&ccx.tcx.sess.parse_sess,
sym::const_fn_unsize,
span,
"unsizing casts to types besides slices are not allowed in const fn",
)
}
}
// Types that cannot appear in the signature or locals of a `const fn`.
pub mod ty {
use super::*;

View file

@ -10,9 +10,7 @@ use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceC
use rustc_middle::mir::*;
use rustc_middle::ty::cast::CastTy;
use rustc_middle::ty::subst::GenericArgKind;
use rustc_middle::ty::{
self, adjustment::PointerCast, Instance, InstanceDef, Ty, TyCtxt, TypeAndMut,
};
use rustc_middle::ty::{self, adjustment::PointerCast, Instance, InstanceDef, Ty, TyCtxt};
use rustc_middle::ty::{Binder, TraitPredicate, TraitRef};
use rustc_span::{sym, Span, Symbol};
use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
@ -636,17 +634,9 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
_,
) => self.check_op(ops::FnPtrCast),
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), _, cast_ty) => {
if let Some(TypeAndMut { ty, .. }) = cast_ty.builtin_deref(true) {
let unsized_ty = self.tcx.struct_tail_erasing_lifetimes(ty, self.param_env);
// Casting/coercing things to slices is fine.
if let ty::Slice(_) | ty::Str = unsized_ty.kind() {
return;
}
}
self.check_op(ops::UnsizingCast);
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), _, _) => {
// Nothing to check here (`check_local_or_return_ty` ensures no trait objects occur
// in the type of any local, which also excludes casts).
}
Rvalue::Cast(CastKind::Misc, ref operand, cast_ty) => {