@@ -1149,7 +1149,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11491149
11501150 // FIXME(inherent_associated_types, #106719): Support self types other than ADTs.
11511151 if let Some ( ( ty, did) ) = self . probe_inherent_assoc_ty (
1152- assoc_ident,
11531152 assoc_segment,
11541153 adt_def. did ( ) ,
11551154 qself_ty,
@@ -1354,7 +1353,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
13541353
13551354 fn probe_inherent_assoc_ty (
13561355 & self ,
1357- name : Ident ,
13581356 segment : & hir:: PathSegment < ' tcx > ,
13591357 adt_did : DefId ,
13601358 self_ty : Ty < ' tcx > ,
@@ -1373,6 +1371,60 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
13731371 return Ok ( None ) ;
13741372 }
13751373
1374+ let Some ( ( def_id, args) ) = self . probe_inherent_assoc_shared (
1375+ segment,
1376+ adt_did,
1377+ self_ty,
1378+ block,
1379+ span,
1380+ ty:: AssocKind :: Type ,
1381+ ) ?
1382+ else {
1383+ return Ok ( None ) ;
1384+ } ;
1385+
1386+ let ty = Ty :: new_alias ( tcx, ty:: Inherent , ty:: AliasTy :: new_from_args ( tcx, def_id, args) ) ;
1387+ Ok ( Some ( ( ty, def_id) ) )
1388+ }
1389+
1390+ fn probe_inherent_assoc_const (
1391+ & self ,
1392+ segment : & hir:: PathSegment < ' tcx > ,
1393+ adt_did : DefId ,
1394+ self_ty : Ty < ' tcx > ,
1395+ block : HirId ,
1396+ span : Span ,
1397+ ) -> Result < Option < ( Const < ' tcx > , DefId ) > , ErrorGuaranteed > {
1398+ let tcx = self . tcx ( ) ;
1399+
1400+ let Some ( ( def_id, args) ) = self . probe_inherent_assoc_shared (
1401+ segment,
1402+ adt_did,
1403+ self_ty,
1404+ block,
1405+ span,
1406+ ty:: AssocKind :: Const ,
1407+ ) ?
1408+ else {
1409+ return Ok ( None ) ;
1410+ } ;
1411+
1412+ let ct = Const :: new_unevaluated ( tcx, ty:: UnevaluatedConst :: new ( def_id, args) ) ;
1413+ Ok ( Some ( ( ct, def_id) ) )
1414+ }
1415+
1416+ fn probe_inherent_assoc_shared (
1417+ & self ,
1418+ segment : & hir:: PathSegment < ' tcx > ,
1419+ adt_did : DefId ,
1420+ self_ty : Ty < ' tcx > ,
1421+ block : HirId ,
1422+ span : Span ,
1423+ kind : ty:: AssocKind ,
1424+ ) -> Result < Option < ( DefId , GenericArgsRef < ' tcx > ) > , ErrorGuaranteed > {
1425+ let tcx = self . tcx ( ) ;
1426+
1427+ let name = segment. ident ;
13761428 let candidates: Vec < _ > = tcx
13771429 . inherent_impls ( adt_did)
13781430 . iter ( )
@@ -1416,8 +1468,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14161468 & mut universes,
14171469 self_ty,
14181470 |self_ty| {
1419- self . select_inherent_assoc_type_candidates (
1420- infcx, name, span, self_ty, param_env, candidates,
1471+ self . select_inherent_assoc_candidates (
1472+ infcx, name, span, self_ty, param_env, candidates, kind ,
14211473 )
14221474 } ,
14231475 ) ?;
@@ -1434,20 +1486,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14341486 . chain ( args. into_iter ( ) . skip ( parent_args. len ( ) ) ) ,
14351487 ) ;
14361488
1437- let ty =
1438- Ty :: new_alias ( tcx, ty:: Inherent , ty:: AliasTy :: new_from_args ( tcx, assoc_item, args) ) ;
1439-
1440- Ok ( Some ( ( ty, assoc_item) ) )
1489+ Ok ( Some ( ( assoc_item, args) ) )
14411490 }
14421491
1443- fn select_inherent_assoc_type_candidates (
1492+ fn select_inherent_assoc_candidates (
14441493 & self ,
14451494 infcx : & InferCtxt < ' tcx > ,
14461495 name : Ident ,
14471496 span : Span ,
14481497 self_ty : Ty < ' tcx > ,
14491498 param_env : ParamEnv < ' tcx > ,
14501499 candidates : Vec < ( DefId , ( DefId , DefId ) ) > ,
1500+ kind : ty:: AssocKind ,
14511501 ) -> Result < ( DefId , ( DefId , DefId ) ) , ErrorGuaranteed > {
14521502 let tcx = self . tcx ( ) ;
14531503 let mut fulfillment_errors = Vec :: new ( ) ;
@@ -1492,17 +1542,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14921542 . collect ( ) ;
14931543
14941544 match & applicable_candidates[ ..] {
1495- & [ ] => Err ( self . complain_about_inherent_assoc_ty_not_found (
1545+ & [ ] => Err ( self . complain_about_inherent_assoc_not_found (
14961546 name,
14971547 self_ty,
14981548 candidates,
14991549 fulfillment_errors,
15001550 span,
1551+ kind,
15011552 ) ) ,
15021553
15031554 & [ applicable_candidate] => Ok ( applicable_candidate) ,
15041555
1505- & [ _, ..] => Err ( self . complain_about_ambiguous_inherent_assoc_ty (
1556+ & [ _, ..] => Err ( self . complain_about_ambiguous_inherent_assoc (
15061557 name,
15071558 applicable_candidates. into_iter ( ) . map ( |( _, ( candidate, _) ) | candidate) . collect ( ) ,
15081559 span,
@@ -2196,6 +2247,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
21962247 debug ! ( ?qself, ?segment) ;
21972248 let ty = self . lower_ty ( qself) ;
21982249 self . lower_const_assoc_path ( hir_id, const_arg. span ( ) , ty, qself, segment)
2250+ . unwrap_or_else ( |guar| Const :: new_error ( tcx, guar) )
21992251 }
22002252 hir:: ConstArgKind :: Path ( qpath @ hir:: QPath :: LangItem ( ..) ) => {
22012253 ty:: Const :: new_error_with_message (
@@ -2317,7 +2369,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23172369 qself_ty : Ty < ' tcx > ,
23182370 qself : & ' tcx hir:: Ty < ' tcx > ,
23192371 assoc_segment : & ' tcx hir:: PathSegment < ' tcx > ,
2320- ) -> Const < ' tcx > {
2372+ ) -> Result < Const < ' tcx > , ErrorGuaranteed > {
23212373 debug ! ( %qself_ty, ?assoc_segment. ident) ;
23222374 let tcx = self . tcx ( ) ;
23232375
@@ -2338,42 +2390,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23382390 GenericsArgsErrExtend :: EnumVariant { qself, assoc_segment, adt_def } ,
23392391 ) ;
23402392 let uv = ty:: UnevaluatedConst :: new ( variant_def. def_id , ty:: List :: empty ( ) ) ;
2341- return Const :: new_unevaluated ( tcx, uv) ;
2393+ return Ok ( Const :: new_unevaluated ( tcx, uv) ) ;
23422394 }
23432395 }
23442396
23452397 // FIXME(mgca): Support self types other than ADTs.
2346- let candidates = tcx
2347- . inherent_impls ( adt_def. did ( ) )
2348- . iter ( )
2349- . filter_map ( |& impl_| {
2350- self . probe_assoc_item (
2351- assoc_ident,
2352- ty:: AssocKind :: Const ,
2353- hir_ref_id,
2354- span,
2355- impl_,
2356- )
2357- . map ( |assoc| ( impl_, assoc) )
2358- } )
2359- . collect :: < Vec < _ > > ( ) ;
2360- match & candidates[ ..] {
2361- [ ] => { }
2362- & [ ( impl_, assoc) ] => {
2363- // FIXME(mgca): adapted from temporary inherent assoc ty code that may be incorrect
2364- let parent_args = ty:: GenericArgs :: identity_for_item ( tcx, impl_) ;
2365- let args = self . lower_generic_args_of_assoc_item (
2366- span,
2367- assoc. def_id ,
2368- assoc_segment,
2369- parent_args,
2370- ) ;
2371- let uv = ty:: UnevaluatedConst :: new ( assoc. def_id , args) ;
2372- return Const :: new_unevaluated ( tcx, uv) ;
2373- }
2374- [ ..] => {
2375- return Const :: new_error_with_message ( tcx, span, "ambiguous assoc const path" ) ;
2376- }
2398+ if let Some ( ( ct, _) ) = self . probe_inherent_assoc_const (
2399+ assoc_segment,
2400+ adt_def. did ( ) ,
2401+ qself_ty,
2402+ hir_ref_id,
2403+ span,
2404+ ) ? {
2405+ return Ok ( ct) ;
23772406 }
23782407 }
23792408
@@ -2422,21 +2451,21 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24222451 } ;
24232452 let bound = match bound_result {
24242453 Ok ( b) => b,
2425- Err ( reported) => return Const :: new_error ( tcx , reported) ,
2454+ Err ( reported) => return Err ( reported) ,
24262455 } ;
24272456
24282457 let trait_did = bound. def_id ( ) ;
24292458 let assoc_const = self
24302459 . probe_assoc_item ( assoc_ident, ty:: AssocKind :: Const , hir_ref_id, span, trait_did)
24312460 . expect ( "failed to find associated const" ) ;
24322461 if assoc_const. has_type_const_attr ( tcx) {
2433- self . lower_assoc_const ( span, assoc_const. def_id , assoc_segment, bound)
2462+ Ok ( self . lower_assoc_const ( span, assoc_const. def_id , assoc_segment, bound) )
24342463 } else {
24352464 let mut err = tcx
24362465 . dcx ( )
24372466 . struct_span_err ( span, "use of trait associated const without `#[type_const]`" ) ;
24382467 err. note ( "the declaration in the trait must be marked with `#[type_const]`" ) ;
2439- Const :: new_error ( tcx , err. emit ( ) )
2468+ Err ( err. emit ( ) )
24402469 }
24412470 }
24422471
0 commit comments