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

Can't cast higher-ranked safe fn to (not higher-ranked) unsafe fn ptr #129285

Closed
compiler-errors opened this issue Aug 20, 2024 · 0 comments · Fixed by #129288
Closed

Can't cast higher-ranked safe fn to (not higher-ranked) unsafe fn ptr #129285

compiler-errors opened this issue Aug 20, 2024 · 0 comments · Fixed by #129288
Assignees
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions C-bug Category: This is a bug. P-medium Medium priority regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@compiler-errors
Copy link
Member

I tried this code:

fn higher_ranked(ctx: &mut ()) {}

fn main() {
    fn as_unsafe<T>(_: unsafe fn(T)) {}
    as_unsafe(higher_ranked);
}

I expected to see it work.

Instead, this happened:

error[E0308]: mismatched types
 --> src/main.rs:5:15
  |
5 |     as_unsafe(higher_ranked);
  |               ^^^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected fn pointer `unsafe fn(&mut ())`
             found fn pointer `unsafe for<'a> fn(&'a mut ())`

This regressed in #129059. This only happens when coercing from safe to unsafe, not unsafe to unsafe or safe to safe. I probably forgot something about how we construct the adjustment from unsafe to safe fn.

@compiler-errors compiler-errors added the C-bug Category: This is a bug. label Aug 20, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 20, 2024
@compiler-errors compiler-errors self-assigned this Aug 20, 2024
@compiler-errors compiler-errors added regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-types Relevant to the types team, which will review and decide on the PR/issue. labels Aug 20, 2024
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Aug 20, 2024
@compiler-errors compiler-errors added A-coercions Area: implicit and explicit `expr as Type` coercions and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Aug 20, 2024
@compiler-errors compiler-errors added the P-medium Medium priority label Aug 20, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 25, 2024
…, r=lcnr

Use subtyping for `UnsafeFnPointer` coercion, too

I overlooked this in rust-lang#129059, which changed MIR typechecking to use subtyping for other fn pointer coercions.

Fixes rust-lang#129285
@bors bors closed this as completed in 621f272 Aug 26, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Aug 26, 2024
Rollup merge of rust-lang#129288 - compiler-errors:unsafe-fn-coercion, r=lcnr

Use subtyping for `UnsafeFnPointer` coercion, too

I overlooked this in rust-lang#129059, which changed MIR typechecking to use subtyping for other fn pointer coercions.

Fixes rust-lang#129285
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. P-medium Medium priority regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants