mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-24 15:54:15 +00:00
Move crate loader to collect_intra_doc_links::early
This groups the similar code together, and also allows making most of collect_intra_doc_links private again
This commit is contained in:
parent
47d1ed9690
commit
cba50731a6
@ -5,8 +5,8 @@ use rustc_driver::abort_on_err;
|
||||
use rustc_errors::emitter::{Emitter, EmitterWriter};
|
||||
use rustc_errors::json::JsonEmitter;
|
||||
use rustc_feature::UnstableFeatures;
|
||||
use rustc_hir::def::{Namespace::TypeNS, Res};
|
||||
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
|
||||
use rustc_hir::def::Res;
|
||||
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, LOCAL_CRATE};
|
||||
use rustc_hir::HirId;
|
||||
use rustc_hir::{
|
||||
intravisit::{self, NestedVisitorMap, Visitor},
|
||||
@ -356,54 +356,7 @@ crate fn create_resolver<'a>(
|
||||
let (krate, resolver, _) = &*parts;
|
||||
let resolver = resolver.borrow().clone();
|
||||
|
||||
// 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 ...
|
||||
struct IntraLinkCrateLoader {
|
||||
current_mod: DefId,
|
||||
resolver: Rc<RefCell<interface::BoxedResolver>>,
|
||||
}
|
||||
impl ast::visit::Visitor<'_> for IntraLinkCrateLoader {
|
||||
fn visit_attribute(&mut self, attr: &ast::Attribute) {
|
||||
use crate::html::markdown::markdown_links;
|
||||
use crate::passes::collect_intra_doc_links::preprocess_link;
|
||||
|
||||
if let Some(doc) = attr.doc_str() {
|
||||
for link in markdown_links(&doc.as_str()) {
|
||||
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(
|
||||
attr.span,
|
||||
&path_str,
|
||||
TypeNS,
|
||||
self.current_mod,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
ast::visit::walk_attribute(self, attr);
|
||||
}
|
||||
|
||||
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.to_def_id());
|
||||
ast::visit::walk_item(self, item);
|
||||
self.current_mod = old_mod;
|
||||
} else {
|
||||
ast::visit::walk_item(self, item);
|
||||
}
|
||||
}
|
||||
}
|
||||
let crate_id = LocalDefId { local_def_index: CRATE_DEF_INDEX }.to_def_id();
|
||||
let mut loader = IntraLinkCrateLoader { current_mod: crate_id, resolver };
|
||||
let mut loader = crate::passes::collect_intra_doc_links::IntraLinkCrateLoader::new(resolver);
|
||||
ast::visit::walk_crate(&mut loader, krate);
|
||||
|
||||
loader.resolver
|
||||
|
@ -39,13 +39,16 @@ use crate::passes::Pass;
|
||||
|
||||
use super::span_of_attrs;
|
||||
|
||||
mod early;
|
||||
crate use early::IntraLinkCrateLoader;
|
||||
|
||||
crate const COLLECT_INTRA_DOC_LINKS: Pass = Pass {
|
||||
name: "collect-intra-doc-links",
|
||||
run: collect_intra_doc_links,
|
||||
description: "resolves intra-doc links",
|
||||
};
|
||||
|
||||
crate fn collect_intra_doc_links(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
|
||||
fn collect_intra_doc_links(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
|
||||
LinkCollector {
|
||||
cx,
|
||||
mod_ids: Vec::new(),
|
||||
@ -68,7 +71,7 @@ impl<'a> From<ResolutionFailure<'a>> for ErrorKind<'a> {
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Hash)]
|
||||
crate enum Res {
|
||||
enum Res {
|
||||
Def(DefKind, DefId),
|
||||
Primitive(PrimitiveType),
|
||||
}
|
||||
@ -134,7 +137,7 @@ impl TryFrom<ResolveRes> for Res {
|
||||
|
||||
/// A link failed to resolve.
|
||||
#[derive(Debug)]
|
||||
crate enum ResolutionFailure<'a> {
|
||||
enum ResolutionFailure<'a> {
|
||||
/// This resolved, but with the wrong namespace.
|
||||
WrongNamespace {
|
||||
/// What the link resolved to.
|
||||
@ -172,7 +175,7 @@ crate enum ResolutionFailure<'a> {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
crate enum MalformedGenerics {
|
||||
enum MalformedGenerics {
|
||||
/// This link has unbalanced angle brackets.
|
||||
///
|
||||
/// For example, `Vec<T` should trigger this, as should `Vec<T>>`.
|
||||
@ -224,7 +227,7 @@ impl ResolutionFailure<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
crate enum AnchorFailure {
|
||||
enum AnchorFailure {
|
||||
/// User error: `[std#x#y]` is not valid
|
||||
MultipleAnchors,
|
||||
/// The anchor provided by the user conflicts with Rustdoc's generated anchor.
|
||||
@ -892,7 +895,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
|
||||
}
|
||||
}
|
||||
|
||||
crate enum PreprocessingError<'a> {
|
||||
enum PreprocessingError<'a> {
|
||||
Anchor(AnchorFailure),
|
||||
Disambiguator(Range<usize>, String),
|
||||
Resolution(ResolutionFailure<'a>, String, Option<Disambiguator>),
|
||||
@ -904,8 +907,8 @@ impl From<AnchorFailure> for PreprocessingError<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
crate struct PreprocessingInfo {
|
||||
crate path_str: String,
|
||||
struct PreprocessingInfo {
|
||||
path_str: String,
|
||||
disambiguator: Option<Disambiguator>,
|
||||
extra_fragment: Option<String>,
|
||||
link_text: String,
|
||||
@ -917,7 +920,7 @@ crate struct PreprocessingInfo {
|
||||
/// - `Some(Ok)` if the link is valid
|
||||
///
|
||||
/// `link_buffer` is needed for lifetime reasons; it will always be overwritten and the contents ignored.
|
||||
crate fn preprocess_link<'a>(
|
||||
fn preprocess_link<'a>(
|
||||
ori_link: &'a MarkdownLink,
|
||||
) -> Option<Result<PreprocessingInfo, PreprocessingError<'a>>> {
|
||||
// [] is mostly likely not supposed to be a link
|
||||
@ -1494,7 +1497,7 @@ fn should_ignore_link(path_str: &str) -> bool {
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
||||
/// Disambiguators for a link.
|
||||
crate enum Disambiguator {
|
||||
enum Disambiguator {
|
||||
/// `prim@`
|
||||
///
|
||||
/// This is buggy, see <https://github.com/rust-lang/rust/pull/77875#discussion_r503583103>
|
||||
@ -1523,7 +1526,7 @@ impl Disambiguator {
|
||||
/// This returns `Ok(Some(...))` if a disambiguator was found,
|
||||
/// `Ok(None)` if no disambiguator was found, or `Err(...)`
|
||||
/// if there was a problem with the disambiguator.
|
||||
crate fn from_str(link: &str) -> Result<Option<(Self, &str)>, (String, Range<usize>)> {
|
||||
fn from_str(link: &str) -> Result<Option<(Self, &str)>, (String, Range<usize>)> {
|
||||
use Disambiguator::{Kind, Namespace as NS, Primitive};
|
||||
|
||||
if let Some(idx) = link.find('@') {
|
||||
|
63
src/librustdoc/passes/collect_intra_doc_links/early.rs
Normal file
63
src/librustdoc/passes/collect_intra_doc_links/early.rs
Normal file
@ -0,0 +1,63 @@
|
||||
use rustc_ast as ast;
|
||||
use rustc_hir::def::Namespace::TypeNS;
|
||||
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX};
|
||||
use rustc_interface::interface;
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::mem;
|
||||
use std::rc::Rc;
|
||||
|
||||
// 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 struct IntraLinkCrateLoader {
|
||||
current_mod: DefId,
|
||||
crate resolver: Rc<RefCell<interface::BoxedResolver>>,
|
||||
}
|
||||
|
||||
impl IntraLinkCrateLoader {
|
||||
crate fn new(resolver: Rc<RefCell<interface::BoxedResolver>>) -> Self {
|
||||
let crate_id = LocalDefId { local_def_index: CRATE_DEF_INDEX }.to_def_id();
|
||||
Self { current_mod: crate_id, resolver }
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::visit::Visitor<'_> for IntraLinkCrateLoader {
|
||||
fn visit_attribute(&mut self, attr: &ast::Attribute) {
|
||||
use crate::html::markdown::markdown_links;
|
||||
use crate::passes::collect_intra_doc_links::preprocess_link;
|
||||
|
||||
if let Some(doc) = attr.doc_str() {
|
||||
for link in markdown_links(&doc.as_str()) {
|
||||
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(
|
||||
attr.span,
|
||||
&path_str,
|
||||
TypeNS,
|
||||
self.current_mod,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
ast::visit::walk_attribute(self, attr);
|
||||
}
|
||||
|
||||
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.to_def_id());
|
||||
ast::visit::walk_item(self, item);
|
||||
self.current_mod = old_mod;
|
||||
} else {
|
||||
ast::visit::walk_item(self, item);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user