Skip to content

Commit 77c8850

Browse files
committed
regression tests for issue #30438.
Fix #30438.
1 parent 8801bdb commit 77c8850

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Original regression test for Issue #30438.
12+
13+
use std::ops::Index;
14+
15+
struct Test<'a> {
16+
s: &'a String
17+
}
18+
19+
impl <'a> Index<usize> for Test<'a> {
20+
type Output = Test<'a>;
21+
fn index(&self, _: usize) -> &Self::Output {
22+
return &Test { s: &self.s};
23+
//~^ ERROR: borrowed value does not live long enough
24+
}
25+
}
26+
27+
fn main() {
28+
let s = "Hello World".to_string();
29+
let test = Test{s: &s};
30+
let r = &test[0];
31+
println!("{}", test.s); // OK since test is valid
32+
println!("{}", r.s); // Segfault since value pointed by r has already been dropped
33+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Modified regression test for Issue #30438 that exposed an
12+
// independent issue (see discussion on ticket).
13+
14+
use std::ops::Index;
15+
16+
struct Test<'a> {
17+
s: &'a String
18+
}
19+
20+
impl <'a> Index<usize> for Test<'a> {
21+
type Output = Test<'a>;
22+
fn index(&self, _: usize) -> &Self::Output {
23+
&Test { s: &self.s}
24+
//~^ ERROR: borrowed value does not live long enough
25+
}
26+
}
27+
28+
fn main() {
29+
let s = "Hello World".to_string();
30+
let test = Test{s: &s};
31+
let r = &test[0];
32+
println!("{}", test.s); // OK since test is valid
33+
println!("{}", r.s); // Segfault since value pointed by r has already been dropped
34+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Simplfied regression test for #30438, inspired by arielb1.
12+
13+
trait Trait { type Out; }
14+
15+
struct Test<'a> { s: &'a str }
16+
17+
fn silly<'y, 'z>(_s: &'y Test<'z>) -> &'y <Test<'z> as Trait>::Out where 'z: 'static {
18+
let x = Test { s: "this cannot last" };
19+
&x
20+
//~^ ERROR: `x` does not live long enough
21+
}
22+
23+
impl<'b> Trait for Test<'b> { type Out = Test<'b>; }
24+
25+
fn main() {
26+
let orig = Test { s: "Hello World" };
27+
let r = silly(&orig);
28+
println!("{}", orig.s); // OK since `orig` is valid
29+
println!("{}", r.s); // Segfault (method does not return a sane value)
30+
}

0 commit comments

Comments
 (0)