@@ -1864,9 +1864,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
1864
1864
//
1865
1865
// We always upcast when we can because of reason 3 (region bounds).
1866
1866
1867
- // All of a's auto traits need to be in b's auto traits.
1868
- data_b. auto_traits ( )
1869
- . all ( |b| data_a. auto_traits ( ) . any ( |a| a == b) )
1867
+ if self . tcx ( ) . features ( ) . trait_upcasting {
1868
+ true
1869
+ } else {
1870
+ // Just allow upcast kinds 2 and 3 from above.
1871
+ data_a. principal_def_id ( ) == data_b. principal_def_id ( ) &&
1872
+ data_b. auto_traits ( )
1873
+ // All of a's auto traits need to be in b's auto traits.
1874
+ . all ( |b| data_a. auto_traits ( ) . any ( |a| a == b) )
1875
+ }
1870
1876
}
1871
1877
1872
1878
// `T` -> `Trait`
@@ -2948,12 +2954,20 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
2948
2954
let iter = data_a
2949
2955
. principal ( )
2950
2956
. map ( ty:: ExistentialPredicate :: Trait )
2951
- . into_iter ( )
2952
- . chain ( data_a. projection_bounds ( ) . map ( ty:: ExistentialPredicate :: Projection ) )
2953
- . chain ( data_b. auto_traits ( ) . map ( ty:: ExistentialPredicate :: AutoTrait ) ) ;
2957
+ . into_iter ( ) . chain (
2958
+ data_a
2959
+ . projection_bounds ( )
2960
+ . map ( |x| ty:: ExistentialPredicate :: Projection ( x) ) ,
2961
+ )
2962
+ . chain (
2963
+ data_b
2964
+ . auto_traits ( )
2965
+ . map ( ty:: ExistentialPredicate :: AutoTrait ) ,
2966
+ ) ;
2954
2967
tcx. mk_existential_predicates ( iter)
2955
2968
} ) ;
2956
- let source_ty = tcx. mk_dynamic ( existential_predicates, region_b) ;
2969
+ let source_with_target_auto_traits =
2970
+ tcx. mk_dynamic ( existential_predicates, region_b) ;
2957
2971
2958
2972
if tcx. features ( ) . trait_upcasting {
2959
2973
// Register obligations for `dyn TraitA1 [TraitA2...]: TraitB1 [TraitB2...]`.
@@ -2974,7 +2988,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
2974
2988
}
2975
2989
} )
2976
2990
. map ( |predicate|
2977
- predicate_to_obligation ( predicate. with_self_ty ( tcx, source_ty ) )
2991
+ predicate_to_obligation ( predicate. with_self_ty ( tcx, source ) )
2978
2992
) ,
2979
2993
) ;
2980
2994
} else {
@@ -2996,7 +3010,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
2996
3010
// with what our behavior should be there. -nikomatsakis
2997
3011
let InferOk { obligations, .. } = self . infcx
2998
3012
. at ( & obligation. cause , obligation. param_env )
2999
- . eq ( target, source_ty ) // FIXME: see above.
3013
+ . eq ( target, source_with_target_auto_traits ) // FIXME: see above.
3000
3014
. map_err ( |_| Unimplemented ) ?;
3001
3015
nested. extend ( obligations) ;
3002
3016
}
0 commit comments