Skip to content

Commit

Permalink
add lazy_normalization_consts feature gate
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Jul 4, 2020
1 parent dbf3ae7 commit 7a3081b
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/librustc_feature/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,9 @@ declare_features! (
/// Allows capturing variables in scope using format_args!
(active, format_args_capture, "1.46.0", Some(67984), None),

/// Lazily evaluate constants. This allows constants to depend on type parameters.
(active, lazy_normalization_consts, "1.46.0", Some(72219), None),

// -------------------------------------------------------------------------
// feature-group-end: actual feature gates
// -------------------------------------------------------------------------
Expand All @@ -586,5 +589,6 @@ pub const INCOMPLETE_FEATURES: &[Symbol] = &[
sym::raw_dylib,
sym::const_trait_impl,
sym::const_trait_bound_opt_out,
sym::lazy_normalization_consts,
sym::specialization,
];
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ impl<'tcx> TyCtxt<'tcx> {
/// we still evaluate them eagerly.
#[inline]
pub fn lazy_normalization(self) -> bool {
self.features().const_generics
self.features().const_generics || self.features().lazy_normalization_consts
}

#[inline]
Expand Down
1 change: 1 addition & 0 deletions src/librustc_span/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ symbols! {
label_break_value,
lang,
lang_items,
lazy_normalization_consts,
lateout,
let_chains,
lhs,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub const fn sof<T>() -> usize {
10
}

fn test<T>() {
let _: [u8; sof::<T>()];
//~^ ERROR the size for values of type `T`
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0277]: the size for values of type `T` cannot be known at compilation time
--> $DIR/feature-gate-lazy_normalization_consts.rs:6:23
|
LL | pub const fn sof<T>() -> usize {
| - required by this bound in `sof`
...
LL | fn test<T>() {
| - this type parameter needs to be `std::marker::Sized`
LL | let _: [u8; sof::<T>()];
| ^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `T`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
help: consider relaxing the implicit `Sized` restriction
|
LL | pub const fn sof<T: ?Sized>() -> usize {
| ^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
16 changes: 16 additions & 0 deletions src/test/ui/lazy_normalization_consts/issue-47814.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// check-pass
#![feature(lazy_normalization_consts)]
#![allow(incomplete_features)]
pub struct ArpIPv4<'a> {
_s: &'a u8
}

impl<'a> ArpIPv4<'a> {
const LENGTH: usize = 20;

pub fn to_buffer() -> [u8; Self::LENGTH] {
unimplemented!()
}
}

fn main() {}
17 changes: 17 additions & 0 deletions src/test/ui/lazy_normalization_consts/issue-57739.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![feature(lazy_normalization_consts)]
//~^ WARN the feature `lazy_normalization_consts` is incomplete
trait ArraySizeTrait {
const SIZE: usize = 0;
}

impl<T: ?Sized> ArraySizeTrait for T {
const SIZE: usize = 1;
}

struct SomeArray<T: ArraySizeTrait> {
array: [u8; T::SIZE],
//~^ ERROR constant expression depends on a generic parameter
phantom: std::marker::PhantomData<T>,
}

fn main() {}
19 changes: 19 additions & 0 deletions src/test/ui/lazy_normalization_consts/issue-57739.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
warning: the feature `lazy_normalization_consts` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/issue-57739.rs:1:12
|
LL | #![feature(lazy_normalization_consts)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #72219 <https://github.com/rust-lang/rust/issues/72219> for more information

error: constant expression depends on a generic parameter
--> $DIR/issue-57739.rs:12:5
|
LL | array: [u8; T::SIZE],
| ^^^^^^^^^^^^^^^^^^^^
|
= note: this may fail depending on what value the parameter takes

error: aborting due to previous error; 1 warning emitted

14 changes: 14 additions & 0 deletions src/test/ui/lazy_normalization_consts/issue-73980.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// check-pass
#![feature(lazy_normalization_consts)]
#![allow(incomplete_features)]

pub struct X<P, Q>(P, Q);
pub struct L<T: ?Sized>(T);

impl<T: ?Sized> L<T> {
const S: usize = 1;
}

impl<T> X<T, [u8; L::<T>::S]> {}

fn main() {}

0 comments on commit 7a3081b

Please sign in to comment.