Skip to content

Commit f98bd7e

Browse files
committed
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 #64992. This PR is for the implementation side of stabilization. r? `@oli-obk` Fixes #64992
2 parents ed20e1e + 5c6f41e commit f98bd7e

14 files changed

+45
-117
lines changed

compiler/rustc_ast/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
)]
1111
#![feature(box_syntax)]
1212
#![feature(box_patterns)]
13+
// On bootstrap bump, this will likely have to become const_fn_unsize
1314
#![cfg_attr(bootstrap, feature(const_fn))] // For the `transmute` in `P::new`
14-
#![cfg_attr(not(bootstrap), feature(const_fn_unsize))] // For the `transmute` in `P::new`
1515
#![feature(const_fn_transmute)]
1616
#![feature(const_panic)]
1717
#![feature(crate_visibility_modifier)]

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ declare_features! (
283283
(accepted, non_ascii_idents, "1.53.0", Some(55467), None),
284284
/// Allows arbitrary expressions in key-value attributes at parse time.
285285
(accepted, extended_key_value_attributes, "1.54.0", Some(78835), None),
286+
/// Allows unsizing coercions in `const fn`.
287+
(accepted, const_fn_unsize, "1.54.0", Some(64992), None),
286288

287289
// -------------------------------------------------------------------------
288290
// feature-group-end: accepted features

compiler/rustc_feature/src/active.rs

-3
Original file line numberDiff line numberDiff line change
@@ -644,9 +644,6 @@ declare_features! (
644644
/// Allows trait bounds in `const fn`.
645645
(active, const_fn_trait_bound, "1.53.0", Some(57563), None),
646646

647-
/// Allows unsizing coercions in `const fn`.
648-
(active, const_fn_unsize, "1.53.0", Some(64992), None),
649-
650647
/// Allows `async {}` expressions in const contexts.
651648
(active, const_async_blocks, "1.53.0", Some(85368), None),
652649

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

-24
Original file line numberDiff line numberDiff line change
@@ -541,30 +541,6 @@ impl NonConstOp for UnionAccess {
541541
}
542542
}
543543

544-
/// See [#64992].
545-
///
546-
/// [#64992]: https://github.com/rust-lang/rust/issues/64992
547-
#[derive(Debug)]
548-
pub struct UnsizingCast;
549-
impl NonConstOp for UnsizingCast {
550-
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
551-
if ccx.const_kind() != hir::ConstContext::ConstFn {
552-
Status::Allowed
553-
} else {
554-
Status::Unstable(sym::const_fn_unsize)
555-
}
556-
}
557-
558-
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
559-
feature_err(
560-
&ccx.tcx.sess.parse_sess,
561-
sym::const_fn_unsize,
562-
span,
563-
"unsizing casts to types besides slices are not allowed in const fn",
564-
)
565-
}
566-
}
567-
568544
// Types that cannot appear in the signature or locals of a `const fn`.
569545
pub mod ty {
570546
use super::*;

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

+4-14
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceC
1010
use rustc_middle::mir::*;
1111
use rustc_middle::ty::cast::CastTy;
1212
use rustc_middle::ty::subst::GenericArgKind;
13-
use rustc_middle::ty::{
14-
self, adjustment::PointerCast, Instance, InstanceDef, Ty, TyCtxt, TypeAndMut,
15-
};
13+
use rustc_middle::ty::{self, adjustment::PointerCast, Instance, InstanceDef, Ty, TyCtxt};
1614
use rustc_middle::ty::{Binder, TraitPredicate, TraitRef};
1715
use rustc_span::{sym, Span, Symbol};
1816
use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
@@ -636,17 +634,9 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
636634
_,
637635
) => self.check_op(ops::FnPtrCast),
638636

639-
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), _, cast_ty) => {
640-
if let Some(TypeAndMut { ty, .. }) = cast_ty.builtin_deref(true) {
641-
let unsized_ty = self.tcx.struct_tail_erasing_lifetimes(ty, self.param_env);
642-
643-
// Casting/coercing things to slices is fine.
644-
if let ty::Slice(_) | ty::Str = unsized_ty.kind() {
645-
return;
646-
}
647-
}
648-
649-
self.check_op(ops::UnsizingCast);
637+
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), _, _) => {
638+
// Nothing to check here (`check_local_or_return_ty` ensures no trait objects occur
639+
// in the type of any local, which also excludes casts).
650640
}
651641

652642
Rvalue::Cast(CastKind::Misc, ref operand, cast_ty) => {

src/test/ui/consts/const_fn_unsize.gated.stderr

-8
This file was deleted.

src/test/ui/consts/const_fn_unsize.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
// gate-test-const_fn_unsize
2-
3-
// revisions: stock gated
4-
5-
#![feature(rustc_attrs)]
6-
#![cfg_attr(gated, feature(const_fn_unsize))]
1+
// run-pass
2+
#![feature(slice_ptr_len)]
73

84
use std::ptr::NonNull;
95

6+
#[allow(unused)]
107
const fn test() {
118
let _x = NonNull::<[i32; 0]>::dangling() as NonNull<[i32]>;
12-
//[stock]~^ unsizing cast
139
}
1410

15-
#[rustc_error]
16-
fn main() {} //[gated]~ fatal error triggered by #[rustc_error]
11+
// Regression test for #75118.
12+
pub const fn dangling_slice<T>() -> NonNull<[T]> {
13+
NonNull::<[T; 1]>::dangling()
14+
}
15+
16+
const C: NonNull<[i32]> = dangling_slice();
17+
18+
fn main() {
19+
assert_eq!(C.as_ptr(), NonNull::<[i32; 1]>::dangling().as_ptr() as *mut _);
20+
assert_eq!(C.as_ptr().len(), 1);
21+
}

src/test/ui/consts/const_fn_unsize.stock.stderr

-12
This file was deleted.

src/test/ui/consts/min_const_fn/min_const_fn.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ const fn no_dyn_trait(_x: &dyn std::fmt::Debug) {}
133133
//~^ ERROR trait bounds other than `Sized`
134134
const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
135135
//~^ ERROR trait bounds other than `Sized`
136-
//~| ERROR unsizing cast
137-
//~| ERROR unsizing cast
138136

139137
const fn no_unsafe() { unsafe {} }
140138

141139
const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }
142-
//~^ ERROR unsizing cast
140+
//~^ ERROR trait bounds other than `Sized`
141+
//~| ERROR trait bounds other than `Sized`
142+
//~| ERROR trait bounds other than `Sized`
143143

144144
const fn no_fn_ptrs(_x: fn()) {}
145145
//~^ ERROR function pointer

src/test/ui/consts/min_const_fn/min_const_fn.stderr

+17-17
Original file line numberDiff line numberDiff line change
@@ -288,32 +288,32 @@ LL | const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
288288
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
289289
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
290290

291-
error[E0658]: unsizing casts to types besides slices are not allowed in const fn
292-
--> $DIR/min_const_fn.rs:134:63
291+
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
292+
--> $DIR/min_const_fn.rs:139:41
293293
|
294-
LL | const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
295-
| ^^^
294+
LL | const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }
295+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
296296
|
297-
= note: see issue #64992 <https://github.com/rust-lang/rust/issues/64992> for more information
298-
= help: add `#![feature(const_fn_unsize)]` to the crate attributes to enable
297+
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
298+
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
299299

300-
error[E0658]: unsizing casts to types besides slices are not allowed in const fn
301-
--> $DIR/min_const_fn.rs:134:63
300+
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
301+
--> $DIR/min_const_fn.rs:139:42
302302
|
303-
LL | const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
304-
| ^^^
303+
LL | const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }
304+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
305305
|
306-
= note: see issue #64992 <https://github.com/rust-lang/rust/issues/64992> for more information
307-
= help: add `#![feature(const_fn_unsize)]` to the crate attributes to enable
306+
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
307+
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
308308

309-
error[E0658]: unsizing casts to types besides slices are not allowed in const fn
310-
--> $DIR/min_const_fn.rs:141:42
309+
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
310+
--> $DIR/min_const_fn.rs:139:42
311311
|
312312
LL | const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }
313-
| ^^^
313+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
314314
|
315-
= note: see issue #64992 <https://github.com/rust-lang/rust/issues/64992> for more information
316-
= help: add `#![feature(const_fn_unsize)]` to the crate attributes to enable
315+
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
316+
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
317317

318318
error[E0658]: function pointers cannot appear in constant functions
319319
--> $DIR/min_const_fn.rs:144:21

src/test/ui/consts/min_const_fn/min_const_fn_dyn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ const fn no_inner_dyn_trait2(x: Hide) {
1010
//~^ ERROR trait bounds other than `Sized`
1111
}
1212
const fn no_inner_dyn_trait_ret() -> Hide { Hide(HasDyn { field: &0 }) }
13-
//~^ ERROR unsizing cast
13+
//~^ ERROR trait bounds other than `Sized`
1414

1515
fn main() {}

src/test/ui/consts/min_const_fn/min_const_fn_dyn.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ LL | x.0.field;
77
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
88
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
99

10-
error[E0658]: unsizing casts to types besides slices are not allowed in const fn
10+
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
1111
--> $DIR/min_const_fn_dyn.rs:12:66
1212
|
1313
LL | const fn no_inner_dyn_trait_ret() -> Hide { Hide(HasDyn { field: &0 }) }
1414
| ^^
1515
|
16-
= note: see issue #64992 <https://github.com/rust-lang/rust/issues/64992> for more information
17-
= help: add `#![feature(const_fn_unsize)]` to the crate attributes to enable
16+
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
17+
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
1818

1919
error: aborting due to 2 previous errors
2020

src/test/ui/consts/unsizing-cast-non-null.rs

-10
This file was deleted.

src/test/ui/consts/unsizing-cast-non-null.stderr

-12
This file was deleted.

0 commit comments

Comments
 (0)