55
66use rustc_infer:: infer:: TyCtxtInferExt ;
77use rustc_middle:: traits:: { DefiningAnchor , ObligationCause } ;
8- use rustc_middle:: ty:: { ParamEnv , Ty , TyCtxt } ;
8+ use rustc_middle:: ty:: { ParamEnv , Ty , TyCtxt , Variance } ;
99use rustc_trait_selection:: traits:: ObligationCtxt ;
1010
1111/// Returns whether the two types are equal up to subtyping.
@@ -24,16 +24,22 @@ pub fn is_equal_up_to_subtyping<'tcx>(
2424 }
2525
2626 // Check for subtyping in either direction.
27- is_subtype ( tcx, param_env, src, dest) || is_subtype ( tcx, param_env, dest, src)
27+ relate_types ( tcx, param_env, Variance :: Covariant , src, dest)
28+ || relate_types ( tcx, param_env, Variance :: Covariant , dest, src)
2829}
2930
3031/// Returns whether `src` is a subtype of `dest`, i.e. `src <: dest`.
3132///
33+ /// For almost all of the use cases variance should be `Covariant`,
34+ /// in `MirPhase` >= `MirPhase::Runtime(RuntimePhase::Initial` variance should
35+ /// be `Invariant`.
36+ ///
3237/// This mostly ignores opaque types as it can be used in constraining contexts
3338/// while still computing the final underlying type.
34- pub fn is_subtype < ' tcx > (
39+ pub fn relate_types < ' tcx > (
3540 tcx : TyCtxt < ' tcx > ,
3641 param_env : ParamEnv < ' tcx > ,
42+ variance : Variance ,
3743 src : Ty < ' tcx > ,
3844 dest : Ty < ' tcx > ,
3945) -> bool {
@@ -48,7 +54,7 @@ pub fn is_subtype<'tcx>(
4854 let cause = ObligationCause :: dummy ( ) ;
4955 let src = ocx. normalize ( & cause, param_env, src) ;
5056 let dest = ocx. normalize ( & cause, param_env, dest) ;
51- match ocx. sub ( & cause, param_env, src, dest) {
57+ match ocx. relate ( & cause, param_env, variance , src, dest) {
5258 Ok ( ( ) ) => { }
5359 Err ( _) => return false ,
5460 } ;
0 commit comments