rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.15k stars 12.69k forks source link

ICE on inline const pattern using const generic parameter #90150

Closed Esper89 closed 2 years ago

Esper89 commented 3 years ago

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', 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::or_insert_with 8: rustc_mir_build::build::matches::::test_candidates_with_or 9: rustc_data_structures::stack::ensure_sufficient_stack 10: rustc_mir_build::build::matches::::lower_match_tree 11: rustc_mir_build::build::matches::::match_expr 12: rustc_mir_build::build::expr::into::::expr_into_dest 13: rustc_mir_build::build::expr::into::::expr_into_dest 14: rustc_mir_build::build::block::::ast_block_stmts 15: rustc_mir_build::build::scope::::in_scope 16: rustc_mir_build::build::expr::into::::expr_into_dest 17: rustc_mir_build::build::expr::into::::expr_into_dest 18: rustc_mir_build::build::expr::into::::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::with_task 23: rustc_data_structures::stack::ensure_sufficient_stack 24: rustc_query_system::query::plumbing::try_execute_query 25: ::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::with_task 29: rustc_data_structures::stack::ensure_sufficient_stack 30: rustc_query_system::query::plumbing::try_execute_query 31: ::unsafety_check_result 32: rustc_mir_transform::mir_const 33: rustc_query_system::dep_graph::graph::DepGraph::with_task 34: rustc_data_structures::stack::ensure_sufficient_stack 35: rustc_query_system::query::plumbing::try_execute_query 36: ::mir_const 37: rustc_mir_transform::mir_promoted 38: rustc_query_system::dep_graph::graph::DepGraph::with_task 39: rustc_data_structures::stack::ensure_sufficient_stack 40: rustc_query_system::query::plumbing::try_execute_query 41: ::mir_promoted 42: core::ops::function::FnOnce::call_once 43: rustc_query_system::dep_graph::graph::DepGraph::with_task 44: rustc_data_structures::stack::ensure_sufficient_stack 45: rustc_query_system::query::plumbing::try_execute_query 46: ::mir_borrowck 47: rustc_middle::hir::map::Map::par_body_owners 48: rustc_session::utils::::time 49: rustc_interface::passes::analysis 50: rustc_query_system::dep_graph::graph::DepGraph::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::::enter 56: rustc_span::with_source_map 57: scoped_tls::ScopedKey::set ```

nhamovitz commented 3 years ago

@rustbot label +A-const-generics