@@ -62,7 +62,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
62
62
& self ,
63
63
path_str : & str ,
64
64
current_item : & Option < String > ,
65
- module_id : LocalDefId ,
65
+ module_id : DefId ,
66
66
) -> Result < ( Res , Option < String > ) , ErrorKind > {
67
67
let cx = self . cx ;
68
68
@@ -167,15 +167,103 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
167
167
disambiguator : Option < & str > ,
168
168
ns : Namespace ,
169
169
current_item : & Option < String > ,
170
- parent_id : Option < hir:: HirId > ,
170
+ mut parent_id : Option < hir:: HirId > ,
171
171
extra_fragment : & Option < String > ,
172
172
item_opt : Option < & Item > ,
173
173
) -> Result < ( Res , Option < String > ) , ErrorKind > {
174
+ use rustc_hir:: { ItemKind , UseKind } ;
175
+
174
176
let cx = self . cx ;
175
177
178
+ // In case this is a re-export, try to resolve the docs relative to the original module.
179
+ // Since we don't document `use` statements,
180
+ // we don't have to consider the case where an item is documented in both the original module and the current module.
181
+ let mut module_id = None ;
182
+ if let Some ( item) = item_opt {
183
+ if let ItemEnum :: ImportItem ( import) = & item. inner {
184
+ if let Import :: Simple ( _, source) = import {
185
+ if let Some ( def_id) = source. did {
186
+ use crate :: rustc_middle:: ty:: DefIdTree ;
187
+
188
+ //let mut current_id = def_id;
189
+ if cx. tcx . def_kind ( def_id) == DefKind :: Mod {
190
+ module_id = Some ( def_id) ;
191
+ debug ! ( "found parent module {:?} for use statement" , def_id) ;
192
+ //break;
193
+ } else {
194
+ debug ! (
195
+ "not a module: {:?} (maybe an associated item?)" ,
196
+ cx. tcx. def_kind( def_id)
197
+ ) ;
198
+ }
199
+
200
+ /*
201
+ // For associated items, the parent module might be multiple nodes above
202
+ while let Some(parent) = cx.tcx.parent(current_id) {
203
+ if cx.tcx.def_kind(parent) == DefKind::Mod {
204
+ parent_id = Some(parent);
205
+ debug!("found parent module {:?} for use statement", parent);
206
+ break;
207
+ }
208
+ current_id = parent;
209
+ }
210
+ */
211
+ } else {
212
+ debug ! ( "no def id found" ) ;
213
+ }
214
+ } else {
215
+ debug ! ( "glob imports not handled for intra-doc links" ) ;
216
+ }
217
+ }
218
+ /*
219
+ if let Some(reexport) = item.reexport {
220
+ use crate::rustc_middle::ty::DefIdTree;
221
+
222
+ let mut current_id = reexport;
223
+ // For associated items, the parent module might be multiple nodes above
224
+ while let Some(parent) = cx.tcx.parent(current_id) {
225
+ if cx.tcx.def_kind(parent) == DefKind::Mod {
226
+ parent_id = Some(parent);
227
+ debug!("found parent module {:?} for use statement", parent);
228
+ break;
229
+ }
230
+ current_id = parent;
231
+ }
232
+ }
233
+ */
234
+ /*
235
+ if let ItemKind::Use(path, use_kind) = item.kind {
236
+ if use_kind == UseKind::Single {
237
+ match path.res {
238
+ Res::Def(def_kind, def_id) => {
239
+ use crate::rustc_middle::ty::DefIdTree;
240
+
241
+ let mut current_id = def_id;
242
+ // For associated items, the parent module might be multiple nodes above
243
+ while let Some(parent) = cx.tcx.parent(current_id) {
244
+ if cx.tcx.def_kind(parent) == DefKind::Mod {
245
+ parent_id = Some(parent);
246
+ debug!("found parent module {:?} for use statement", parent);
247
+ break;
248
+ }
249
+ current_id = parent;
250
+ }
251
+ }
252
+ _ => debug!("use {:?} was not a definition, not treating as cross-crate", item.name),
253
+ }
254
+ } else {
255
+ debug!("don't know how to resolve multiple imports for {:?}, not treating as cross-crate", path);
256
+ }
257
+ }
258
+ */
259
+ }
260
+
176
261
// In case we're in a module, try to resolve the relative path.
177
- if let Some ( module_id) = parent_id. or ( self . mod_ids . last ( ) . cloned ( ) ) {
178
- let module_id = cx. tcx . hir ( ) . local_def_id ( module_id) ;
262
+ if module_id. is_none ( ) {
263
+ let id = parent_id. or ( self . mod_ids . last ( ) . cloned ( ) ) ;
264
+ module_id = id. map ( |id| cx. tcx . hir ( ) . local_def_id ( id) . to_def_id ( ) ) ;
265
+ }
266
+ if let Some ( module_id) = module_id {
179
267
let result = cx. enter_resolver ( |resolver| {
180
268
resolver. resolve_str_path_error ( DUMMY_SP , & path_str, ns, module_id)
181
269
} ) ;
0 commit comments