Skip to content

Commit 3003f3f

Browse files
committed
rustdoc: Reuse ParseSess for intra-doc links
1 parent 7b72880 commit 3003f3f

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

src/librustdoc/core.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,7 @@ pub(crate) fn run_global_ctxt(
444444
krate =
445445
tcx.sess.time("create_format_cache", || Cache::populate(&mut ctxt, krate, &render_options));
446446

447-
let mut collector =
448-
LinkCollector { cx: &mut ctxt, visited_links: visited, ambiguous_links: ambiguous };
447+
let mut collector = LinkCollector::new_with(&mut ctxt, visited, ambiguous);
449448
collector.resolve_ambiguities();
450449

451450
tcx.dcx().abort_if_errors();

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ pub(crate) fn collect_intra_doc_links<'a, 'tcx>(
4949
krate: Crate,
5050
cx: &'a mut DocContext<'tcx>,
5151
) -> (Crate, LinkCollector<'a, 'tcx>) {
52-
let mut collector = LinkCollector {
53-
cx,
54-
visited_links: FxHashMap::default(),
55-
ambiguous_links: FxIndexMap::default(),
56-
};
52+
let mut collector = LinkCollector::new(cx);
5753
collector.visit_crate(&krate);
5854
(krate, collector)
5955
}
@@ -270,6 +266,7 @@ pub(crate) struct LinkCollector<'a, 'tcx> {
270266
/// codepaths, but we want to distinguish different kinds of error conditions, and this is easy
271267
/// to do by resolving links as soon as possible.
272268
pub(crate) ambiguous_links: FxIndexMap<(ItemId, String), Vec<AmbiguousLinks>>,
269+
psess: ParseSess,
273270
}
274271

275272
pub(crate) struct AmbiguousLinks {
@@ -278,7 +275,20 @@ pub(crate) struct AmbiguousLinks {
278275
resolved: Vec<(Res, Option<UrlFragment>)>,
279276
}
280277

281-
impl<'tcx> LinkCollector<'_, 'tcx> {
278+
impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
279+
pub(crate) fn new(cx: &'a mut DocContext<'tcx>) -> Self {
280+
Self::new_with(cx, FxHashMap::default(), FxIndexMap::default())
281+
}
282+
283+
pub(crate) fn new_with(
284+
cx: &'a mut DocContext<'tcx>,
285+
visited_links: FxHashMap<ResolutionInfo, Option<(Res, Option<UrlFragment>)>>,
286+
ambiguous_links: FxIndexMap<(ItemId, String), Vec<AmbiguousLinks>>,
287+
) -> Self {
288+
let psess = ParseSess::new(rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec());
289+
Self { cx, visited_links, ambiguous_links, psess }
290+
}
291+
282292
/// Convenience wrapper around `doc_link_resolutions`.
283293
///
284294
/// This also handles resolving `true` and `false` as booleans.
@@ -349,7 +359,7 @@ impl<'tcx> LinkCollector<'_, 'tcx> {
349359
});
350360
}
351361

352-
if let Ok(path) = parse_path(path_str) {
362+
if let Ok(path) = parse_path(&self.psess, path_str) {
353363
let candidates =
354364
self.resolve_type_relative_path(&path, ns, disambiguator, item_id, module_id);
355365
if !candidates.is_empty() {
@@ -470,11 +480,10 @@ fn full_res(tcx: TyCtxt<'_>, (base, assoc_item): (Res, Option<DefId>)) -> Res {
470480
assoc_item.map_or(base, |def_id| Res::from_def_id(tcx, def_id))
471481
}
472482

473-
fn parse_path(path_str: &str) -> Result<Path, ()> {
474-
let psess = ParseSess::new(rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec());
483+
fn parse_path(psess: &ParseSess, path_str: &str) -> Result<Path, ()> {
475484
let file_name = FileName::anon_source_code(path_str);
476485
let mut parser = match rustc_parse::new_parser_from_source_str(
477-
&psess,
486+
psess,
478487
file_name,
479488
path_str.to_owned(),
480489
StripTokens::Nothing,

0 commit comments

Comments
 (0)