Skip to content

Closures in const generics cause ICE #63322

Description

@Tyler-Hardin

Pretty straightforward. The idea is to plug logic into a template. This seems like something that should/could work.

#![feature(const_generics)]

fn test<const F: &'static dyn Fn(u32) -> u32>(i: u32) -> u32 {
    F(i)
}

const F: &'static dyn Fn(u32) -> u32 = &|x| x*x;

fn main () {
    println!("{}", test::<{F}>(5));
}

Playground link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=7eefeb472380da1061af1be06967d30a

Activity

  1. added
    A-closuresArea: Closures (`|…| { … }`)
    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.
    on Aug 6, 2019
  2. added
    requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.
    on Aug 6, 2019
  3. hellow554 commented on Aug 6, 2019

    @hellow554
    Contributor

    "smaller":

    #![feature(const_generics)]
    
    trait A {}
    struct B;
    impl A for B {}
    
    fn test<const T: &'static dyn A>() {
        unimplemented!()
    }
    
    fn main() {
        test::<{ &B }>();
    }

    Error:

    error: internal compiler error: broken MIR in DefId(0:18 ~ playground[af62]::main[0]) (CanonicalUserTypeAnnotation { user_ty: Canonical { max_universe: U0, variables: [], value: TypeOf(DefId(0:16 ~ playground[af62]::test[0]), UserSubsts { substs: [Const { ty: &'static (dyn A + 'static), val: Unevaluated(DefId(0:19 ~ playground[af62]::main[0]::{{constant}}[0]), []) }], user_self_ty: None }) }, span: src/main.rs:12:5: 12:19, inferred_ty: fn() {test::<ByRef { alloc: Allocation { bytes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [(Size { raw: 0 }, ((), AllocId(1))), (Size { raw: 8 }, ((), AllocId(2)))] }), undef_mask: UndefMask { blocks: [65535], len: Size { raw: 16 } }, align: Align { pow2: 3 }, mutability: Immutable, extra: () }, offset: Size { raw: 0 } } : &dyn A>} }): bad user type AscribeUserType(fn() {test::<ByRef { alloc: Allocation { bytes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [(Size { raw: 0 }, ((), AllocId(1))), (Size { raw: 8 }, ((), AllocId(2)))] }), undef_mask: UndefMask { blocks: [65535], len: Size { raw: 16 } }, align: Align { pow2: 3 }, mutability: Immutable, extra: () }, offset: Size { raw: 0 } } : &dyn A>}, DefId(0:16 ~ playground[af62]::test[0]) UserSubsts { substs: [Const { ty: &'static (dyn A + 'static), val: Unevaluated(DefId(0:19 ~ playground[af62]::main[0]::{{constant}}[0]), []) }], user_self_ty: None }): NoSolution
    
    error: internal compiler error: broken MIR in DefId(0:18 ~ playground[af62]::main[0]) (const test::<ByRef { alloc: Allocation { bytes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [(Size { raw: 0 }, ((), AllocId(1))), (Size { raw: 8 }, ((), AllocId(2)))] }), undef_mask: UndefMask { blocks: [65535], len: Size { raw: 16 } }, align: Align { pow2: 3 }, mutability: Immutable, extra: () }, offset: Size { raw: 0 } } : &dyn A>): constant const test::<ByRef { alloc: Allocation { bytes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [(Size { raw: 0 }, ((), AllocId(1))), (Size { raw: 8 }, ((), AllocId(2)))] }), undef_mask: UndefMask { blocks: [65535], len: Size { raw: 16 } }, align: Align { pow2: 3 }, mutability: Immutable, extra: () }, offset: Size { raw: 0 } } : &dyn A> should have type fn() {test::<ByRef { alloc: Allocation { bytes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [(Size { raw: 0 }, ((), AllocId(1))), (Size { raw: 8 }, ((), AllocId(2)))] }), undef_mask: UndefMask { blocks: [65535], len: Size { raw: 16 } }, align: Align { pow2: 3 }, mutability: Immutable, extra: () }, offset: Size { raw: 0 } } : &dyn A>} but has fn() {test::<ByRef { alloc: Allocation { bytes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [(Size { raw: 0 }, ((), AllocId(1))), (Size { raw: 8 }, ((), AllocId(2)))] }), undef_mask: UndefMask { blocks: [65535], len: Size { raw: 16 } }, align: Align { pow2: 3 }, mutability: Immutable, extra: () }, offset: Size { raw: 0 } } : &dyn A>} (NoSolution)
      --> src/main.rs:12:5
       |
    12 |     test::<{ &B }>();
       |     ^^^^^^^^^^^^^^
    
    error: internal compiler error: broken MIR in DefId(0:18 ~ playground[af62]::main[0]) (const test::<ByRef { alloc: Allocation { bytes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [(Size { raw: 0 }, ((), AllocId(1))), (Size { raw: 8 }, ((), AllocId(2)))] }), undef_mask: UndefMask { blocks: [65535], len: Size { raw: 16 } }, align: Align { pow2: 3 }, mutability: Immutable, extra: () }, offset: Size { raw: 0 } } : &dyn A>): bad constant user type CanonicalUserTypeAnnotation { user_ty: Canonical { max_universe: U0, variables: [], value: TypeOf(DefId(0:16 ~ playground[af62]::test[0]), UserSubsts { substs: [Const { ty: &'static (dyn A + 'static), val: Unevaluated(DefId(0:19 ~ playground[af62]::main[0]::{{constant}}[0]), []) }], user_self_ty: None }) }, span: src/main.rs:12:5: 12:19, inferred_ty: fn() {test::<ByRef { alloc: Allocation { bytes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [(Size { raw: 0 }, ((), AllocId(1))), (Size { raw: 8 }, ((), AllocId(2)))] }), undef_mask: UndefMask { blocks: [65535], len: Size { raw: 16 } }, align: Align { pow2: 3 }, mutability: Immutable, extra: () }, offset: Size { raw: 0 } } : &dyn A>} } vs fn() {test::<ByRef { alloc: Allocation { bytes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [(Size { raw: 0 }, ((), AllocId(1))), (Size { raw: 8 }, ((), AllocId(2)))] }), undef_mask: UndefMask { blocks: [65535], len: Size { raw: 16 } }, align: Align { pow2: 3 }, mutability: Immutable, extra: () }, offset: Size { raw: 0 } } : &dyn A>}: NoSolution
      --> src/main.rs:12:5
       |
    12 |     test::<{ &B }>();
       |     ^^^^^^^^^^^^^^
    
    thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:361:17
    stack backtrace:
       0: backtrace::backtrace::libunwind::trace
                 at /cargo/registry/src/github.life-white.uk-1ecc6299db9ec823/backtrace-0.3.34/src/backtrace/libunwind.rs:88
       1: backtrace::backtrace::trace_unsynchronized
                 at /cargo/registry/src/github.life-white.uk-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 as core::ops::drop::Drop>::drop
      10: core::ptr::real_drop_in_place
      11: <alloc::rc::Rc<T> as core::ops::drop::Drop>::drop
      12: core::ptr::real_drop_in_place
      13: rustc_interface::interface::run_compiler_in_existing_thread_pool
      14: std::thread::local::LocalKey<T>::with
      15: 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: internal compiler error: unexpected panic
    
  4. lcnr commented on Apr 11, 2020

    @lcnr
    Contributor

    @rustbot claim

  5. self-assigned this
    on Apr 11, 2020
  6. lcnr commented on Apr 11, 2020

    @lcnr
    Contributor

    @rustbot release-assignment

    &dyn Trait is not structural match so this should not compile. We probably shouldn't ICE here though 😆

    cc @eddyb

  7. removed their assignment
    on Apr 11, 2020
  8. eddyb commented on Apr 11, 2020

    @eddyb
    Contributor

    &dyn Trait is not structural match

    It shouldn't be, but it currently is.

    To fix it you need to change src/librustc_trait_selection/traits/structural_match.rs, like this.

  9. added 2 commits that reference this issue on Apr 28, 2020
    19ee378
    679431f
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-closuresArea: Closures (`|…| { … }`)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. When possible, use a F-* label instead.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions