Skip to content

Commit fbfaab2

Browse files
committed
separate feature flag for unsizing casts in const fn
1 parent fdad6ab commit fbfaab2

File tree

8 files changed

+33
-43
lines changed

8 files changed

+33
-43
lines changed

compiler/rustc_feature/src/active.rs

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

582+
/// Allows unsizing coercions in `const fn`.
583+
(active, const_fn_unsize, "1.53.0", Some(64992), None),
584+
582585
/// Allows to use the `#[cmse_nonsecure_entry]` attribute.
583586
(active, cmse_nonsecure_entry, "1.48.0", Some(75835), None),
584587

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

+9-22
Original file line numberDiff line numberDiff line change
@@ -540,14 +540,19 @@ impl NonConstOp for UnionAccess {
540540
pub struct UnsizingCast;
541541
impl NonConstOp for UnsizingCast {
542542
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
543-
mcf_status_in_item(ccx)
543+
if ccx.const_kind() != hir::ConstContext::ConstFn {
544+
Status::Allowed
545+
} else {
546+
Status::Unstable(sym::const_fn_unsize)
547+
}
544548
}
545549

546550
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
547-
mcf_build_error(
548-
ccx,
551+
feature_err(
552+
&ccx.tcx.sess.parse_sess,
553+
sym::const_fn_unsize,
549554
span,
550-
"unsizing casts to types besides slices are not allowed in const fn",
555+
"unsizing casts to types besides slices are not allowed in const fn"
551556
)
552557
}
553558
}
@@ -677,21 +682,3 @@ pub mod ty {
677682
}
678683
}
679684
}
680-
681-
fn mcf_status_in_item(ccx: &ConstCx<'_, '_>) -> Status {
682-
if ccx.const_kind() != hir::ConstContext::ConstFn {
683-
Status::Allowed
684-
} else {
685-
Status::Unstable(sym::const_fn)
686-
}
687-
}
688-
689-
fn mcf_build_error(ccx: &ConstCx<'_, 'tcx>, span: Span, msg: &str) -> DiagnosticBuilder<'tcx> {
690-
let mut err = struct_span_err!(ccx.tcx.sess, span, E0723, "{}", msg);
691-
err.note(
692-
"see issue #57563 <https://github.com/rust-lang/rust/issues/57563> \
693-
for more information",
694-
);
695-
err.help("add `#![feature(const_fn)]` to the crate attributes to enable");
696-
err
697-
}

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ symbols! {
385385
const_fn_trait_bound,
386386
const_fn_transmute,
387387
const_fn_union,
388+
const_fn_unsize,
388389
const_generic_defaults,
389390
const_generics,
390391
const_generics_defaults,

library/alloc/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
#![feature(cfg_target_has_atomic)]
9090
#![feature(coerce_unsized)]
9191
#![feature(const_btree_new)]
92-
#![feature(const_fn)]
92+
#![cfg_attr(bootstrap, feature(const_fn))]
9393
#![cfg_attr(not(bootstrap), feature(const_fn_trait_bound))]
9494
#![feature(cow_is_borrowed)]
9595
#![feature(const_cow_is_borrowed)]

library/core/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
#![feature(const_refs_to_cell)]
8282
#![feature(const_panic)]
8383
#![feature(const_pin)]
84-
#![feature(const_fn)]
84+
#![cfg_attr(bootstrap, feature(const_fn))]
8585
#![feature(const_fn_union)]
8686
#![feature(const_impl_trait)]
8787
#![feature(const_fn_floating_point_arithmetic)]

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

+10-10
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[E0723]: unsizing casts to types besides slices are not allowed in const fn
291+
error[E0658]: unsizing casts to types besides slices are not allowed in const fn
292292
--> $DIR/min_const_fn.rs:134:63
293293
|
294294
LL | const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
295295
| ^^^
296296
|
297-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
298-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
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
299299

300-
error[E0723]: unsizing casts to types besides slices are not allowed in const fn
300+
error[E0658]: unsizing casts to types besides slices are not allowed in const fn
301301
--> $DIR/min_const_fn.rs:134:63
302302
|
303303
LL | const fn no_dyn_trait_ret() -> &'static dyn std::fmt::Debug { &() }
304304
| ^^^
305305
|
306-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
307-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
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
308308

309-
error[E0723]: unsizing casts to types besides slices are not allowed in const fn
309+
error[E0658]: unsizing casts to types besides slices are not allowed in const fn
310310
--> $DIR/min_const_fn.rs:141:42
311311
|
312312
LL | const fn really_no_traits_i_mean_it() { (&() as &dyn std::fmt::Debug, ()).1 }
313313
| ^^^
314314
|
315-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
316-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
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
317317

318318
error[E0658]: function pointers cannot appear in constant functions
319319
--> $DIR/min_const_fn.rs:144:21
@@ -344,5 +344,5 @@ LL | const fn no_fn_ptrs2() -> fn() { fn foo() {} foo }
344344

345345
error: aborting due to 39 previous errors
346346

347-
Some errors have detailed explanations: E0013, E0493, E0658, E0723.
347+
Some errors have detailed explanations: E0013, E0493, E0658.
348348
For more information about an error, try `rustc --explain E0013`.

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ 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[E0723]: unsizing casts to types besides slices are not allowed in const fn
10+
error[E0658]: unsizing casts to types besides slices are not allowed in const fn
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 #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
17-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
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
1818

1919
error: aborting due to 2 previous errors
2020

21-
Some errors have detailed explanations: E0658, E0723.
22-
For more information about an error, try `rustc --explain E0658`.
21+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error[E0723]: unsizing casts to types besides slices are not allowed in const fn
1+
error[E0658]: unsizing casts to types besides slices are not allowed in const fn
22
--> $DIR/unsizing-cast-non-null.rs:6:5
33
|
44
LL | NonNull::<[T; 0]>::dangling()
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
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
7+
= note: see issue #64992 <https://github.com/rust-lang/rust/issues/64992> for more information
8+
= help: add `#![feature(const_fn_unsize)]` to the crate attributes to enable
99

1010
error: aborting due to previous error
1111

12-
For more information about this error, try `rustc --explain E0723`.
12+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)