Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lukas-code opened this issue Oct 17, 2024 · 0 comments
Open
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@lukas-code
Copy link
Member

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

@lukas-code lukas-code added the C-bug Category: This is a bug. label Oct 17, 2024
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-coercions Area: implicit and explicit `expr as Type` coercions T-types Relevant to the types team, which will review and decide on the PR/issue. labels Oct 17, 2024
@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants