LUB coercions can't combine unsafe fn item with safe fn item or pointer
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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