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

Closures in const generics cause ICE #63322

Closed
Tyler-Hardin opened this issue Aug 6, 2019 · 4 comments · Fixed by #71038
Closed

Closures in const generics cause ICE #63322

Tyler-Hardin opened this issue Aug 6, 2019 · 4 comments · Fixed by #71038
Labels
A-closures Area: Closures (`|…| { … }`) A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` 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

@Tyler-Hardin
Copy link

Tyler-Hardin commented Aug 6, 2019

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

@jonas-schievink jonas-schievink added A-closures Area: Closures (`|…| { … }`) A-const-generics Area: const generics (parameters and arguments) 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 Aug 6, 2019
@Centril Centril added F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. labels Aug 6, 2019
@hellow554
Copy link
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.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 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

@lcnr
Copy link
Contributor

lcnr commented Apr 11, 2020

@rustbot claim

@lcnr
Copy link
Contributor

lcnr commented Apr 11, 2020

@rustbot release-assignment

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

cc @eddyb

@rustbot rustbot removed their assignment Apr 11, 2020
@eddyb
Copy link
Member

eddyb commented Apr 11, 2020

&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.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 28, 2020
…pnkfelix

forbid `dyn Trait` in patterns

Do not allow `&dyn Trait` as a generic const parameters.
This also changes dyn trait in pattern from ICE to error.

closes rust-lang#63322
closes rust-lang#70972

r? @eddyb
@bors bors closed this as completed in 679431f May 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` 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.

8 participants