Skip to content

ICE: Compiler incorrectly assumes a Copy bound is met for a type. #119915

Closed
@Yurihaia

Description

@Yurihaia

This ICEs on both the latest beta and the latest nightly with the same error.
stable is not affected.

I'm not quite sure what the name of the issue should be so sorry for the bad description.
I tried to comment the code on what seems to be causing the problem however.

Code

The minimal reproduction code is also here in the playground.
https://play.rust-lang.org/?version=beta&mode=debug&edition=2021&gist=3f34d3bdda874425b1333311501e54e2

use std::marker::PhantomData;

// The variance of both generics don't seem to matter.
// I tried with *mut E, *const E, and fn(E). Every one gives the same result.
struct Example<E, FakeParam>(PhantomData<(E, FakeParam)>);

 // This can also have a lifetime of any variance, it doesn't appear to matter.
struct NoLifetime;
// Likewise, the variance of `'a` here also doesn't seem to matter.
struct Immutable<'a>(PhantomData<&'a ()>);

// The E: 'a is part of what causes the ICE. if it is just `E`, the compiler correctly
// finds the `Copy` bound missing on `Example<E, Mutable<'a>>`
impl<'a, E: 'a> Copy for Example<E, Immutable<'a>> {}
impl<'a, E: 'a> Clone for Example<E, Immutable<'a>> {
    fn clone(&self) -> Self { *self }
}

impl<E, FakeParam> Example<E, FakeParam> {
    // there is no ICE if this (correctly) takes `&self`.
    unsafe fn change<NewFakeParam>(self) -> Example<E, NewFakeParam> {
        Example(PhantomData)
    }
}

impl<E> Example<E, NoLifetime> {
    fn the_ice(&mut self) -> Example<E, Immutable<'_>> {
        unsafe { self.change() }
    }
}

Meta

rustc --version --verbose:

beta:

rustc 1.76.0-beta.5 (f732c37b4 2024-01-12)
binary: rustc
commit-hash: f732c37b4175158d3af9e2e156142ffb0bff8969
commit-date: 2024-01-12
host: x86_64-pc-windows-msvc
release: 1.76.0-beta.5
LLVM version: 17.0.6

nightly:

rustc 1.77.0-nightly (2319be8e2 2024-01-12)
binary: rustc
commit-hash: 2319be8e265dd19973574eb581d28297baf44b11
commit-date: 2024-01-12
host: x86_64-pc-windows-msvc
release: 1.77.0-nightly
LLVM version: 17.0.6

Error output

error: internal compiler error: compiler\rustc_middle\src\ty\generic_args.rs:817:13: Unexpected parameter Type(E/#1) when substituting in region 'a (index=0)
thread 'rustc' panicked at /rustc/f732c37b4175158d3af9e2e156142ffb0bff8969\compiler\rustc_errors\src\lib.rs:1119:75:
Box<dyn Any>
Backtrace

beta backtrace:
beta-ice-backtrace.txt

nightly backtrace:
rustc-ice-2024-01-13T04_35_40-20512.txt

Metadata

Metadata

Labels

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.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions