@@ -4,6 +4,7 @@ use super::SubregionOrigin;
44use crate :: infer:: combine:: ConstEquateRelation ;
55use crate :: infer:: { TypeVariableOrigin , TypeVariableOriginKind } ;
66use crate :: traits:: Obligation ;
7+ use rustc_middle:: ty:: error:: { ExpectedFound , TypeError } ;
78use rustc_middle:: ty:: relate:: { Cause , Relate , RelateResult , TypeRelation } ;
89use rustc_middle:: ty:: visit:: TypeVisitable ;
910use rustc_middle:: ty:: TyVar ;
@@ -141,17 +142,27 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
141142 Ok ( infcx. tcx . mk_ty_var ( var) )
142143 } ;
143144 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 ( ) ) {
145146 ( & ty:: Opaque ( ..) , _) => ( a, generalize ( b, true ) ?) ,
146147 ( _, & ty:: Opaque ( ..) ) => ( generalize ( a, false ) ?, b) ,
147148 _ => unreachable ! ( ) ,
148149 } ;
149150 self . fields . obligations . extend (
150151 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+ } ) ?
152163 . obligations ,
153164 ) ;
154- Ok ( a )
165+ Ok ( ga )
155166 }
156167
157168 _ => {
0 commit comments