rust-lang / rust-lang/rust

Function item subtyping can change behavior

Open
#155,986 10 comments 0 reactions 1 assignee View on GitHub

@spastorino is already working on this.

Since Jul 30, 2026.

A-closures A-higher-ranked A-trait-system A-variance C-bug T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I'm not sure if this is a bug or not.

In the following code, F2 is a subtype of F1. It seems that this causes the type of the F2::method function item to be a subtype of the F1::method function item. This implies that function items that are subtypes of each other can have different behavior. This is rather strange.

type F1 = fn(&'static ());
type F2 = for<'a> fn(&'a ());

trait Trait: Sized {
    fn method() -> Option<Self>;
}

impl Trait for F1 {
    fn method() -> Option<Self> {
        println!("F1");
        None
    }
}

#[expect(coherence_leak_check)]
impl Trait for F2 {
    fn method() -> Option<Self> {
        println!("F2");
        None
    }
}

fn main() {
    let mut x = F1::method;
    x();
    let y = F2::method;
    y();
    x = y;
    x();
}

The above code prints:

F1
F2
F1

cc @lcnr

Meta

Reproducible on the playground with version 1.97.0-nightly (2026-04-28 37d85e592f9ae5f20f7d)

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.