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 on inline const pattern using const generic parameter #90150

Closed
Esper89 opened this issue Oct 22, 2021 · 1 comment · Fixed by #91570
Closed

ICE on inline const pattern using const generic parameter #90150

Esper89 opened this issue Oct 22, 2021 · 1 comment · Fixed by #91570
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)]` F-inline_const Inline constants (aka: const blocks, const expressions, anonymous constants) glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ 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

@Esper89
Copy link

Esper89 commented Oct 22, 2021

Using a const parameter in an inline const pattern causes an ICE, but only when you don't directly use the parameter. This seems to be the same problem as #82518, although under different circumstances.

Code

#![feature(inline_const)]
#![feature(generic_const_exprs)]

const fn foo(val: usize) -> usize { val }

fn bar<const N: usize>(num: usize) -> &'static str
where [(); foo(N)]:
{
    match num {
        const { foo(N) } => "fizz",
        _ => "buzz"
    }
}

Using const { N } instead of const { foo(N) } gives

error[E0158]: const parameters cannot be referenced in patterns

Disappointing, but undoubtedly the intended behavior.

The above example wouldn't be easy to come across naturally, but if the const parameter was (for example) an array and you tried to index it, the compiler would tell you to use a const fn to do that. This code causes the same ICE:

#![feature(adt_const_params)]
#![feature(inline_const)]
#![feature(generic_const_exprs)]

const fn get(array: [usize; 5], index: usize) -> usize { array[index] }

fn bar<const A: [usize; 5]>(num: usize) -> &'static str
where [(); get(A, 0)]:
{
    match num {
        const { get(A, 0) } => "fizz",
        _ => "buzz"
    }
}

I should note that this issue is only present with the inline_const feature and the generic_const_exprs feature.

Meta

rustc --version --verbose:

rustc 1.58.0-nightly (efd048394 2021-10-20)
binary: rustc
commit-hash: efd0483949496b067cd5f7569d1b28cd3d5d3c72
commit-date: 2021-10-20
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0

Error output

error: internal compiler error: /rustc/efd0483949496b067cd5f7569d1b28cd3d5d3c72/compiler/rustc_middle/src/ty/consts.rs:194:32: expected bits of usize, got Const {
    ty: usize,
    val: Unevaluated(
        Unevaluated {
            def: WithOptConstParam {
                did: DefId(0:62 ~ const_testing_stuff[3d48]::arrays::bar::{constant#2}),
                const_param_did: None,
            },
            substs_: None,
            promoted: None,
        },
    ),
}
Backtrace

thread 'rustc' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1147:9
stack backtrace:
   0: std::panicking::begin_panic
   1: std::panic::panic_any
   2: rustc_errors::HandlerInner::bug
   3: rustc_errors::Handler::bug
   4: rustc_middle::ty::context::tls::with_opt
   5: rustc_middle::util::bug::opt_span_bug_fmt
   6: rustc_middle::util::bug::bug_fmt
   7: indexmap::map::core::Entry<K,V>::or_insert_with
   8: rustc_mir_build::build::matches::<impl rustc_mir_build::build::Builder>::test_candidates_with_or
   9: rustc_data_structures::stack::ensure_sufficient_stack
  10: rustc_mir_build::build::matches::<impl rustc_mir_build::build::Builder>::lower_match_tree
  11: rustc_mir_build::build::matches::<impl rustc_mir_build::build::Builder>::match_expr
  12: rustc_mir_build::build::expr::into::<impl rustc_mir_build::build::Builder>::expr_into_dest
  13: rustc_mir_build::build::expr::into::<impl rustc_mir_build::build::Builder>::expr_into_dest
  14: rustc_mir_build::build::block::<impl rustc_mir_build::build::Builder>::ast_block_stmts
  15: rustc_mir_build::build::scope::<impl rustc_mir_build::build::Builder>::in_scope
  16: rustc_mir_build::build::expr::into::<impl rustc_mir_build::build::Builder>::expr_into_dest
  17: rustc_mir_build::build::expr::into::<impl rustc_mir_build::build::Builder>::expr_into_dest
  18: rustc_mir_build::build::expr::into::<impl rustc_mir_build::build::Builder>::expr_into_dest
  19: rustc_mir_build::build::construct_fn
  20: rustc_infer::infer::InferCtxtBuilder::enter
  21: rustc_mir_build::build::mir_built
  22: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task
  23: rustc_data_structures::stack::ensure_sufficient_stack
  24: rustc_query_system::query::plumbing::try_execute_query
  25: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::mir_built
  26: rustc_mir_transform::check_unsafety::unsafety_check_result
  27: core::ops::function::FnOnce::call_once
  28: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task
  29: rustc_data_structures::stack::ensure_sufficient_stack
  30: rustc_query_system::query::plumbing::try_execute_query
  31: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::unsafety_check_result
  32: rustc_mir_transform::mir_const
  33: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task
  34: rustc_data_structures::stack::ensure_sufficient_stack
  35: rustc_query_system::query::plumbing::try_execute_query
  36: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::mir_const
  37: rustc_mir_transform::mir_promoted
  38: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task
  39: rustc_data_structures::stack::ensure_sufficient_stack
  40: rustc_query_system::query::plumbing::try_execute_query
  41: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::mir_promoted
  42: core::ops::function::FnOnce::call_once
  43: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task
  44: rustc_data_structures::stack::ensure_sufficient_stack
  45: rustc_query_system::query::plumbing::try_execute_query
  46: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::mir_borrowck
  47: rustc_middle::hir::map::Map::par_body_owners
  48: rustc_session::utils::<impl rustc_session::session::Session>::time
  49: rustc_interface::passes::analysis
  50: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task
  51: rustc_data_structures::stack::ensure_sufficient_stack
  52: rustc_query_system::query::plumbing::try_execute_query
  53: rustc_query_system::query::plumbing::get_query
  54: rustc_interface::passes::QueryContext::enter
  55: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  56: rustc_span::with_source_map
  57: scoped_tls::ScopedKey<T>::set

@Esper89 Esper89 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 Oct 22, 2021
@nhamovitz
Copy link
Contributor

@rustbot label +A-const-generics

@rustbot rustbot added the A-const-generics Area: const generics (parameters and arguments) label Oct 22, 2021
@JohnTitor JohnTitor added F-generic_const_exprs `#![feature(generic_const_exprs)]` F-inline_const Inline constants (aka: const blocks, const expressions, anonymous constants) requires-nightly This issue requires a nightly compiler in some way. labels Oct 22, 2021
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Oct 22, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 23, 2021
Split inline const to two feature gates and mark expression position inline const complete

This PR splits inline const in pattern position into its own `#![feature(inline_const_pat)]` feature gate, and make the usage in expression position complete.

I think I have resolved most outstanding issues related to `inline_const` with rust-lang#89561 and other PRs. The only thing left that I am aware of is rust-lang#90150 and the lack of lifetime checks when inline const is used in pattern position (FIXME in rust-lang#89561). Implementation-wise when used in pattern position it has to be lowered during MIR building while in expression position it's evaluated only when monomorphizing (just like normal consts), so it makes some sense to separate it into two feature gates so one can progress without being blocked by another.

`@rustbot` label: T-compiler F-inline_const
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 23, 2021
Split inline const to two feature gates and mark expression position inline const complete

This PR splits inline const in pattern position into its own `#![feature(inline_const_pat)]` feature gate, and make the usage in expression position complete.

I think I have resolved most outstanding issues related to `inline_const` with rust-lang#89561 and other PRs. The only thing left that I am aware of is rust-lang#90150 and the lack of lifetime checks when inline const is used in pattern position (FIXME in rust-lang#89561). Implementation-wise when used in pattern position it has to be lowered during MIR building while in expression position it's evaluated only when monomorphizing (just like normal consts), so it makes some sense to separate it into two feature gates so one can progress without being blocked by another.

``@rustbot`` label: T-compiler F-inline_const
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 8, 2021
Evaluate inline const pat early and report error if too generic

Fix rust-lang#90150

`@rustbot` label: T-compiler F-inline_const
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 8, 2021
Evaluate inline const pat early and report error if too generic

Fix rust-lang#90150

``@rustbot`` label: T-compiler F-inline_const
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 8, 2021
Evaluate inline const pat early and report error if too generic

Fix rust-lang#90150

```@rustbot``` label: T-compiler F-inline_const
@bors bors closed this as completed in 67c5832 Dec 8, 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-generic_const_exprs `#![feature(generic_const_exprs)]` F-inline_const Inline constants (aka: const blocks, const expressions, anonymous constants) glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ 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

Successfully merging a pull request may close this issue.

5 participants