@@ -32,11 +32,28 @@ pub(crate) struct Module<'hir> {
32
32
pub ( crate ) def_id : LocalDefId ,
33
33
pub ( crate ) renamed : Option < Symbol > ,
34
34
pub ( crate ) import_id : Option < LocalDefId > ,
35
- /// The key is the item `ItemId` and the value is: (item, renamed, import_id).
35
+ /// The key is the item `ItemId` and the value is: (item, renamed, Vec< import_id> ).
36
36
/// We use `FxIndexMap` to keep the insert order.
37
+ ///
38
+ /// `import_id` needs to be a `Vec` because we live in a dark world where you can have code
39
+ /// like:
40
+ ///
41
+ /// ```
42
+ /// mod raw {
43
+ /// pub fn foo() {}
44
+ /// }
45
+ ///
46
+ /// /// Foobar
47
+ /// pub use raw::foo;
48
+ ///
49
+ /// pub use raw::*;
50
+ /// ```
51
+ ///
52
+ /// So in this case, we don't want to have two items but just one with attributes from both
53
+ /// imports to be merged.
37
54
pub ( crate ) items : FxIndexMap <
38
55
( LocalDefId , Option < Symbol > ) ,
39
- ( & ' hir hir:: Item < ' hir > , Option < Symbol > , Option < LocalDefId > ) ,
56
+ ( & ' hir hir:: Item < ' hir > , Option < Symbol > , Vec < LocalDefId > ) ,
40
57
> ,
41
58
42
59
/// (def_id, renamed) -> (res, local_import_id)
@@ -154,7 +171,9 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
154
171
{
155
172
let item = self . cx . tcx . hir_expect_item ( local_def_id) ;
156
173
let ( ident, _, _) = item. expect_macro ( ) ;
157
- top_level_module. items . insert ( ( local_def_id, Some ( ident. name ) ) , ( item, None , None ) ) ;
174
+ top_level_module
175
+ . items
176
+ . insert ( ( local_def_id, Some ( ident. name ) ) , ( item, None , Vec :: new ( ) ) ) ;
158
177
}
159
178
}
160
179
@@ -236,7 +255,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
236
255
) -> bool {
237
256
debug ! ( "maybe_inline_local (renamed: {renamed:?}) res: {res:?}" ) ;
238
257
239
- let glob = renamed. is_none ( ) ;
240
258
if renamed == Some ( kw:: Underscore ) {
241
259
// We never inline `_` reexports.
242
260
return false ;
@@ -261,14 +279,15 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
261
279
return false ;
262
280
}
263
281
282
+ let is_glob = renamed. is_none ( ) ;
264
283
let is_hidden = !document_hidden && tcx. is_doc_hidden ( ori_res_did) ;
265
284
let Some ( res_did) = ori_res_did. as_local ( ) else {
266
285
// For cross-crate impl inlining we need to know whether items are
267
286
// reachable in documentation -- a previously unreachable item can be
268
287
// made reachable by cross-crate inlining which we're checking here.
269
288
// (this is done here because we need to know this upfront).
270
289
crate :: visit_lib:: lib_embargo_visit_item ( self . cx , ori_res_did) ;
271
- if is_hidden || glob {
290
+ if is_hidden || is_glob {
272
291
return false ;
273
292
}
274
293
// We store inlined foreign items otherwise, it'd mean that the `use` item would be kept
@@ -316,10 +335,10 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
316
335
// Bang macros are handled a bit on their because of how they are handled by the
317
336
// compiler. If they have `#[doc(hidden)]` and the re-export doesn't have
318
337
// `#[doc(inline)]`, then we don't inline it.
319
- Node :: Item ( _) if is_bang_macro && !please_inline && renamed . is_some ( ) && is_hidden => {
338
+ Node :: Item ( _) if is_bang_macro && !please_inline && !is_glob && is_hidden => {
320
339
return false ;
321
340
}
322
- Node :: Item ( & hir:: Item { kind : hir:: ItemKind :: Mod ( _, m) , .. } ) if glob => {
341
+ Node :: Item ( & hir:: Item { kind : hir:: ItemKind :: Mod ( _, m) , .. } ) if is_glob => {
323
342
let prev = mem:: replace ( & mut self . inlining , true ) ;
324
343
for & i in m. item_ids {
325
344
let i = tcx. hir_item ( i) ;
@@ -328,13 +347,13 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
328
347
self . inlining = prev;
329
348
true
330
349
}
331
- Node :: Item ( it) if !glob => {
350
+ Node :: Item ( it) if !is_glob => {
332
351
let prev = mem:: replace ( & mut self . inlining , true ) ;
333
352
self . visit_item_inner ( it, renamed, Some ( def_id) ) ;
334
353
self . inlining = prev;
335
354
true
336
355
}
337
- Node :: ForeignItem ( it) if !glob => {
356
+ Node :: ForeignItem ( it) if !is_glob => {
338
357
let prev = mem:: replace ( & mut self . inlining , true ) ;
339
358
self . visit_foreign_item_inner ( it, renamed, Some ( def_id) ) ;
340
359
self . inlining = prev;
@@ -378,8 +397,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
378
397
fn add_to_current_mod (
379
398
& mut self ,
380
399
item : & ' tcx hir:: Item < ' _ > ,
381
- renamed : Option < Symbol > ,
382
- parent_id : Option < LocalDefId > ,
400
+ mut renamed : Option < Symbol > ,
401
+ import_id : Option < LocalDefId > ,
383
402
) {
384
403
if self . is_importable_from_parent
385
404
// If we're inside an item, only impl blocks and `macro_rules!` with the `macro_export`
@@ -392,11 +411,21 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
392
411
_ => false ,
393
412
}
394
413
{
395
- self . modules
396
- . last_mut ( )
397
- . unwrap ( )
398
- . items
399
- . insert ( ( item. owner_id . def_id , renamed) , ( item, renamed, parent_id) ) ;
414
+ if renamed == item. kind . ident ( ) . map ( |ident| ident. name ) {
415
+ renamed = None ;
416
+ }
417
+ let key = ( item. owner_id . def_id , renamed) ;
418
+ if let Some ( import_id) = import_id {
419
+ self . modules
420
+ . last_mut ( )
421
+ . unwrap ( )
422
+ . items
423
+ . entry ( key)
424
+ . and_modify ( |v| v. 2 . push ( import_id) )
425
+ . or_insert_with ( || ( item, renamed, vec ! [ import_id] ) ) ;
426
+ } else {
427
+ self . modules . last_mut ( ) . unwrap ( ) . items . insert ( key, ( item, renamed, Vec :: new ( ) ) ) ;
428
+ }
400
429
}
401
430
}
402
431
@@ -468,7 +497,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
468
497
_ => false ,
469
498
} ) ;
470
499
let ident = match kind {
471
- hir:: UseKind :: Single ( ident) => Some ( renamed . unwrap_or ( ident. name ) ) ,
500
+ hir:: UseKind :: Single ( ident) => Some ( ident. name ) ,
472
501
hir:: UseKind :: Glob => None ,
473
502
hir:: UseKind :: ListStem => unreachable ! ( ) ,
474
503
} ;
0 commit comments