@@ -1150,7 +1150,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1150
1150
1151
1151
// FIXME(inherent_associated_types, #106719): Support self types other than ADTs.
1152
1152
if let Some ( ( ty, did) ) = self . probe_inherent_assoc_ty (
1153
- assoc_ident,
1154
1153
assoc_segment,
1155
1154
adt_def. did ( ) ,
1156
1155
qself_ty,
@@ -1355,7 +1354,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1355
1354
1356
1355
fn probe_inherent_assoc_ty (
1357
1356
& self ,
1358
- name : Ident ,
1359
1357
segment : & hir:: PathSegment < ' tcx > ,
1360
1358
adt_did : DefId ,
1361
1359
self_ty : Ty < ' tcx > ,
@@ -1374,6 +1372,60 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1374
1372
return Ok ( None ) ;
1375
1373
}
1376
1374
1375
+ let Some ( ( def_id, args) ) = self . probe_inherent_assoc_shared (
1376
+ segment,
1377
+ adt_did,
1378
+ self_ty,
1379
+ block,
1380
+ span,
1381
+ ty:: AssocKind :: Type ,
1382
+ ) ?
1383
+ else {
1384
+ return Ok ( None ) ;
1385
+ } ;
1386
+
1387
+ let ty = Ty :: new_alias ( tcx, ty:: Inherent , ty:: AliasTy :: new_from_args ( tcx, def_id, args) ) ;
1388
+ Ok ( Some ( ( ty, def_id) ) )
1389
+ }
1390
+
1391
+ fn probe_inherent_assoc_const (
1392
+ & self ,
1393
+ segment : & hir:: PathSegment < ' tcx > ,
1394
+ adt_did : DefId ,
1395
+ self_ty : Ty < ' tcx > ,
1396
+ block : HirId ,
1397
+ span : Span ,
1398
+ ) -> Result < Option < ( Const < ' tcx > , DefId ) > , ErrorGuaranteed > {
1399
+ let tcx = self . tcx ( ) ;
1400
+
1401
+ let Some ( ( def_id, args) ) = self . probe_inherent_assoc_shared (
1402
+ segment,
1403
+ adt_did,
1404
+ self_ty,
1405
+ block,
1406
+ span,
1407
+ ty:: AssocKind :: Const ,
1408
+ ) ?
1409
+ else {
1410
+ return Ok ( None ) ;
1411
+ } ;
1412
+
1413
+ let ct = Const :: new_unevaluated ( tcx, ty:: UnevaluatedConst :: new ( def_id, args) ) ;
1414
+ Ok ( Some ( ( ct, def_id) ) )
1415
+ }
1416
+
1417
+ fn probe_inherent_assoc_shared (
1418
+ & self ,
1419
+ segment : & hir:: PathSegment < ' tcx > ,
1420
+ adt_did : DefId ,
1421
+ self_ty : Ty < ' tcx > ,
1422
+ block : HirId ,
1423
+ span : Span ,
1424
+ kind : ty:: AssocKind ,
1425
+ ) -> Result < Option < ( DefId , GenericArgsRef < ' tcx > ) > , ErrorGuaranteed > {
1426
+ let tcx = self . tcx ( ) ;
1427
+
1428
+ let name = segment. ident ;
1377
1429
let candidates: Vec < _ > = tcx
1378
1430
. inherent_impls ( adt_did)
1379
1431
. iter ( )
@@ -1417,8 +1469,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1417
1469
& mut universes,
1418
1470
self_ty,
1419
1471
|self_ty| {
1420
- self . select_inherent_assoc_type_candidates (
1421
- infcx, name, span, self_ty, param_env, candidates,
1472
+ self . select_inherent_assoc_candidates (
1473
+ infcx, name, span, self_ty, param_env, candidates, kind ,
1422
1474
)
1423
1475
} ,
1424
1476
) ?;
@@ -1435,20 +1487,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1435
1487
. chain ( args. into_iter ( ) . skip ( parent_args. len ( ) ) ) ,
1436
1488
) ;
1437
1489
1438
- let ty =
1439
- Ty :: new_alias ( tcx, ty:: Inherent , ty:: AliasTy :: new_from_args ( tcx, assoc_item, args) ) ;
1440
-
1441
- Ok ( Some ( ( ty, assoc_item) ) )
1490
+ Ok ( Some ( ( assoc_item, args) ) )
1442
1491
}
1443
1492
1444
- fn select_inherent_assoc_type_candidates (
1493
+ fn select_inherent_assoc_candidates (
1445
1494
& self ,
1446
1495
infcx : & InferCtxt < ' tcx > ,
1447
1496
name : Ident ,
1448
1497
span : Span ,
1449
1498
self_ty : Ty < ' tcx > ,
1450
1499
param_env : ParamEnv < ' tcx > ,
1451
1500
candidates : Vec < ( DefId , ( DefId , DefId ) ) > ,
1501
+ kind : ty:: AssocKind ,
1452
1502
) -> Result < ( DefId , ( DefId , DefId ) ) , ErrorGuaranteed > {
1453
1503
let tcx = self . tcx ( ) ;
1454
1504
let mut fulfillment_errors = Vec :: new ( ) ;
@@ -1493,17 +1543,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1493
1543
. collect ( ) ;
1494
1544
1495
1545
match & applicable_candidates[ ..] {
1496
- & [ ] => Err ( self . complain_about_inherent_assoc_ty_not_found (
1546
+ & [ ] => Err ( self . complain_about_inherent_assoc_not_found (
1497
1547
name,
1498
1548
self_ty,
1499
1549
candidates,
1500
1550
fulfillment_errors,
1501
1551
span,
1552
+ kind,
1502
1553
) ) ,
1503
1554
1504
1555
& [ applicable_candidate] => Ok ( applicable_candidate) ,
1505
1556
1506
- & [ _, ..] => Err ( self . complain_about_ambiguous_inherent_assoc_ty (
1557
+ & [ _, ..] => Err ( self . complain_about_ambiguous_inherent_assoc (
1507
1558
name,
1508
1559
applicable_candidates. into_iter ( ) . map ( |( _, ( candidate, _) ) | candidate) . collect ( ) ,
1509
1560
span,
@@ -2197,6 +2248,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2197
2248
debug ! ( ?qself, ?segment) ;
2198
2249
let ty = self . lower_ty ( qself) ;
2199
2250
self . lower_const_assoc_path ( hir_id, const_arg. span ( ) , ty, qself, segment)
2251
+ . unwrap_or_else ( |guar| Const :: new_error ( tcx, guar) )
2200
2252
}
2201
2253
hir:: ConstArgKind :: Path ( qpath @ hir:: QPath :: LangItem ( ..) ) => {
2202
2254
ty:: Const :: new_error_with_message (
@@ -2318,7 +2370,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2318
2370
qself_ty : Ty < ' tcx > ,
2319
2371
qself : & ' tcx hir:: Ty < ' tcx > ,
2320
2372
assoc_segment : & ' tcx hir:: PathSegment < ' tcx > ,
2321
- ) -> Const < ' tcx > {
2373
+ ) -> Result < Const < ' tcx > , ErrorGuaranteed > {
2322
2374
debug ! ( %qself_ty, ?assoc_segment. ident) ;
2323
2375
let tcx = self . tcx ( ) ;
2324
2376
@@ -2339,42 +2391,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2339
2391
GenericsArgsErrExtend :: EnumVariant { qself, assoc_segment, adt_def } ,
2340
2392
) ;
2341
2393
let uv = ty:: UnevaluatedConst :: new ( variant_def. def_id , ty:: List :: empty ( ) ) ;
2342
- return Const :: new_unevaluated ( tcx, uv) ;
2394
+ return Ok ( Const :: new_unevaluated ( tcx, uv) ) ;
2343
2395
}
2344
2396
}
2345
2397
2346
2398
// FIXME(mgca): Support self types other than ADTs.
2347
- let candidates = tcx
2348
- . inherent_impls ( adt_def. did ( ) )
2349
- . iter ( )
2350
- . filter_map ( |& impl_| {
2351
- self . probe_assoc_item (
2352
- assoc_ident,
2353
- ty:: AssocKind :: Const ,
2354
- hir_ref_id,
2355
- span,
2356
- impl_,
2357
- )
2358
- . map ( |assoc| ( impl_, assoc) )
2359
- } )
2360
- . collect :: < Vec < _ > > ( ) ;
2361
- match & candidates[ ..] {
2362
- [ ] => { }
2363
- & [ ( impl_, assoc) ] => {
2364
- // FIXME(mgca): adapted from temporary inherent assoc ty code that may be incorrect
2365
- let parent_args = ty:: GenericArgs :: identity_for_item ( tcx, impl_) ;
2366
- let args = self . lower_generic_args_of_assoc_item (
2367
- span,
2368
- assoc. def_id ,
2369
- assoc_segment,
2370
- parent_args,
2371
- ) ;
2372
- let uv = ty:: UnevaluatedConst :: new ( assoc. def_id , args) ;
2373
- return Const :: new_unevaluated ( tcx, uv) ;
2374
- }
2375
- [ ..] => {
2376
- return Const :: new_error_with_message ( tcx, span, "ambiguous assoc const path" ) ;
2377
- }
2399
+ if let Some ( ( ct, _) ) = self . probe_inherent_assoc_const (
2400
+ assoc_segment,
2401
+ adt_def. did ( ) ,
2402
+ qself_ty,
2403
+ hir_ref_id,
2404
+ span,
2405
+ ) ? {
2406
+ return Ok ( ct) ;
2378
2407
}
2379
2408
}
2380
2409
@@ -2423,21 +2452,21 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2423
2452
} ;
2424
2453
let bound = match bound_result {
2425
2454
Ok ( b) => b,
2426
- Err ( reported) => return Const :: new_error ( tcx , reported) ,
2455
+ Err ( reported) => return Err ( reported) ,
2427
2456
} ;
2428
2457
2429
2458
let trait_did = bound. def_id ( ) ;
2430
2459
let assoc_const = self
2431
2460
. probe_assoc_item ( assoc_ident, ty:: AssocKind :: Const , hir_ref_id, span, trait_did)
2432
2461
. expect ( "failed to find associated const" ) ;
2433
2462
if assoc_const. has_type_const_attr ( tcx) {
2434
- self . lower_assoc_const ( span, assoc_const. def_id , assoc_segment, bound)
2463
+ Ok ( self . lower_assoc_const ( span, assoc_const. def_id , assoc_segment, bound) )
2435
2464
} else {
2436
2465
let mut err = tcx
2437
2466
. dcx ( )
2438
2467
. struct_span_err ( span, "use of trait associated const without `#[type_const]`" ) ;
2439
2468
err. note ( "the declaration in the trait must be marked with `#[type_const]`" ) ;
2440
- Const :: new_error ( tcx , err. emit ( ) )
2469
+ Err ( err. emit ( ) )
2441
2470
}
2442
2471
}
2443
2472
0 commit comments