@@ -50,7 +50,6 @@ use std::fmt;
5050use std:: marker:: PhantomData ;
5151use std:: mem;
5252use std:: rc:: Rc ;
53- use std:: iter;
5453use syntax:: abi:: Abi ;
5554use hir;
5655use util:: nodemap:: FxHashMap ;
@@ -1094,38 +1093,30 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
10941093 // and applicable impls. There is a certain set of precedence rules here.
10951094
10961095 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) ;
11291120 }
11301121
11311122 self . assemble_candidates_from_projected_tys ( obligation, & mut candidates) ;
@@ -1446,7 +1437,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
14461437
14471438 if self . tcx ( ) . trait_has_default_impl ( def_id) {
14481439 match self_ty. sty {
1449- ty:: TyTrait ( ..) => {
1440+ ty:: TyDynamic ( ..) => {
14501441 // For object types, we don't know what the closed
14511442 // over types are. For most traits, this means we
14521443 // conservatively say nothing; a candidate may be
@@ -1516,7 +1507,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
15161507 // any LBR.
15171508 let self_ty = this. tcx ( ) . erase_late_bound_regions ( & obligation. self_ty ( ) ) ;
15181509 let poly_trait_ref = match self_ty. sty {
1519- ty:: TyTrait ( ref data) => {
1510+ ty:: TyDynamic ( ref data, .. ) => {
15201511 if data. auto_traits ( ) . any ( |did| did == obligation. predicate . def_id ( ) ) {
15211512 debug ! ( "assemble_candidates_from_object_ty: matched builtin bound, \
15221513 pushing candidate") ;
@@ -1525,7 +1516,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
15251516 }
15261517
15271518 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) ,
15291520 None => return ,
15301521 }
15311522 }
@@ -1598,7 +1589,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
15981589
15991590 let may_apply = match ( & source. sty , & target. sty ) {
16001591 // 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, .. ) ) => {
16021593 // Upcasts permit two things:
16031594 //
16041595 // 1. Dropping builtin bounds, e.g. `Foo+Send` to `Foo`
@@ -1611,7 +1602,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
16111602 // We always upcast when we can because of reason
16121603 // #2 (region bounds).
16131604 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 ( ) &&
16151606 data_b. auto_traits ( )
16161607 // All of a's auto traits need to be in b's auto traits.
16171608 . all ( |b| data_a. auto_traits ( ) . any ( |a| a == b) ) ,
@@ -1620,7 +1611,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
16201611 }
16211612
16221613 // T -> Trait.
1623- ( _, & ty:: TyTrait ( _ ) ) => true ,
1614+ ( _, & ty:: TyDynamic ( .. ) ) => true ,
16241615
16251616 // Ambiguous handling is below T -> Trait, because inference
16261617 // variables can still implement Unsize<Trait> and nested
@@ -1772,7 +1763,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
17721763 Where ( ty:: Binder ( Vec :: new ( ) ) )
17731764 }
17741765
1775- ty:: TyStr | ty:: TySlice ( _) | ty:: TyTrait ( ..) => Never ,
1766+ ty:: TyStr | ty:: TySlice ( _) | ty:: TyDynamic ( ..) => Never ,
17761767
17771768 ty:: TyTuple ( tys) => {
17781769 Where ( ty:: Binder ( tys. last ( ) . into_iter ( ) . cloned ( ) . collect ( ) ) )
@@ -1818,7 +1809,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
18181809 Where ( ty:: Binder ( Vec :: new ( ) ) )
18191810 }
18201811
1821- ty:: TyBox ( _) | ty:: TyTrait ( ..) | ty:: TyStr | ty:: TySlice ( ..) |
1812+ ty:: TyBox ( _) | ty:: TyDynamic ( ..) | ty:: TyStr | ty:: TySlice ( ..) |
18221813 ty:: TyClosure ( ..) |
18231814 ty:: TyRef ( _, ty:: TypeAndMut { ty : _, mutbl : hir:: MutMutable } ) => {
18241815 Never
@@ -1883,7 +1874,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
18831874 Vec :: new ( )
18841875 }
18851876
1886- ty:: TyTrait ( ..) |
1877+ ty:: TyDynamic ( ..) |
18871878 ty:: TyParam ( ..) |
18881879 ty:: TyProjection ( ..) |
18891880 ty:: TyAnon ( ..) |
@@ -2169,11 +2160,11 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
21692160 // OK to skip binder, it is reintroduced below
21702161 let self_ty = self . infcx . shallow_resolve ( obligation. predicate . skip_binder ( ) . self_ty ( ) ) ;
21712162 match self_ty. sty {
2172- ty:: TyTrait ( ref data) => {
2163+ ty:: TyDynamic ( ref data, .. ) => {
21732164 // OK to skip the binder, it is reintroduced below
21742165 let principal = data. principal ( ) . unwrap ( ) ;
21752166 let input_types = principal. input_types ( ) ;
2176- let assoc_types = data. projection_bounds . iter ( )
2167+ let assoc_types = data. projection_bounds ( )
21772168 . map ( |pb| pb. skip_binder ( ) . ty ) ;
21782169 let all_types: Vec < _ > = input_types. chain ( assoc_types)
21792170 . collect ( ) ;
@@ -2305,7 +2296,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
23052296 // case that results. -nmatsakis
23062297 let self_ty = self . infcx . shallow_resolve ( * obligation. self_ty ( ) . skip_binder ( ) ) ;
23072298 let poly_trait_ref = match self_ty. sty {
2308- ty:: TyTrait ( ref data) => {
2299+ ty:: TyDynamic ( ref data, .. ) => {
23092300 data. principal ( ) . unwrap ( ) . with_self_ty ( self . tcx ( ) , self_ty)
23102301 }
23112302 _ => {
@@ -2474,14 +2465,16 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
24742465 let mut nested = vec ! [ ] ;
24752466 match ( & source. sty , & target. sty ) {
24762467 // 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 ) ) => {
24782469 // 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) ;
24852478 let InferOk { obligations, .. } =
24862479 self . infcx . sub_types ( false , & obligation. cause , new_trait, target)
24872480 . map_err ( |_| Unimplemented ) ?;
@@ -2491,17 +2484,16 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
24912484 let cause = ObligationCause :: new ( obligation. cause . span ,
24922485 obligation. cause . body_id ,
24932486 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) ;
24962488 nested. push ( Obligation :: with_depth ( cause,
24972489 obligation. recursion_depth + 1 ,
24982490 ty:: Binder ( outlives) . to_predicate ( ) ) ) ;
24992491 }
25002492
25012493 // T -> Trait.
2502- ( _, & ty:: TyTrait ( ref data) ) => {
2494+ ( _, & ty:: TyDynamic ( ref data, r ) ) => {
25032495 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 ( ) ) ) ;
25052497 if let Some ( did) = object_dids. find ( |did| {
25062498 !tcx. is_object_safe ( * did)
25072499 } ) {
@@ -2517,35 +2509,27 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
25172509 predicate) ) ;
25182510 } ;
25192511
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) ) ;
25392520 }
25402521
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 ( ) ) ;
25452529
25462530 // If the type is `Foo+'a`, ensures that the type
25472531 // being cast to `Foo+'a` outlives `'a`:
2548- let outlives = ty:: OutlivesPredicate ( source, data . region_bound ) ;
2532+ let outlives = ty:: OutlivesPredicate ( source, r ) ;
25492533 push ( ty:: Binder ( outlives) . to_predicate ( ) ) ;
25502534 }
25512535
0 commit comments