@@ -32,6 +32,7 @@ use crate::clean::{
32
32
self , types:: ExternalLocation , utils:: find_nearest_parent_module, ExternalCrate , ItemId ,
33
33
PrimitiveType ,
34
34
} ;
35
+ use crate :: formats:: cache:: Cache ;
35
36
use crate :: formats:: item_type:: ItemType ;
36
37
use crate :: html:: escape:: Escape ;
37
38
use crate :: html:: render:: Context ;
@@ -581,7 +582,7 @@ fn generate_macro_def_id_path(
581
582
cx : & Context < ' _ > ,
582
583
root_path : Option < & str > ,
583
584
) -> Result < ( String , ItemType , Vec < Symbol > ) , HrefError > {
584
- let tcx = cx. shared . tcx ;
585
+ let tcx = cx. tcx ( ) ;
585
586
let crate_name = tcx. crate_name ( def_id. krate ) ;
586
587
let cache = cx. cache ( ) ;
587
588
@@ -651,44 +652,151 @@ fn generate_macro_def_id_path(
651
652
Ok ( ( url, ItemType :: Macro , fqp) )
652
653
}
653
654
655
+ fn generate_item_def_id_path (
656
+ def_id : DefId ,
657
+ original_def_id : DefId ,
658
+ cx : & Context < ' _ > ,
659
+ root_path : Option < & str > ,
660
+ ) -> Result < ( String , ItemType , Vec < Symbol > ) , HrefError > {
661
+ use crate :: rustc_trait_selection:: infer:: TyCtxtInferExt ;
662
+ use crate :: rustc_trait_selection:: traits:: query:: normalize:: QueryNormalizeExt ;
663
+ use rustc_middle:: traits:: ObligationCause ;
664
+
665
+ let tcx = cx. tcx ( ) ;
666
+ let crate_name = tcx. crate_name ( def_id. krate ) ;
667
+
668
+ let infcx = tcx. infer_ctxt ( ) . build ( ) ;
669
+ let def_id = infcx
670
+ . at ( & ObligationCause :: dummy ( ) , tcx. param_env ( def_id) )
671
+ . query_normalize ( ty:: Binder :: dummy ( tcx. type_of ( def_id) . instantiate_identity ( ) ) )
672
+ . map ( |resolved| infcx. resolve_vars_if_possible ( resolved. value ) )
673
+ . ok ( )
674
+ . and_then ( |normalized| normalized. skip_binder ( ) . ty_adt_def ( ) )
675
+ . map ( |adt| adt. did ( ) )
676
+ . unwrap_or ( def_id) ;
677
+
678
+ let relative: Vec < Symbol > = tcx
679
+ . def_path ( def_id)
680
+ . data
681
+ . into_iter ( )
682
+ . filter_map ( |elem| {
683
+ // extern blocks (and a few others things) have an empty name.
684
+ match elem. data . get_opt_name ( ) {
685
+ Some ( s) if !s. is_empty ( ) => Some ( s) ,
686
+ _ => None ,
687
+ }
688
+ } )
689
+ . collect ( ) ;
690
+ let fqp: Vec < Symbol > = once ( crate_name) . chain ( relative) . collect ( ) ;
691
+
692
+ let shortty = tcx. def_kind ( def_id) . into ( ) ;
693
+ let module_fqp = to_module_fqp ( shortty, & fqp) ;
694
+ let mut is_remote = false ;
695
+
696
+ let url_parts = url_parts ( cx. cache ( ) , def_id, & module_fqp, & cx. current , & mut is_remote) ?;
697
+ let ( url_parts, shortty, fqp) = make_href ( root_path, shortty, url_parts, & fqp, is_remote) ?;
698
+ if def_id == original_def_id {
699
+ return Ok ( ( url_parts, shortty, fqp) ) ;
700
+ }
701
+ let kind = match tcx. def_kind ( original_def_id) {
702
+ DefKind :: Variant => "variant" ,
703
+ DefKind :: AssocFn => "method" ,
704
+ DefKind :: AssocConst => "associatedconstant" ,
705
+ DefKind :: AssocTy => "associatedtype" ,
706
+ _ => return Ok ( ( url_parts, shortty, fqp) ) ,
707
+ } ;
708
+ Ok ( ( format ! ( "{url_parts}#{kind}.{}" , tcx. item_name( original_def_id) ) , shortty, fqp) )
709
+ }
710
+
711
+ fn to_module_fqp ( shortty : ItemType , fqp : & [ Symbol ] ) -> & [ Symbol ] {
712
+ if shortty == ItemType :: Module { fqp } else { & fqp[ ..fqp. len ( ) - 1 ] }
713
+ }
714
+
715
+ fn url_parts (
716
+ cache : & Cache ,
717
+ def_id : DefId ,
718
+ module_fqp : & [ Symbol ] ,
719
+ relative_to : & [ Symbol ] ,
720
+ is_remote : & mut bool ,
721
+ ) -> Result < UrlPartsBuilder , HrefError > {
722
+ match cache. extern_locations [ & def_id. krate ] {
723
+ ExternalLocation :: Remote ( ref s) => {
724
+ * is_remote = true ;
725
+ let s = s. trim_end_matches ( '/' ) ;
726
+ let mut builder = UrlPartsBuilder :: singleton ( s) ;
727
+ builder. extend ( module_fqp. iter ( ) . copied ( ) ) ;
728
+ Ok ( builder)
729
+ }
730
+ ExternalLocation :: Local => Ok ( href_relative_parts ( module_fqp, relative_to) . collect ( ) ) ,
731
+ ExternalLocation :: Unknown => Err ( HrefError :: DocumentationNotBuilt ) ,
732
+ }
733
+ }
734
+
735
+ fn make_href (
736
+ root_path : Option < & str > ,
737
+ shortty : ItemType ,
738
+ mut url_parts : UrlPartsBuilder ,
739
+ fqp : & [ Symbol ] ,
740
+ is_remote : bool ,
741
+ ) -> Result < ( String , ItemType , Vec < Symbol > ) , HrefError > {
742
+ if !is_remote && let Some ( root_path) = root_path {
743
+ let root = root_path. trim_end_matches ( '/' ) ;
744
+ url_parts. push_front ( root) ;
745
+ }
746
+ debug ! ( ?url_parts) ;
747
+ match shortty {
748
+ ItemType :: Module => {
749
+ url_parts. push ( "index.html" ) ;
750
+ }
751
+ _ => {
752
+ let prefix = shortty. as_str ( ) ;
753
+ let last = fqp. last ( ) . unwrap ( ) ;
754
+ url_parts. push_fmt ( format_args ! ( "{prefix}.{last}.html" ) ) ;
755
+ }
756
+ }
757
+ Ok ( ( url_parts. finish ( ) , shortty, fqp. to_vec ( ) ) )
758
+ }
759
+
654
760
pub ( crate ) fn href_with_root_path (
655
- did : DefId ,
761
+ original_did : DefId ,
656
762
cx : & Context < ' _ > ,
657
763
root_path : Option < & str > ,
658
764
) -> Result < ( String , ItemType , Vec < Symbol > ) , HrefError > {
659
765
let tcx = cx. tcx ( ) ;
660
- let def_kind = tcx. def_kind ( did ) ;
766
+ let def_kind = tcx. def_kind ( original_did ) ;
661
767
let did = match def_kind {
662
768
DefKind :: AssocTy | DefKind :: AssocFn | DefKind :: AssocConst | DefKind :: Variant => {
663
769
// documented on their parent's page
664
- tcx. parent ( did )
770
+ tcx. parent ( original_did )
665
771
}
666
772
DefKind :: ExternCrate => {
667
773
// Link to the crate itself, not the `extern crate` item.
668
- if let Some ( local_did) = did . as_local ( ) {
774
+ if let Some ( local_did) = original_did . as_local ( ) {
669
775
tcx. extern_mod_stmt_cnum ( local_did) . unwrap_or ( LOCAL_CRATE ) . as_def_id ( )
670
776
} else {
671
- did
777
+ original_did
672
778
}
673
779
}
674
- _ => did ,
780
+ _ => original_did ,
675
781
} ;
676
782
let cache = cx. cache ( ) ;
677
783
let relative_to = & cx. current ;
678
- fn to_module_fqp ( shortty : ItemType , fqp : & [ Symbol ] ) -> & [ Symbol ] {
679
- if shortty == ItemType :: Module { fqp } else { & fqp[ ..fqp. len ( ) - 1 ] }
680
- }
681
784
682
- if !did. is_local ( )
683
- && !cache. effective_visibilities . is_directly_public ( tcx, did)
684
- && !cache. document_private
685
- && !cache. primitive_locations . values ( ) . any ( |& id| id == did)
686
- {
687
- return Err ( HrefError :: Private ) ;
785
+ if !did. is_local ( ) {
786
+ if root_path. is_some ( ) {
787
+ if tcx. is_doc_hidden ( did) {
788
+ return Err ( HrefError :: Private ) ;
789
+ }
790
+ } else if !cache. effective_visibilities . is_directly_public ( tcx, did)
791
+ && !cache. document_private
792
+ && !cache. primitive_locations . values ( ) . any ( |& id| id == did)
793
+ {
794
+ return Err ( HrefError :: Private ) ;
795
+ }
688
796
}
689
797
690
798
let mut is_remote = false ;
691
- let ( fqp, shortty, mut url_parts) = match cache. paths . get ( & did) {
799
+ let ( fqp, shortty, url_parts) = match cache. paths . get ( & did) {
692
800
Some ( & ( ref fqp, shortty) ) => ( fqp, shortty, {
693
801
let module_fqp = to_module_fqp ( shortty, fqp. as_slice ( ) ) ;
694
802
debug ! ( ?fqp, ?shortty, ?module_fqp) ;
@@ -697,46 +805,17 @@ pub(crate) fn href_with_root_path(
697
805
None => {
698
806
if let Some ( & ( ref fqp, shortty) ) = cache. external_paths . get ( & did) {
699
807
let module_fqp = to_module_fqp ( shortty, fqp) ;
700
- (
701
- fqp,
702
- shortty,
703
- match cache. extern_locations [ & did. krate ] {
704
- ExternalLocation :: Remote ( ref s) => {
705
- is_remote = true ;
706
- let s = s. trim_end_matches ( '/' ) ;
707
- let mut builder = UrlPartsBuilder :: singleton ( s) ;
708
- builder. extend ( module_fqp. iter ( ) . copied ( ) ) ;
709
- builder
710
- }
711
- ExternalLocation :: Local => {
712
- href_relative_parts ( module_fqp, relative_to) . collect ( )
713
- }
714
- ExternalLocation :: Unknown => return Err ( HrefError :: DocumentationNotBuilt ) ,
715
- } ,
716
- )
808
+ ( fqp, shortty, url_parts ( cache, did, module_fqp, relative_to, & mut is_remote) ?)
717
809
} else if matches ! ( def_kind, DefKind :: Macro ( _) ) {
718
810
return generate_macro_def_id_path ( did, cx, root_path) ;
811
+ } else if did. is_local ( ) {
812
+ return Err ( HrefError :: Private ) ;
719
813
} else {
720
- return Err ( HrefError :: NotInExternalCache ) ;
814
+ return generate_item_def_id_path ( did , original_did , cx , root_path ) ;
721
815
}
722
816
}
723
817
} ;
724
- if !is_remote && let Some ( root_path) = root_path {
725
- let root = root_path. trim_end_matches ( '/' ) ;
726
- url_parts. push_front ( root) ;
727
- }
728
- debug ! ( ?url_parts) ;
729
- match shortty {
730
- ItemType :: Module => {
731
- url_parts. push ( "index.html" ) ;
732
- }
733
- _ => {
734
- let prefix = shortty. as_str ( ) ;
735
- let last = fqp. last ( ) . unwrap ( ) ;
736
- url_parts. push_fmt ( format_args ! ( "{prefix}.{last}.html" ) ) ;
737
- }
738
- }
739
- Ok ( ( url_parts. finish ( ) , shortty, fqp. to_vec ( ) ) )
818
+ make_href ( root_path, shortty, url_parts, fqp, is_remote)
740
819
}
741
820
742
821
pub ( crate ) fn href (
0 commit comments