Stream: general

Topic: references to fn pointers changed from stable to beta


Jake Goulding (Oct 16 2019 at 15:33, on Zulip):

Casting a function reference producing an invalid pointer?

Jake Goulding (Oct 16 2019 at 15:34, on Zulip):

Ran on stable 1.38.0 this prints the function pointer, but beta (1.39.0-beta.6) and nightly return '1'.

Is this a bug / regression? Is it related to const eval stuff at all?

/cc @oli

Wesley Wiser (Oct 16 2019 at 15:49, on Zulip):

The MIR doesn't appear to have changed. Perhaps it's related the the recent-ish LLVM upgrade?

Wesley Wiser (Oct 16 2019 at 15:53, on Zulip):

Looking at the MIR, the type of ptr is unchanged; for both stable and beta it is:

    let _1: *const unsafe fn() {foo};    // "ptr" in scope 0 at src/main.rs:6:9: 6:12
RalfJ (Oct 16 2019 at 17:20, on Zulip):

that code is weird though. &foo is not a fn ptr but a ref-to-fn-ptr...

RalfJ (Oct 16 2019 at 17:21, on Zulip):

probably &foo now infers to a ref-to-the-unnamble-ZST-representing-foo

RalfJ (Oct 16 2019 at 17:21, on Zulip):

which would explain the 1

RalfJ (Oct 16 2019 at 17:21, on Zulip):

you see, for every function in Rust there exists a correpsonding type. that type has no run-time information so its size is 0.

RalfJ (Oct 16 2019 at 17:21, on Zulip):

the expression foo has that type

RalfJ (Oct 16 2019 at 17:22, on Zulip):

but that unnamable ZST coerces to fn() which is what happens when people create fn ptrs

RalfJ (Oct 16 2019 at 17:23, on Zulip):

but here maybe the coercion doesnt trigger anymore for... reasons. so now instead of a reference to a fn ptr, you now get a reference to the ZST. and because there's no memory backing them, &ZST just become 1 in some cases.

RalfJ (Oct 16 2019 at 17:24, on Zulip):

evidence that even on stable this does not print foo's address:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7ba08d2b12d4caf4f95b876797907b6d

Jake Goulding (Oct 16 2019 at 23:56, on Zulip):

@RalfJ yeah, should have said I know all that part :sweat_smile:

Jake Goulding (Oct 16 2019 at 23:56, on Zulip):

The change / “... reasons” is the thing I’m worried about.

Jake Goulding (Oct 16 2019 at 23:57, on Zulip):

(deleted)

Last update: Nov 22 2019 at 00:25UTC