@@ -11,6 +11,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
1111use rustc_hir:: lang_items:: LangItem ;
1212use rustc_infer:: infer:: BoundRegionConversionTime :: HigherRankedType ;
1313use rustc_infer:: infer:: { DefineOpaqueTypes , InferOk } ;
14+ use rustc_middle:: traits:: select:: MetadataCastKind ;
1415use rustc_middle:: traits:: { BuiltinImplSource , SignatureMismatchData } ;
1516use rustc_middle:: ty:: {
1617 self , GenericArgs , GenericArgsRef , GenericParamDefKind , ToPolyTraitRef , ToPredicate ,
@@ -51,22 +52,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
5152 ImplSource :: Builtin ( BuiltinImplSource :: Misc , data)
5253 }
5354
54- MetadataCastCandidate { require_eq } => {
55- let data = if require_eq {
56- let source = obligation. self_ty ( ) . skip_binder ( ) ;
57- let target = obligation. predicate . skip_binder ( ) . trait_ref . args . type_at ( 1 ) ;
58-
59- let InferOk { obligations, .. } = self
60- . infcx
61- . at ( & obligation. cause , obligation. param_env )
62- . sub ( DefineOpaqueTypes :: No , source, target)
63- . map_err ( |_| Unimplemented ) ?;
64-
65- obligations
66- } else {
67- Vec :: new ( )
68- } ;
69-
55+ MetadataCastCandidate ( kind) => {
56+ let data = self . confirm_metadata_cast_candidate ( obligation, kind) ?;
7057 ImplSource :: Builtin ( BuiltinImplSource :: Misc , data)
7158 }
7259
@@ -291,6 +278,37 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
291278 obligations
292279 }
293280
281+ fn confirm_metadata_cast_candidate (
282+ & mut self ,
283+ obligation : & PolyTraitObligation < ' tcx > ,
284+ kind : MetadataCastKind < ' tcx > ,
285+ ) -> Result < Vec < PredicateObligation < ' tcx > > , SelectionError < ' tcx > > {
286+ match kind {
287+ MetadataCastKind :: Unconditional => Ok ( Vec :: new ( ) ) ,
288+ MetadataCastKind :: Subtype => {
289+ let source = obligation. self_ty ( ) . skip_binder ( ) ;
290+ let target = obligation. predicate . skip_binder ( ) . trait_ref . args . type_at ( 1 ) ;
291+
292+ let InferOk { obligations, .. } = self
293+ . infcx
294+ . at ( & obligation. cause , obligation. param_env )
295+ . sub ( DefineOpaqueTypes :: No , source, target)
296+ . map_err ( |_| Unimplemented ) ?;
297+
298+ Ok ( obligations)
299+ }
300+ MetadataCastKind :: Dyn ( source, target) => {
301+ let InferOk { obligations, .. } = self
302+ . infcx
303+ . at ( & obligation. cause , obligation. param_env )
304+ . eq ( DefineOpaqueTypes :: No , source, target)
305+ . map_err ( |_| Unimplemented ) ?;
306+
307+ Ok ( obligations)
308+ }
309+ }
310+ }
311+
294312 #[ instrument( level = "debug" , skip( self ) ) ]
295313 fn confirm_transmutability_candidate (
296314 & mut self ,
0 commit comments