@@ -1151,8 +1151,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1151
1151
} ;
1152
1152
1153
1153
let trait_did = bound. def_id ( ) ;
1154
- let assoc_ty_did = self . probe_assoc_ty ( assoc_ident, hir_ref_id, span, trait_did) . unwrap ( ) ;
1155
- let ty = self . lower_assoc_ty ( span, assoc_ty_did, assoc_segment, bound) ;
1154
+ let assoc_ty = self
1155
+ . probe_assoc_item ( assoc_ident, ty:: AssocKind :: Type , hir_ref_id, span, trait_did)
1156
+ . expect ( "failed to find associated type" ) ;
1157
+ let ty = self . lower_assoc_ty ( span, assoc_ty. def_id , assoc_segment, bound) ;
1156
1158
1157
1159
if let Some ( variant_def_id) = variant_resolution {
1158
1160
tcx. node_span_lint ( AMBIGUOUS_ASSOCIATED_ITEMS , hir_ref_id, span, |lint| {
@@ -1168,7 +1170,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1168
1170
} ;
1169
1171
1170
1172
could_refer_to ( DefKind :: Variant , variant_def_id, "" ) ;
1171
- could_refer_to ( DefKind :: AssocTy , assoc_ty_did , " also" ) ;
1173
+ could_refer_to ( DefKind :: AssocTy , assoc_ty . def_id , " also" ) ;
1172
1174
1173
1175
lint. span_suggestion (
1174
1176
span,
@@ -1178,7 +1180,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1178
1180
) ;
1179
1181
} ) ;
1180
1182
}
1181
- Ok ( ( ty, DefKind :: AssocTy , assoc_ty_did ) )
1183
+ Ok ( ( ty, DefKind :: AssocTy , assoc_ty . def_id ) )
1182
1184
}
1183
1185
1184
1186
fn probe_inherent_assoc_ty (
@@ -1205,7 +1207,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1205
1207
let candidates: Vec < _ > = tcx
1206
1208
. inherent_impls ( adt_did) ?
1207
1209
. iter ( )
1208
- . filter_map ( |& impl_| Some ( ( impl_, self . probe_assoc_ty_unchecked ( name, block, impl_) ?) ) )
1210
+ . filter_map ( |& impl_| {
1211
+ let ( item, scope) =
1212
+ self . probe_assoc_item_unchecked ( name, ty:: AssocKind :: Type , block, impl_) ?;
1213
+ Some ( ( impl_, ( item. def_id , scope) ) )
1214
+ } )
1209
1215
. collect ( ) ;
1210
1216
1211
1217
if candidates. is_empty ( ) {
@@ -1249,7 +1255,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1249
1255
} ,
1250
1256
) ?;
1251
1257
1252
- self . check_assoc_ty ( assoc_item, name, def_scope, block, span) ;
1258
+ self . check_assoc_item ( assoc_item, name, def_scope, block, span) ;
1253
1259
1254
1260
// FIXME(fmease): Currently creating throwaway `parent_args` to please
1255
1261
// `lower_generic_args_of_assoc_item`. Modify the latter instead (or sth. similar) to
@@ -1336,50 +1342,69 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
1336
1342
}
1337
1343
}
1338
1344
1339
- fn probe_assoc_ty ( & self , name : Ident , block : HirId , span : Span , scope : DefId ) -> Option < DefId > {
1340
- let ( item, def_scope) = self . probe_assoc_ty_unchecked ( name, block, scope) ?;
1341
- self . check_assoc_ty ( item, name, def_scope, block, span) ;
1345
+ /// Given name and kind search for the assoc item in the provided scope and check if it's accessible[^1].
1346
+ ///
1347
+ /// [^1]: I.e., accessible in the provided scope wrt. visibility and stability.
1348
+ fn probe_assoc_item (
1349
+ & self ,
1350
+ ident : Ident ,
1351
+ kind : ty:: AssocKind ,
1352
+ block : HirId ,
1353
+ span : Span ,
1354
+ scope : DefId ,
1355
+ ) -> Option < ty:: AssocItem > {
1356
+ let ( item, scope) = self . probe_assoc_item_unchecked ( ident, kind, block, scope) ?;
1357
+ self . check_assoc_item ( item. def_id , ident, scope, block, span) ;
1342
1358
Some ( item)
1343
1359
}
1344
1360
1345
- fn probe_assoc_ty_unchecked (
1361
+ /// Given name and kind search for the assoc item in the provided scope
1362
+ /// *without* checking if it's accessible[^1].
1363
+ ///
1364
+ /// [^1]: I.e., accessible in the provided scope wrt. visibility and stability.
1365
+ fn probe_assoc_item_unchecked (
1346
1366
& self ,
1347
- name : Ident ,
1367
+ ident : Ident ,
1368
+ kind : ty:: AssocKind ,
1348
1369
block : HirId ,
1349
1370
scope : DefId ,
1350
- ) -> Option < ( DefId , DefId ) > {
1371
+ ) -> Option < ( ty :: AssocItem , /*scope*/ DefId ) > {
1351
1372
let tcx = self . tcx ( ) ;
1352
- let ( ident, def_scope) = tcx. adjust_ident_and_get_scope ( name, scope, block) ;
1353
1373
1374
+ let ( ident, def_scope) = tcx. adjust_ident_and_get_scope ( ident, scope, block) ;
1354
1375
// We have already adjusted the item name above, so compare with `.normalize_to_macros_2_0()`
1355
1376
// instead of calling `filter_by_name_and_kind` which would needlessly normalize the
1356
1377
// `ident` again and again.
1357
- let item = tcx. associated_items ( scope ) . in_definition_order ( ) . find ( |i| {
1358
- i . kind . namespace ( ) == Namespace :: TypeNS
1359
- && i . ident ( tcx ) . normalize_to_macros_2_0 ( ) == ident
1360
- } ) ?;
1378
+ let item = tcx
1379
+ . associated_items ( scope )
1380
+ . filter_by_name_unhygienic ( ident . name )
1381
+ . find ( |i| i . kind == kind && i . ident ( tcx ) . normalize_to_macros_2_0 ( ) == ident ) ?;
1361
1382
1362
- Some ( ( item. def_id , def_scope) )
1383
+ Some ( ( * item, def_scope) )
1363
1384
}
1364
1385
1365
- fn check_assoc_ty ( & self , item : DefId , name : Ident , def_scope : DefId , block : HirId , span : Span ) {
1386
+ /// Check if the given assoc item is accessible in the provided scope wrt. visibility and stability.
1387
+ fn check_assoc_item (
1388
+ & self ,
1389
+ item_def_id : DefId ,
1390
+ ident : Ident ,
1391
+ scope : DefId ,
1392
+ block : HirId ,
1393
+ span : Span ,
1394
+ ) {
1366
1395
let tcx = self . tcx ( ) ;
1367
- let kind = DefKind :: AssocTy ;
1368
-
1369
- if !tcx. visibility ( item) . is_accessible_from ( def_scope, tcx) {
1370
- let kind = tcx. def_kind_descr ( kind, item) ;
1371
- let msg = format ! ( "{kind} `{name}` is private" ) ;
1372
- let def_span = tcx. def_span ( item) ;
1373
- let reported = tcx
1374
- . dcx ( )
1375
- . struct_span_err ( span, msg)
1376
- . with_code ( E0624 )
1377
- . with_span_label ( span, format ! ( "private {kind}" ) )
1378
- . with_span_label ( def_span, format ! ( "{kind} defined here" ) )
1379
- . emit ( ) ;
1396
+
1397
+ if !tcx. visibility ( item_def_id) . is_accessible_from ( scope, tcx) {
1398
+ let reported = tcx. dcx ( ) . emit_err ( crate :: errors:: AssocItemIsPrivate {
1399
+ span,
1400
+ kind : tcx. def_descr ( item_def_id) ,
1401
+ name : ident,
1402
+ defined_here_label : tcx. def_span ( item_def_id) ,
1403
+ } ) ;
1380
1404
self . set_tainted_by_errors ( reported) ;
1381
1405
}
1382
- tcx. check_stability ( item, Some ( block) , span, None ) ;
1406
+
1407
+ tcx. check_stability ( item_def_id, Some ( block) , span, None ) ;
1383
1408
}
1384
1409
1385
1410
fn probe_traits_that_match_assoc_ty (
0 commit comments