Stream: general

Topic: fn ptr in static


Jethro (Nov 21 2018 at 16:19, on Zulip):

I have this type:

pub struct PanicOutput<W: Write>(pub Option<fn() -> io::Result<W>>);
Jethro (Nov 21 2018 at 16:19, on Zulip):

when I try to store it in a const or static, I get this error:

80 | pub static PANIC_OUTPUT: PanicOutput<Stderr> =  PanicOutput(Some(Stderr::new));
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected something that cannot possibly be outside the (wrapping) range 1..=0
   |
   = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
Jethro (Nov 21 2018 at 16:19, on Zulip):

I'm not sure I understand what's going on. can you not store fn pointers in a static?

RalfJ (Nov 21 2018 at 16:22, on Zulip):

@Jethro which rustc version? have you tried nightly?

RalfJ (Nov 21 2018 at 16:22, on Zulip):

I recall fixing something similar recently

Jethro (Nov 21 2018 at 16:22, on Zulip):

how can I have x.py output the compiler version it's using?

RalfJ (Nov 21 2018 at 16:23, on Zulip):

oh you are doing this in rustc?

RalfJ (Nov 21 2018 at 16:23, on Zulip):

then it is using the bootstrap compiler, which is a beta

RalfJ (Nov 21 2018 at 16:23, on Zulip):
build/x86_64-unknown-linux-gnu/stage0/bin/rustc --version

would show the version

RalfJ (Nov 21 2018 at 16:24, on Zulip):

but yeah, possible that the fix didn't make it into beta yet

Jethro (Nov 21 2018 at 16:24, on Zulip):

ah yeah it's rustc 1.31.0-beta.1 (2824a67b0 2018-10-29)

RalfJ (Nov 21 2018 at 16:24, on Zulip):

how critical is this? I guess we could try to get it backported

Jethro (Nov 21 2018 at 16:24, on Zulip):

so um.. if I want to do this in std, what are my options? wait until bootstrap is fixed?

oli (Nov 21 2018 at 16:25, on Zulip):

you can also use #[cfg(not(stage0))]

Jethro (Nov 21 2018 at 16:25, on Zulip):

except I'm changing existing code

oli (Nov 21 2018 at 16:25, on Zulip):

but all uses of the static will also need the cfg

oli (Nov 21 2018 at 16:25, on Zulip):

yea, you'd need to keep the old code around, too

RalfJ (Nov 21 2018 at 16:25, on Zulip):

@Oli hm why does https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=1840f8bef870ba3faa6085455f3a882c work?

oli (Nov 21 2018 at 16:26, on Zulip):

why should it not work?

oli (Nov 21 2018 at 16:26, on Zulip):

function pointers in constants are fine

oli (Nov 21 2018 at 16:26, on Zulip):

and have been since 1.0

RalfJ (Nov 21 2018 at 16:26, on Zulip):

right but this is the bug where we dont know that fn pointers are non-NULL

Jake Goulding (Nov 21 2018 at 16:27, on Zulip):

isn't fn() a function reference?

Jake Goulding (Nov 21 2018 at 16:27, on Zulip):

although we always say "pointer"

RalfJ (Nov 21 2018 at 16:27, on Zulip):

shrug I use these terms rather interchangeably

Jake Goulding (Nov 21 2018 at 16:27, on Zulip):

right, as do I, but Rust makes a difference between the two ;-)

oli (Nov 21 2018 at 16:27, on Zulip):

uh... maybe it's related to the newtype around the option?

Jethro (Nov 21 2018 at 16:27, on Zulip):

I don't know if it makes sense to distinguish for fn ptrs

Jethro (Nov 21 2018 at 16:28, on Zulip):

there is no meaningful semantic difference (except if you want to say non-NULL is the difference)

oli (Nov 21 2018 at 16:28, on Zulip):

a function pointer cannot be null

Jake Goulding (Nov 21 2018 at 16:28, on Zulip):

@Jethro I feel like that... is a huge semantic difference...

Jethro (Nov 21 2018 at 16:28, on Zulip):

@Oli this also works on playground:

fn a() {} struct A<T>(Option<fn() -> T>); const X: A<()> = A(Some(a));
Jethro (Nov 21 2018 at 16:29, on Zulip):

I guess you need a more pathological case to trigger the UB warning

oli (Nov 21 2018 at 16:29, on Zulip):

I think it was just broken in a few rustc versions

Jethro (Nov 21 2018 at 16:29, on Zulip):

oh play is using a much newer beta than bootstrap

oli (Nov 21 2018 at 16:29, on Zulip):

but apparently one that made it into a specific beta, but not the nightly beta?

oli (Nov 21 2018 at 16:29, on Zulip):

we probably backported it ;)

oli (Nov 21 2018 at 16:29, on Zulip):

maybe you can get away with bumping the bootstrap compiler

Jethro (Nov 21 2018 at 16:29, on Zulip):

1.31.0-beta.1 vs 1.31.0-beta.15

Jake Goulding (Nov 21 2018 at 16:30, on Zulip):

you could probably upgrade the beta release without much objection

Jethro (Nov 21 2018 at 16:30, on Zulip):

any pointers for this?

Jake Goulding (Nov 21 2018 at 16:30, on Zulip):

err, the bootstrap beta. I'm a little surprised it doesn't happen "automatically", but it's probably only when needed

Jake Goulding (Nov 21 2018 at 16:31, on Zulip):

I'd skim https://forge.rust-lang.org/release-process.html

RalfJ (Nov 21 2018 at 16:31, on Zulip):

@Jethro just to be sure, could you try compiling this with build/x86_64-unknown-linux-gnu/stage0/bin/rustc?

pub static TEST: Option<fn()> = Some(main);

fn main() {}
Jake Goulding (Nov 21 2018 at 16:32, on Zulip):

Maybe just

Send a PR to the master branch to:

Jethro (Nov 21 2018 at 16:32, on Zulip):

@RalfJ that works, but the example I just posted doesn't (it produces the same error I was seeing)

fn a() {} struct A<T>(Option<fn() -> T>); const X: A<()> = A(Some(a));
Jethro (Nov 21 2018 at 16:32, on Zulip):

@Jake Goulding yeah that looks good

Jethro (Nov 21 2018 at 16:35, on Zulip):

@Oli @RalfJ do you know which PR fixed this? I can't quite identify it in the beta commit log

Jethro (Nov 21 2018 at 16:38, on Zulip):

maybe https://github.com/rust-lang/rust/pull/55474 ?

oli (Nov 21 2018 at 16:39, on Zulip):

looks like the one

RalfJ (Nov 21 2018 at 16:50, on Zulip):

oh, so it wasn't even about fn pointers

RalfJ (Nov 21 2018 at 16:50, on Zulip):

that explains it

RalfJ (Nov 21 2018 at 16:50, on Zulip):

yeah then @Jethro try getting the bootstrap compiler updated. I am afraid I have no idea how to do that.
Cc @nikomatsakis @Alex Crichton @eddyb

Jethro (Nov 21 2018 at 16:51, on Zulip):

I already found out!

Jethro (Nov 21 2018 at 16:51, on Zulip):

https://github.com/rust-lang/rust/pull/56139

RalfJ (Nov 21 2018 at 16:56, on Zulip):

awesome :)

Last update: Nov 22 2019 at 01:00UTC