Skip to content

Generic Constants Nightly breaks with Rust 1.68 #107265

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
JacobGorman613 opened this issue Jan 24, 2023 · 4 comments
Open

Generic Constants Nightly breaks with Rust 1.68 #107265

JacobGorman613 opened this issue Jan 24, 2023 · 4 comments
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]` requires-incomplete-features This issue requires the use of incomplete features. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@JacobGorman613
Copy link

The following code compiles on Rust 1.67 (nightly) but not on Rust 1.68 (nightly)

Code

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

fn main() {
    println!("Hello, world!");
}

// FUNCTION THAT IS NOW BROKEN
fn do_something<T: SuperTrait>() where [(); T::MyType::LEN]:, [(); T::SUPER_SIZE]:  {
    let _ = T::do_something_super().to_bytes();
}

// Trait I implement for a few subtypes of my main type "SuperTrait"
trait MySerde {
    const LEN: usize;

    fn to_bytes(&self) -> [u8; Self::LEN];
    fn from_bytes(bytes: [u8; Self::LEN]) -> Self;
}

// each subtrait would have its own requirements
trait SubTrait: MySerde {
    // stuff
}

// We need a subtrait for constants or we get a cycle when defining SUPER_ARR
trait ConstantsForSuper {
    const SUPER_SIZE: usize;
}

// in my actual example SuperTrait has several types implementing differeent subtraits
// (all of which require serde)
trait SuperTrait: ConstantsForSuper where [(); Self::SUPER_SIZE]: {
    type MyType: SubTrait;

    const SUPER_ARR: [Self::MyType; Self::SUPER_SIZE];

    fn do_something_super() -> Self::MyType;
}

Version it worked on

It most recently worked on: 1.67

Version with regression

rustc --version --verbose:

rustc 1.68.0-nightly (4781233a7 2023-01-16)
binary: rustc
commit-hash: 4781233a77e879e49cb5ce3c98d2abba6a6ade7a
commit-date: 2023-01-16
host: x86_64-unknown-linux-gnu
release: 1.68.0-nightly
LLVM version: 15.0.6

Backtrace

Backtrace

/~/project$ RUST_BACKTRACE=1 cargo build
   Compiling minimal_breakage v0.1.0 (/~/project)
error[E0391]: cycle detected when building an abstract representation for `do_something::{constant#0}`
 --> src/main.rs:9:45
  |
9 | fn do_something<T: SuperTrait>() where [(); T::MyType::LEN]:, [(); T::SUPER_SIZE]:  {
  |                                             ^^^^^^^^^^^^^^
  |
note: ...which requires building THIR for `do_something::{constant#0}`...
 --> src/main.rs:9:45
  |
9 | fn do_something<T: SuperTrait>() where [(); T::MyType::LEN]:, [(); T::SUPER_SIZE]:  {
  |                                             ^^^^^^^^^^^^^^
note: ...which requires type-checking `do_something::{constant#0}`...
 --> src/main.rs:9:45
  |
9 | fn do_something<T: SuperTrait>() where [(); T::MyType::LEN]:, [(); T::SUPER_SIZE]:  {
  |                                             ^^^^^^^^^^^^^^
  = note: ...which again requires building an abstract representation for `do_something::{constant#0}`, completing the cycle
note: cycle used when checking that `do_something` is well-formed
 --> src/main.rs:9:1
  |
9 | fn do_something<T: SuperTrait>() where [(); T::MyType::LEN]:, [(); T::SUPER_SIZE]:  {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0391`.
error: could not compile `minimal_breakage` due to previous error

@JacobGorman613 JacobGorman613 added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Jan 24, 2023
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jan 24, 2023
@BoxyUwU BoxyUwU added A-const-generics Area: const generics (parameters and arguments) F-generic_const_exprs `#![feature(generic_const_exprs)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. regression-untriaged Untriaged performance or correctness regression. labels Jan 24, 2023
@compiler-errors compiler-errors added the E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc label Jan 24, 2023
@apiraino apiraino added the requires-nightly This issue requires a nightly compiler in some way. label Jan 25, 2023
@clubby789
Copy link
Contributor

searched nightlies: from nightly-2022-12-01 to nightly-2023-01-26
regressed nightly: nightly-2023-01-10
searched commit range: cc47b06...3020239
regressed commit: 3020239

bisected with cargo-bisect-rustc v0.6.5

Host triple: x86_64-unknown-linux-gnu
Reproduce with:

cargo bisect-rustc --start 2022-12-1 --end 2023-01-26 
Might be #105292

@rustbot label -E-needs-bisection

@rustbot rustbot removed the E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc label Jan 26, 2023
@JulianKnodt
Copy link
Contributor

I would note that this may have accidentally been permitted to compile. I reran the example with rustc 1.64.0-nightly (affe0d3a0 2022-08-05), and it also doesn't compile with the same error message. It was likely that a prior commit "fixed" this, but it was not intended to compile.

I am of the personal belief that this should compile, since it seems like the error is continually pointing at the same variable for a cyclic type, but I'm not sure whether that is actually something that Rust would want to support.

Independent of whether it should compile or not, the error messages also expose a lot of internal details of the compiler, which is fine for now since generic_const_exprs is a WIP, but probably should be cleaned up at some point?

@workingjubilee workingjubilee added the requires-incomplete-features This issue requires the use of incomplete features. label Mar 4, 2023
@ajwerner
Copy link

I'm seeing a similar (but not identical regression) which I also bisected to #105292.

My impl block begins:

    impl<'a, T: Type> Copied<'a, Pointer<T>>
    where
        [(); <NonNullPointer<T>>::SIZE]:,
        [(); <Pointer<T>>::SIZE]:,
        [(); T::SIZE]:,
    {

and the compiler errors are

error: unconstrained generic constant                                                                                                                                                        
    --> src/main.rs:1760:9                                                                                                                                                                   
     |                                                                                                                                                                                       
1760 |         [(); <NonNullPointer<T>>::SIZE]:,                                                                                                                                             
     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                               
     |                                                                                                                                                                                       
     = help: try adding a `where` bound using this expression: `where [(); <NonNullPointer<T>>::SIZE]:`                                                                                      
note: required by a bound in `target::Copied<'a, target::Pointer<T>>`                                                                                                                        
    --> src/main.rs:1760:14                                                                                                                                                                  
     |                                                                                                                                                                                       
1760 |         [(); <NonNullPointer<T>>::SIZE]:,                                                                                                                                             
     |              ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Copied<'a, Pointer<T>>`                                                                                             
                                                                                                                                                                                             
error: unconstrained generic constant                                                                                                                                                        
    --> src/main.rs:1760:9                                                                                                                                                                   
     |                                                                                                                                                                                       
1760 |         [(); <NonNullPointer<T>>::SIZE]:,                                                                                                                                             
     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                               
     |
     = help: try adding a `where` bound using this expression: `where [(); <Pointer<T>>::SIZE]:`
note: required by a bound in `target::Copied<'a, target::Pointer<T>>`
    --> src/main.rs:1761:14
     |
1761 |         [(); <Pointer<T>>::SIZE]:,
     |              ^^^^^^^^^^^^^^^^^^ required by this bound in `Copied<'a, Pointer<T>>`

error: unconstrained generic constant
    --> src/main.rs:1758:23
     |
1758 |     impl<'a, T: Type> Copied<'a, Pointer<T>>
     |                       ^^^^^^^^^^^^^^^^^^^^^^
     |
     = help: try adding a `where` bound using this expression: `where [(); T::SIZE]:`
note: required by a bound in `target::Copied`
    --> src/main.rs:1731:14
     |
1729 |     pub enum Copied<'a, T: Type>
     |              ------ required by a bound in this enum
1730 |     where
1731 |         [(); T::SIZE]:,
     |              ^^^^^^^ required by this bound in `Copied`

Note that at the very least the suggested remediation is not helpful - the compiler is suggesting to add the same code that is already present.

/cc @tamird

@ajwerner
Copy link

Here is a minimal reproduction that works before #105292.

tamird added a commit to tamird/rust that referenced this issue Nov 7, 2023
./x.py test --stage 1 tests/ui/const-generics/generic_const_exprs

rust-lang#107265
tamird added a commit to tamird/rust that referenced this issue Nov 17, 2023
./x.py test --stage 1 tests/ui/const-generics/generic_const_exprs

rust-lang#107265
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]` requires-incomplete-features This issue requires the use of incomplete features. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

9 participants