@@ -50,7 +50,6 @@ use std::fmt;
50
50
use std:: marker:: PhantomData ;
51
51
use std:: mem;
52
52
use std:: rc:: Rc ;
53
- use std:: iter;
54
53
use syntax:: abi:: Abi ;
55
54
use hir;
56
55
use util:: nodemap:: FxHashMap ;
@@ -1094,38 +1093,30 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
1094
1093
// and applicable impls. There is a certain set of precedence rules here.
1095
1094
1096
1095
let def_id = obligation. predicate . def_id ( ) ;
1097
- match obligation. predicate . def_id ( ) {
1098
- _ if self . tcx ( ) . lang_items . copy_trait ( ) == Some ( def_id) => {
1099
- debug ! ( "obligation self ty is {:?}" ,
1100
- obligation. predicate. 0 . self_ty( ) ) ;
1101
-
1102
- // User-defined copy impls are permitted, but only for
1103
- // structs and enums.
1104
- self . assemble_candidates_from_impls ( obligation, & mut candidates) ?;
1105
-
1106
- // For other types, we'll use the builtin rules.
1107
- let copy_conditions = self . copy_conditions ( obligation) ;
1108
- self . assemble_builtin_bound_candidates ( copy_conditions, & mut candidates) ?;
1109
- }
1110
- _ if self . tcx ( ) . lang_items . sized_trait ( ) == Some ( def_id) => {
1111
- // Sized is never implementable by end-users, it is
1112
- // always automatically computed.
1113
- let sized_conditions = self . sized_conditions ( obligation) ;
1114
- self . assemble_builtin_bound_candidates ( sized_conditions,
1115
- & mut candidates) ?;
1116
- }
1117
-
1118
- _ if self . tcx ( ) . lang_items . unsize_trait ( ) == Some ( def_id) => {
1119
- self . assemble_candidates_for_unsizing ( obligation, & mut candidates) ;
1120
- }
1121
-
1122
- // For non-builtins and Send/Sync
1123
- _ => {
1124
- self . assemble_closure_candidates ( obligation, & mut candidates) ?;
1125
- self . assemble_fn_pointer_candidates ( obligation, & mut candidates) ?;
1126
- self . assemble_candidates_from_impls ( obligation, & mut candidates) ?;
1127
- self . assemble_candidates_from_object_ty ( obligation, & mut candidates) ;
1128
- }
1096
+ if self . tcx ( ) . lang_items . copy_trait ( ) == Some ( def_id) {
1097
+ debug ! ( "obligation self ty is {:?}" ,
1098
+ obligation. predicate. 0 . self_ty( ) ) ;
1099
+
1100
+ // User-defined copy impls are permitted, but only for
1101
+ // structs and enums.
1102
+ self . assemble_candidates_from_impls ( obligation, & mut candidates) ?;
1103
+
1104
+ // For other types, we'll use the builtin rules.
1105
+ let copy_conditions = self . copy_conditions ( obligation) ;
1106
+ self . assemble_builtin_bound_candidates ( copy_conditions, & mut candidates) ?;
1107
+ } else if self . tcx ( ) . lang_items . sized_trait ( ) == Some ( def_id) {
1108
+ // Sized is never implementable by end-users, it is
1109
+ // always automatically computed.
1110
+ let sized_conditions = self . sized_conditions ( obligation) ;
1111
+ self . assemble_builtin_bound_candidates ( sized_conditions,
1112
+ & mut candidates) ?;
1113
+ } else if self . tcx ( ) . lang_items . unsize_trait ( ) == Some ( def_id) {
1114
+ self . assemble_candidates_for_unsizing ( obligation, & mut candidates) ;
1115
+ } else {
1116
+ self . assemble_closure_candidates ( obligation, & mut candidates) ?;
1117
+ self . assemble_fn_pointer_candidates ( obligation, & mut candidates) ?;
1118
+ self . assemble_candidates_from_impls ( obligation, & mut candidates) ?;
1119
+ self . assemble_candidates_from_object_ty ( obligation, & mut candidates) ;
1129
1120
}
1130
1121
1131
1122
self . assemble_candidates_from_projected_tys ( obligation, & mut candidates) ;
@@ -1446,7 +1437,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
1446
1437
1447
1438
if self . tcx ( ) . trait_has_default_impl ( def_id) {
1448
1439
match self_ty. sty {
1449
- ty:: TyTrait ( ..) => {
1440
+ ty:: TyDynamic ( ..) => {
1450
1441
// For object types, we don't know what the closed
1451
1442
// over types are. For most traits, this means we
1452
1443
// conservatively say nothing; a candidate may be
@@ -1516,7 +1507,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
1516
1507
// any LBR.
1517
1508
let self_ty = this. tcx ( ) . erase_late_bound_regions ( & obligation. self_ty ( ) ) ;
1518
1509
let poly_trait_ref = match self_ty. sty {
1519
- ty:: TyTrait ( ref data) => {
1510
+ ty:: TyDynamic ( ref data, .. ) => {
1520
1511
if data. auto_traits ( ) . any ( |did| did == obligation. predicate . def_id ( ) ) {
1521
1512
debug ! ( "assemble_candidates_from_object_ty: matched builtin bound, \
1522
1513
pushing candidate") ;
@@ -1525,7 +1516,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
1525
1516
}
1526
1517
1527
1518
match data. principal ( ) {
1528
- Some ( ref p) => p. with_self_ty ( this. tcx ( ) , self_ty) ,
1519
+ Some ( p) => p. with_self_ty ( this. tcx ( ) , self_ty) ,
1529
1520
None => return ,
1530
1521
}
1531
1522
}
@@ -1598,7 +1589,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
1598
1589
1599
1590
let may_apply = match ( & source. sty , & target. sty ) {
1600
1591
// Trait+Kx+'a -> Trait+Ky+'b (upcasts).
1601
- ( & ty:: TyTrait ( ref data_a) , & ty:: TyTrait ( ref data_b) ) => {
1592
+ ( & ty:: TyDynamic ( ref data_a, .. ) , & ty:: TyDynamic ( ref data_b, .. ) ) => {
1602
1593
// Upcasts permit two things:
1603
1594
//
1604
1595
// 1. Dropping builtin bounds, e.g. `Foo+Send` to `Foo`
@@ -1611,7 +1602,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
1611
1602
// We always upcast when we can because of reason
1612
1603
// #2 (region bounds).
1613
1604
match ( data_a. principal ( ) , data_b. principal ( ) ) {
1614
- ( Some ( ref a) , Some ( ref b) ) => a. def_id ( ) == b. def_id ( ) &&
1605
+ ( Some ( a) , Some ( b) ) => a. def_id ( ) == b. def_id ( ) &&
1615
1606
data_b. auto_traits ( )
1616
1607
// All of a's auto traits need to be in b's auto traits.
1617
1608
. all ( |b| data_a. auto_traits ( ) . any ( |a| a == b) ) ,
@@ -1620,7 +1611,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
1620
1611
}
1621
1612
1622
1613
// T -> Trait.
1623
- ( _, & ty:: TyTrait ( _ ) ) => true ,
1614
+ ( _, & ty:: TyDynamic ( .. ) ) => true ,
1624
1615
1625
1616
// Ambiguous handling is below T -> Trait, because inference
1626
1617
// variables can still implement Unsize<Trait> and nested
@@ -1772,7 +1763,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
1772
1763
Where ( ty:: Binder ( Vec :: new ( ) ) )
1773
1764
}
1774
1765
1775
- ty:: TyStr | ty:: TySlice ( _) | ty:: TyTrait ( ..) => Never ,
1766
+ ty:: TyStr | ty:: TySlice ( _) | ty:: TyDynamic ( ..) => Never ,
1776
1767
1777
1768
ty:: TyTuple ( tys) => {
1778
1769
Where ( ty:: Binder ( tys. last ( ) . into_iter ( ) . cloned ( ) . collect ( ) ) )
@@ -1818,7 +1809,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
1818
1809
Where ( ty:: Binder ( Vec :: new ( ) ) )
1819
1810
}
1820
1811
1821
- ty:: TyBox ( _) | ty:: TyTrait ( ..) | ty:: TyStr | ty:: TySlice ( ..) |
1812
+ ty:: TyBox ( _) | ty:: TyDynamic ( ..) | ty:: TyStr | ty:: TySlice ( ..) |
1822
1813
ty:: TyClosure ( ..) |
1823
1814
ty:: TyRef ( _, ty:: TypeAndMut { ty : _, mutbl : hir:: MutMutable } ) => {
1824
1815
Never
@@ -1883,7 +1874,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
1883
1874
Vec :: new ( )
1884
1875
}
1885
1876
1886
- ty:: TyTrait ( ..) |
1877
+ ty:: TyDynamic ( ..) |
1887
1878
ty:: TyParam ( ..) |
1888
1879
ty:: TyProjection ( ..) |
1889
1880
ty:: TyAnon ( ..) |
@@ -2169,11 +2160,11 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
2169
2160
// OK to skip binder, it is reintroduced below
2170
2161
let self_ty = self . infcx . shallow_resolve ( obligation. predicate . skip_binder ( ) . self_ty ( ) ) ;
2171
2162
match self_ty. sty {
2172
- ty:: TyTrait ( ref data) => {
2163
+ ty:: TyDynamic ( ref data, .. ) => {
2173
2164
// OK to skip the binder, it is reintroduced below
2174
2165
let principal = data. principal ( ) . unwrap ( ) ;
2175
2166
let input_types = principal. input_types ( ) ;
2176
- let assoc_types = data. projection_bounds . iter ( )
2167
+ let assoc_types = data. projection_bounds ( )
2177
2168
. map ( |pb| pb. skip_binder ( ) . ty ) ;
2178
2169
let all_types: Vec < _ > = input_types. chain ( assoc_types)
2179
2170
. collect ( ) ;
@@ -2305,7 +2296,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
2305
2296
// case that results. -nmatsakis
2306
2297
let self_ty = self . infcx . shallow_resolve ( * obligation. self_ty ( ) . skip_binder ( ) ) ;
2307
2298
let poly_trait_ref = match self_ty. sty {
2308
- ty:: TyTrait ( ref data) => {
2299
+ ty:: TyDynamic ( ref data, .. ) => {
2309
2300
data. principal ( ) . unwrap ( ) . with_self_ty ( self . tcx ( ) , self_ty)
2310
2301
}
2311
2302
_ => {
@@ -2474,14 +2465,16 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
2474
2465
let mut nested = vec ! [ ] ;
2475
2466
match ( & source. sty , & target. sty ) {
2476
2467
// Trait+Kx+'a -> Trait+Ky+'b (upcasts).
2477
- ( & ty:: TyTrait ( ref data_a) , & ty:: TyTrait ( ref data_b) ) => {
2468
+ ( & ty:: TyDynamic ( ref data_a, r_a ) , & ty:: TyDynamic ( ref data_b, r_b ) ) => {
2478
2469
// See assemble_candidates_for_unsizing for more info.
2479
- let new_trait = tcx. mk_trait ( ty:: TraitObject :: new (
2480
- data_a. principal ( ) ,
2481
- data_b. region_bound ,
2482
- data_b. auto_traits ( ) . collect ( ) ,
2483
- data_a. projection_bounds . clone ( ) ,
2484
- ) ) ;
2470
+ // Binders reintroduced below in call to mk_existential_predicates.
2471
+ let principal = data_a. skip_binder ( ) . principal ( ) ;
2472
+ let iter = principal. into_iter ( ) . map ( ty:: ExistentialPredicate :: Trait )
2473
+ . chain ( data_a. skip_binder ( ) . projection_bounds ( )
2474
+ . map ( |x| ty:: ExistentialPredicate :: Projection ( x) ) )
2475
+ . chain ( data_b. auto_traits ( ) . map ( ty:: ExistentialPredicate :: AutoTrait ) ) ;
2476
+ let new_trait = tcx. mk_dynamic (
2477
+ ty:: Binder ( tcx. mk_existential_predicates ( iter) ) , r_b) ;
2485
2478
let InferOk { obligations, .. } =
2486
2479
self . infcx . sub_types ( false , & obligation. cause , new_trait, target)
2487
2480
. map_err ( |_| Unimplemented ) ?;
@@ -2491,17 +2484,16 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
2491
2484
let cause = ObligationCause :: new ( obligation. cause . span ,
2492
2485
obligation. cause . body_id ,
2493
2486
ObjectCastObligation ( target) ) ;
2494
- let outlives = ty:: OutlivesPredicate ( data_a. region_bound ,
2495
- data_b. region_bound ) ;
2487
+ let outlives = ty:: OutlivesPredicate ( r_a, r_b) ;
2496
2488
nested. push ( Obligation :: with_depth ( cause,
2497
2489
obligation. recursion_depth + 1 ,
2498
2490
ty:: Binder ( outlives) . to_predicate ( ) ) ) ;
2499
2491
}
2500
2492
2501
2493
// T -> Trait.
2502
- ( _, & ty:: TyTrait ( ref data) ) => {
2494
+ ( _, & ty:: TyDynamic ( ref data, r ) ) => {
2503
2495
let mut object_dids =
2504
- data. auto_traits ( ) . chain ( data. principal ( ) . map ( |ref p| p. def_id ( ) ) ) ;
2496
+ data. auto_traits ( ) . chain ( data. principal ( ) . map ( |p| p. def_id ( ) ) ) ;
2505
2497
if let Some ( did) = object_dids. find ( |did| {
2506
2498
!tcx. is_object_safe ( * did)
2507
2499
} ) {
@@ -2517,35 +2509,27 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
2517
2509
predicate) ) ;
2518
2510
} ;
2519
2511
2520
- // Create the obligation for casting from T to Trait.
2521
- push ( data. principal ( ) . unwrap ( ) . with_self_ty ( tcx, source) . to_predicate ( ) ) ;
2522
-
2523
- // We can only make objects from sized types.
2524
- let trait_refs = data. auto_traits ( )
2525
- . chain ( iter:: once (
2526
- tcx. lang_items . require ( lang_items:: SizedTraitLangItem )
2527
- . unwrap_or_else ( |msg| tcx. sess . fatal ( & msg[ ..] ) ) ) )
2528
- . map ( |did| ty:: TraitRef {
2529
- def_id : did,
2530
- substs : tcx. mk_substs_trait ( source, & [ ] ) ,
2531
- } ) ;
2532
-
2533
- // Create additional obligations for all the various builtin
2534
- // bounds attached to the object cast. (In other words, if the
2535
- // object type is Foo+Send, this would create an obligation
2536
- // for the Send check.)
2537
- for tr in trait_refs {
2538
- push ( tr. to_predicate ( ) ) ;
2512
+ // Create obligations:
2513
+ // - Casting T to Trait
2514
+ // - For all the various builtin bounds attached to the object cast. (In other
2515
+ // words, if the object type is Foo+Send, this would create an obligation for the
2516
+ // Send check.)
2517
+ // - Projection predicates
2518
+ for predicate in data. iter ( ) {
2519
+ push ( predicate. with_self_ty ( tcx, source) ) ;
2539
2520
}
2540
2521
2541
- // Create obligations for the projection predicates.
2542
- for bound in & data. projection_bounds {
2543
- push ( bound. with_self_ty ( tcx, source) . to_predicate ( ) ) ;
2544
- }
2522
+ // We can only make objects from sized types.
2523
+ let tr = ty:: TraitRef {
2524
+ def_id : tcx. lang_items . require ( lang_items:: SizedTraitLangItem )
2525
+ . unwrap_or_else ( |msg| tcx. sess . fatal ( & msg[ ..] ) ) ,
2526
+ substs : tcx. mk_substs_trait ( source, & [ ] ) ,
2527
+ } ;
2528
+ push ( tr. to_predicate ( ) ) ;
2545
2529
2546
2530
// If the type is `Foo+'a`, ensures that the type
2547
2531
// being cast to `Foo+'a` outlives `'a`:
2548
- let outlives = ty:: OutlivesPredicate ( source, data . region_bound ) ;
2532
+ let outlives = ty:: OutlivesPredicate ( source, r ) ;
2549
2533
push ( ty:: Binder ( outlives) . to_predicate ( ) ) ;
2550
2534
}
2551
2535
0 commit comments