@@ -565,7 +565,7 @@ fn generate_macro_def_id_path(
565
565
def_id : DefId ,
566
566
cx : & Context < ' _ > ,
567
567
root_path : Option < & str > ,
568
- ) -> ( String , ItemType , Vec < Symbol > ) {
568
+ ) -> Result < ( String , ItemType , Vec < Symbol > ) , HrefError > {
569
569
let tcx = cx. shared . tcx ;
570
570
let crate_name = tcx. crate_name ( def_id. krate ) . to_string ( ) ;
571
571
let cache = cx. cache ( ) ;
@@ -583,9 +583,15 @@ fn generate_macro_def_id_path(
583
583
} )
584
584
. collect ( ) ;
585
585
let relative = fqp. iter ( ) . map ( |elem| elem. to_string ( ) ) ;
586
+ let cstore = CStore :: from_tcx ( tcx) ;
587
+ // We need this to prevent a `panic` when this function is used from intra doc links...
588
+ if !cstore. has_crate_data ( def_id. krate ) {
589
+ debug ! ( "No data for crate {}" , crate_name) ;
590
+ return Err ( HrefError :: NotInExternalCache ) ;
591
+ }
586
592
// Check to see if it is a macro 2.0 or built-in macro.
587
593
// More information in <https://rust-lang.github.io/rfcs/1584-macros.html>.
588
- let is_macro_2 = match CStore :: from_tcx ( tcx ) . load_macro_untracked ( def_id, tcx. sess ) {
594
+ let is_macro_2 = match cstore . load_macro_untracked ( def_id, tcx. sess ) {
589
595
LoadedMacro :: MacroDef ( def, _) => {
590
596
// If `ast_def.macro_rules` is `true`, then it's not a macro 2.0.
591
597
matches ! ( & def. kind, ast:: ItemKind :: MacroDef ( ast_def) if !ast_def. macro_rules)
@@ -601,7 +607,8 @@ fn generate_macro_def_id_path(
601
607
if path. len ( ) < 2 {
602
608
// The minimum we can have is the crate name followed by the macro name. If shorter, then
603
609
// it means that that `relative` was empty, which is an error.
604
- panic ! ( "macro path cannot be empty!" ) ;
610
+ debug ! ( "macro path cannot be empty!" ) ;
611
+ return Err ( HrefError :: NotInExternalCache ) ;
605
612
}
606
613
607
614
if let Some ( last) = path. last_mut ( ) {
@@ -618,10 +625,11 @@ fn generate_macro_def_id_path(
618
625
format ! ( "{}{}/{}" , root_path. unwrap_or( "" ) , crate_name, path. join( "/" ) )
619
626
}
620
627
ExternalLocation :: Unknown => {
621
- panic ! ( "crate {} not in cache when linkifying macros" , crate_name)
628
+ debug ! ( "crate {} not in cache when linkifying macros" , crate_name) ;
629
+ return Err ( HrefError :: NotInExternalCache ) ;
622
630
}
623
631
} ;
624
- ( url, ItemType :: Macro , fqp)
632
+ Ok ( ( url, ItemType :: Macro , fqp) )
625
633
}
626
634
627
635
pub ( crate ) fn href_with_root_path (
@@ -680,7 +688,7 @@ pub(crate) fn href_with_root_path(
680
688
} ,
681
689
)
682
690
} else if matches ! ( def_kind, DefKind :: Macro ( _) ) {
683
- return Ok ( generate_macro_def_id_path ( did, cx, root_path) ) ;
691
+ return generate_macro_def_id_path ( did, cx, root_path) ;
684
692
} else {
685
693
return Err ( HrefError :: NotInExternalCache ) ;
686
694
}
0 commit comments