Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upHow to extract static reference from Option? #28839
Comments
This comment has been minimized.
This comment has been minimized.
|
The following works on both stable and nightly: use std::ptr;
pub trait Foo { }
static mut _foo: Option<&'static mut Foo> = None;
pub fn get_foo() -> &'static mut Foo {
unsafe {
match ptr::read(&_foo) {
Some(x) => x,
None => panic!(),
}
}
}
|
This comment has been minimized.
This comment has been minimized.
|
Hmm... it looks like there's a bug here, though: the following, which should be equivalent to your original testcase, works: pub trait Foo { }
static mut _foo: Option<&'static mut Foo> = None;
pub fn get_foo() -> &'static mut Foo {
unsafe { match _foo {
Some(ref mut x) => &mut **x,
None => panic!(),
} }
}
fn main() {} |
This comment has been minimized.
This comment has been minimized.
|
@eefriedman Thanks, that works! I'm using your second suggestion now. I'm curious to know whether this is indeed a bug. |
This comment has been minimized.
This comment has been minimized.
|
@rust-lang/lang, is this a bug or not? |
steveklabnik
added
the
A-lang
label
Oct 4, 2015
This comment has been minimized.
This comment has been minimized.
|
There's definitely something weird going on here, and I don't think it has anything to do with static lifetimes. Another related testcase: pub struct Foo;
// Accepted on stable and nightly
pub fn get_foo<'a, 'b>(foo: &'a mut Option<&'b mut Foo>) -> &'a mut Foo {
match foo {
&mut Some(ref mut x) => *x,
&mut None => panic!(),
}
}
// Rejected on nightly, accepted on stable
pub fn get_foo2<'a>(foo: &'a mut Option<&'a mut Foo>) -> &'a mut Foo {
match foo {
&mut Some(ref mut x) => *x,
&mut None => panic!(),
}
} |
nikomatsakis
added
the
regression-from-stable-to-nightly
label
Oct 5, 2015
This comment has been minimized.
This comment has been minimized.
|
I'm not 100% sure what's going on. It seems to have to do with the coercion rules. I'm not aware that they changed recently, but I'll have to review the PRs. triage: P-high (regression) |
rust-highfive
added
the
P-high
label
Oct 5, 2015
nikomatsakis
self-assigned this
Oct 5, 2015
This comment has been minimized.
This comment has been minimized.
|
This works on 1.4 too, but is broken on 3e6d724 - so something recent, but not too recent. Oddly enough, pub fn get_foo2<'a>(foo: &'a mut Option<&'a mut Foo>) -> &'a mut Foo {
match foo {
&mut Some(ref mut x) => match x { x => *x },
&mut None => panic!(),
}
}works. |
This comment has been minimized.
This comment has been minimized.
|
2be0d0a looks like the problem commit |
This comment has been minimized.
This comment has been minimized.
|
Hmm... Interesting. I guess we could revert that change while keeping the
|
eefriedman
referenced this issue
Oct 6, 2015
Closed
Nightly regression: lifetime error with autoderef #28854
This comment has been minimized.
This comment has been minimized.
|
Ah, yes, that makes sense. I should have thought of that interaction. Sigh. |
Virtlink commentedOct 4, 2015
I have a
static Option<&'static mut Foo>and I want to get the reference out of it. I know this is unsafe, but I can't seem to be able to figure out how to do this. First I tried this:The types should match, but I understand I'm trying to move
x, which gives me this error:So then I tried to use
ref mut xto get a mutable reference, followed by*xto get the object back, but that also doesn't work:Finally I tried to clone or copy the
&'static mut Foo, which also failed.How do I fix this?
I'm using Rust 1.5.0-nightly (6108e8c 2015-09-28).
P.S. While typing this post I figured I try it on the Rust Playground. It fails to compile on nightly, but succeeds on beta and stable? (I'm writing a kernel and need the nightly features.)