Skip to content

Commit 2cc2639

Browse files
author
Timothée Delabrouille
committed
only store locations in extern_locations
1 parent 7d09dba commit 2cc2639

File tree

5 files changed

+47
-43
lines changed

5 files changed

+47
-43
lines changed

src/librustdoc/clean/types.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -106,38 +106,39 @@ impl ExternalCrate {
106106
crate fn location(
107107
&self,
108108
extern_url: Option<&str>,
109-
ast_attrs: &[ast::Attribute],
110109
dst: &std::path::Path,
111110
tcx: TyCtxt<'_>,
112111
) -> ExternalLocation {
113112
use ExternalLocation::*;
113+
114+
fn to_remote(url: impl ToString) -> ExternalLocation {
115+
let mut url = url.to_string();
116+
if !url.ends_with('/') {
117+
url.push('/');
118+
}
119+
Remote(url)
120+
}
121+
114122
// See if there's documentation generated into the local directory
123+
// WARNING: since rustdoc creates these directories as it generates documentation, this check is only accurate before rendering starts.
124+
// Make sure to call `location()` by that time.
115125
let local_location = dst.join(&*self.name(tcx).as_str());
116126
if local_location.is_dir() {
117127
return Local;
118128
}
119129

120130
if let Some(url) = extern_url {
121-
let mut url = url.to_string();
122-
if !url.ends_with('/') {
123-
url.push('/');
124-
}
125-
return Remote(url);
131+
return to_remote(url);
126132
}
127133

128134
// Failing that, see if there's an attribute specifying where to find this
129135
// external crate
130-
ast_attrs
136+
let did = DefId { krate: self.crate_num, index: CRATE_DEF_INDEX };
137+
tcx.get_attrs(did)
131138
.lists(sym::doc)
132139
.filter(|a| a.has_name(sym::html_root_url))
133140
.filter_map(|a| a.value_str())
134-
.map(|url| {
135-
let mut url = url.to_string();
136-
if !url.ends_with('/') {
137-
url.push('/')
138-
}
139-
Remote(url)
140-
})
141+
.map(to_remote)
141142
.next()
142143
.unwrap_or(Unknown) // Well, at least we tried.
143144
}
@@ -433,7 +434,7 @@ impl Item {
433434
let relative_to = &cx.current;
434435
if let Some(ref fragment) = *fragment {
435436
let url = match cx.cache().extern_locations.get(&self.def_id.krate) {
436-
Some(&(_, _, ExternalLocation::Local)) => {
437+
Some(ExternalLocation::Local) => {
437438
if relative_to[0] == "std" {
438439
let depth = relative_to.len() - 1;
439440
"../".repeat(depth)
@@ -442,10 +443,10 @@ impl Item {
442443
format!("{}std/", "../".repeat(depth))
443444
}
444445
}
445-
Some(&(_, _, ExternalLocation::Remote(ref s))) => {
446+
Some(ExternalLocation::Remote(ref s)) => {
446447
format!("{}/std/", s.trim_end_matches('/'))
447448
}
448-
Some(&(_, _, ExternalLocation::Unknown)) | None => format!(
449+
Some(ExternalLocation::Unknown) | None => format!(
449450
"https://doc.rust-lang.org/{}/std/",
450451
crate::doc_rust_lang_org_channel(),
451452
),

src/librustdoc/formats/cache.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use std::collections::BTreeMap;
22
use std::mem;
3-
use std::path::{Path, PathBuf};
3+
use std::path::Path;
44

55
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
66
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
77
use rustc_middle::middle::privacy::AccessLevels;
88
use rustc_middle::ty::TyCtxt;
99
use rustc_span::symbol::sym;
10-
use rustc_span::Symbol;
1110

1211
use crate::clean::{self, GetDefId};
1312
use crate::fold::DocFolder;
@@ -71,7 +70,7 @@ crate struct Cache {
7170
crate implementors: FxHashMap<DefId, Vec<Impl>>,
7271

7372
/// Cache of where external crate documentation can be found.
74-
crate extern_locations: FxHashMap<CrateNum, (Symbol, PathBuf, ExternalLocation)>,
73+
crate extern_locations: FxHashMap<CrateNum, ExternalLocation>,
7574

7675
/// Cache of where documentation for primitives can be found.
7776
crate primitive_locations: FxHashMap<clean::PrimitiveType, DefId>,
@@ -157,10 +156,7 @@ impl Cache {
157156
let name = e.name(tcx);
158157
let extern_url = extern_html_root_urls.get(&*name.as_str()).map(|u| &**u);
159158
let did = DefId { krate: n, index: CRATE_DEF_INDEX };
160-
self.extern_locations.insert(
161-
n,
162-
(name, e.src_root(tcx), e.location(extern_url, tcx.get_attrs(did), &dst, tcx)),
163-
);
159+
self.extern_locations.insert(n, e.location(extern_url, &dst, tcx));
164160
self.external_paths.insert(did, (vec![name.to_string()], ItemType::Module));
165161
}
166162

src/librustdoc/html/format.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_middle::ty::TyCtxt;
1616
use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
1717
use rustc_target::spec::abi::Abi;
1818

19-
use crate::clean::{self, utils::find_nearest_parent_module, PrimitiveType};
19+
use crate::clean::{self, utils::find_nearest_parent_module, ExternalCrate, PrimitiveType};
2020
use crate::formats::item_type::ItemType;
2121
use crate::html::escape::Escape;
2222
use crate::html::render::cache::ExternalLocation;
@@ -461,14 +461,14 @@ crate fn href(did: DefId, cx: &Context<'_>) -> Option<(String, ItemType, Vec<Str
461461
fqp,
462462
shortty,
463463
match cache.extern_locations[&did.krate] {
464-
(.., ExternalLocation::Remote(ref s)) => {
464+
ExternalLocation::Remote(ref s) => {
465465
let s = s.trim_end_matches('/');
466466
let mut s = vec![&s[..]];
467467
s.extend(module_fqp[..].iter().map(String::as_str));
468468
s
469469
}
470-
(.., ExternalLocation::Local) => href_relative_parts(module_fqp, relative_to),
471-
(.., ExternalLocation::Unknown) => return None,
470+
ExternalLocation::Local => href_relative_parts(module_fqp, relative_to),
471+
ExternalLocation::Unknown => return None,
472472
},
473473
)
474474
}
@@ -574,20 +574,22 @@ fn primitive_link(
574574
Some(&def_id) => {
575575
let cname_str;
576576
let loc = match m.extern_locations[&def_id.krate] {
577-
(ref cname, _, ExternalLocation::Remote(ref s)) => {
578-
cname_str = cname.as_str();
577+
ExternalLocation::Remote(ref s) => {
578+
cname_str =
579+
ExternalCrate { crate_num: def_id.krate }.name(cx.tcx()).as_str();
579580
Some(vec![s.trim_end_matches('/'), &cname_str[..]])
580581
}
581-
(ref cname, _, ExternalLocation::Local) => {
582-
cname_str = cname.as_str();
582+
ExternalLocation::Local => {
583+
cname_str =
584+
ExternalCrate { crate_num: def_id.krate }.name(cx.tcx()).as_str();
583585
Some(if cx.current.first().map(|x| &x[..]) == Some(&cname_str[..]) {
584586
iter::repeat("..").take(cx.current.len() - 1).collect()
585587
} else {
586588
let cname = iter::once(&cname_str[..]);
587589
iter::repeat("..").take(cx.current.len()).chain(cname).collect()
588590
})
589591
}
590-
(.., ExternalLocation::Unknown) => None,
592+
ExternalLocation::Unknown => None,
591593
};
592594
if let Some(loc) = loc {
593595
write!(

src/librustdoc/html/render/context.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use super::print_item::{full_path, item_path, print_item};
1818
use super::write_shared::write_shared;
1919
use super::{print_sidebar, settings, AllTypes, NameDoc, StylePath, BASIC_KEYWORDS};
2020

21-
use crate::clean;
21+
use crate::clean::{self, ExternalCrate};
2222
use crate::config::RenderOptions;
2323
use crate::docfs::{DocFS, PathError};
2424
use crate::error::Error;
@@ -304,12 +304,16 @@ impl<'tcx> Context<'tcx> {
304304
}
305305
} else {
306306
let (krate, src_root) = match *self.cache.extern_locations.get(&cnum)? {
307-
(name, ref src, ExternalLocation::Local) => (name, src),
308-
(name, ref src, ExternalLocation::Remote(ref s)) => {
307+
ExternalLocation::Local => {
308+
let e = ExternalCrate { crate_num: cnum };
309+
(e.name(self.tcx()), e.src_root(self.tcx()))
310+
}
311+
ExternalLocation::Remote(ref s) => {
309312
root = s.to_string();
310-
(name, src)
313+
let e = ExternalCrate { crate_num: cnum };
314+
(e.name(self.tcx()), e.src_root(self.tcx()))
311315
}
312-
(_, _, ExternalLocation::Unknown) => return None,
316+
ExternalLocation::Unknown => return None,
313317
};
314318

315319
sources::clean_path(&src_root, file, false, |component| {

src/librustdoc/json/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_session::Session;
1717

1818
use rustdoc_json_types as types;
1919

20-
use crate::clean;
20+
use crate::clean::{self, ExternalCrate};
2121
use crate::config::RenderOptions;
2222
use crate::error::Error;
2323
use crate::formats::cache::Cache;
@@ -218,12 +218,13 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
218218
.cache
219219
.extern_locations
220220
.iter()
221-
.map(|(k, v)| {
221+
.map(|(crate_num, external_location)| {
222+
let e = ExternalCrate { crate_num: *crate_num };
222223
(
223-
k.as_u32(),
224+
crate_num.as_u32(),
224225
types::ExternalCrate {
225-
name: v.0.to_string(),
226-
html_root_url: match &v.2 {
226+
name: e.name(self.tcx).to_string(),
227+
html_root_url: match external_location {
227228
ExternalLocation::Remote(s) => Some(s.clone()),
228229
_ => None,
229230
},

0 commit comments

Comments
 (0)