Skip to content

mir typeck: subtyping cannot deal with bivariance #104409

Closed
@lcnr

Description

@lcnr
trait Trait {
    type Assoc;
}

struct Foo<T, U>(T)
where
    T: Trait<Assoc = U>;

impl Trait for for<'a> fn(&'a ()) {
    type Assoc = u32;
}
impl Trait for fn(&'static ()) {
    type Assoc = String;
}
    
fn foo(x: Foo<for<'a> fn(&'a ()), u32>) -> Foo<fn(&'static ()), String> {
    x
}

fn main() {}

results in the following ICE:

error: internal compiler error: broken MIR in DefId(0:15 ~ example[9884]::foo) (_0 = move _1): bad assignment (Foo<fn(&()), std::string::String> = Foo<for<'a> fn(&'a ()), u32>): NoSolution
  --> <source>:17:5
   |
17 |     x
   |     ^

thread 'rustc' panicked at 'aborting due to `-Z treat-err-as-bug=1`', compiler/rustc_errors/src/lib.rs:1561:30
stack backtrace:
----------- SNIP ----------------------
  17:     0x7fe0e8c751b3 - <rustc_errors[1020869c0a53ac34]::HandlerInner>::panic_if_treat_err_as_bug
  18:     0x7fe0e8c73258 - <rustc_errors[1020869c0a53ac34]::HandlerInner>::emit_diagnostic
  19:     0x7fe0ea774481 - <rustc_errors[1020869c0a53ac34]::HandlerInner>::emit_diag_at_span::<rustc_span[efd8b0f1895919e0]::span_encoding::Span>
  20:     0x7fe0ea774603 - <rustc_errors[1020869c0a53ac34]::HandlerInner>::span_bug::<rustc_span[efd8b0f1895919e0]::span_encoding::Span, &str>
  21:     0x7fe0ea773c55 - <rustc_errors[1020869c0a53ac34]::Handler>::delay_span_bug::<rustc_span[efd8b0f1895919e0]::span_encoding::Span, &str>
  22:     0x7fe0e9228157 - <rustc_borrowck[e2ad9be94bf77b7a]::type_check::TypeChecker>::typeck_mir
  23:     0x7fe0e919c351 - rustc_borrowck[e2ad9be94bf77b7a]::type_check::type_check
  24:     0x7fe0e9187179 - rustc_borrowck[e2ad9be94bf77b7a]::nll::compute_regions
  25:     0x7fe0e915c6e8 - rustc_borrowck[e2ad9be94bf77b7a]::do_mir_borrowck
  26:     0x7fe0e9b8f296 - rustc_borrowck[e2ad9be94bf77b7a]::mir_borrowck

godbolt

This should compile

Metadata

Metadata

Assignees

Labels

A-varianceArea: Variance (https://doc.rust-lang.org/nomicon/subtyping.html)C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-typesRelevant to the types 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