@@ -45,6 +45,7 @@ use tracing::{debug, instrument};
45
45
46
46
use crate :: check:: intrinsic:: intrinsic_operation_unsafety;
47
47
use crate :: errors;
48
+ use crate :: hir_ty_lowering:: errors:: assoc_kind_str;
48
49
use crate :: hir_ty_lowering:: { FeedConstTy , HirTyLowerer , RegionInferReason } ;
49
50
50
51
pub ( crate ) mod dump;
@@ -450,84 +451,15 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
450
451
item_segment : & hir:: PathSegment < ' tcx > ,
451
452
poly_trait_ref : ty:: PolyTraitRef < ' tcx > ,
452
453
) -> Ty < ' tcx > {
453
- if let Some ( trait_ref) = poly_trait_ref. no_bound_vars ( ) {
454
- let item_args = self . lowerer ( ) . lower_generic_args_of_assoc_item (
455
- span,
456
- item_def_id,
457
- item_segment,
458
- trait_ref. args ,
459
- ) ;
460
- Ty :: new_projection_from_args ( self . tcx ( ) , item_def_id, item_args)
461
- } else {
462
- // There are no late-bound regions; we can just ignore the binder.
463
- let ( mut mpart_sugg, mut inferred_sugg) = ( None , None ) ;
464
- let mut bound = String :: new ( ) ;
465
-
466
- match self . node ( ) {
467
- hir:: Node :: Field ( _) | hir:: Node :: Ctor ( _) | hir:: Node :: Variant ( _) => {
468
- let item = self
469
- . tcx
470
- . hir ( )
471
- . expect_item ( self . tcx . hir_get_parent_item ( self . hir_id ( ) ) . def_id ) ;
472
- match & item. kind {
473
- hir:: ItemKind :: Enum ( _, generics)
474
- | hir:: ItemKind :: Struct ( _, generics)
475
- | hir:: ItemKind :: Union ( _, generics) => {
476
- let lt_name = get_new_lifetime_name ( self . tcx , poly_trait_ref, generics) ;
477
- let ( lt_sp, sugg) = match generics. params {
478
- [ ] => ( generics. span , format ! ( "<{lt_name}>" ) ) ,
479
- [ bound, ..] => ( bound. span . shrink_to_lo ( ) , format ! ( "{lt_name}, " ) ) ,
480
- } ;
481
- mpart_sugg = Some ( errors:: AssociatedItemTraitUninferredGenericParamsMultipartSuggestion {
482
- fspan : lt_sp,
483
- first : sugg,
484
- sspan : span. with_hi ( item_segment. ident . span . lo ( ) ) ,
485
- second : format ! (
486
- "{}::" ,
487
- // Replace the existing lifetimes with a new named lifetime.
488
- self . tcx. instantiate_bound_regions_uncached(
489
- poly_trait_ref,
490
- |_| {
491
- ty:: Region :: new_early_param( self . tcx, ty:: EarlyParamRegion {
492
- index: 0 ,
493
- name: Symbol :: intern( & lt_name) ,
494
- } )
495
- }
496
- ) ,
497
- ) ,
498
- } ) ;
499
- }
500
- _ => { }
501
- }
502
- }
503
- hir:: Node :: Item ( hir:: Item {
504
- kind :
505
- hir:: ItemKind :: Struct ( ..) | hir:: ItemKind :: Enum ( ..) | hir:: ItemKind :: Union ( ..) ,
506
- ..
507
- } ) => { }
508
- hir:: Node :: Item ( _)
509
- | hir:: Node :: ForeignItem ( _)
510
- | hir:: Node :: TraitItem ( _)
511
- | hir:: Node :: ImplItem ( _) => {
512
- inferred_sugg = Some ( span. with_hi ( item_segment. ident . span . lo ( ) ) ) ;
513
- bound = format ! (
514
- "{}::" ,
515
- // Erase named lt, we want `<A as B<'_>::C`, not `<A as B<'a>::C`.
516
- self . tcx. anonymize_bound_vars( poly_trait_ref) . skip_binder( ) ,
517
- ) ;
518
- }
519
- _ => { }
520
- }
521
- Ty :: new_error (
522
- self . tcx ( ) ,
523
- self . tcx ( ) . dcx ( ) . emit_err ( errors:: AssociatedItemTraitUninferredGenericParams {
524
- span,
525
- inferred_sugg,
526
- bound,
527
- mpart_sugg,
528
- what : "type" ,
529
- } ) ,
530
- )
454
+ match self . lower_assoc_shared (
455
+ span,
456
+ item_def_id,
457
+ item_segment,
458
+ poly_trait_ref,
459
+ ty:: AssocKind :: Type ,
460
+ ) {
461
+ Ok ( ( def_id, args) ) => Ty :: new_projection_from_args ( self . tcx ( ) , def_id, args) ,
462
+ Err ( witness) => Ty :: new_error ( self . tcx ( ) , witness) ,
531
463
}
532
464
}
533
465
@@ -538,15 +470,37 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
538
470
item_segment : & hir:: PathSegment < ' tcx > ,
539
471
poly_trait_ref : ty:: PolyTraitRef < ' tcx > ,
540
472
) -> Const < ' tcx > {
473
+ match self . lower_assoc_shared (
474
+ span,
475
+ item_def_id,
476
+ item_segment,
477
+ poly_trait_ref,
478
+ ty:: AssocKind :: Const ,
479
+ ) {
480
+ Ok ( ( def_id, args) ) => {
481
+ let uv = ty:: UnevaluatedConst :: new ( def_id, args) ;
482
+ Const :: new_unevaluated ( self . tcx ( ) , uv)
483
+ }
484
+ Err ( witness) => Const :: new_error ( self . tcx ( ) , witness) ,
485
+ }
486
+ }
487
+
488
+ fn lower_assoc_shared (
489
+ & self ,
490
+ span : Span ,
491
+ item_def_id : DefId ,
492
+ item_segment : & rustc_hir:: PathSegment < ' tcx > ,
493
+ poly_trait_ref : ty:: PolyTraitRef < ' tcx > ,
494
+ kind : ty:: AssocKind ,
495
+ ) -> Result < ( DefId , ty:: GenericArgsRef < ' tcx > ) , ErrorGuaranteed > {
541
496
if let Some ( trait_ref) = poly_trait_ref. no_bound_vars ( ) {
542
497
let item_args = self . lowerer ( ) . lower_generic_args_of_assoc_item (
543
498
span,
544
499
item_def_id,
545
500
item_segment,
546
501
trait_ref. args ,
547
502
) ;
548
- let uv = ty:: UnevaluatedConst :: new ( item_def_id, item_args) ;
549
- Const :: new_unevaluated ( self . tcx ( ) , uv)
503
+ Ok ( ( item_def_id, item_args) )
550
504
} else {
551
505
// There are no late-bound regions; we can just ignore the binder.
552
506
let ( mut mpart_sugg, mut inferred_sugg) = ( None , None ) ;
@@ -607,16 +561,14 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
607
561
}
608
562
_ => { }
609
563
}
610
- Const :: new_error (
611
- self . tcx ( ) ,
612
- self . tcx ( ) . dcx ( ) . emit_err ( errors:: AssociatedItemTraitUninferredGenericParams {
613
- span,
614
- inferred_sugg,
615
- bound,
616
- mpart_sugg,
617
- what : "const" ,
618
- } ) ,
619
- )
564
+
565
+ Err ( self . tcx ( ) . dcx ( ) . emit_err ( errors:: AssociatedItemTraitUninferredGenericParams {
566
+ span,
567
+ inferred_sugg,
568
+ bound,
569
+ mpart_sugg,
570
+ what : assoc_kind_str ( kind) ,
571
+ } ) )
620
572
}
621
573
}
622
574
0 commit comments