Closed
Description
I've been playing a bit around std::ops::Index
and the associated sugar.
I found a case where it may cause a segfault. I tried to reproduce the issue in a simplified code and got the following one (also available on playpen)
use std::ops::Index;
struct Test<'a> {
s: &'a String
}
impl <'a> Index<usize> for Test<'a> {
type Output = Test<'a>;
fn index(&self, _: usize) -> &Self::Output {
return &Test { s: &self.s};
}
}
fn main() {
let s = "Hello World".to_string();
let test = Test{s: &s};
let r = &test[0];
println!("{}", test.s); // OK since test is valid
println!("{}", r.s); // Segfault since value pointed by r has already been dropped
}
I guess this code should not compile since on the last line, r
points to a dropped value.