rust-lang / rust-lang/rust

LUB coercions can't combine unsafe fn item with safe fn item or pointer

Open
#131,854 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I tried this code: playground

fn safe_fn() {}
unsafe fn unsafe_fn() {}

fn lub() {
    // safe fn item + unsafe fn item
    // doesn't work
    let _ = if true { safe_fn } else { unsafe_fn };

    // safe fn ptr + unsafe fn item
    // doesn't work
    let _ = if true { safe_fn as fn() } else { unsafe_fn };

    // safe fn item + unsafe fn ptr
    // works
    let _ = if true { safe_fn } else { unsafe_fn as unsafe fn() };

    // closure + unsafe fn item
    // works
    let _ = if true { || {} } else { unsafe_fn };
}

I expected to see this happen: I expected the safe fn item and unsafe fn item to combine to an unsafe fn ptr, especially since the closure and unsafe fn item do combine to an unsafe fn ptr.

Instead, this happened:

error[E0308]: `if` and `else` have incompatible types
 --> src/lib.rs:7:40
  |
7 |     let _ = if true { safe_fn } else { unsafe_fn };
  |                       -------          ^^^^^^^^^ expected safe fn, found unsafe fn
  |                       |
  |                       expected because of this
  |
  = note: expected fn item `fn() {safe_fn}`
             found fn item `unsafe fn() {unsafe_fn}`

error[E0308]: `if` and `else` have incompatible types
  --> src/lib.rs:11:48
   |
11 |     let _ = if true { safe_fn as fn() } else { unsafe_fn };
   |                       ---------------          ^^^^^^^^^ expected safe fn, found unsafe fn
   |                       |
   |                       expected because of this
   |
   = note: expected fn pointer `fn()`
                 found fn item `unsafe fn() {unsafe_fn}`
   = note: unsafe functions cannot be coerced into safe function pointers
Meta

playground nightly

Build using the Nightly version: 1.84.0-nightly

(2024-10-16 798fb83f7d24e31b16ac)

@rustbot label A-coercions T-types

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

Start with the Rust Playground example in the issue and reproduce the diagnostics on nightly. Compare the four safe/unsafe function and closure combinations shown, then trace the compiler's coercion and least-upper-bound handling for function items. Done means the two failing combinations compile with the expected unsafe function-pointer type and existing behavior remains covered by tests.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.