@@ -4,6 +4,7 @@ use super::SubregionOrigin;
4
4
use crate :: infer:: combine:: ConstEquateRelation ;
5
5
use crate :: infer:: { TypeVariableOrigin , TypeVariableOriginKind } ;
6
6
use crate :: traits:: Obligation ;
7
+ use rustc_middle:: ty:: error:: { ExpectedFound , TypeError } ;
7
8
use rustc_middle:: ty:: relate:: { Cause , Relate , RelateResult , TypeRelation } ;
8
9
use rustc_middle:: ty:: visit:: TypeVisitable ;
9
10
use rustc_middle:: ty:: TyVar ;
@@ -141,17 +142,27 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
141
142
Ok ( infcx. tcx . mk_ty_var ( var) )
142
143
} ;
143
144
let ( a, b) = if self . a_is_expected { ( a, b) } else { ( b, a) } ;
144
- let ( a , b ) = match ( a. kind ( ) , b. kind ( ) ) {
145
+ let ( ga , gb ) = match ( a. kind ( ) , b. kind ( ) ) {
145
146
( & ty:: Opaque ( ..) , _) => ( a, generalize ( b, true ) ?) ,
146
147
( _, & ty:: Opaque ( ..) ) => ( generalize ( a, false ) ?, b) ,
147
148
_ => unreachable ! ( ) ,
148
149
} ;
149
150
self . fields . obligations . extend (
150
151
infcx
151
- . handle_opaque_type ( a, b, true , & self . fields . trace . cause , self . param_env ( ) ) ?
152
+ . handle_opaque_type ( ga, gb, true , & self . fields . trace . cause , self . param_env ( ) )
153
+ // Don't leak any generalized type variables out of this
154
+ // subtyping relation in the case of a type error.
155
+ . map_err ( |err| {
156
+ let ( ga, gb) = self . fields . infcx . resolve_vars_if_possible ( ( ga, gb) ) ;
157
+ if let TypeError :: Sorts ( sorts) = err && sorts. expected == ga && sorts. found == gb {
158
+ TypeError :: Sorts ( ExpectedFound { expected : a, found : b } )
159
+ } else {
160
+ err
161
+ }
162
+ } ) ?
152
163
. obligations ,
153
164
) ;
154
- Ok ( a )
165
+ Ok ( ga )
155
166
}
156
167
157
168
_ => {
0 commit comments