Skip to content

Commit f9d422e

Browse files
committed
Auto merge of #75136 - JohnTitor:unsizing-casts-non-null, r=oli-obk
Forbid non-derefable types explicitly in unsizing casts Fixes #75118 r? @oli-obk
2 parents d08eb98 + cd7204e commit f9d422e

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/librustc_mir/transform/qualify_min_const_fn.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,15 @@ fn check_rvalue(
193193
_,
194194
) => Err((span, "function pointer casts are not allowed in const fn".into())),
195195
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), op, cast_ty) => {
196-
let pointee_ty = cast_ty.builtin_deref(true).unwrap().ty;
196+
let pointee_ty = if let Some(deref_ty) = cast_ty.builtin_deref(true) {
197+
deref_ty.ty
198+
} else {
199+
// We cannot allow this for now.
200+
return Err((
201+
span,
202+
"unsizing casts are only allowed for references right now".into(),
203+
));
204+
};
197205
let unsized_ty = tcx.struct_tail_erasing_lifetimes(pointee_ty, tcx.param_env(def_id));
198206
if let ty::Slice(_) | ty::Str = unsized_ty.kind {
199207
check_operand(tcx, op, span, def_id, body)?;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Regression test for #75118.
2+
3+
use std::ptr::NonNull;
4+
5+
pub const fn dangling_slice<T>() -> NonNull<[T]> {
6+
NonNull::<[T; 0]>::dangling()
7+
//~^ ERROR: unsizing casts are only allowed for references right now
8+
}
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0723]: unsizing casts are only allowed for references right now
2+
--> $DIR/unsizing-cast-non-null.rs:6:5
3+
|
4+
LL | NonNull::<[T; 0]>::dangling()
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
8+
= help: add `#![feature(const_fn)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0723`.

0 commit comments

Comments
 (0)