Skip to content

[ICE] with const generics and specified constant #71805

Closed

Description

@leonardo-m

Related to ER #71387

#![feature(const_generics)]
#![allow(incomplete_features)]

use std::mem::MaybeUninit;

trait CollectSlice<'a>: Iterator {
    fn inner_array<const N: usize>(&mut self) -> [Self::Item; N];

    fn collect_array<const N: usize>(&mut self) -> [Self::Item; N] {
        let result = self.inner_array();
        assert!(self.next().is_none());
        result
    }
}

impl<'a, I: ?Sized> CollectSlice<'a> for I where I: Iterator {
    fn inner_array<const N: usize>(&mut self) -> [Self::Item; N] {
        let mut result: [MaybeUninit<Self::Item>; N] = unsafe {
            MaybeUninit::uninit().assume_init()
        };

        let mut count = 0;
        for (dest, item) in result.iter_mut().zip(self) {
            *dest = MaybeUninit::new(item);
            count += 1;
        }

        assert_eq!(N, count);

        let temp_ptr: *const [MaybeUninit<Self::Item>; N] = &result;
        unsafe { std::ptr::read(temp_ptr as *const [Self::Item; N]) }
    }
}

fn main() {
    let foos = [0_u64; 9].iter().cloned();
    let _bar: [u64; 9] = foos.collect_array::<9_usize>();
}

Gives:

error: internal compiler error: unexpected const parent in type_of_def_id(): Expr(Expr { hir_id: HirId { owner: DefId(0:21 ~ bug[317d]::main[0]), local_id: 23 }, kind: MethodCall(PathSegment { ident: collect_array#0, hir_id: Some(HirId { owner: DefId(0:21 ~ bug[317d]::main[0]), local_id: 20 }), res: Some(Err), args: Some(GenericArgs { args: [Const(ConstArg { value: AnonConst { hir_id: HirId { owner: DefId(0:21 ~ bug[317d]::main[0]), local_id: 18 }, body: BodyId { hir_id: HirId { owner: DefId(0:21 ~ bug[317d]::main[0]), local_id: 19 } } }, span: ...\bug.rs:37:47: 37:54 })], bindings: [], parenthesized: false }), infer_args: false }, ...\bug.rs:37:31: 37:44, [Expr { hir_id: HirId { owner: DefId(0:21 ~ bug[317d]::main[0]), local_id: 22 }, kind: Path(Resolved(None, Path { span: ...\bug.rs:37:26: 37:30, res: Local(HirId { owner: DefId(0:21 ~ bug[317d]::main[0]), local_id: 1 }), segments: [PathSegment { ident: foos#0, hir_id: Some(HirId { owner: DefId(0:21 ~ bug[317d]::main[0]), local_id: 21 }), res: Some(Local(HirId { owner: DefId(0:21 ~ bug[317d]::main[0]), local_id: 1 })), args: None, infer_args: true }] })), attrs: ThinVec(None), span: ...\bug.rs:37:26: 37:30 }]), attrs: ThinVec(None), span: ...\bug.rs:37:26: 37:57 })

error: internal compiler error: Const::from_anon_const: couldn't lit_to_const
  --> ...\bug.rs:37:47
   |
37 |     let _bar: [u64; 9] = foos.collect_array::<9_usize>();
   |                                               ^^^^^^^

error: internal compiler error: `ErrorReported` without an error
  --> ...\bug.rs:37:47
   |
37 |     let _bar: [u64; 9] = foos.collect_array::<9_usize>();
   |                                               ^^^^^^^

error: internal compiler error: cat_expr Errd
  --> ...\bug.rs:35:11
   |
35 |   fn main() {
   |  ___________^
36 | |     let foos = [0_u64; 9].iter().cloned();
37 | |     let _bar: [u64; 9] = foos.collect_array::<9_usize>();
38 | | }
   | |_^

error: internal compiler error: cat_expr Errd
  --> ...\bug.rs:37:26
   |
37 |     let _bar: [u64; 9] = foos.collect_array::<9_usize>();
   |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: internal compiler error: PromoteTemps: MIR had errors
  --> ...\bug.rs:35:1
   |
35 | / fn main() {
36 | |     let foos = [0_u64; 9].iter().cloned();
37 | |     let _bar: [u64; 9] = foos.collect_array::<9_usize>();
38 | | }
   | |_^

error: internal compiler error: broken MIR in DefId(0:21 ~ bug[317d]::main[0]) ("return type"): bad type [type error]
  --> ...\bug.rs:35:1
   |
35 | / fn main() {
36 | |     let foos = [0_u64; 9].iter().cloned();
37 | |     let _bar: [u64; 9] = foos.collect_array::<9_usize>();
38 | | }
   | |_^

error: internal compiler error: broken MIR in DefId(0:21 ~ bug[317d]::main[0]) (LocalDecl { mutability: Mut, local_info: Other, internal: false, is_block_tail: None, ty: [type error], user_ty: UserTypeProjections { contents: [] }, source_info: SourceInfo { span: ...\bug.rs:35:1: 38:2, scope: scope[0] } }): bad type [type error]
  --> ...\bug.rs:35:1
   |
35 | / fn main() {
36 | |     let foos = [0_u64; 9].iter().cloned();
37 | |     let _bar: [u64; 9] = foos.collect_array::<9_usize>();
38 | | }
   | |_^

error: internal compiler error: mir_const_qualif: MIR had errors
  --> ...\bug.rs:37:47
   |
37 |     let _bar: [u64; 9] = foos.collect_array::<9_usize>();
   |                                               ^^^^^^^

error: internal compiler error: PromoteTemps: MIR had errors
  --> ...\bug.rs:37:47
   |
37 |     let _bar: [u64; 9] = foos.collect_array::<9_usize>();
   |                                               ^^^^^^^

error: internal compiler error: broken MIR in DefId(0:24 ~ bug[317d]::main[0]::{{constant}}[2]) ("return type"): bad type [type error]
  --> ...\bug.rs:37:47
   |
37 |     let _bar: [u64; 9] = foos.collect_array::<9_usize>();
   |                                               ^^^^^^^

error: internal compiler error: broken MIR in DefId(0:24 ~ bug[317d]::main[0]::{{constant}}[2]) (LocalDecl { mutability: Mut, local_info: Other, internal: false, is_block_tail: None, ty: [type error], user_ty: UserTypeProjections { contents: [] }, source_info: SourceInfo { span: ...\bug.rs:37:47: 37:54, scope: scope[0] } }): bad type [type error]
  --> ...\bug.rs:37:47
   |
37 |     let _bar: [u64; 9] = foos.collect_array::<9_usize>();
   |                                               ^^^^^^^

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

note: rustc 1.45.0-nightly (7f65393b9 2020-05-01) running on x86_64-pc-windows-gnu

Activity

  1. added
    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 May 2, 2020
  2. added
    A-const-genericsArea: const generics (parameters and arguments)
    requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.
    on May 2, 2020
  3. lcnr commented on May 2, 2020

    @lcnr
    Contributor

    Probably related to #70507 (comment)

    cc @varkor

  4. added 2 commits that reference this issue on Jul 16, 2020
    658d5d0
    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. 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