@@ -1166,8 +1166,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1166
1166
} ;
1167
1167
1168
1168
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) ;
1171
1173
1172
1174
if let Some ( variant_def_id) = variant_resolution {
1173
1175
tcx. node_span_lint ( AMBIGUOUS_ASSOCIATED_ITEMS , hir_ref_id, span, |lint| {
@@ -1183,7 +1185,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1183
1185
} ;
1184
1186
1185
1187
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" ) ;
1187
1189
1188
1190
lint. span_suggestion (
1189
1191
span,
@@ -1193,7 +1195,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1193
1195
) ;
1194
1196
} ) ;
1195
1197
}
1196
- Ok ( ( ty, DefKind :: AssocTy , assoc_ty_did ) )
1198
+ Ok ( ( ty, DefKind :: AssocTy , assoc_ty . def_id ) )
1197
1199
}
1198
1200
1199
1201
fn probe_inherent_assoc_ty (
@@ -1220,7 +1222,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1220
1222
let candidates: Vec < _ > = tcx
1221
1223
. inherent_impls ( adt_did) ?
1222
1224
. 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
+ } )
1224
1230
. collect ( ) ;
1225
1231
1226
1232
if candidates. is_empty ( ) {
@@ -1264,7 +1270,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1264
1270
} ,
1265
1271
) ?;
1266
1272
1267
- self . check_assoc_ty ( assoc_item, name, def_scope, block, span) ;
1273
+ self . check_assoc_item ( assoc_item, name, def_scope, block, span) ;
1268
1274
1269
1275
// FIXME(fmease): Currently creating throwaway `parent_args` to please
1270
1276
// `lower_generic_args_of_assoc_item`. Modify the latter instead (or sth. similar) to
@@ -1351,50 +1357,69 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1351
1357
}
1352
1358
}
1353
1359
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) ;
1357
1373
Some ( item)
1358
1374
}
1359
1375
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 (
1361
1381
& self ,
1362
- name : Ident ,
1382
+ ident : Ident ,
1383
+ kind : ty:: AssocKind ,
1363
1384
block : HirId ,
1364
1385
scope : DefId ,
1365
- ) -> Option < ( DefId , DefId ) > {
1386
+ ) -> Option < ( ty :: AssocItem , /*scope*/ DefId ) > {
1366
1387
let tcx = self . tcx ( ) ;
1367
- let ( ident, def_scope) = tcx. adjust_ident_and_get_scope ( name, scope, block) ;
1368
1388
1389
+ let ( ident, def_scope) = tcx. adjust_ident_and_get_scope ( ident, scope, block) ;
1369
1390
// We have already adjusted the item name above, so compare with `.normalize_to_macros_2_0()`
1370
1391
// instead of calling `filter_by_name_and_kind` which would needlessly normalize the
1371
1392
// `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 ) ?;
1376
1397
1377
- Some ( ( item. def_id , def_scope) )
1398
+ Some ( ( * item, def_scope) )
1378
1399
}
1379
1400
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
+ ) {
1381
1410
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
+ } ) ;
1395
1419
self . set_tainted_by_errors ( reported) ;
1396
1420
}
1397
- tcx. check_stability ( item, Some ( block) , span, None ) ;
1421
+
1422
+ tcx. check_stability ( item_def_id, Some ( block) , span, None ) ;
1398
1423
}
1399
1424
1400
1425
fn probe_traits_that_match_assoc_ty (
0 commit comments