-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Different borrow checker behavior with externally similar code #18961
Comments
As noted in the SO comments, the second snippet builds when you remove the lifetime annotation on |
This is #3598. The problem is variance. |
Hmm, actually, I could be mistaken. Let me read a bit more. |
I take it back, I am not sure what's going on here just yet, I'll have to dig in. |
Updated code: //struct S1<T> { s: Option<T> }
struct S1<T> { s: *mut T }
struct Foo<'a> {
bar: S1<&'a str>
}
impl<'a> Foo<'a> {
pub fn new() -> Foo<'a> { // '
//Foo { bar: S1 { s: None } }
Foo { bar: S1 { s: std::ptr::null_mut() } }
}
pub fn baz(&'a self) -> Option<isize> {
None
}
pub fn qux(&'a mut self, retry: bool) {
let opt = self.baz();
if retry { self.qux(false); }
}
}
pub fn main() {
let mut foo = Foo::new();
foo.qux(true);
} Same error today. |
I don't think this is actually a bug. I minimized the code, and I'm going to close this, but please do reopen if it's actually a bug, and if possible provide at least a quick explanation as to why. use std::marker::PhantomData;
struct Foo<'a>(PhantomData<&'a i32>);
impl<'a> Foo<'a> {
fn qux(&'a mut self) {}
}
fn main() {
let mut foo = Foo(PhantomData);
foo.qux();
foo.qux();
} |
Consider this piece of code:
It compiles. However, if you change
S1
definition to the commented one:it stops compiling with borrow checking error (and very weird one):
Originally discovered in this Stackoverflow question. It is much more confusing there because
Vec
andHashMap
instead of custom structure are used.cc @nikomatsakis (right?)
The text was updated successfully, but these errors were encountered: