@@ -1938,8 +1938,20 @@ fn clean_use_statement(
19381938 let inline_attr = attrs. lists ( sym:: doc) . get_word_attr ( sym:: inline) ;
19391939 let pub_underscore = visibility. is_public ( ) && name == kw:: Underscore ;
19401940 let current_mod = cx. tcx . parent_module_from_def_id ( import. def_id ) ;
1941+
1942+ // The parent of the module in which this import resides. This
1943+ // is the same as `current_mod` if that's already the top
1944+ // level module.
19411945 let parent_mod = cx. tcx . parent_module_from_def_id ( current_mod) ;
19421946
1947+ // This checks if the import can be seen from a higher level module.
1948+ // In other words, it checks if the visibility is the equivalent of
1949+ // `pub(super)` or higher. If the current module is the top level
1950+ // module, there isn't really a parent module, which makes the results
1951+ // meaningless. In this case, we make sure the answer is `false`.
1952+ let is_visible_from_parent_mod = visibility. is_accessible_from ( parent_mod. to_def_id ( ) , cx. tcx )
1953+ && !current_mod. is_top_level_module ( ) ;
1954+
19431955 if pub_underscore {
19441956 if let Some ( ref inline) = inline_attr {
19451957 rustc_errors:: struct_span_err!(
@@ -1958,8 +1970,7 @@ fn clean_use_statement(
19581970 // #[doc(no_inline)] attribute is present.
19591971 // Don't inline doc(hidden) imports so they can be stripped at a later stage.
19601972 let mut denied = !( visibility. is_public ( )
1961- || ( cx. render_options . document_private
1962- && visibility. is_accessible_from ( parent_mod. to_def_id ( ) , cx. tcx ) ) )
1973+ || ( cx. render_options . document_private && is_visible_from_parent_mod) )
19631974 || pub_underscore
19641975 || attrs. iter ( ) . any ( |a| {
19651976 a. has_name ( sym:: doc)
0 commit comments