Skip to content

ICE caused by using a const_generic fn from a trait or impl #63695

Closed
@kkoning

Description

@kkoning

A function that compiles and runs successfully outside of a trait causes an ICE when it's in a trait. The expectation is that this should work similarly to other generics on trait functions--while they will prevent a trait object reference, they should otherwise be allowed. (On playground)

#![feature(const_generics)]
fn test<const A: i32>() -> i32 { A }

trait T {
    fn test<const A: i32>(&self) -> i32 { A }
}

struct S();

impl T for S {}

fn main() {
  let foo = S();
  println!("{}", test::<{8i32}>());
  println!("{}", foo.test::<{16i32}>());  // <- Causes ICE
}

Error and relevant backtrace:

error: internal compiler error: src/librustc_codegen_ssa/mir/operand.rs:79: unevaluated constant in `OperandRef::from_const`

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:643:9
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.34/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.34/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:47
   3: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:36
   4: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:200
   5: std::panicking::default_hook
             at src/libstd/panicking.rs:214
   6: rustc::util::common::panic_hook
   7: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:481
   8: std::panicking::begin_panic
   9: rustc_errors::Handler::bug
  10: rustc::util::bug::opt_span_bug_fmt::{{closure}}
  11: rustc::ty::context::tls::with_opt::{{closure}}
  12: rustc::ty::context::tls::with_context_opt
  13: rustc::ty::context::tls::with_opt
  14: rustc::util::bug::opt_span_bug_fmt
  15: rustc::util::bug::bug_fmt
  16: rustc_codegen_ssa::mir::operand::OperandRef<V>::from_const
  17: rustc_codegen_ssa::mir::operand::<impl rustc_codegen_ssa::mir::FunctionCx<Bx>>::codegen_operand
  18: rustc_codegen_ssa::mir::rvalue::<impl rustc_codegen_ssa::mir::FunctionCx<Bx>>::codegen_rvalue_operand
  19: rustc_codegen_ssa::mir::codegen_mir
  20: rustc_codegen_ssa::base::codegen_instance
  21: <rustc::mir::mono::MonoItem as rustc_codegen_ssa::mono_item::MonoItemExt>::define
  22: rustc_codegen_llvm::base::compile_codegen_unit::module_codegen
  23: rustc::dep_graph::graph::DepGraph::with_task
  24: rustc_codegen_llvm::base::compile_codegen_unit
  25: rustc_codegen_ssa::base::codegen_crate
  26: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_utils::codegen_backend::CodegenBackend>::codegen_crate
  27: rustc::util::common::time
  28: rustc_interface::passes::BoxedGlobalCtxt::access::{{closure}}
  29: rustc_interface::passes::create_global_ctxt::{{closure}}
  30: rustc_interface::passes::BoxedGlobalCtxt::enter
  31: rustc_interface::queries::Query<T>::compute
  32: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::ongoing_codegen
  33: rustc_interface::interface::run_compiler_in_existing_thread_pool
  34: std::thread::local::LocalKey<T>::with
  35: scoped_tls::ScopedKey<T>::set
  36: syntax::with_globals
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
query stack during panic:
end of query stack
error: aborting due to previous error


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.39.0-nightly (2111aed0a 2019-08-17) running on x86_64-unknown-linux-gnu

note: compiler flags: -C codegen-units=1 -C debuginfo=2 --crate-type bin

Activity

hellow554

hellow554 commented on Aug 19, 2019

@hellow554
Contributor

It doesn't have to be in a trait, a regular impl block would do it as well:

#![feature(const_generics)]

struct S;
impl S {
    fn test<const A: i32>() -> i32 {
        A
    }
}

fn main() {
    S::test::<{ 16i32 }>();
}
added
A-const-genericsArea: const generics (parameters and arguments)
C-bugCategory: This is a bug.
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.
requires-nightlyThis issue requires a nightly compiler in some way.
on Aug 19, 2019
changed the title [-]Trait function with a const_generic causes an ICE[/-] [+]ICE caused by using a const_generic fn from a trait or impl[/+] on Aug 19, 2019
kkoning

kkoning commented on Aug 19, 2019

@kkoning
Author

Looks like it's the same ICE at src/librustc_codegen_ssa/mir/operand.rs:79 in both cases.

BenLewis-Seequent

BenLewis-Seequent commented on Oct 20, 2019

@BenLewis-Seequent

It appears that the ICE at src/librustc_codegen_ssa/mir/operand.rs:79 is masking some earlier bugs, as running cargo check instead, yields the following ICEs:

For the original snippet:

error: internal compiler error: unexpected const parent in type_of_def_id(): Expr(expr(HirId { owner: DefIndex(20), local_id: 82 }: foo.test::<>()))

error: internal compiler error: cat_expr Errd
  --> src/main.rs:83:31
   |
83 |     println!("{}", foo.test::<{16i32}>());  // <- Causes ICE
   |                               ^^^^^^^

error: internal compiler error: mir_const_qualif: MIR had errors
  --> src/main.rs:83:31
   |
83 |     println!("{}", foo.test::<{16i32}>());  // <- Causes ICE
   |                               ^^^^^^^

error: internal compiler error: QualifyAndPromoteConstants: MIR had errors
  --> src/main.rs:83:31
   |
83 |     println!("{}", foo.test::<{16i32}>());  // <- Causes ICE
   |                               ^^^^^^^

error: internal compiler error: broken MIR in DefId(0:22 ~ rust_test[143a]::main[0]::{{constant}}[1]) ("return type"): bad type [type error]
  --> src/main.rs:83:31
   |
83 |     println!("{}", foo.test::<{16i32}>());  // <- Causes ICE
   |                               ^^^^^^^

error: internal compiler error: broken MIR in DefId(0:22 ~ rust_test[143a]::main[0]::{{constant}}[1]) (LocalDecl { mutability: Mut, is_user_variable: None, internal: false, is_block_tail: None, ty: [type error], user_ty: UserTypeProjections { contents: [] }, name: None, source_info: SourceInfo { span: src/main.rs:83:31: 83:38, scope: scope[0] }, visibility_scope: scope[0] }): bad type [type error]
  --> src/main.rs:83:31
   |
83 |     println!("{}", foo.test::<{16i32}>());  // <- Causes ICE
   |                               ^^^^^^^

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:391:17

and for @hellow554 snippet:

error: internal compiler error: unexpected const parent path Expr(expr(HirId { owner: DefIndex(17), local_id: 8 }: <S>::test::<>))

error: internal compiler error: cat_expr Errd
  --> src/main.rs:78:15
   |
78 |     S::test::<{ 16i32 }>();
   |               ^^^^^^^^^

error: internal compiler error: mir_const_qualif: MIR had errors
  --> src/main.rs:78:15
   |
78 |     S::test::<{ 16i32 }>();
   |               ^^^^^^^^^

error: internal compiler error: QualifyAndPromoteConstants: MIR had errors
  --> src/main.rs:78:15
   |
78 |     S::test::<{ 16i32 }>();
   |               ^^^^^^^^^

error: internal compiler error: broken MIR in DefId(0:18 ~ rust_test[143a]::main[0]::{{constant}}[0]) ("return type"): bad type [type error]
  --> src/main.rs:78:15
   |
78 |     S::test::<{ 16i32 }>();
   |               ^^^^^^^^^

error: internal compiler error: broken MIR in DefId(0:18 ~ rust_test[143a]::main[0]::{{constant}}[0]) (LocalDecl { mutability: Mut, is_user_variable: None, internal: false, is_block_tail: None, ty: [type error], user_ty: UserTypeProjections { contents: [] }, name: None, source_info: SourceInfo { span: src/main.rs:78:15: 78:24, scope: scope[0] }, visibility_scope: scope[0] }): bad type [type error]
  --> src/main.rs:78:15
   |
78 |     S::test::<{ 16i32 }>();
   |               ^^^^^^^^^

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:391:17

Both of these are related to #64537.

varkor

varkor commented on Jan 5, 2020

@varkor
Member

Also seems related to #63695, although the error is different.

lcnr

lcnr commented on Mar 23, 2020

@lcnr
Contributor

this probably depends on #70125 (comment)

lcnr

lcnr commented on Mar 23, 2020

@lcnr
Contributor

@rustbot claim

self-assigned this
on Mar 23, 2020

11 remaining items

added 2 commits that reference this issue on Jul 16, 2020

Rollup merge of rust-lang#74392 - lcnr:const-generics-update, r=varkor

658d5d0

Rollup merge of rust-lang#74392 - lcnr:const-generics-update, r=varkor

c354524
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @matthiaskrgr@kkoning@hellow554@jonas-schievink@Alexendoo

      Issue actions

        ICE caused by using a const_generic fn from a trait or impl · Issue #63695 · rust-lang/rust