]> git.lizzy.rs Git - rust.git/commitdiff
Added run-pass tests for associated generic types
authorSunjay Varma <varma.sunjay@gmail.com>
Thu, 9 Nov 2017 23:15:02 +0000 (18:15 -0500)
committerSunjay Varma <varma.sunjay@gmail.com>
Fri, 1 Dec 2017 06:24:53 +0000 (01:24 -0500)
src/libsyntax/parse/parser.rs
src/test/run-pass/rfc1598-generic-associated-types/construct_with_other_type.rs [new file with mode: 0644]
src/test/run-pass/rfc1598-generic-associated-types/iterable.rs [new file with mode: 0644]
src/test/run-pass/rfc1598-generic-associated-types/pointer_family.rs [new file with mode: 0644]
src/test/run-pass/rfc1598-generic-associated-types/streaming_iterator.rs [new file with mode: 0644]
src/test/run-pass/rfc1598-generic-associated-types/where.rs [new file with mode: 0644]

index ad7d0d22f81fca0117fcf52e203a734a16373a24..077e547e052889d8a2a3105143778265cf18e045 100644 (file)
@@ -4444,7 +4444,7 @@ fn parse_ty_param(&mut self, preceding_attrs: Vec<Attribute>) -> PResult<'a, TyP
     }
 
     fn parse_trait_item_assoc_ty(&mut self, preceding_attrs: Vec<Attribute>)
-        -> PResult<'a, (Generics, TyParam)> {
+        -> PResult<'a, (ast::Generics, TyParam)> {
         let span = self.span;
         let ident = self.parse_ident()?;
         let mut generics = self.parse_generics()?;
@@ -4463,7 +4463,7 @@ fn parse_trait_item_assoc_ty(&mut self, preceding_attrs: Vec<Attribute>)
         };
         generics.where_clause = self.parse_where_clause()?;
 
-        Ok((Generics, TyParam {
+        Ok((generics, TyParam {
             attrs: preceding_attrs.into(),
             ident,
             id: ast::DUMMY_NODE_ID,
diff --git a/src/test/run-pass/rfc1598-generic-associated-types/construct_with_other_type.rs b/src/test/run-pass/rfc1598-generic-associated-types/construct_with_other_type.rs
new file mode 100644 (file)
index 0000000..d310236
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+trait Foo {
+    type Bar<'a, 'b>;
+}
+
+trait Baz {
+    type Quux<'a>;
+}
+
+impl<T> Baz for T where T: Foo {
+    type Quux<'a> = <T as Foo>::Bar<'a, 'static>;
+}
+
+fn main() {}
diff --git a/src/test/run-pass/rfc1598-generic-associated-types/iterable.rs b/src/test/run-pass/rfc1598-generic-associated-types/iterable.rs
new file mode 100644 (file)
index 0000000..f52a77f
--- /dev/null
@@ -0,0 +1,18 @@
+// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+trait Iterable {
+    type Item<'a>;
+    type Iter<'a>: Iterator<Item = Self::Item<'a>>;
+
+    fn iter<'a>(&'a self) -> Self::Iter<'a>;
+}
+
+fn main() {}
diff --git a/src/test/run-pass/rfc1598-generic-associated-types/pointer_family.rs b/src/test/run-pass/rfc1598-generic-associated-types/pointer_family.rs
new file mode 100644 (file)
index 0000000..4ec6b41
--- /dev/null
@@ -0,0 +1,42 @@
+// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use std::rc::Rc;
+use std::sync::Arc;
+use std::ops::Deref;
+
+trait PointerFamily {
+    type Pointer<T>: Deref<Target = T>;
+    fn new<T>(value: T) -> Self::Pointer<T>;
+}
+
+struct ArcFamily;
+
+impl PointerFamily for ArcFamily {
+    type Pointer<T> = Arc<T>;
+    fn new<T>(value: T) -> Self::Pointer<T> {
+        Arc::new(value)
+    }
+}
+
+struct RcFamily;
+
+impl PointerFamily for RcFamily {
+    type Pointer<T> = Rc<T>;
+    fn new<T>(value: T) -> Self::Pointer<T> {
+        Rc::new(value)
+    }
+}
+
+struct Foo<P: PointerFamily> {
+    bar: P::Pointer<String>,
+}
+
+fn main() {}
diff --git a/src/test/run-pass/rfc1598-generic-associated-types/streaming_iterator.rs b/src/test/run-pass/rfc1598-generic-associated-types/streaming_iterator.rs
new file mode 100644 (file)
index 0000000..33be5c1
--- /dev/null
@@ -0,0 +1,29 @@
+// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use std::fmt::Display;
+
+trait StreamingIterator {
+    type Item<'a>;
+    // Applying the lifetime parameter `'a` to `Self::Item` inside the trait.
+    fn next<'a>(&'a self) -> Option<Self::Item<'a>>;
+}
+
+struct Foo<T: StreamingIterator> {
+    // Applying a concrete lifetime to the constructor outside the trait.
+    bar: <T as StreamingIterator>::Item<'static>,
+}
+
+// Users can bound parameters by the type constructed by that trait's associated type constructor
+// of a trait using HRTB. Both type equality bounds and trait bounds of this kind are valid:
+//fn foo<T: for<'a> StreamingIterator<Item<'a>=&'a [i32]>>(iter: T) { ... }
+fn foo<T>(iter: T) where T: StreamingIterator, for<'a> T::Item<'a>: Display { /* ... */ }
+
+fn main() {}
diff --git a/src/test/run-pass/rfc1598-generic-associated-types/where.rs b/src/test/run-pass/rfc1598-generic-associated-types/where.rs
new file mode 100644 (file)
index 0000000..8fe7ebc
--- /dev/null
@@ -0,0 +1,30 @@
+// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Checking the interaction with this other feature
+#![feature(associated_type_defaults)]
+
+use std::fmt::{Display, Debug};
+
+trait Foo {
+    type Assoc where Self: Sized;
+    type Assoc2<T> where T: Display;
+    type WithDefault<T> = Iterator<Item=T> where T: Debug;
+}
+
+struct Bar;
+
+impl Foo for Bar {
+    type Assoc = usize;
+    type Assoc2<T> = Vec<T>;
+    type WithDefault<'a, T> = &'a Iterator<T>;
+}
+
+fn main() {}