Skip to content

ICE w/ const_generics when trying to use the size of an associated type size as an array length #67739

Closed
@petertodd

Description

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

use std::mem;

pub trait Trait {
    type Associated : Sized;

    fn associated_size(&self) -> usize {
        [0u8; mem::size_of::<Self::Associated>()];
        0   
    }   
}

fn main() {
}

results in:

error: internal compiler error: src/librustc_traits/normalize_erasing_regions.rs:34: could not fully normalize `fn() -> usize {std::mem::size_of::<<Self as Trait>::Associated>}`

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:889:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

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.42.0-nightly (da3629b05 2019-12-29) running on x86_64-unknown-linux-gnu

error: aborting due to previous error

Removing the const_generics feature flag gets the expected error message:

error[E0220]: associated type `Associated` not found for `Self`
  --> normalize-err.rs:10:36
   |
10 |         [0u8; mem::size_of::<Self::Associated>()];
   |                                    ^^^^^^^^^^ associated type `Associated` not found

error: aborting due to previous error

For more information about this error, try `rustc --explain E0220`.

(IIUC that itself is a bug related to #43408)

Using mem::size_of::<Self::Associated>() directly outside of a const-context works, which I guess is something const_generics enables that isn't otherwise allowed.

Finally, the error message is the same whether or not the code is in a provided method or a impl block; I actually ran into the bug in the later.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)A-lazy-normalizationArea: Lazy normalization (tracking issue: #60471)C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.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

    No branches or pull requests

    Issue actions