Skip to content

const_generics ICE when propagating const parameter within inherent implementation #64537

Closed

Description

@CDirkx

The following compiles:

#![feature(const_generics)]

pub fn x<const I: usize>() {}
pub fn y<const I: usize>() {
    x::<{I}>();
}

playground

And so does:

#![feature(const_generics)]

struct S;

impl S {
    pub fn x() {}
    pub fn y<const I: usize>() {
        Self::x();
    }
}

playground

However, the following does not:

#![feature(const_generics)]

struct S;

impl S {
    pub fn x<const I: usize>() {}
    pub fn y<const I: usize>() {
        Self::x::<{I}>();
    }
}

playground

error: internal compiler error: unexpected const parent path Expr(expr(HirId { owner: DefIndex(17), local_id: 9 }: <Self>::x::<>))

error: internal compiler error: cat_expr Errd
 --> src\lib.rs:8:19
  |
8 |         Self::x::<{I}>();
  |                   ^^^

error: internal compiler error: mir_const_qualif: MIR had errors
 --> src\lib.rs:8:19
  |
8 |         Self::x::<{I}>();
  |                   ^^^

error: internal compiler error: QualifyAndPromoteConstants: MIR had errors
 --> src\lib.rs:8:19
  |
8 |         Self::x::<{I}>();
  |                   ^^^

error: internal compiler error: broken MIR in DefId(0:19 ~ reproduce[b9f9]::{{impl}}[0]::y[0]::{{constant}}[0]) ("return type"): bad type [type error]
 --> src\lib.rs:8:19
  |
8 |         Self::x::<{I}>();
  |                   ^^^

error: internal compiler error: broken MIR in DefId(0:19 ~ reproduce[b9f9]::{{impl}}[0]::y[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\lib.rs:8:19: 8:22, scope: scope[0] }, visibility_scope: scope[0] }): bad type [type error]
 --> src\lib.rs:8:19
  |
8 |         Self::x::<{I}>();
  |                   ^^^

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src\librustc_errors\lib.rs:360:17
stack backtrace:
   0:     0x7ffaeae25aa9 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hb20333da0418f90f
   1:     0x7ffaeae541eb - core::fmt::write::he93a8f5e9fe72b9f
   2:     0x7ffaeae189a4 - <std::io::IoSliceMut as core::fmt::Debug>::fmt::hbe3650c5b670aece
   3:     0x7ffaeae2a080 - std::panicking::take_hook::hb08b66bde672a629
   4:     0x7ffaeae29caa - std::panicking::take_hook::hb08b66bde672a629
   5:     0x7ffacd165b1b - rustc_driver::report_ice::hcb418958ffca673b
   6:     0x7ffaeae2a8fd - std::panicking::rust_panic_with_hook::h7312cebf7b30fd61
   7:     0x7ffacee3a8d5 - rustc_errors::annotate_snippet_emitter_writer::AnnotateSnippetEmitterWriter::ui_testing::h250e4c9012a8a178
   8:     0x7ffacee1dbe2 - <rustc_errors::Handler as core::ops::drop::Drop>::drop::h4b24e07dbae1ad74
   9:     0x7ffacd1751c0 - <rustc_driver::Compilation as core::fmt::Debug>::fmt::h7e7b295ba58950ac
  10:     0x7ffacd17c266 - <rustc_driver::Compilation as core::fmt::Debug>::fmt::h7e7b295ba58950ac
  11:     0x7ffacd1331bc - <rustc_driver::args::Error as core::fmt::Debug>::fmt::h42f1798a01c1f811
  12:     0x7ffacd12cbb4 - <rustc_driver::args::Error as core::fmt::Debug>::fmt::h42f1798a01c1f811
  13:     0x7ffacd141ba6 - <rustc_driver::args::Error as core::fmt::Debug>::fmt::h42f1798a01c1f811
  14:     0x7ffacd167ed1 - <rustc_driver::Compilation as core::fmt::Debug>::fmt::h7e7b295ba58950ac
  15:     0x7ffacd184409 - <rustc_driver::Compilation as core::fmt::Debug>::fmt::h7e7b295ba58950ac
  16:     0x7ffacd0e513c - rustc_data_structures::graph::implementation::NodeIndex::node_id::h6bc48a6929e638bf
  17:     0x7ffaeae3c9c2 - _rust_maybe_catch_panic
  18:     0x7ffacd11e612 - <rustc_driver::pretty::UserIdentifiedItem as core::fmt::Debug>::fmt::haefe1a67821b54d1
  19:     0x7ffaeae07af7 - ZN244_$LT$std..error..$LT$impl$u20$core..convert..From$LT$alloc..string..String$GT$$u20$for$u20$alloc..boxed..Box$LT$dyn$u20$std..error..Error$u2b$core..marker..Send$u2b$core..marker..Sync$GT$$GT$..from..StringError$u20$as$u20$core..fmt..Display$GT$3fmt17
  20:     0x7ffaeae3a267 - std::sys::windows::thread::Thread::new::h5c25099711a7639e
  21:     0x7ffb281e4034 - BaseThreadInitThunk
  22:     0x7ffb28333691 - RtlUserThreadStart

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

Activity

  1. added
    A-const-generics
    I-ICE
    on Sep 17, 2019
  2. added
    C-bug
    T-compiler
    F-const_generics
    requires-nightly
    on Sep 17, 2019
  3. changed the title [-]const_generics ICE when propagating const parameter within struct[/-] [+]const_generics ICE when propagating const parameter within inherent implementation[/+] on Sep 17, 2019
  4. CDirkx commented on Sep 17, 2019

    @CDirkx
    ContributorAuthor

    Updated title to reflect that the ICE occurs in all inherent implementations (struct, union, enum).

    Trait implementations however, do compile:

    #![feature(const_generics)]
    
    struct S;
    
    trait T {
        fn x<const I: usize>();
        fn y<const I: usize>();
    }
    
    impl T for S {
        fn x<const I: usize>() {}
        fn y<const I: usize>() {
            <S as T>::x::<{I}>()
        }
    }

    playground

    Edit: when further experimenting: changing <S as T>::x::<{I}>() to S::x::<{I}>() again causes an ICE.
    playground

  5. CDirkx commented on Sep 17, 2019

    @CDirkx
    ContributorAuthor

    Slightly reduced error case:

    #![feature(const_generics)]
    
    struct S;
    
    impl S {
        pub fn x<const I: usize>() {}
        pub fn y() {
            Self::x::<{3usize}>();
        }
    }

    playground

  6. added
    glacier
    on Oct 15, 2019
  7. BenLewis-Seequent commented on Oct 20, 2019

    @BenLewis-Seequent

    The cause of this bug is that to type check the expression Self::x::<{3usize}>() it must evaluate the constant, but to evaluate the constant it must know what function Self::x resolves to in able to know the type of the const parameter. Thus this is most likely blocked by lazy normalization(#60471) to avoid the cycle.

  8. added
    A-lazy-normalization
    on Jan 5, 2020
  9. lcnr commented on Jul 16, 2020

    @lcnr
    Contributor

    Fixed by #74113, we already have nearly identical tests here, so I am closing this without adding a regression test for this issue.

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-genericsA-lazy-normalizationC-bugF-const_genericsI-ICET-compilerglacierrequires-nightly

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions