Skip to content

String-based const generics, associated types and where Self clause ICE when implementors are explicitly selected #71348

Closed
@hadronized

Description

@hadronized

Hi,

I was playing with const_generics and get ICEd. Basically, I was trying to make a “type-level-string-based” getter, by using a const &'static str, using a trait and associated type to get. The trait declaration and implementors are okay. However, usage is not, especially when using the trait in a where Self: _ clause.

Code

#![feature(const_generics)]

struct Foo {
  i: i32,
}

trait Get<'a, const N: &'static str> {
  type Target: 'a;

  fn get(&'a self) -> &'a Self::Target;
}

impl Foo {
  fn ask<'a, const N: &'static str>(&'a self) -> &'a <Self as Get<N>>::Target
  where
    Self: Get<'a, N>,
  {
    self.get()
  }
}

impl<'a> Get<'a, "int"> for Foo {
  type Target = i32;

  fn get(&'a self) -> &'a Self::Target {
    &self.i
  }
}

fn main() {
  let foo = Foo { i: 123 };

  //println!("{}", foo.ask()); // okay
  println!("{}", foo.ask::<"int">()); // ICE
}

Meta

rustc --version --verbose:

rustc 1.44.0-nightly (52fa23add 2020-04-18)
binary: rustc
commit-hash: 52fa23add6fb0776b32cc591ac928618391bdf41
commit-date: 2020-04-18
host: x86_64-unknown-linux-gnu
release: 1.44.0-nightly
LLVM version: 9.0

Error output

whoopsies is 📦 v0.1.0 via 🦀 v1.44.0-nightly via  25% 
 cargo build 
   Compiling whoopsies v0.1.0 (/tmp/whoopsies)
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
 --> src/main.rs:1:12
  |
1 | #![feature(const_generics)]
  |            ^^^^^^^^^^^^^^
  |
  = note: `#[warn(incomplete_features)]` on by default

thread 'rustc' panicked at 'index out of bounds: the len is 0 but the index is 0', src/librustc_mir_build/hair/pattern/_match.rs:2325: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/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.44.0-nightly (52fa23add 2020-04-18) running on x86_64-unknown-linux-gnu

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

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

warning: 1 warning emitted

error: internal compiler error: unexpected const parent in type_of_def_id(): Expr(Expr { hir_id: HirId { owner: DefId(0:19 ~ whoopsies[b95c]::main[0]), local_id: 30 }, kind: MethodCall(PathSegment { ident: ask#0, hir_id: Some(HirId { owner: DefId(0:19 ~ whoopsies[b95c]::main[0]), local_id: 27 }), res: Some(Err), args: Some(GenericArgs { args: [Const(ConstArg { value: AnonConst { hir_id: HirId { owner: DefId(0:19 ~ whoopsies[b95c]::main[0]), local_id: 25 }, body: BodyId { hir_id: HirId { owner: DefId(0:19 ~ whoopsies[b95c]::main[0]), local_id: 26 } } }, span: src/main.rs:34:28: 34:33 })], bindings: [], parenthesized: false }), infer_args: false }, src/main.rs:34:22: 34:25, [Expr { hir_id: HirId { owner: DefId(0:19 ~ whoopsies[b95c]::main[0]), local_id: 29 }, kind: Path(Resolved(None, Path { span: src/main.rs:34:18: 34:21, res: Local(HirId { owner: DefId(0:19 ~ whoopsies[b95c]::main[0]), local_id: 1 }), segments: [PathSegment { ident: foo#0, hir_id: Some(HirId { owner: DefId(0:19 ~ whoopsies[b95c]::main[0]), local_id: 28 }), res: Some(Local(HirId { owner: DefId(0:19 ~ whoopsies[b95c]::main[0]), local_id: 1 })), args: None, infer_args: true }] })), attrs: ThinVec(None), span: src/main.rs:34:18: 34:21 }]), attrs: ThinVec(None), span: src/main.rs:34:18: 34:36 })

error: internal compiler error: Const::from_anon_const: couldn't lit_to_const
  --> src/main.rs:34:28
   |
34 |   println!("{}", foo.ask::<"int">()); // ICE
   |                            ^^^^^

error: internal compiler error: `ErrorReported` without an error
  --> src/main.rs:34:28
   |
34 |   println!("{}", foo.ask::<"int">()); // ICE
   |                            ^^^^^

error: internal compiler error: cat_expr Errd
  --> src/main.rs:30:11
   |
30 |   fn main() {
   |  ___________^
31 | |   let foo = Foo { i: 123 };
32 | |
33 | |   //println!("{}", foo.ask()); // okay
34 | |   println!("{}", foo.ask::<"int">()); // ICE
35 | | }
   | |_^

error: internal compiler error: cat_expr Errd
  --> src/main.rs:34:3
   |
34 |   println!("{}", foo.ask::<"int">()); // ICE
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: this error: internal compiler error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: internal compiler error: cat_expr Errd
  --> src/main.rs:34:3
   |
34 |   println!("{}", foo.ask::<"int">()); // ICE
   |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: this error: internal compiler error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: internal compiler error: cat_expr Errd
  --> src/main.rs:34:18
   |
34 |   println!("{}", foo.ask::<"int">()); // ICE
   |                  ^^^^^^^^^^^^^^^^^^

error: internal compiler error: cat_expr Errd
  --> src/main.rs:34:18
   |
34 |   println!("{}", foo.ask::<"int">()); // ICE
   |                  ^^^^^^^^^^^^^^^^^^
   |
   = note: this error: internal compiler error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:366:17
stack backtrace:
   0:     0x7f89c43c0a94 - backtrace::backtrace::libunwind::trace::hc5714ade624e7bac
                               at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/libunwind.rs:86
   1:     0x7f89c43c0a94 - backtrace::backtrace::trace_unsynchronized::haa5e08805f4339b9
                               at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/mod.rs:66
   2:     0x7f89c43c0a94 - std::sys_common::backtrace::_print_fmt::h94291f4baf974a09
                               at src/libstd/sys_common/backtrace.rs:78
   3:     0x7f89c43c0a94 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::ha253e2bf79e5a84a
                               at src/libstd/sys_common/backtrace.rs:59
   4:     0x7f89c43ff06c - core::fmt::write::hd8b3f531eb511fbe
                               at src/libcore/fmt/mod.rs:1069
   5:     0x7f89c43b24e3 - std::io::Write::write_fmt::h6b2c99f4d38522cf
                               at src/libstd/io/mod.rs:1504
   6:     0x7f89c43c5a95 - std::sys_common::backtrace::_print::hd70702ad9fa74e40
                               at src/libstd/sys_common/backtrace.rs:62
   7:     0x7f89c43c5a95 - std::sys_common::backtrace::print::h3071bb179093451d
                               at src/libstd/sys_common/backtrace.rs:49
   8:     0x7f89c43c5a95 - std::panicking::default_hook::{{closure}}::h49d39000445c23a9
                               at src/libstd/panicking.rs:198
   9:     0x7f89c43c57d2 - std::panicking::default_hook::hf9989e87f77de7cf
                               at src/libstd/panicking.rs:218
  10:     0x7f89c4960af3 - rustc_driver::report_ice::h92aac6410c14e5c7
  11:     0x7f89c43c6215 - std::panicking::rust_panic_with_hook::h6eb63704179cf3ce
                               at src/libstd/panicking.rs:515
  12:     0x7f89c7119b5e - std::panicking::begin_panic::hec6523dbf464630f
  13:     0x7f89c7152402 - <rustc_errors::HandlerInner as core::ops::drop::Drop>::drop::h193c1006dd552ff4
  14:     0x7f89c4982f76 - core::ptr::drop_in_place::h7754c613cf18870d
  15:     0x7f89c498beb6 - <alloc::rc::Rc<T> as core::ops::drop::Drop>::drop::h77c15289e3603ebb
  16:     0x7f89c4abec3d - core::ptr::drop_in_place::h6e2079277ccb9e64
  17:     0x7f89c4ab77b6 - rustc_interface::interface::run_compiler_in_existing_thread_pool::h9012d81d36f91414
  18:     0x7f89c496a91d - scoped_tls::ScopedKey<T>::set::h09d493364109fb29
  19:     0x7f89c49677a4 - rustc_ast::attr::with_globals::h1dda6bb6d764eded
  20:     0x7f89c4973064 - std::sys_common::backtrace::__rust_begin_short_backtrace::h3996bdb49e3d17cc
  21:     0x7f89c4ab981e - core::ops::function::FnOnce::call_once{{vtable.shim}}::h97f5dde05fab5a67
  22:     0x7f89c43a1e5f - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::hf12d465e9e76f5f4
                               at /rustc/52fa23add6fb0776b32cc591ac928618391bdf41/src/liballoc/boxed.rs:1008
  23:     0x7f89c43d6283 - <alloc::boxed::Box<F> as core::ops::function::FnOnce<A>>::call_once::h5fae10ec3a3213e6
                               at /rustc/52fa23add6fb0776b32cc591ac928618391bdf41/src/liballoc/boxed.rs:1008
  24:     0x7f89c43d6283 - std::sys::unix::thread::Thread::new::thread_start::hb6571af50d4ca2f2
                               at src/libstd/sys/unix/thread.rs:87
  25:     0x7f89c42f446f - start_thread
  26:     0x7f89c42143d3 - clone
  27:                0x0 - <unknown>

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/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.44.0-nightly (52fa23add 2020-04-18) running on x86_64-unknown-linux-gnu

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

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

thread panicked while panicking. aborting.
error: could not compile `whoopsies`.

Caused by:
  process didn't exit successfully: `rustc --crate-name whoopsies --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=3331e52733535916 -C extra-filename=-3331e52733535916 --out-dir /tmp/whoopsies/target/debug/deps -C incremental=/tmp/whoopsies/target/debug/incremental -L dependency=/tmp/whoopsies/target/debug/deps` (signal: 4, SIGILL: illegal instruction)
Backtrace

thread 'rustc' panicked at 'index out of bounds: the len is 0 but the index is 0', src/librustc_mir_build/hair/pattern/_match.rs:2325:13
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/libunwind.rs:86
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:78
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:59
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1069
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1504
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:62
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:49
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:198
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:218
  10: rustc_driver::report_ice
  11: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:515
  12: rust_begin_unwind
             at src/libstd/panicking.rs:419
  13: core::panicking::panic_fmt
             at src/libcore/panicking.rs:111
  14: core::panicking::panic_bounds_check
             at src/libcore/panicking.rs:69
  15: rustc_mir_build::hair::pattern::_match::PatStack::specialize_constructor
  16: rustc_mir_build::hair::pattern::_match::is_useful_specialized
  17: <core::iter::adapters::Map<I,F> as core::iter::traits::iterator::Iterator>::try_fold
  18: rustc_mir_build::hair::pattern::_match::is_useful
  19: rustc_mir_build::hair::pattern::check_match::check_not_useful
  20: rustc_mir_build::hair::pattern::check_match::check_exhaustive
  21: rustc_mir_build::hair::pattern::_match::MatchCheckCtxt::create_and_enter
  22: <rustc_mir_build::hair::pattern::check_match::MatchVisitor as rustc_hir::intravisit::Visitor>::visit_expr
  23: <rustc_mir_build::hair::pattern::check_match::MatchVisitor as rustc_hir::intravisit::Visitor>::visit_expr
  24: rustc_hir::intravisit::walk_expr
  25: <rustc_mir_build::hair::pattern::check_match::MatchVisitor as rustc_hir::intravisit::Visitor>::visit_expr
  26: rustc_hir::intravisit::walk_expr
  27: <rustc_mir_build::hair::pattern::check_match::MatchVisitor as rustc_hir::intravisit::Visitor>::visit_expr
  28: rustc_hir::intravisit::walk_block
  29: <rustc_mir_build::hair::pattern::check_match::MatchVisitor as rustc_hir::intravisit::Visitor>::visit_expr
  30: rustc_hir::intravisit::walk_block
  31: <rustc_mir_build::hair::pattern::check_match::MatchVisitor as rustc_hir::intravisit::Visitor>::visit_expr
  32: rustc_mir_build::hair::pattern::check_match::check_match
  33: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::check_match>::compute
  34: rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps
  35: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  36: rustc_query_system::query::plumbing::get_query
  37: rustc_query_system::query::plumbing::ensure_query
  38: <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
  39: rustc_session::utils::<impl rustc_session::session::Session>::time
  40: rustc_interface::passes::analysis
  41: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::analysis>::compute
  42: rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps
  43: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  44: rustc_query_system::query::plumbing::get_query
  45: rustc_middle::ty::context::tls::enter_global
  46: rustc_interface::interface::run_compiler_in_existing_thread_pool
  47: scoped_tls::ScopedKey<T>::set
  48: rustc_ast::attr::with_globals

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)C-bugCategory: This is a bug.F-const_generics`#![feature(const_generics)]`I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.glacierICE tracked in rust-lang/glacier.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions