Skip to content

Diagnostic type format doesn't match actual type when higher-rank lifetime bound and extern fn are both involved #151393

@lynzrand

Description

@lynzrand

Code

extern "C" fn foo(v: &i32) {
    dbg!(v);
}

static BAR: for<'a> fn(&'a i32) = foo;    // this is a type mismatch

Current output

error[E0308]: mismatched types
 --> src/lib.rs:5:35
  |
5 | static BAR: for<'a> fn(&'a i32) = foo;
  |                                   ^^^ expected "Rust" fn, found "C" fn
  |
  = note: expected fn pointer `for<'a> fn(&'a _)`
                found fn item `extern "C" for<'a> fn(&'a _) {foo}`

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

Desired output

error[E0308]: mismatched types
 --> src/lib.rs:5:35
  |
5 | static BAR: for<'a> fn(&'a i32) = foo;
  |                                   ^^^ expected "Rust" fn, found "C" fn
  |
  = note: expected fn pointer `for<'a> fn(&'a _)`
                found fn item `for<'a> extern "C" fn(&'a _) {foo}`     // <- Order swapped

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

Rationale and extra context

extern "C" for<'a> fn(...) is currently not a valid type signature:

error: expected type, found keyword `extern`
  --> src/lib.rs:16:13
   |
16 | static BAZ: extern "C" for<'a> fn(&'a i32) = foo;
   |             ^^^^^^ expected type

The user trying to use the found type will meet a parsing error.
The correct way to express the type is actually for<'a> extern "C" fn(...).

Other cases

Rust Version

rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: x86_64-unknown-linux-gnu
release: 1.92.0
LLVM version: 21.1.3

(also tested with latest nightly in playground: 1.95.0-nightly (2026-01-19 d940e56841ddcc05671e))

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions