Skip to content

Commit 2fd9a86

Browse files
committed
Two tests for rust-lang#22246
1 parent 6547509 commit 2fd9a86

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2015 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+
// Test that we are imposing the requirement that every associated
12+
// type of a bound that appears in the where clause on a struct must
13+
// outlive the location in which the type appears. Issue #22246.
14+
15+
#![allow(dead_code)]
16+
17+
use std::mem::transmute;
18+
use std::ops::Deref;
19+
20+
///////////////////////////////////////////////////////////////////////////
21+
22+
pub trait TheTrait {
23+
type TheAssocType;
24+
25+
fn dummy(&self) { }
26+
}
27+
28+
pub struct TheType<'b> {
29+
m: [fn(&'b()); 0]
30+
}
31+
32+
impl<'b> TheTrait for TheType<'b> {
33+
type TheAssocType = &'b ();
34+
}
35+
36+
///////////////////////////////////////////////////////////////////////////
37+
38+
pub struct WithAssoc<T:TheTrait> {
39+
m: [T; 0]
40+
}
41+
42+
pub struct WithoutAssoc<T> {
43+
m: [T; 0]
44+
}
45+
46+
fn with_assoc<'a,'b>() {
47+
// For this type to be valid, the rules require that all
48+
// associated types of traits that appear in `WithAssoc` must
49+
// outlive 'a. In this case, that means TheType<'b>::TheAssocType,
50+
// which is &'b (), must outlive 'a.
51+
52+
let _: &'a WithAssoc<TheType<'b>> = loop { }; //~ ERROR cannot infer
53+
}
54+
55+
fn with_assoc1<'a,'b>() where 'b : 'a {
56+
// For this type to be valid, the rules require that all
57+
// associated types of traits that appear in `WithAssoc` must
58+
// outlive 'a. In this case, that means TheType<'b>::TheAssocType,
59+
// which is &'b (), must outlive 'a, so 'b : 'a must hold, and
60+
// that is in the where clauses, so we're fine.
61+
62+
let _: &'a WithAssoc<TheType<'b>> = loop { };
63+
}
64+
65+
fn make_bar<'a,'b>() {
66+
// Here there are no associated types and the `'b` appearing in
67+
// `TheType<'b>` is purely covariant, so there is no requirement
68+
// that `'b:'a` holds.
69+
70+
let _: &'a WithoutAssoc<TheType<'b>> = loop { };
71+
}
72+
73+
fn main() {
74+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2015 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+
// Regression test for issue #22246 -- we should be able to deduce
12+
// that `&'a B::Owned` implies that `B::Owned : 'a`.
13+
14+
#![allow(dead_code)]
15+
16+
use std::ops::Deref;
17+
18+
pub trait ToOwned {
19+
type Owned: Borrow<Self>;
20+
fn to_owned(&self) -> Self::Owned;
21+
}
22+
23+
pub trait Borrow<Borrowed> {
24+
fn borrow(&self) -> &Borrowed;
25+
}
26+
27+
pub struct Foo<B:ToOwned> {
28+
owned: B::Owned
29+
}
30+
31+
fn foo<B:ToOwned>(this: &Foo<B>) -> &B {
32+
this.owned.borrow()
33+
}
34+
35+
fn main() { }

0 commit comments

Comments
 (0)