Skip to content

Commit a2f06be

Browse files
authored
Rollup merge of rust-lang#74000 - lcnr:lazy_normalisation_consts, r=varkor
add `lazy_normalization_consts` feature gate In rust-lang#71973 I underestimated the amount of code which is influenced by lazy normalization of consts and decided against having a separate feature flag for this. Looking a bit more into this, the following issues are already working with lazy norm in its current state rust-lang#47814 rust-lang#57739 rust-lang#73980 I therefore think it is worth it to enable lazy norm separately. Note that `#![feature(const_generics)]` still automatically activates this feature, so using `#![feature(const_generics, lazy_normalization_consts)]` is redundant. r? @varkor @nikomatsakis
2 parents e25fd26 + 7a3081b commit a2f06be

File tree

10 files changed

+103
-1
lines changed

10 files changed

+103
-1
lines changed

src/librustc_feature/active.rs

+4
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,9 @@ declare_features! (
570570
/// Allows capturing variables in scope using format_args!
571571
(active, format_args_capture, "1.46.0", Some(67984), None),
572572

573+
/// Lazily evaluate constants. This allows constants to depend on type parameters.
574+
(active, lazy_normalization_consts, "1.46.0", Some(72219), None),
575+
573576
// -------------------------------------------------------------------------
574577
// feature-group-end: actual feature gates
575578
// -------------------------------------------------------------------------
@@ -586,5 +589,6 @@ pub const INCOMPLETE_FEATURES: &[Symbol] = &[
586589
sym::raw_dylib,
587590
sym::const_trait_impl,
588591
sym::const_trait_bound_opt_out,
592+
sym::lazy_normalization_consts,
589593
sym::specialization,
590594
];

src/librustc_middle/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,7 @@ impl<'tcx> TyCtxt<'tcx> {
13701370
/// we still evaluate them eagerly.
13711371
#[inline]
13721372
pub fn lazy_normalization(self) -> bool {
1373-
self.features().const_generics
1373+
self.features().const_generics || self.features().lazy_normalization_consts
13741374
}
13751375

13761376
#[inline]

src/librustc_span/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ symbols! {
459459
label_break_value,
460460
lang,
461461
lang_items,
462+
lazy_normalization_consts,
462463
lateout,
463464
let_chains,
464465
lhs,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pub const fn sof<T>() -> usize {
2+
10
3+
}
4+
5+
fn test<T>() {
6+
let _: [u8; sof::<T>()];
7+
//~^ ERROR the size for values of type `T`
8+
}
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0277]: the size for values of type `T` cannot be known at compilation time
2+
--> $DIR/feature-gate-lazy_normalization_consts.rs:6:23
3+
|
4+
LL | pub const fn sof<T>() -> usize {
5+
| - required by this bound in `sof`
6+
...
7+
LL | fn test<T>() {
8+
| - this type parameter needs to be `std::marker::Sized`
9+
LL | let _: [u8; sof::<T>()];
10+
| ^ doesn't have a size known at compile-time
11+
|
12+
= help: the trait `std::marker::Sized` is not implemented for `T`
13+
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
14+
help: consider relaxing the implicit `Sized` restriction
15+
|
16+
LL | pub const fn sof<T: ?Sized>() -> usize {
17+
| ^^^^^^^^
18+
19+
error: aborting due to previous error
20+
21+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
#![feature(lazy_normalization_consts)]
3+
#![allow(incomplete_features)]
4+
pub struct ArpIPv4<'a> {
5+
_s: &'a u8
6+
}
7+
8+
impl<'a> ArpIPv4<'a> {
9+
const LENGTH: usize = 20;
10+
11+
pub fn to_buffer() -> [u8; Self::LENGTH] {
12+
unimplemented!()
13+
}
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(lazy_normalization_consts)]
2+
//~^ WARN the feature `lazy_normalization_consts` is incomplete
3+
trait ArraySizeTrait {
4+
const SIZE: usize = 0;
5+
}
6+
7+
impl<T: ?Sized> ArraySizeTrait for T {
8+
const SIZE: usize = 1;
9+
}
10+
11+
struct SomeArray<T: ArraySizeTrait> {
12+
array: [u8; T::SIZE],
13+
//~^ ERROR constant expression depends on a generic parameter
14+
phantom: std::marker::PhantomData<T>,
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
warning: the feature `lazy_normalization_consts` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/issue-57739.rs:1:12
3+
|
4+
LL | #![feature(lazy_normalization_consts)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
= note: see issue #72219 <https://github.com/rust-lang/rust/issues/72219> for more information
9+
10+
error: constant expression depends on a generic parameter
11+
--> $DIR/issue-57739.rs:12:5
12+
|
13+
LL | array: [u8; T::SIZE],
14+
| ^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: this may fail depending on what value the parameter takes
17+
18+
error: aborting due to previous error; 1 warning emitted
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// check-pass
2+
#![feature(lazy_normalization_consts)]
3+
#![allow(incomplete_features)]
4+
5+
pub struct X<P, Q>(P, Q);
6+
pub struct L<T: ?Sized>(T);
7+
8+
impl<T: ?Sized> L<T> {
9+
const S: usize = 1;
10+
}
11+
12+
impl<T> X<T, [u8; L::<T>::S]> {}
13+
14+
fn main() {}

0 commit comments

Comments
 (0)