5
5
6
6
use rustc_infer:: infer:: TyCtxtInferExt ;
7
7
use rustc_middle:: traits:: { DefiningAnchor , ObligationCause } ;
8
- use rustc_middle:: ty:: { ParamEnv , Ty , TyCtxt } ;
8
+ use rustc_middle:: ty:: { ParamEnv , Ty , TyCtxt , Variance } ;
9
9
use rustc_trait_selection:: traits:: ObligationCtxt ;
10
10
11
11
/// Returns whether the two types are equal up to subtyping.
@@ -24,16 +24,22 @@ pub fn is_equal_up_to_subtyping<'tcx>(
24
24
}
25
25
26
26
// 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)
28
29
}
29
30
30
31
/// Returns whether `src` is a subtype of `dest`, i.e. `src <: dest`.
31
32
///
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
+ ///
32
37
/// This mostly ignores opaque types as it can be used in constraining contexts
33
38
/// while still computing the final underlying type.
34
- pub fn is_subtype < ' tcx > (
39
+ pub fn relate_types < ' tcx > (
35
40
tcx : TyCtxt < ' tcx > ,
36
41
param_env : ParamEnv < ' tcx > ,
42
+ variance : Variance ,
37
43
src : Ty < ' tcx > ,
38
44
dest : Ty < ' tcx > ,
39
45
) -> bool {
@@ -48,7 +54,7 @@ pub fn is_subtype<'tcx>(
48
54
let cause = ObligationCause :: dummy ( ) ;
49
55
let src = ocx. normalize ( & cause, param_env, src) ;
50
56
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) {
52
58
Ok ( ( ) ) => { }
53
59
Err ( _) => return false ,
54
60
} ;
0 commit comments