rust-lang / rust-lang/rust

Coercing higher-ranked function definitions to non-hr function pointers doesn't work

Open
#145,572 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-coercions A-higher-ranked C-enhancement T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

fn foo() {
    let _: [fn(_); 2] = [x, y];
}
fn x(_: &str) {
    todo!()
}
fn y(_: &str) {
    todo!()
}

I expected to see this happen: compiles

Instead, this happened:

error[E0308]: mismatched types
 --> src/lib.rs:2:29
  |
2 |     let _: [fn(_); 2] = [x, y];
  |                             ^ one type is more general than the other
  |
  = note: expected fn pointer `fn(&_)`
                found fn item `for<'a> fn(&'a _) {y}`

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

rustc --version --verbose:

stable 1.89

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.

Research direction

Reproduce the example in src/lib.rs with rustc 1.89 and inspect the E0308 diagnostic for the array of function definitions coerced to fn pointers. Trace the handling of higher-ranked function items and non-higher-ranked function pointer coercions. Done means the provided example compiles without changing its intended types.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.