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