@@ -368,6 +368,8 @@ pub(crate) enum HrefError {
368
368
Private ,
369
369
// Not in external cache, href link should be in same page
370
370
NotInExternalCache ,
371
+ /// Refers to an unnamable item, such as one defined within a function or const block.
372
+ UnnamableItem ,
371
373
}
372
374
373
375
/// This function is to get the external macro path because they are not in the cache used in
@@ -479,6 +481,26 @@ fn generate_item_def_id_path(
479
481
Ok ( ( url_parts, shortty, fqp) )
480
482
}
481
483
484
+ /// Checks if the given defid refers to an item that is unnamable, such as one defined in a const block.
485
+ fn is_unnamable ( tcx : TyCtxt < ' _ > , did : DefId ) -> bool {
486
+ let mut cur_did = did;
487
+ while let Some ( parent) = tcx. opt_parent ( cur_did) {
488
+ match tcx. def_kind ( parent) {
489
+ // items defined in these can be linked to, as long as they are visible
490
+ DefKind :: Mod | DefKind :: ForeignMod => cur_did = parent,
491
+ // items in impls can be linked to,
492
+ // as long as we can link to the item the impl is on.
493
+ // since associated traits are not a thing,
494
+ // it should not be possible to refer to an impl item if
495
+ // the base type is not namable.
496
+ DefKind :: Impl { .. } => return false ,
497
+ // everything else does not have docs generated for it
498
+ _ => return true ,
499
+ }
500
+ }
501
+ return false ;
502
+ }
503
+
482
504
fn to_module_fqp ( shortty : ItemType , fqp : & [ Symbol ] ) -> & [ Symbol ] {
483
505
if shortty == ItemType :: Module { fqp } else { & fqp[ ..fqp. len ( ) - 1 ] }
484
506
}
@@ -552,6 +574,9 @@ pub(crate) fn href_with_root_path(
552
574
}
553
575
_ => original_did,
554
576
} ;
577
+ if is_unnamable ( cx. tcx ( ) , did) {
578
+ return Err ( HrefError :: UnnamableItem ) ;
579
+ }
555
580
let cache = cx. cache ( ) ;
556
581
let relative_to = & cx. current ;
557
582
0 commit comments