rust-lang / rust-lang/rust

Test of least upper bound coercion behavior triggers ICE

Open
#148,283 1 comment 0 reactions 1 assignee View on GitHub

@adwinwhite is already working on this.

Since Oct 31, 2025.

A-coercions C-bug I-ICE T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

In working on an example for the Reference related to least upper bound coercions (https://doc.rust-lang.org/1.90.0/reference/type-coercions.html#least-upper-bound-coercions), I notice that the following triggers an internal compiler error.

use core::ops::Deref;

struct A; struct B; struct C;

impl Deref for C {
    type Target = B;
    fn deref(&self) -> &Self::Target {
        &B
    }
}

impl Deref for B {
    type Target = A;
    fn deref(&self) -> &Self::Target {
        &A
    }
}

fn f(v: u8) {
    let _ = match v {
        0 => &C,
        1 => &B,
        _ => &A,
    };
}

fn main() {}

Playground link

note: no errors encountered even though delayed bugs were created

note: those delayed bugs will now be shown as internal compiler errors

error: internal compiler error: while adjusting Expr { hir_id: HirId(DefId(0:16 ~ playground_main[5299]::f).11), kind: AddrOf(Ref, Not, Expr { hir_id: HirId(DefId(0:16 ~ playground_main[5299]::f).12), kind: Path(Resolved(None, Path { span: src/main.rs:21:15: 21:16 (#0), res: Def(Ctor(Struct, Const), DefId(0:9 ~ playground_main[5299]::C::{constructor#0})), segments: [PathSegment { ident: C#0, hir_id: HirId(DefId(0:16 ~ playground_main[5299]::f).13), res: Def(Ctor(Struct, Const), DefId(0:9 ~ playground_main[5299]::C::{constructor#0})), args: None, infer_args: true }] })), span: src/main.rs:21:15: 21:16 (#0) }), span: src/main.rs:21:14: 21:16 (#0) }, can't compose [Deref(None) -> C, Deref(Some(OverloadedDeref { mutbl: Not, span: src/main.rs:22:14: 22:16 (#0) })) -> B, Borrow(Ref(Not)) -> &B] and [Deref(None) -> B, Deref(Some(OverloadedDeref { mutbl: Not, span: src/main.rs:23:14: 23:16 (#0) })) -> A, Borrow(Ref(Not)) -> &A]
  --> src/main.rs:21:14
   |
21 |         0 => &C,
   |              ^^
   |
note: delayed at compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs:343:36 - disabled backtrace
  --> src/main.rs:21:14
   |
21 |         0 => &C,
   |              ^^

error: internal compiler error: broken MIR in DefId(0:16 ~ playground_main[5299]::f) (_4 = &(*_5)): bad assignment (&'?20 B = &'?15 C): NoSolution
  --> src/main.rs:21:14
   |
21 |         0 => &C,
   |              ^^
   |
note: delayed at compiler/rustc_borrowck/src/type_check/mod.rs:676:21 - disabled backtrace
  --> src/main.rs:21:14
   |
21 |         0 => &C,
   |              ^^

This is on latest nightly. I tested back to Rust 1.85 and observed the same behavior.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.