7
7
8
8
use std:: cell:: Cell ;
9
9
use std:: fmt;
10
+ use std:: iter;
10
11
11
12
use rustc_data_structures:: captures:: Captures ;
12
13
use rustc_data_structures:: fx:: FxHashSet ;
@@ -16,12 +17,10 @@ use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
16
17
use rustc_target:: spec:: abi:: Abi ;
17
18
18
19
use crate :: clean:: { self , utils:: find_nearest_parent_module, PrimitiveType } ;
19
- use crate :: formats:: cache:: Cache ;
20
20
use crate :: formats:: item_type:: ItemType ;
21
21
use crate :: html:: escape:: Escape ;
22
22
use crate :: html:: render:: cache:: ExternalLocation ;
23
23
use crate :: html:: render:: Context ;
24
- use crate :: html:: render:: CURRENT_DEPTH ;
25
24
26
25
crate trait Print {
27
26
fn print ( self , buffer : & mut Buffer ) ;
@@ -497,7 +496,7 @@ crate fn href_relative_parts<'a>(fqp: &'a [String], relative_to_fqp: &'a [String
497
496
if f != r {
498
497
let dissimilar_part_count = relative_to_fqp. len ( ) - i;
499
498
let fqp_module = fqp[ i..fqp. len ( ) ] . iter ( ) . map ( String :: as_str) ;
500
- return std :: iter:: repeat ( ".." ) . take ( dissimilar_part_count) . chain ( fqp_module) . collect ( ) ;
499
+ return iter:: repeat ( ".." ) . take ( dissimilar_part_count) . chain ( fqp_module) . collect ( ) ;
501
500
}
502
501
}
503
502
// e.g. linking to std::sync::atomic from std::sync
@@ -506,7 +505,7 @@ crate fn href_relative_parts<'a>(fqp: &'a [String], relative_to_fqp: &'a [String
506
505
// e.g. linking to std::sync from std::sync::atomic
507
506
} else if fqp. len ( ) < relative_to_fqp. len ( ) {
508
507
let dissimilar_part_count = relative_to_fqp. len ( ) - fqp. len ( ) ;
509
- std :: iter:: repeat ( ".." ) . take ( dissimilar_part_count) . collect ( )
508
+ iter:: repeat ( ".." ) . take ( dissimilar_part_count) . collect ( )
510
509
// linking to the same module
511
510
} else {
512
511
Vec :: new ( )
@@ -555,13 +554,14 @@ fn primitive_link(
555
554
f : & mut fmt:: Formatter < ' _ > ,
556
555
prim : clean:: PrimitiveType ,
557
556
name : & str ,
558
- m : & Cache ,
557
+ cx : & Context < ' _ > ,
559
558
) -> fmt:: Result {
559
+ let m = & cx. cache ( ) ;
560
560
let mut needs_termination = false ;
561
561
if !f. alternate ( ) {
562
562
match m. primitive_locations . get ( & prim) {
563
563
Some ( & def_id) if def_id. is_local ( ) => {
564
- let len = CURRENT_DEPTH . with ( |s| s . get ( ) ) ;
564
+ let len = cx . current . len ( ) ;
565
565
let len = if len == 0 { 0 } else { len - 1 } ;
566
566
write ! (
567
567
f,
@@ -572,20 +572,28 @@ fn primitive_link(
572
572
needs_termination = true ;
573
573
}
574
574
Some ( & def_id) => {
575
+ let cname_str;
575
576
let loc = match m. extern_locations [ & def_id. krate ] {
576
- ( ref cname, _, ExternalLocation :: Remote ( ref s) ) => Some ( ( cname, s. to_string ( ) ) ) ,
577
+ ( ref cname, _, ExternalLocation :: Remote ( ref s) ) => {
578
+ cname_str = cname. as_str ( ) ;
579
+ Some ( vec ! [ s. trim_end_matches( '/' ) , & cname_str[ ..] ] )
580
+ }
577
581
( ref cname, _, ExternalLocation :: Local ) => {
578
- let len = CURRENT_DEPTH . with ( |s| s. get ( ) ) ;
579
- Some ( ( cname, "../" . repeat ( len) ) )
582
+ cname_str = cname. as_str ( ) ;
583
+ Some ( if cx. current . first ( ) . map ( |x| & x[ ..] ) == Some ( & cname_str[ ..] ) {
584
+ iter:: repeat ( ".." ) . take ( cx. current . len ( ) - 1 ) . collect ( )
585
+ } else {
586
+ let cname = iter:: once ( & cname_str[ ..] ) ;
587
+ iter:: repeat ( ".." ) . take ( cx. current . len ( ) ) . chain ( cname) . collect ( )
588
+ } )
580
589
}
581
590
( .., ExternalLocation :: Unknown ) => None ,
582
591
} ;
583
- if let Some ( ( cname , root ) ) = loc {
592
+ if let Some ( loc ) = loc {
584
593
write ! (
585
594
f,
586
- "<a class=\" primitive\" href=\" {}{}/primitive.{}.html\" >" ,
587
- root,
588
- cname,
595
+ "<a class=\" primitive\" href=\" {}/primitive.{}.html\" >" ,
596
+ loc. join( "/" ) ,
589
597
prim. to_url_str( )
590
598
) ?;
591
599
needs_termination = true ;
@@ -660,7 +668,7 @@ fn fmt_type<'cx>(
660
668
fmt:: Display :: fmt ( & tybounds ( param_names, cx) , f)
661
669
}
662
670
clean:: Infer => write ! ( f, "_" ) ,
663
- clean:: Primitive ( prim) => primitive_link ( f, prim, prim. as_str ( ) , & cx . cache ( ) ) ,
671
+ clean:: Primitive ( prim) => primitive_link ( f, prim, prim. as_str ( ) , cx ) ,
664
672
clean:: BareFunction ( ref decl) => {
665
673
if f. alternate ( ) {
666
674
write ! (
@@ -679,46 +687,46 @@ fn fmt_type<'cx>(
679
687
decl. unsafety. print_with_space( ) ,
680
688
print_abi_with_space( decl. abi)
681
689
) ?;
682
- primitive_link ( f, PrimitiveType :: Fn , "fn" , & cx . cache ( ) ) ?;
690
+ primitive_link ( f, PrimitiveType :: Fn , "fn" , cx ) ?;
683
691
write ! ( f, "{}" , decl. decl. print( cx) )
684
692
}
685
693
}
686
694
clean:: Tuple ( ref typs) => {
687
695
match & typs[ ..] {
688
- & [ ] => primitive_link ( f, PrimitiveType :: Unit , "()" , & cx . cache ( ) ) ,
696
+ & [ ] => primitive_link ( f, PrimitiveType :: Unit , "()" , cx ) ,
689
697
& [ ref one] => {
690
- primitive_link ( f, PrimitiveType :: Tuple , "(" , & cx . cache ( ) ) ?;
698
+ primitive_link ( f, PrimitiveType :: Tuple , "(" , cx ) ?;
691
699
// Carry `f.alternate()` into this display w/o branching manually.
692
700
fmt:: Display :: fmt ( & one. print ( cx) , f) ?;
693
- primitive_link ( f, PrimitiveType :: Tuple , ",)" , & cx . cache ( ) )
701
+ primitive_link ( f, PrimitiveType :: Tuple , ",)" , cx )
694
702
}
695
703
many => {
696
- primitive_link ( f, PrimitiveType :: Tuple , "(" , & cx . cache ( ) ) ?;
704
+ primitive_link ( f, PrimitiveType :: Tuple , "(" , cx ) ?;
697
705
for ( i, item) in many. iter ( ) . enumerate ( ) {
698
706
if i != 0 {
699
707
write ! ( f, ", " ) ?;
700
708
}
701
709
fmt:: Display :: fmt ( & item. print ( cx) , f) ?;
702
710
}
703
- primitive_link ( f, PrimitiveType :: Tuple , ")" , & cx . cache ( ) )
711
+ primitive_link ( f, PrimitiveType :: Tuple , ")" , cx )
704
712
}
705
713
}
706
714
}
707
715
clean:: Slice ( ref t) => {
708
- primitive_link ( f, PrimitiveType :: Slice , "[" , & cx . cache ( ) ) ?;
716
+ primitive_link ( f, PrimitiveType :: Slice , "[" , cx ) ?;
709
717
fmt:: Display :: fmt ( & t. print ( cx) , f) ?;
710
- primitive_link ( f, PrimitiveType :: Slice , "]" , & cx . cache ( ) )
718
+ primitive_link ( f, PrimitiveType :: Slice , "]" , cx )
711
719
}
712
720
clean:: Array ( ref t, ref n) => {
713
- primitive_link ( f, PrimitiveType :: Array , "[" , & cx . cache ( ) ) ?;
721
+ primitive_link ( f, PrimitiveType :: Array , "[" , cx ) ?;
714
722
fmt:: Display :: fmt ( & t. print ( cx) , f) ?;
715
723
if f. alternate ( ) {
716
- primitive_link ( f, PrimitiveType :: Array , & format ! ( "; {}]" , n) , & cx . cache ( ) )
724
+ primitive_link ( f, PrimitiveType :: Array , & format ! ( "; {}]" , n) , cx )
717
725
} else {
718
- primitive_link ( f, PrimitiveType :: Array , & format ! ( "; {}]" , Escape ( n) ) , & cx . cache ( ) )
726
+ primitive_link ( f, PrimitiveType :: Array , & format ! ( "; {}]" , Escape ( n) ) , cx )
719
727
}
720
728
}
721
- clean:: Never => primitive_link ( f, PrimitiveType :: Never , "!" , & cx . cache ( ) ) ,
729
+ clean:: Never => primitive_link ( f, PrimitiveType :: Never , "!" , cx ) ,
722
730
clean:: RawPointer ( m, ref t) => {
723
731
let m = match m {
724
732
hir:: Mutability :: Mut => "mut" ,
@@ -731,24 +739,19 @@ fn fmt_type<'cx>(
731
739
f,
732
740
clean:: PrimitiveType :: RawPointer ,
733
741
& format ! ( "*{} {:#}" , m, t. print( cx) ) ,
734
- & cx . cache ( ) ,
742
+ cx ,
735
743
)
736
744
} else {
737
745
primitive_link (
738
746
f,
739
747
clean:: PrimitiveType :: RawPointer ,
740
748
& format ! ( "*{} {}" , m, t. print( cx) ) ,
741
- & cx . cache ( ) ,
749
+ cx ,
742
750
)
743
751
}
744
752
}
745
753
_ => {
746
- primitive_link (
747
- f,
748
- clean:: PrimitiveType :: RawPointer ,
749
- & format ! ( "*{} " , m) ,
750
- & cx. cache ( ) ,
751
- ) ?;
754
+ primitive_link ( f, clean:: PrimitiveType :: RawPointer , & format ! ( "*{} " , m) , cx) ?;
752
755
fmt:: Display :: fmt ( & t. print ( cx) , f)
753
756
}
754
757
}
@@ -770,14 +773,14 @@ fn fmt_type<'cx>(
770
773
f,
771
774
PrimitiveType :: Slice ,
772
775
& format ! ( "{}{}{}[{:#}]" , amp, lt, m, bt. print( cx) ) ,
773
- & cx . cache ( ) ,
776
+ cx ,
774
777
)
775
778
} else {
776
779
primitive_link (
777
780
f,
778
781
PrimitiveType :: Slice ,
779
782
& format ! ( "{}{}{}[{}]" , amp, lt, m, bt. print( cx) ) ,
780
- & cx . cache ( ) ,
783
+ cx ,
781
784
)
782
785
}
783
786
}
@@ -786,14 +789,14 @@ fn fmt_type<'cx>(
786
789
f,
787
790
PrimitiveType :: Slice ,
788
791
& format ! ( "{}{}{}[" , amp, lt, m) ,
789
- & cx . cache ( ) ,
792
+ cx ,
790
793
) ?;
791
794
if f. alternate ( ) {
792
795
write ! ( f, "{:#}" , bt. print( cx) ) ?;
793
796
} else {
794
797
write ! ( f, "{}" , bt. print( cx) ) ?;
795
798
}
796
- primitive_link ( f, PrimitiveType :: Slice , "]" , & cx . cache ( ) )
799
+ primitive_link ( f, PrimitiveType :: Slice , "]" , cx )
797
800
}
798
801
}
799
802
}
@@ -807,7 +810,7 @@ fn fmt_type<'cx>(
807
810
f,
808
811
PrimitiveType :: Reference ,
809
812
& format ! ( "{}{}{}" , amp, lt, m) ,
810
- & cx . cache ( ) ,
813
+ cx ,
811
814
) ?;
812
815
fmt_type ( & ty, f, use_absolute, cx)
813
816
}
@@ -1292,7 +1295,7 @@ impl clean::ImportSource {
1292
1295
}
1293
1296
let name = self . path . last_name ( ) ;
1294
1297
if let hir:: def:: Res :: PrimTy ( p) = self . path . res {
1295
- primitive_link ( f, PrimitiveType :: from ( p) , & * name, & cx . cache ( ) ) ?;
1298
+ primitive_link ( f, PrimitiveType :: from ( p) , & * name, cx ) ?;
1296
1299
} else {
1297
1300
write ! ( f, "{}" , name) ?;
1298
1301
}
0 commit comments