@@ -46,6 +46,7 @@ use tracing::{debug, instrument};
4646
4747use crate :: check:: intrinsic:: intrinsic_operation_unsafety;
4848use crate :: errors;
49+ use crate :: hir_ty_lowering:: errors:: assoc_kind_str;
4950use crate :: hir_ty_lowering:: { FeedConstTy , HirTyLowerer , RegionInferReason } ;
5051
5152pub ( crate ) mod dump;
@@ -468,84 +469,15 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
468469 item_segment : & hir:: PathSegment < ' tcx > ,
469470 poly_trait_ref : ty:: PolyTraitRef < ' tcx > ,
470471 ) -> Ty < ' tcx > {
471- if let Some ( trait_ref) = poly_trait_ref. no_bound_vars ( ) {
472- let item_args = self . lowerer ( ) . lower_generic_args_of_assoc_item (
473- span,
474- item_def_id,
475- item_segment,
476- trait_ref. args ,
477- ) ;
478- Ty :: new_projection_from_args ( self . tcx ( ) , item_def_id, item_args)
479- } else {
480- // There are no late-bound regions; we can just ignore the binder.
481- let ( mut mpart_sugg, mut inferred_sugg) = ( None , None ) ;
482- let mut bound = String :: new ( ) ;
483-
484- match self . node ( ) {
485- hir:: Node :: Field ( _) | hir:: Node :: Ctor ( _) | hir:: Node :: Variant ( _) => {
486- let item = self
487- . tcx
488- . hir ( )
489- . expect_item ( self . tcx . hir ( ) . get_parent_item ( self . hir_id ( ) ) . def_id ) ;
490- match & item. kind {
491- hir:: ItemKind :: Enum ( _, generics)
492- | hir:: ItemKind :: Struct ( _, generics)
493- | hir:: ItemKind :: Union ( _, generics) => {
494- let lt_name = get_new_lifetime_name ( self . tcx , poly_trait_ref, generics) ;
495- let ( lt_sp, sugg) = match generics. params {
496- [ ] => ( generics. span , format ! ( "<{lt_name}>" ) ) ,
497- [ bound, ..] => ( bound. span . shrink_to_lo ( ) , format ! ( "{lt_name}, " ) ) ,
498- } ;
499- mpart_sugg = Some ( errors:: AssociatedItemTraitUninferredGenericParamsMultipartSuggestion {
500- fspan : lt_sp,
501- first : sugg,
502- sspan : span. with_hi ( item_segment. ident . span . lo ( ) ) ,
503- second : format ! (
504- "{}::" ,
505- // Replace the existing lifetimes with a new named lifetime.
506- self . tcx. instantiate_bound_regions_uncached(
507- poly_trait_ref,
508- |_| {
509- ty:: Region :: new_early_param( self . tcx, ty:: EarlyParamRegion {
510- index: 0 ,
511- name: Symbol :: intern( & lt_name) ,
512- } )
513- }
514- ) ,
515- ) ,
516- } ) ;
517- }
518- _ => { }
519- }
520- }
521- hir:: Node :: Item ( hir:: Item {
522- kind :
523- hir:: ItemKind :: Struct ( ..) | hir:: ItemKind :: Enum ( ..) | hir:: ItemKind :: Union ( ..) ,
524- ..
525- } ) => { }
526- hir:: Node :: Item ( _)
527- | hir:: Node :: ForeignItem ( _)
528- | hir:: Node :: TraitItem ( _)
529- | hir:: Node :: ImplItem ( _) => {
530- inferred_sugg = Some ( span. with_hi ( item_segment. ident . span . lo ( ) ) ) ;
531- bound = format ! (
532- "{}::" ,
533- // Erase named lt, we want `<A as B<'_>::C`, not `<A as B<'a>::C`.
534- self . tcx. anonymize_bound_vars( poly_trait_ref) . skip_binder( ) ,
535- ) ;
536- }
537- _ => { }
538- }
539- Ty :: new_error (
540- self . tcx ( ) ,
541- self . tcx ( ) . dcx ( ) . emit_err ( errors:: AssociatedItemTraitUninferredGenericParams {
542- span,
543- inferred_sugg,
544- bound,
545- mpart_sugg,
546- what : "type" ,
547- } ) ,
548- )
472+ match self . lower_assoc_shared (
473+ span,
474+ item_def_id,
475+ item_segment,
476+ poly_trait_ref,
477+ ty:: AssocKind :: Type ,
478+ ) {
479+ Ok ( ( def_id, args) ) => Ty :: new_projection_from_args ( self . tcx ( ) , def_id, args) ,
480+ Err ( witness) => Ty :: new_error ( self . tcx ( ) , witness) ,
549481 }
550482 }
551483
@@ -556,15 +488,37 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
556488 item_segment : & hir:: PathSegment < ' tcx > ,
557489 poly_trait_ref : ty:: PolyTraitRef < ' tcx > ,
558490 ) -> Const < ' tcx > {
491+ match self . lower_assoc_shared (
492+ span,
493+ item_def_id,
494+ item_segment,
495+ poly_trait_ref,
496+ ty:: AssocKind :: Const ,
497+ ) {
498+ Ok ( ( def_id, args) ) => {
499+ let uv = ty:: UnevaluatedConst :: new ( def_id, args) ;
500+ Const :: new_unevaluated ( self . tcx ( ) , uv)
501+ }
502+ Err ( witness) => Const :: new_error ( self . tcx ( ) , witness) ,
503+ }
504+ }
505+
506+ fn lower_assoc_shared (
507+ & self ,
508+ span : Span ,
509+ item_def_id : DefId ,
510+ item_segment : & rustc_hir:: PathSegment < ' tcx > ,
511+ poly_trait_ref : ty:: PolyTraitRef < ' tcx > ,
512+ kind : ty:: AssocKind ,
513+ ) -> Result < ( DefId , ty:: GenericArgsRef < ' tcx > ) , ErrorGuaranteed > {
559514 if let Some ( trait_ref) = poly_trait_ref. no_bound_vars ( ) {
560515 let item_args = self . lowerer ( ) . lower_generic_args_of_assoc_item (
561516 span,
562517 item_def_id,
563518 item_segment,
564519 trait_ref. args ,
565520 ) ;
566- let uv = ty:: UnevaluatedConst :: new ( item_def_id, item_args) ;
567- Const :: new_unevaluated ( self . tcx ( ) , uv)
521+ Ok ( ( item_def_id, item_args) )
568522 } else {
569523 // There are no late-bound regions; we can just ignore the binder.
570524 let ( mut mpart_sugg, mut inferred_sugg) = ( None , None ) ;
@@ -625,16 +579,14 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
625579 }
626580 _ => { }
627581 }
628- Const :: new_error (
629- self . tcx ( ) ,
630- self . tcx ( ) . dcx ( ) . emit_err ( errors:: AssociatedItemTraitUninferredGenericParams {
631- span,
632- inferred_sugg,
633- bound,
634- mpart_sugg,
635- what : "const" ,
636- } ) ,
637- )
582+
583+ Err ( self . tcx ( ) . dcx ( ) . emit_err ( errors:: AssociatedItemTraitUninferredGenericParams {
584+ span,
585+ inferred_sugg,
586+ bound,
587+ mpart_sugg,
588+ what : assoc_kind_str ( kind) ,
589+ } ) )
638590 }
639591 }
640592
0 commit comments