@@ -36,14 +36,14 @@ mod field_access {
36
36
let y = GenericThing { a : S } ;
37
37
println ! ( "{:?}" , x. a) ;
38
38
39
- // The type of the field `a` can only be infered from the concrete type
39
+ // The type of the field `a` can only be inferred from the concrete type
40
40
// in the struct declaration.
41
41
let x = OptionS {
42
42
a : MyOption :: MyNone ( ) ,
43
43
} ;
44
44
println ! ( "{:?}" , x. a) ;
45
45
46
- // The type of the field `a` can only be infered from the type argument
46
+ // The type of the field `a` can only be inferred from the type argument
47
47
let x = GenericThing :: < MyOption < S > > {
48
48
a : MyOption :: MyNone ( ) ,
49
49
} ;
@@ -191,6 +191,68 @@ mod method_non_parametric_trait_impl {
191
191
}
192
192
}
193
193
194
+ mod type_parameter_bounds {
195
+ use std:: fmt:: Debug ;
196
+
197
+ #[ derive( Debug ) ]
198
+ struct S1 ;
199
+
200
+ #[ derive( Debug ) ]
201
+ struct S2 ;
202
+
203
+ // Two traits with the same method name.
204
+
205
+ trait FirstTrait < FT > {
206
+ fn method ( self ) -> FT ;
207
+ }
208
+
209
+ trait SecondTrait < ST > {
210
+ fn method ( self ) -> ST ;
211
+ }
212
+
213
+ fn call_first_trait_per_bound < I : Debug , T : SecondTrait < I > > ( x : T ) {
214
+ // The type parameter bound determines which method this call is resolved to.
215
+ let s1 = x. method ( ) ;
216
+ println ! ( "{:?}" , s1) ;
217
+ }
218
+
219
+ fn call_second_trait_per_bound < I : Debug , T : SecondTrait < I > > ( x : T ) {
220
+ // The type parameter bound determines which method this call is resolved to.
221
+ let s2 = x. method ( ) ;
222
+ println ! ( "{:?}" , s2) ;
223
+ }
224
+
225
+ fn trait_bound_with_type < T : FirstTrait < S1 > > ( x : T ) {
226
+ let s = x. method ( ) ;
227
+ println ! ( "{:?}" , s) ;
228
+ }
229
+
230
+ fn trait_per_bound_with_type < T : FirstTrait < S1 > > ( x : T ) {
231
+ let s = x. method ( ) ;
232
+ println ! ( "{:?}" , s) ;
233
+ }
234
+
235
+ trait Pair < P1 , P2 > {
236
+ fn fst ( self ) -> P1 ;
237
+
238
+ fn snd ( self ) -> P2 ;
239
+ }
240
+
241
+ fn call_trait_per_bound_with_type_1 < T : Pair < S1 , S2 > > ( x : T , y : T ) {
242
+ // The type in the type parameter bound determines the return type.
243
+ let s1 = x. fst ( ) ;
244
+ let s2 = y. snd ( ) ;
245
+ println ! ( "{:?}, {:?}" , s1, s2) ;
246
+ }
247
+
248
+ fn call_trait_per_bound_with_type_2 < T2 : Debug , T : Pair < S1 , T2 > > ( x : T , y : T ) {
249
+ // The type in the type parameter bound determines the return type.
250
+ let s1 = x. fst ( ) ;
251
+ let s2 = y. snd ( ) ;
252
+ println ! ( "{:?}, {:?}" , s1, s2) ;
253
+ }
254
+ }
255
+
194
256
mod function_trait_bounds {
195
257
#[ derive( Debug ) ]
196
258
struct MyThing < A > {
@@ -443,6 +505,49 @@ mod function_trait_bounds_2 {
443
505
}
444
506
}
445
507
508
+ mod type_aliases {
509
+ #[ derive( Debug ) ]
510
+ enum PairOption < Fst , Snd > {
511
+ PairNone ( ) ,
512
+ PairFst ( Fst ) ,
513
+ PairSnd ( Snd ) ,
514
+ PairBoth ( Fst , Snd ) ,
515
+ }
516
+
517
+ #[ derive( Debug ) ]
518
+ struct S1 ;
519
+
520
+ #[ derive( Debug ) ]
521
+ struct S2 ;
522
+
523
+ #[ derive( Debug ) ]
524
+ struct S3 ;
525
+
526
+ // Non-generic type alias that fully applies the generic type
527
+ type MyPair = PairOption < S1 , S2 > ;
528
+
529
+ // Generic type alias that partially applies the generic type
530
+ type AnotherPair < Thr > = PairOption < S2 , Thr > ;
531
+
532
+ pub fn f ( ) {
533
+ // Type can be inferred from the constructor
534
+ let p1: MyPair = PairOption :: PairBoth ( S1 , S2 ) ;
535
+ println ! ( "{:?}" , p1) ;
536
+
537
+ // Type can be only inferred from the type alias
538
+ let p2: MyPair = PairOption :: PairNone ( ) ; // types for `Fst` and `Snd` missing
539
+ println ! ( "{:?}" , p2) ;
540
+
541
+ // First type from alias, second from constructor
542
+ let p3: AnotherPair < _ > = PairOption :: PairSnd ( S3 ) ; // type for `Fst` missing
543
+ println ! ( "{:?}" , p3) ;
544
+
545
+ // First type from alias definition, second from argument to alias
546
+ let p3: AnotherPair < S3 > = PairOption :: PairNone ( ) ; // type for `Snd` missing, spurious `S3` for `Fst`
547
+ println ! ( "{:?}" , p3) ;
548
+ }
549
+ }
550
+
446
551
mod option_methods {
447
552
#[ derive( Debug ) ]
448
553
enum MyOption < T > {
0 commit comments