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 compare unsafe function pointers #718

Closed
steveklabnik opened this issue Jan 23, 2015 · 8 comments
Closed

Can't compare unsafe function pointers #718

steveklabnik opened this issue Jan 23, 2015 · 8 comments

Comments

@steveklabnik
Copy link
Member

Issue by jdm
Monday Apr 07, 2014 at 20:28 GMT

For earlier discussion, see rust-lang/rust#13385

This issue was labelled with: in the Rust repository


type JSPropertyOp = extern "C" fn();
type JSPropertyOp2 = extern "C" unsafe fn();

extern fn foo() {
}

fn main() {
    let bar: JSPropertyOp = foo;
    if bar == foo {
    }

    let bar: JSPropertyOp2 = foo;
    if bar == foo {
    }
}

yields

[jdm@rosencrantz rust-mozjs]$ rustc /tmp/unsafe.rs 
/tmp/unsafe.rs:13:8: 13:18 error: binary operation `==` cannot be applied to type `extern "C" unsafe fn()`
/tmp/unsafe.rs:13     if bar == foo {
                         ^~~~~~~~~~
error: aborting due to previous error
@petrochenkov
Copy link
Contributor

If the title is correct, then this is resolved by rust-lang/rust#28268, if the example is correct, then some kind of heterogeneous comparison between safe function items and unsafe function pointers is wanted. The example is probably incorrect.

@gkoz
Copy link

gkoz commented Sep 22, 2015

Is there a reason extern fn doesn't coerce to unsafe extern fn? If it did would the example work?

@petrochenkov
Copy link
Contributor

@gkoz
extern fn() does coerce to unsafe extern fn(), but:

  1. transitive coercions are not implemented, so extern fn() {foo} doesn't coerce to unsafe extern fn() despite extern fn() {foo} coercing to extern fn() and extern fn() coercing to unsafe extern fn().
  2. Due to the way how method resolution works arguments of == are not... equal. The coerced argument should be to the right from the operator.
type SafeFnPtr = extern fn();
type UnsafeFnPtr = unsafe extern fn();

extern fn foo() {}

fn main() {
    let a: SafeFnPtr = foo;
    let b: UnsafeFnPtr = a;
    b == a; // Works!
    // a == b; // Doesn't work
}

So, if 1) is fixed, then the @jdm's example should actually work, even if doesn't correspond to the issue title

@gkoz
Copy link

gkoz commented Sep 23, 2015

@petrochenkov thanks for the clarification!

@petrochenkov
Copy link
Contributor

The example in #718 (comment) compiles now.

@jdm
Copy link

jdm commented Feb 20, 2017

It doesn't on the 2/19 nightly. Is there a more recent change that addressed it?

@petrochenkov
Copy link
Contributor

@jdm
It compiles even on stable 1.15 if you fix the incorrect syntax extern "C" unsafe -> unsafe extern "C".

@jdm
Copy link

jdm commented Feb 20, 2017

Ah, whoops! This can be closed, in that case.

Eijebong added a commit to Eijebong/servo that referenced this issue Nov 1, 2017
bors-servo pushed a commit to servo/servo that referenced this issue Nov 1, 2017
Remove a workaround for rust-lang/rfcs#718

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19082)
<!-- Reviewable:end -->
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this issue Nov 1, 2017
…m Eijebong:fixme_issue_718); r=jdm

Source-Repo: https://github.com/servo/servo
Source-Revision: fa93a79ed0c93f88e7fc6473da378eadaabe7b1a

--HG--
extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear
extra : subtree_revision : ec825f73bff763418cb9d6035969ff80dfc536b4
xeonchen pushed a commit to xeonchen/gecko-cinnabar that referenced this issue Nov 2, 2017
…m Eijebong:fixme_issue_718); r=jdm

Source-Repo: https://github.com/servo/servo
Source-Revision: fa93a79ed0c93f88e7fc6473da378eadaabe7b1a
JerryShih pushed a commit to JerryShih/gecko-dev that referenced this issue Nov 3, 2017
…m Eijebong:fixme_issue_718); r=jdm

Source-Repo: https://github.com/servo/servo
Source-Revision: fa93a79ed0c93f88e7fc6473da378eadaabe7b1a
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified-and-comments-removed that referenced this issue Oct 2, 2019
…m Eijebong:fixme_issue_718); r=jdm

Source-Repo: https://github.com/servo/servo
Source-Revision: fa93a79ed0c93f88e7fc6473da378eadaabe7b1a

UltraBlame original commit: 67f2b6f0a1d32470702d1ef61844671881f6ad57
gecko-dev-updater pushed a commit to marco-c/gecko-dev-comments-removed that referenced this issue Oct 2, 2019
…m Eijebong:fixme_issue_718); r=jdm

Source-Repo: https://github.com/servo/servo
Source-Revision: fa93a79ed0c93f88e7fc6473da378eadaabe7b1a

UltraBlame original commit: 67f2b6f0a1d32470702d1ef61844671881f6ad57
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified that referenced this issue Oct 2, 2019
…m Eijebong:fixme_issue_718); r=jdm

Source-Repo: https://github.com/servo/servo
Source-Revision: fa93a79ed0c93f88e7fc6473da378eadaabe7b1a

UltraBlame original commit: 67f2b6f0a1d32470702d1ef61844671881f6ad57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants