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