11use super :: potentially_plural_count;
22use crate :: errors:: LifetimesOrBoundsMismatchOnTrait ;
3- use hir:: def_id:: DefId ;
3+ use hir:: def_id:: { DefId , LocalDefId } ;
44use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
55use rustc_errors:: { pluralize, struct_span_err, Applicability , DiagnosticId , ErrorGuaranteed } ;
66use rustc_hir as hir;
@@ -1300,17 +1300,20 @@ fn compare_generic_param_kinds<'tcx>(
13001300 Ok ( ( ) )
13011301}
13021302
1303- pub ( crate ) fn compare_const_impl < ' tcx > (
1303+ /// Use `tcx.compare_assoc_const_impl_item_with_trait_item` instead
1304+ pub ( crate ) fn raw_compare_const_impl < ' tcx > (
13041305 tcx : TyCtxt < ' tcx > ,
1305- impl_c : & ty :: AssocItem ,
1306- impl_c_span : Span ,
1307- trait_c : & ty :: AssocItem ,
1308- impl_trait_ref : ty :: TraitRef < ' tcx > ,
1309- ) {
1306+ ( impl_const_item_def , trait_const_item_def ) : ( LocalDefId , DefId ) ,
1307+ ) -> Result < ( ) , ErrorGuaranteed > {
1308+ let impl_const_item = tcx . associated_item ( impl_const_item_def ) ;
1309+ let trait_const_item = tcx. associated_item ( trait_const_item_def ) ;
1310+ let impl_trait_ref = tcx . impl_trait_ref ( impl_const_item . container_id ( tcx ) ) . unwrap ( ) ;
13101311 debug ! ( "compare_const_impl(impl_trait_ref={:?})" , impl_trait_ref) ;
13111312
1313+ let impl_c_span = tcx. def_span ( impl_const_item_def. to_def_id ( ) ) ;
1314+
13121315 tcx. infer_ctxt ( ) . enter ( |infcx| {
1313- let param_env = tcx. param_env ( impl_c . def_id ) ;
1316+ let param_env = tcx. param_env ( impl_const_item_def . to_def_id ( ) ) ;
13141317 let ocx = ObligationCtxt :: new ( & infcx) ;
13151318
13161319 // The below is for the most part highly similar to the procedure
@@ -1322,18 +1325,18 @@ pub(crate) fn compare_const_impl<'tcx>(
13221325
13231326 // Create a parameter environment that represents the implementation's
13241327 // method.
1325- let impl_c_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( impl_c . def_id . expect_local ( ) ) ;
1328+ let impl_c_hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( impl_const_item_def ) ;
13261329
13271330 // Compute placeholder form of impl and trait const tys.
1328- let impl_ty = tcx. type_of ( impl_c . def_id ) ;
1329- let trait_ty = tcx. bound_type_of ( trait_c . def_id ) . subst ( tcx, trait_to_impl_substs) ;
1331+ let impl_ty = tcx. type_of ( impl_const_item_def . to_def_id ( ) ) ;
1332+ let trait_ty = tcx. bound_type_of ( trait_const_item_def ) . subst ( tcx, trait_to_impl_substs) ;
13301333 let mut cause = ObligationCause :: new (
13311334 impl_c_span,
13321335 impl_c_hir_id,
13331336 ObligationCauseCode :: CompareImplItemObligation {
1334- impl_item_def_id : impl_c . def_id . expect_local ( ) ,
1335- trait_item_def_id : trait_c . def_id ,
1336- kind : impl_c . kind ,
1337+ impl_item_def_id : impl_const_item_def ,
1338+ trait_item_def_id : trait_const_item_def ,
1339+ kind : impl_const_item . kind ,
13371340 } ,
13381341 ) ;
13391342
@@ -1358,24 +1361,24 @@ pub(crate) fn compare_const_impl<'tcx>(
13581361 ) ;
13591362
13601363 // Locate the Span containing just the type of the offending impl
1361- match tcx. hir ( ) . expect_impl_item ( impl_c . def_id . expect_local ( ) ) . kind {
1364+ match tcx. hir ( ) . expect_impl_item ( impl_const_item_def ) . kind {
13621365 ImplItemKind :: Const ( ref ty, _) => cause. span = ty. span ,
1363- _ => bug ! ( "{:?} is not a impl const" , impl_c ) ,
1366+ _ => bug ! ( "{:?} is not a impl const" , impl_const_item ) ,
13641367 }
13651368
13661369 let mut diag = struct_span_err ! (
13671370 tcx. sess,
13681371 cause. span,
13691372 E0326 ,
13701373 "implemented const `{}` has an incompatible type for trait" ,
1371- trait_c . name
1374+ trait_const_item . name
13721375 ) ;
13731376
1374- let trait_c_span = trait_c . def_id . as_local ( ) . map ( |trait_c_def_id| {
1377+ let trait_c_span = trait_const_item_def . as_local ( ) . map ( |trait_c_def_id| {
13751378 // Add a label to the Span containing just the type of the const
13761379 match tcx. hir ( ) . expect_trait_item ( trait_c_def_id) . kind {
13771380 TraitItemKind :: Const ( ref ty, _) => ty. span ,
1378- _ => bug ! ( "{:?} is not a trait const" , trait_c ) ,
1381+ _ => bug ! ( "{:?} is not a trait const" , trait_const_item ) ,
13791382 }
13801383 } ) ;
13811384
@@ -1391,23 +1394,22 @@ pub(crate) fn compare_const_impl<'tcx>(
13911394 false ,
13921395 false ,
13931396 ) ;
1394- diag. emit ( ) ;
1395- }
1397+ return Err ( diag. emit ( ) ) ;
1398+ } ;
13961399
13971400 // Check that all obligations are satisfied by the implementation's
13981401 // version.
13991402 let errors = ocx. select_all_or_error ( ) ;
14001403 if !errors. is_empty ( ) {
1401- infcx. report_fulfillment_errors ( & errors, None , false ) ;
1402- return ;
1404+ return Err ( infcx. report_fulfillment_errors ( & errors, None , false ) ) ;
14031405 }
14041406
1407+ // FIXME return `ErrorReported` if region obligations error?
14051408 let outlives_environment = OutlivesEnvironment :: new ( param_env) ;
1406- infcx. check_region_obligations_and_report_errors (
1407- impl_c. def_id . expect_local ( ) ,
1408- & outlives_environment,
1409- ) ;
1410- } ) ;
1409+ infcx
1410+ . check_region_obligations_and_report_errors ( impl_const_item_def, & outlives_environment) ;
1411+ Ok ( ( ) )
1412+ } )
14111413}
14121414
14131415pub ( crate ) fn compare_ty_impl < ' tcx > (
0 commit comments