@@ -1166,8 +1166,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11661166 } ;
11671167
11681168 let trait_did = bound. def_id ( ) ;
1169- let assoc_ty_did = self . probe_assoc_ty ( assoc_ident, hir_ref_id, span, trait_did) . unwrap ( ) ;
1170- let ty = self . lower_assoc_ty ( span, assoc_ty_did, assoc_segment, bound) ;
1169+ let assoc_ty = self
1170+ . probe_assoc_item ( assoc_ident, ty:: AssocKind :: Type , hir_ref_id, span, trait_did)
1171+ . expect ( "failed to find associated type" ) ;
1172+ let ty = self . lower_assoc_ty ( span, assoc_ty. def_id , assoc_segment, bound) ;
11711173
11721174 if let Some ( variant_def_id) = variant_resolution {
11731175 tcx. node_span_lint ( AMBIGUOUS_ASSOCIATED_ITEMS , hir_ref_id, span, |lint| {
@@ -1183,7 +1185,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11831185 } ;
11841186
11851187 could_refer_to ( DefKind :: Variant , variant_def_id, "" ) ;
1186- could_refer_to ( DefKind :: AssocTy , assoc_ty_did , " also" ) ;
1188+ could_refer_to ( DefKind :: AssocTy , assoc_ty . def_id , " also" ) ;
11871189
11881190 lint. span_suggestion (
11891191 span,
@@ -1193,7 +1195,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11931195 ) ;
11941196 } ) ;
11951197 }
1196- Ok ( ( ty, DefKind :: AssocTy , assoc_ty_did ) )
1198+ Ok ( ( ty, DefKind :: AssocTy , assoc_ty . def_id ) )
11971199 }
11981200
11991201 fn probe_inherent_assoc_ty (
@@ -1220,7 +1222,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12201222 let candidates: Vec < _ > = tcx
12211223 . inherent_impls ( adt_did) ?
12221224 . iter ( )
1223- . filter_map ( |& impl_| Some ( ( impl_, self . probe_assoc_ty_unchecked ( name, block, impl_) ?) ) )
1225+ . filter_map ( |& impl_| {
1226+ let ( item, scope) =
1227+ self . probe_assoc_item_unchecked ( name, ty:: AssocKind :: Type , block, impl_) ?;
1228+ Some ( ( impl_, ( item. def_id , scope) ) )
1229+ } )
12241230 . collect ( ) ;
12251231
12261232 if candidates. is_empty ( ) {
@@ -1264,7 +1270,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12641270 } ,
12651271 ) ?;
12661272
1267- self . check_assoc_ty ( assoc_item, name, def_scope, block, span) ;
1273+ self . check_assoc_item ( assoc_item, name, def_scope, block, span) ;
12681274
12691275 // FIXME(fmease): Currently creating throwaway `parent_args` to please
12701276 // `lower_generic_args_of_assoc_item`. Modify the latter instead (or sth. similar) to
@@ -1351,50 +1357,69 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
13511357 }
13521358 }
13531359
1354- fn probe_assoc_ty ( & self , name : Ident , block : HirId , span : Span , scope : DefId ) -> Option < DefId > {
1355- let ( item, def_scope) = self . probe_assoc_ty_unchecked ( name, block, scope) ?;
1356- self . check_assoc_ty ( item, name, def_scope, block, span) ;
1360+ /// Given name and kind search for the assoc item in the provided scope and check if it's accessible[^1].
1361+ ///
1362+ /// [^1]: I.e., accessible in the provided scope wrt. visibility and stability.
1363+ fn probe_assoc_item (
1364+ & self ,
1365+ ident : Ident ,
1366+ kind : ty:: AssocKind ,
1367+ block : HirId ,
1368+ span : Span ,
1369+ scope : DefId ,
1370+ ) -> Option < ty:: AssocItem > {
1371+ let ( item, scope) = self . probe_assoc_item_unchecked ( ident, kind, block, scope) ?;
1372+ self . check_assoc_item ( item. def_id , ident, scope, block, span) ;
13571373 Some ( item)
13581374 }
13591375
1360- fn probe_assoc_ty_unchecked (
1376+ /// Given name and kind search for the assoc item in the provided scope
1377+ /// *without* checking if it's accessible[^1].
1378+ ///
1379+ /// [^1]: I.e., accessible in the provided scope wrt. visibility and stability.
1380+ fn probe_assoc_item_unchecked (
13611381 & self ,
1362- name : Ident ,
1382+ ident : Ident ,
1383+ kind : ty:: AssocKind ,
13631384 block : HirId ,
13641385 scope : DefId ,
1365- ) -> Option < ( DefId , DefId ) > {
1386+ ) -> Option < ( ty :: AssocItem , /*scope*/ DefId ) > {
13661387 let tcx = self . tcx ( ) ;
1367- let ( ident, def_scope) = tcx. adjust_ident_and_get_scope ( name, scope, block) ;
13681388
1389+ let ( ident, def_scope) = tcx. adjust_ident_and_get_scope ( ident, scope, block) ;
13691390 // We have already adjusted the item name above, so compare with `.normalize_to_macros_2_0()`
13701391 // instead of calling `filter_by_name_and_kind` which would needlessly normalize the
13711392 // `ident` again and again.
1372- let item = tcx. associated_items ( scope ) . in_definition_order ( ) . find ( |i| {
1373- i . kind . namespace ( ) == Namespace :: TypeNS
1374- && i . ident ( tcx ) . normalize_to_macros_2_0 ( ) == ident
1375- } ) ?;
1393+ let item = tcx
1394+ . associated_items ( scope )
1395+ . filter_by_name_unhygienic ( ident . name )
1396+ . find ( |i| i . kind == kind && i . ident ( tcx ) . normalize_to_macros_2_0 ( ) == ident ) ?;
13761397
1377- Some ( ( item. def_id , def_scope) )
1398+ Some ( ( * item, def_scope) )
13781399 }
13791400
1380- fn check_assoc_ty ( & self , item : DefId , name : Ident , def_scope : DefId , block : HirId , span : Span ) {
1401+ /// Check if the given assoc item is accessible in the provided scope wrt. visibility and stability.
1402+ fn check_assoc_item (
1403+ & self ,
1404+ item_def_id : DefId ,
1405+ ident : Ident ,
1406+ scope : DefId ,
1407+ block : HirId ,
1408+ span : Span ,
1409+ ) {
13811410 let tcx = self . tcx ( ) ;
1382- let kind = DefKind :: AssocTy ;
1383-
1384- if !tcx. visibility ( item) . is_accessible_from ( def_scope, tcx) {
1385- let kind = tcx. def_kind_descr ( kind, item) ;
1386- let msg = format ! ( "{kind} `{name}` is private" ) ;
1387- let def_span = tcx. def_span ( item) ;
1388- let reported = tcx
1389- . dcx ( )
1390- . struct_span_err ( span, msg)
1391- . with_code ( E0624 )
1392- . with_span_label ( span, format ! ( "private {kind}" ) )
1393- . with_span_label ( def_span, format ! ( "{kind} defined here" ) )
1394- . emit ( ) ;
1411+
1412+ if !tcx. visibility ( item_def_id) . is_accessible_from ( scope, tcx) {
1413+ let reported = tcx. dcx ( ) . emit_err ( crate :: errors:: AssocItemIsPrivate {
1414+ span,
1415+ kind : tcx. def_descr ( item_def_id) ,
1416+ name : ident,
1417+ defined_here_label : tcx. def_span ( item_def_id) ,
1418+ } ) ;
13951419 self . set_tainted_by_errors ( reported) ;
13961420 }
1397- tcx. check_stability ( item, Some ( block) , span, None ) ;
1421+
1422+ tcx. check_stability ( item_def_id, Some ( block) , span, None ) ;
13981423 }
13991424
14001425 fn probe_traits_that_match_assoc_ty (
0 commit comments