Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

ICE using const_generics_defaults #83938

Closed
HeroicKatora opened this issue Apr 6, 2021 · 1 comment · Fixed by #86580
Closed

ICE using const_generics_defaults #83938

HeroicKatora opened this issue Apr 6, 2021 · 1 comment · Fixed by #86580
Assignees
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics_defaults `#![feature(const_generics_defaults)]` F-const_generics `#![feature(const_generics)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@HeroicKatora
Copy link
Contributor

HeroicKatora commented Apr 6, 2021

Code

#![crate_type = "lib"]
#![feature(const_evaluatable_checked, const_generics, const_generics_defaults)]
#![allow(incomplete_features)]

type NpOne<T, const N: usize, const NP: usize = {N+1usize}> = [T; NP];

pub fn push<T, const N: usize>(_: [T; N], _: T)
    -> NpOne<T, N>
{
    todo!()
}

(Playground link)

I'm not sure how to treat this. It seems that we can sneakily misuse defaults in type definitions to insert a dependent computation on a const parameter which definitely should be forbidden at the moment.

Meta

rustc --version --verbose:

 rustc 1.53.0-nightly (d32238532 2021-04-05) running on x86_64-unknown-linux-gnu

Error output

error: internal compiler error: broken MIR in DefId(0:9 ~ playground[e970]::push) (LocalDecl { mutability: Mut, local_info: None, internal: false, is_block_tail: None, ty: [type error], user_ty: None, source_info: SourceInfo { span: src/lib.rs:5:1: 6:19 (#0), scope: scope[0] } }): bad type [type error]
 --> src/lib.rs:5:1
  |
5 | / pub fn push<T, const N: usize>(_: [T; N], _: T)
6 | |     -> NpOne<T, N>
  | |__________________^
  |
  = note: delayed at compiler/rustc_mir/src/borrow_check/type_check/mod.rs:252:27

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', compiler/rustc_errors/src/lib.rs:1018:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Backtrace

warning: the feature `const_generics_defaults` is incomplete and may not be safe to use and/or cause compiler crashes
 --> src/lib.rs:1:12
  |
1 | #![feature(const_generics_defaults)]
  |            ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(incomplete_features)]` on by default
  = note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information

warning: 1 warning emitted

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at compiler/rustc_typeck/src/check/expr.rs:473:21

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at compiler/rustc_typeck/src/check/coercion.rs:154:49

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at compiler/rustc_typeck/src/check/coercion.rs:868:53

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at compiler/rustc_trait_selection/src/traits/project.rs:710:28

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at compiler/rustc_infer/src/infer/sub.rs:124:31

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at compiler/rustc_typeck/src/check/coercion.rs:1261:42

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at compiler/rustc_typeck/src/check/fn_ctxt/checks.rs:714:27

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs:652:60

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at /rustc/d32238532138485c80db4f2cd596372bce214e00/compiler/rustc_middle/src/ty/relate.rs:334:59

error: internal compiler error: cat_expr Errd
 --> src/lib.rs:3:49
  |
3 | type NpOne<T, const N: usize, const NP: usize = {N+1usize}> = [T; NP];
  |                                                 ^^^^^^^^^^
  |
  = note: delayed at compiler/rustc_typeck/src/check/regionck.rs:402:31

error: internal compiler error: cat_expr Errd
 --> src/lib.rs:3:50
  |
3 | type NpOne<T, const N: usize, const NP: usize = {N+1usize}> = [T; NP];
  |                                                  ^^^^^^^^
  |
  = note: delayed at compiler/rustc_typeck/src/check/regionck.rs:402:31

error: internal compiler error: cat_expr Errd
 --> src/lib.rs:3:50
  |
3 | type NpOne<T, const N: usize, const NP: usize = {N+1usize}> = [T; NP];
  |                                                  ^
  |
  = note: delayed at compiler/rustc_typeck/src/check/regionck.rs:402:31

error: internal compiler error: ty::ConstKind::Error constructed but no error reported.
  |
  = note: delayed at /rustc/d32238532138485c80db4f2cd596372bce214e00/compiler/rustc_middle/src/ty/consts.rs:184:43

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at /rustc/d32238532138485c80db4f2cd596372bce214e00/compiler/rustc_trait_selection/src/opaque_types.rs:1007:32

error: internal compiler error: cat_expr Errd
 --> src/lib.rs:7:1
  |
7 | / {
8 | |     todo!()
9 | | }
  | |_^
  |
  = note: delayed at compiler/rustc_typeck/src/check/regionck.rs:402:31

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at compiler/rustc_mir_build/src/build/mod.rs:742:18

error: internal compiler error: mir_const_qualif: MIR had errors
 --> src/lib.rs:3:49
  |
3 | type NpOne<T, const N: usize, const NP: usize = {N+1usize}> = [T; NP];
  |                                                 ^^^^^^^^^^
  |
  = note: delayed at compiler/rustc_mir/src/transform/mod.rs:237:18

error: internal compiler error: PromoteTemps: MIR had errors
 --> src/lib.rs:3:49
  |
3 | type NpOne<T, const N: usize, const NP: usize = {N+1usize}> = [T; NP];
  |                                                 ^^^^^^^^^^
  |
  = note: delayed at compiler/rustc_mir/src/transform/promote_consts.rs:55:22

error: internal compiler error: broken MIR in DefId(0:7 ~ playground[e970]::NpOne::{constant#0}) ("return type"): bad type [type error]
 --> src/lib.rs:3:49
  |
3 | type NpOne<T, const N: usize, const NP: usize = {N+1usize}> = [T; NP];
  |                                                 ^^^^^^^^^^
  |
  = note: delayed at compiler/rustc_mir/src/borrow_check/type_check/mod.rs:252:27

error: internal compiler error: TyKind::Error constructed but no error reported
  |
  = note: delayed at compiler/rustc_mir/src/borrow_check/type_check/mod.rs:721:20

error: internal compiler error: broken MIR in DefId(0:7 ~ playground[e970]::NpOne::{constant#0}) (LocalDecl { mutability: Mut, local_info: None, internal: false, is_block_tail: None, ty: [type error], user_ty: None, source_info: SourceInfo { span: src/lib.rs:3:49: 3:59 (#0), scope: scope[0] } }): bad type [type error]
 --> src/lib.rs:3:49
  |
3 | type NpOne<T, const N: usize, const NP: usize = {N+1usize}> = [T; NP];
  |                                                 ^^^^^^^^^^
  |
  = note: delayed at compiler/rustc_mir/src/borrow_check/type_check/mod.rs:252:27

error: internal compiler error: PromoteTemps: MIR had errors
 --> src/lib.rs:5:1
  |
5 | / pub fn push<T, const N: usize>(_: [T; N], _: T)
6 | |     -> NpOne<T, N>
  | |__________________^
  |
  = note: delayed at compiler/rustc_mir/src/transform/promote_consts.rs:55:22

error: internal compiler error: broken MIR in DefId(0:9 ~ playground[e970]::push) ("return type"): bad type [type error]
 --> src/lib.rs:5:1
  |
5 | / pub fn push<T, const N: usize>(_: [T; N], _: T)
6 | |     -> NpOne<T, N>
  | |__________________^
  |
  = note: delayed at compiler/rustc_mir/src/borrow_check/type_check/mod.rs:252:27

error: internal compiler error: broken MIR in DefId(0:9 ~ playground[e970]::push) (LocalDecl { mutability: Mut, local_info: None, internal: false, is_block_tail: None, ty: [type error], user_ty: None, source_info: SourceInfo { span: src/lib.rs:5:1: 6:19 (#0), scope: scope[0] } }): bad type [type error]
 --> src/lib.rs:5:1
  |
5 | / pub fn push<T, const N: usize>(_: [T; N], _: T)
6 | |     -> NpOne<T, N>
  | |__________________^
  |
  = note: delayed at compiler/rustc_mir/src/borrow_check/type_check/mod.rs:252:27

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', compiler/rustc_errors/src/lib.rs:1018:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.53.0-nightly (d32238532 2021-04-05) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type lib

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
error: could not compile `playground`

To learn more, run the command again with --verbose.

@HeroicKatora HeroicKatora added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 6, 2021
@jonas-schievink jonas-schievink added the A-const-generics Area: const generics (parameters and arguments) label Apr 6, 2021
fanninpm added a commit to fanninpm/glacier that referenced this issue Apr 9, 2021
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Apr 10, 2021
@JohnTitor JohnTitor added F-const_generics `#![feature(const_generics)]` F-const_generics_defaults `#![feature(const_generics_defaults)]` labels Apr 26, 2021
@BoxyUwU
Copy link
Member

BoxyUwU commented Jun 9, 2021

while still ICE'ing, the error output is different now:
playground

#![feature(const_evaluatable_checked, const_generics, const_generics_defaults)]
#![allow(incomplete_features)]

pub struct Bar<const N: usize, const M: usize = { N + 1 }>;
pub fn foo<const N1: usize>() -> Bar<N1> { loop {} }

fn main() {}

output:

error: internal compiler error: compiler/rustc_middle/src/ty/subst.rs:577:17: const parameter `M/#1` (Const { ty: usize, val: Param(M/#1) }/1) out of range when substituting substs=[Const { ty: usize, val: Param(N1/#0) }]
 --> src/main.rs:5:34
  |
5 | pub fn foo<const N1: usize>() -> Bar<N1> { loop {} }
  |                                  ^^^^^^^

thread 'rustc' panicked at 'Box<dyn Any>', /media/ellen-nyan/Nyoomies/rust/compiler/rustc_errors/src/lib.rs:953:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.54.0-dev running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type bin

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [fn_sig] computing function signature of `foo`
#1 [collect_mod_item_types] collecting item types in top-level module
end of query stack
error: aborting due to previous error

error: could not compile `playground-cgd-subst-ice`

I believe the issue here is that when we build the substs for Bar<N1> we call .subst on the { N + 1 } anon const which currently looks a bit like: ConstKind::Unevaluated({N + 1}, substs: [N1, M]). This causes us to try and substitute the M with something from the provided substs but we dont have anything to subst as we're currently creating the value to use as substs for M

@rustbot claim

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jul 21, 2021
dont provide fwd declared params to cg defaults

Fixes rust-lang#83938

```rust
#![feature(const_evaluatable_checked, const_generics, const_generics_defaults)]
#![allow(incomplete_features)]

pub struct Bar<const N: usize, const M: usize = { N + 1 }>;
pub fn foo<const N1: usize>() -> Bar<N1> { loop {} }

fn main() {}
```
This PR makes this code no longer ICE, it was ICE'ing previously because when building substs for `Bar<N1>` we would subst the anon ct: `ConstKind::Unevaluated({N + 1}, substs: [N, M])` with substs of `[N1]`. the anon const has forward declared params supplied though so we end up trying to substitute the provided `M` param which causes the ICE.

This PR doesn't handle the predicates of the const so
```rust
trait Foo<const N: usize> { const Assoc: usize; }
pub struct Bar<const N: usize = { <()>::Assoc }> where (): Foo<N>;
```
Resolves to `<() as Foo<N>>::Assoc` which can allow for using fwd declared params indirectly.

```rust
trait Foo<const N: usize> {}
struct Bar<const N: usize = { 2 + 3 }> where (): Foo<N>;
```
This code also ICEs under this PR because instantiating the default's predicates causes an ICE as predicates_of contains predicates with fwd declared params

PR was briefly discussed [in this zulip thread](https://rust-lang.zulipchat.com/#narrow/stream/260443-project-const-generics/topic/evil.20preds.20in.20param.20env.20.2386580)
@bors bors closed this as completed in d9aa287 Jul 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics_defaults `#![feature(const_generics_defaults)]` F-const_generics `#![feature(const_generics)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants