- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Reland #83738: "rustdoc: Don't load all extern crates unconditionally" #88215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| use rustc_ast as ast; | ||
| use rustc_hir::def::Namespace::TypeNS; | ||
| use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID}; | ||
| use rustc_interface::interface; | ||
| use rustc_span::Span; | ||
|  | ||
| use std::cell::RefCell; | ||
| use std::mem; | ||
| use std::rc::Rc; | ||
|  | ||
| type Resolver = Rc<RefCell<interface::BoxedResolver>>; | ||
| // Letting the resolver escape at the end of the function leads to inconsistencies between the | ||
| // crates the TyCtxt sees and the resolver sees (because the resolver could load more crates | ||
| // after escaping). Hopefully `IntraLinkCrateLoader` gets all the crates we need ... | ||
| crate fn load_intra_link_crates(resolver: Resolver, krate: &ast::Crate) -> Resolver { | ||
| let mut loader = IntraLinkCrateLoader { current_mod: CRATE_DEF_ID, resolver }; | ||
| // `walk_crate` doesn't visit the crate itself for some reason. | ||
| loader.load_links_in_attrs(&krate.attrs, krate.span); | ||
| ast::visit::walk_crate(&mut loader, krate); | ||
| loader.resolver | ||
| } | ||
|  | ||
| struct IntraLinkCrateLoader { | ||
| current_mod: LocalDefId, | ||
| resolver: Rc<RefCell<interface::BoxedResolver>>, | ||
| } | ||
|  | ||
| impl IntraLinkCrateLoader { | ||
| fn load_links_in_attrs(&mut self, attrs: &[ast::Attribute], span: Span) { | ||
| use crate::html::markdown::markdown_links; | ||
| use crate::passes::collect_intra_doc_links::preprocess_link; | ||
|  | ||
| // FIXME: this probably needs to consider inlining | ||
| let attrs = crate::clean::Attributes::from_ast(attrs, None); | ||
| for (parent_module, doc) in attrs.collapsed_doc_value_by_module_level() { | ||
| debug!(?doc); | ||
| for link in markdown_links(&doc.as_str()) { | ||
| debug!(?link.link); | ||
| let path_str = if let Some(Ok(x)) = preprocess_link(&link) { | ||
| x.path_str | ||
| } else { | ||
| continue; | ||
| }; | ||
| self.resolver.borrow_mut().access(|resolver| { | ||
| let _ = resolver.resolve_str_path_error( | ||
| span, | ||
| &path_str, | ||
| TypeNS, | ||
| parent_module.unwrap_or(self.current_mod.to_def_id()), | ||
| ); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|  | ||
| impl ast::visit::Visitor<'_> for IntraLinkCrateLoader { | ||
| fn visit_item(&mut self, item: &ast::Item) { | ||
| use rustc_ast_lowering::ResolverAstLowering; | ||
|  | ||
| if let ast::ItemKind::Mod(..) = item.kind { | ||
| let new_mod = | ||
| self.resolver.borrow_mut().access(|resolver| resolver.local_def_id(item.id)); | ||
| let old_mod = mem::replace(&mut self.current_mod, new_mod); | ||
|  | ||
| self.load_links_in_attrs(&item.attrs, item.span); | ||
| ast::visit::walk_item(self, item); | ||
|  | ||
| self.current_mod = old_mod; | ||
| } else { | ||
| self.load_links_in_attrs(&item.attrs, item.span); | ||
| ast::visit::walk_item(self, item); | ||
| } | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // no-prefer-dynamic | ||
| #![crate_type = "lib"] | ||
| #![no_std] | ||
| #![feature(lang_items)] | ||
|  | ||
| use core::panic::PanicInfo; | ||
| use core::sync::atomic::{self, Ordering}; | ||
|  | ||
| #[panic_handler] | ||
| fn panic(_info: &PanicInfo) -> ! { | ||
| loop { | ||
| atomic::compiler_fence(Ordering::SeqCst); | ||
| } | ||
| } | ||
|  | ||
| #[lang = "eh_personality"] | ||
| fn foo() {} | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // check-pass | ||
| // aux-crate:panic_item=panic-item.rs | ||
| // @has unused_extern_crate/index.html | 
This file was deleted.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pub struct SomeStruct; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| // compile-flags: --extern pub_struct | ||
| // aux-build:pub-struct.rs | ||
|  | ||
| /// [SomeStruct] | ||
| /// | ||
| /// [SomeStruct]: pub_struct::SomeStruct | ||
| pub fn foo() {} | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, apparently github dropped my comment because I wrote it on a commit instead of the PR itself. Do you happen to have it in your email? If not I can try to rewrite it from memory, it was discussing exactly how this bug could happen and how I don't know how to test it with a UI test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I don't see any traces of the extended version of this comment in my mail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, it went something like this: "inlining" is refering to
doc(inline), not#[inline]. The test case for it would look something like:In particular, the first time
inner_cratewill be loaded is when resolving the links onbar, which I don't think this pass will catch because it doesn't look at the attributes of the original definition, only of the re-export.I'm not sure how to test this with a UI test because it requires three nested crates, and AFAIK
aux-buildonly supports two.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to work fine when testing locally though 🤷 so this is probably ok.