@@ -1149,7 +1149,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1149
1149
1150
1150
// FIXME(inherent_associated_types, #106719): Support self types other than ADTs.
1151
1151
if let Some ( ( ty, did) ) = self . probe_inherent_assoc_ty (
1152
- assoc_ident,
1153
1152
assoc_segment,
1154
1153
adt_def. did ( ) ,
1155
1154
qself_ty,
@@ -1354,7 +1353,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1354
1353
1355
1354
fn probe_inherent_assoc_ty (
1356
1355
& self ,
1357
- name : Ident ,
1358
1356
segment : & hir:: PathSegment < ' tcx > ,
1359
1357
adt_did : DefId ,
1360
1358
self_ty : Ty < ' tcx > ,
@@ -1373,6 +1371,60 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1373
1371
return Ok ( None ) ;
1374
1372
}
1375
1373
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 ;
1376
1428
let candidates: Vec < _ > = tcx
1377
1429
. inherent_impls ( adt_did)
1378
1430
. iter ( )
@@ -1416,8 +1468,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1416
1468
& mut universes,
1417
1469
self_ty,
1418
1470
|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 ,
1421
1473
)
1422
1474
} ,
1423
1475
) ?;
@@ -1434,20 +1486,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1434
1486
. chain ( args. into_iter ( ) . skip ( parent_args. len ( ) ) ) ,
1435
1487
) ;
1436
1488
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) ) )
1441
1490
}
1442
1491
1443
- fn select_inherent_assoc_type_candidates (
1492
+ fn select_inherent_assoc_candidates (
1444
1493
& self ,
1445
1494
infcx : & InferCtxt < ' tcx > ,
1446
1495
name : Ident ,
1447
1496
span : Span ,
1448
1497
self_ty : Ty < ' tcx > ,
1449
1498
param_env : ParamEnv < ' tcx > ,
1450
1499
candidates : Vec < ( DefId , ( DefId , DefId ) ) > ,
1500
+ kind : ty:: AssocKind ,
1451
1501
) -> Result < ( DefId , ( DefId , DefId ) ) , ErrorGuaranteed > {
1452
1502
let tcx = self . tcx ( ) ;
1453
1503
let mut fulfillment_errors = Vec :: new ( ) ;
@@ -1492,17 +1542,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1492
1542
. collect ( ) ;
1493
1543
1494
1544
match & applicable_candidates[ ..] {
1495
- & [ ] => Err ( self . complain_about_inherent_assoc_ty_not_found (
1545
+ & [ ] => Err ( self . complain_about_inherent_assoc_not_found (
1496
1546
name,
1497
1547
self_ty,
1498
1548
candidates,
1499
1549
fulfillment_errors,
1500
1550
span,
1551
+ kind,
1501
1552
) ) ,
1502
1553
1503
1554
& [ applicable_candidate] => Ok ( applicable_candidate) ,
1504
1555
1505
- & [ _, ..] => Err ( self . complain_about_ambiguous_inherent_assoc_ty (
1556
+ & [ _, ..] => Err ( self . complain_about_ambiguous_inherent_assoc (
1506
1557
name,
1507
1558
applicable_candidates. into_iter ( ) . map ( |( _, ( candidate, _) ) | candidate) . collect ( ) ,
1508
1559
span,
@@ -2196,6 +2247,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2196
2247
debug ! ( ?qself, ?segment) ;
2197
2248
let ty = self . lower_ty ( qself) ;
2198
2249
self . lower_const_assoc_path ( hir_id, const_arg. span ( ) , ty, qself, segment)
2250
+ . unwrap_or_else ( |guar| Const :: new_error ( tcx, guar) )
2199
2251
}
2200
2252
hir:: ConstArgKind :: Path ( qpath @ hir:: QPath :: LangItem ( ..) ) => {
2201
2253
ty:: Const :: new_error_with_message (
@@ -2317,7 +2369,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2317
2369
qself_ty : Ty < ' tcx > ,
2318
2370
qself : & ' tcx hir:: Ty < ' tcx > ,
2319
2371
assoc_segment : & ' tcx hir:: PathSegment < ' tcx > ,
2320
- ) -> Const < ' tcx > {
2372
+ ) -> Result < Const < ' tcx > , ErrorGuaranteed > {
2321
2373
debug ! ( %qself_ty, ?assoc_segment. ident) ;
2322
2374
let tcx = self . tcx ( ) ;
2323
2375
@@ -2338,42 +2390,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2338
2390
GenericsArgsErrExtend :: EnumVariant { qself, assoc_segment, adt_def } ,
2339
2391
) ;
2340
2392
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) ) ;
2342
2394
}
2343
2395
}
2344
2396
2345
2397
// 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) ;
2377
2406
}
2378
2407
}
2379
2408
@@ -2422,21 +2451,21 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2422
2451
} ;
2423
2452
let bound = match bound_result {
2424
2453
Ok ( b) => b,
2425
- Err ( reported) => return Const :: new_error ( tcx , reported) ,
2454
+ Err ( reported) => return Err ( reported) ,
2426
2455
} ;
2427
2456
2428
2457
let trait_did = bound. def_id ( ) ;
2429
2458
let assoc_const = self
2430
2459
. probe_assoc_item ( assoc_ident, ty:: AssocKind :: Const , hir_ref_id, span, trait_did)
2431
2460
. expect ( "failed to find associated const" ) ;
2432
2461
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) )
2434
2463
} else {
2435
2464
let mut err = tcx
2436
2465
. dcx ( )
2437
2466
. struct_span_err ( span, "use of trait associated const without `#[type_const]`" ) ;
2438
2467
err. note ( "the declaration in the trait must be marked with `#[type_const]`" ) ;
2439
- Const :: new_error ( tcx , err. emit ( ) )
2468
+ Err ( err. emit ( ) )
2440
2469
}
2441
2470
}
2442
2471
0 commit comments