@@ -5,7 +5,7 @@ use rustc_data_structures::fx::FxHashMap;
55use rustc_hir:: def:: { DefKind , Res } ;
66use rustc_hir:: def_id:: DefId ;
77use rustc_hir:: intravisit:: { self , NestedVisitorMap , Visitor } ;
8- use rustc_hir:: { ExprKind , GenericParam , GenericParamKind , HirId } ;
8+ use rustc_hir:: { ExprKind , GenericParam , GenericParamKind , HirId , Mod , Node } ;
99use rustc_middle:: ty:: TyCtxt ;
1010use rustc_span:: Span ;
1111
@@ -20,14 +20,14 @@ crate fn collect_spans_and_sources(
2020 krate : clean:: Crate ,
2121 src_root : & std:: path:: Path ,
2222 include_sources : bool ,
23- _generate_link_to_definition : bool ,
23+ generate_link_to_definition : bool ,
2424) -> ( clean:: Crate , FxHashMap < std:: path:: PathBuf , String > , FxHashMap < ( u32 , u32 ) , LinkFromSrc > ) {
2525 let mut visitor = SpanMapVisitor { tcx, matches : FxHashMap :: default ( ) } ;
2626
2727 if include_sources {
28- // if generate_link_to_definition {
29- intravisit:: walk_crate ( & mut visitor, tcx. hir ( ) . krate ( ) ) ;
30- // }
28+ if generate_link_to_definition {
29+ intravisit:: walk_crate ( & mut visitor, tcx. hir ( ) . krate ( ) ) ;
30+ }
3131 let ( krate, sources) = sources:: collect_local_sources ( tcx, src_root, krate) ;
3232 ( krate, sources, visitor. matches )
3333 } else {
@@ -94,6 +94,25 @@ impl Visitor<'tcx> for SpanMapVisitor<'tcx> {
9494 intravisit:: walk_path ( self , path) ;
9595 }
9696
97+ fn visit_mod ( & mut self , m : & ' tcx Mod < ' tcx > , span : Span , id : HirId ) {
98+ // To make the difference between "mod foo {}" and "mod foo;". In case we "import" another
99+ // file, we want to link to it. Otherwise no need to create a link.
100+ if !span. overlaps ( m. inner ) {
101+ // Now that we confirmed it's a file import, we want to get the span for the module
102+ // name only and not all the "mod foo;".
103+ if let Some ( node) = self . tcx . hir ( ) . find ( id) {
104+ match node {
105+ Node :: Item ( item) => {
106+ self . matches
107+ . insert ( span_to_tuple ( item. ident . span ) , LinkFromSrc :: Local ( m. inner ) ) ;
108+ }
109+ _ => { }
110+ }
111+ }
112+ }
113+ intravisit:: walk_mod ( self , m, id) ;
114+ }
115+
97116 fn visit_expr ( & mut self , expr : & ' tcx rustc_hir:: Expr < ' tcx > ) {
98117 match expr. kind {
99118 ExprKind :: MethodCall ( segment, method_span, _, _) => {
0 commit comments