Skip to content

Commit 6bd8ea1

Browse files
committed
Added run-pass tests for associated generic types
1 parent 19e25b6 commit 6bd8ea1

File tree

6 files changed

+144
-2
lines changed

6 files changed

+144
-2
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4444,7 +4444,7 @@ impl<'a> Parser<'a> {
44444444
}
44454445

44464446
fn parse_trait_item_assoc_ty(&mut self, preceding_attrs: Vec<Attribute>)
4447-
-> PResult<'a, (Generics, TyParam)> {
4447+
-> PResult<'a, (ast::Generics, TyParam)> {
44484448
let span = self.span;
44494449
let ident = self.parse_ident()?;
44504450
let mut generics = self.parse_generics()?;
@@ -4463,7 +4463,7 @@ impl<'a> Parser<'a> {
44634463
};
44644464
generics.where_clause = self.parse_where_clause()?;
44654465

4466-
Ok((Generics, TyParam {
4466+
Ok((generics, TyParam {
44674467
attrs: preceding_attrs.into(),
44684468
ident,
44694469
id: ast::DUMMY_NODE_ID,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2012 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+
trait Foo {
12+
type Bar<'a, 'b>;
13+
}
14+
15+
trait Baz {
16+
type Quux<'a>;
17+
}
18+
19+
impl<T> Baz for T where T: Foo {
20+
type Quux<'a> = <T as Foo>::Bar<'a, 'static>;
21+
}
22+
23+
fn main() {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2012 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+
trait Iterable {
12+
type Item<'a>;
13+
type Iter<'a>: Iterator<Item = Self::Item<'a>>;
14+
15+
fn iter<'a>(&'a self) -> Self::Iter<'a>;
16+
}
17+
18+
fn main() {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2012 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+
use std::rc::Rc;
12+
use std::sync::Arc;
13+
use std::ops::Deref;
14+
15+
trait PointerFamily {
16+
type Pointer<T>: Deref<Target = T>;
17+
fn new<T>(value: T) -> Self::Pointer<T>;
18+
}
19+
20+
struct ArcFamily;
21+
22+
impl PointerFamily for ArcFamily {
23+
type Pointer<T> = Arc<T>;
24+
fn new<T>(value: T) -> Self::Pointer<T> {
25+
Arc::new(value)
26+
}
27+
}
28+
29+
struct RcFamily;
30+
31+
impl PointerFamily for RcFamily {
32+
type Pointer<T> = Rc<T>;
33+
fn new<T>(value: T) -> Self::Pointer<T> {
34+
Rc::new(value)
35+
}
36+
}
37+
38+
struct Foo<P: PointerFamily> {
39+
bar: P::Pointer<String>,
40+
}
41+
42+
fn main() {}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2012 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+
use std::fmt::Display;
12+
13+
trait StreamingIterator {
14+
type Item<'a>;
15+
// Applying the lifetime parameter `'a` to `Self::Item` inside the trait.
16+
fn next<'a>(&'a self) -> Option<Self::Item<'a>>;
17+
}
18+
19+
struct Foo<T: StreamingIterator> {
20+
// Applying a concrete lifetime to the constructor outside the trait.
21+
bar: <T as StreamingIterator>::Item<'static>,
22+
}
23+
24+
// Users can bound parameters by the type constructed by that trait's associated type constructor
25+
// of a trait using HRTB. Both type equality bounds and trait bounds of this kind are valid:
26+
//fn foo<T: for<'a> StreamingIterator<Item<'a>=&'a [i32]>>(iter: T) { ... }
27+
fn foo<T>(iter: T) where T: StreamingIterator, for<'a> T::Item<'a>: Display { /* ... */ }
28+
29+
fn main() {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2012 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+
// Checking the interaction with this other feature
12+
#![feature(associated_type_defaults)]
13+
14+
use std::fmt::{Display, Debug};
15+
16+
trait Foo {
17+
type Assoc where Self: Sized;
18+
type Assoc2<T> where T: Display;
19+
type WithDefault<T> = Iterator<Item=T> where T: Debug;
20+
}
21+
22+
struct Bar;
23+
24+
impl Foo for Bar {
25+
type Assoc = usize;
26+
type Assoc2<T> = Vec<T>;
27+
type WithDefault<'a, T> = &'a Iterator<T>;
28+
}
29+
30+
fn main() {}

0 commit comments

Comments
 (0)