Skip to content

Commit 80a0dc2

Browse files
committed
Remove last uses of CURRENT_DEPTH in html/format.rs
1 parent a3f56b3 commit 80a0dc2

File tree

5 files changed

+85
-40
lines changed

5 files changed

+85
-40
lines changed

src/librustdoc/clean/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2292,14 +2292,16 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
22922292
if matchers.len() <= 1 {
22932293
format!(
22942294
"{}macro {}{} {{\n ...\n}}",
2295-
vis.print_with_space(cx.tcx, def_id, &cx.cache),
2295+
// FIXME(camelid): this might create broken links!
2296+
vis.print_with_space(cx.tcx, def_id, &cx.cache, 0),
22962297
name,
22972298
matchers.iter().map(|span| span.to_src(cx)).collect::<String>(),
22982299
)
22992300
} else {
23002301
format!(
23012302
"{}macro {} {{\n{}}}",
2302-
vis.print_with_space(cx.tcx, def_id, &cx.cache),
2303+
// FIXME(camelid): this might create broken links!
2304+
vis.print_with_space(cx.tcx, def_id, &cx.cache, 0),
23032305
name,
23042306
matchers
23052307
.iter()

src/librustdoc/clean/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ impl Attributes {
855855
.filter_map(|ItemLink { link: s, link_text, did, fragment }| {
856856
match *did {
857857
Some(did) => {
858-
if let Some((mut href, ..)) = href(did, cache) {
858+
if let Some((mut href, ..)) = href(did, cache, depth) {
859859
if let Some(ref fragment) = *fragment {
860860
href.push('#');
861861
href.push_str(fragment);

src/librustdoc/html/format.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use crate::formats::cache::Cache;
2020
use crate::formats::item_type::ItemType;
2121
use crate::html::escape::Escape;
2222
use crate::html::render::cache::ExternalLocation;
23-
use crate::html::render::CURRENT_DEPTH;
2423

2524
crate trait Print {
2625
fn print(self, buffer: &mut Buffer);
@@ -489,12 +488,11 @@ impl clean::Path {
489488
}
490489
}
491490

492-
crate fn href(did: DefId, cache: &Cache) -> Option<(String, ItemType, Vec<String>)> {
491+
crate fn href(did: DefId, cache: &Cache, depth: usize) -> Option<(String, ItemType, Vec<String>)> {
493492
if !did.is_local() && !cache.access_levels.is_public(did) && !cache.document_private {
494493
return None;
495494
}
496495

497-
let depth = CURRENT_DEPTH.with(|l| l.get());
498496
let (fqp, shortty, mut url) = match cache.paths.get(&did) {
499497
Some(&(ref fqp, shortty)) => (fqp, shortty, "../".repeat(depth)),
500498
None => {
@@ -551,17 +549,17 @@ fn resolved_path(
551549
write!(w, "{}{:#}", &last.name, last.args.print(cache, depth))?;
552550
} else {
553551
let path = if use_absolute {
554-
if let Some((_, _, fqp)) = href(did, cache) {
552+
if let Some((_, _, fqp)) = href(did, cache, depth) {
555553
format!(
556554
"{}::{}",
557555
fqp[..fqp.len() - 1].join("::"),
558-
anchor(did, fqp.last().unwrap(), cache)
556+
anchor(did, fqp.last().unwrap(), cache, depth)
559557
)
560558
} else {
561559
last.name.to_string()
562560
}
563561
} else {
564-
anchor(did, &*last.name.as_str(), cache).to_string()
562+
anchor(did, &*last.name.as_str(), cache, depth).to_string()
565563
};
566564
write!(w, "{}{}", path, last.args.print(cache, depth))?;
567565
}
@@ -633,9 +631,14 @@ fn tybounds<'a>(
633631
})
634632
}
635633

636-
crate fn anchor<'a>(did: DefId, text: &'a str, cache: &'a Cache) -> impl fmt::Display + 'a {
634+
crate fn anchor<'a>(
635+
did: DefId,
636+
text: &'a str,
637+
cache: &'a Cache,
638+
depth: usize,
639+
) -> impl fmt::Display + 'a {
637640
display_fn(move |f| {
638-
if let Some((url, short_ty, fqp)) = href(did, cache) {
641+
if let Some((url, short_ty, fqp)) = href(did, cache, depth) {
639642
write!(
640643
f,
641644
r#"<a class="{}" href="{}" title="{} {}">{}</a>"#,
@@ -884,7 +887,7 @@ fn fmt_type(
884887
// everything comes in as a fully resolved QPath (hard to
885888
// look at).
886889
box clean::ResolvedPath { did, ref param_names, .. } => {
887-
match href(did, cache) {
890+
match href(did, cache, depth) {
888891
Some((ref url, _, ref path)) if !f.alternate() => {
889892
write!(
890893
f,
@@ -1143,6 +1146,7 @@ impl clean::Visibility {
11431146
tcx: TyCtxt<'tcx>,
11441147
item_did: DefId,
11451148
cache: &Cache,
1149+
depth: usize,
11461150
) -> impl fmt::Display + 'tcx {
11471151
use rustc_span::symbol::kw;
11481152

@@ -1174,7 +1178,7 @@ impl clean::Visibility {
11741178
path.data[0].data.get_opt_name().expect("modules are always named");
11751179
// modified from `resolved_path()` to work with `DefPathData`
11761180
let last_name = path.data.last().unwrap().data.get_opt_name().unwrap();
1177-
let anchor = anchor(vis_did, &last_name.as_str(), cache).to_string();
1181+
let anchor = anchor(vis_did, &last_name.as_str(), cache, depth).to_string();
11781182

11791183
let mut s = "pub(".to_owned();
11801184
if path.data.len() != 1

src/librustdoc/html/render/mod.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,10 @@ fn document_short(
674674
MarkdownSummaryLine(&s, &item.links(&cx.cache, cx.depth())).into_string();
675675

676676
if s.contains('\n') {
677-
let link =
678-
format!(r#" <a href="{}">Read more</a>"#, naive_assoc_href(item, link, cx.cache()));
677+
let link = format!(
678+
r#" <a href="{}">Read more</a>"#,
679+
naive_assoc_href(item, link, cx.cache(), cx.depth())
680+
);
679681

680682
if let Some(idx) = summary_html.rfind("</p>") {
681683
summary_html.insert_str(idx, &link);
@@ -894,7 +896,12 @@ fn render_impls(
894896
w.write_str(&impls.join(""));
895897
}
896898

897-
fn naive_assoc_href(it: &clean::Item, link: AssocItemLink<'_>, cache: &Cache) -> String {
899+
fn naive_assoc_href(
900+
it: &clean::Item,
901+
link: AssocItemLink<'_>,
902+
cache: &Cache,
903+
depth: usize,
904+
) -> String {
898905
use crate::formats::item_type::ItemType::*;
899906

900907
let name = it.name.as_ref().unwrap();
@@ -908,7 +915,7 @@ fn naive_assoc_href(it: &clean::Item, link: AssocItemLink<'_>, cache: &Cache) ->
908915
AssocItemLink::Anchor(Some(ref id)) => format!("#{}", id),
909916
AssocItemLink::Anchor(None) => anchor,
910917
AssocItemLink::GotoSource(did, _) => {
911-
href(did, cache).map(|p| format!("{}{}", p.0, anchor)).unwrap_or(anchor)
918+
href(did, cache, depth).map(|p| format!("{}{}", p.0, anchor)).unwrap_or(anchor)
912919
}
913920
}
914921
}
@@ -926,8 +933,8 @@ fn assoc_const(
926933
w,
927934
"{}{}const <a href=\"{}\" class=\"constant\"><b>{}</b></a>: {}",
928935
extra,
929-
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()),
930-
naive_assoc_href(it, link, cx.cache()),
936+
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()),
937+
naive_assoc_href(it, link, cx.cache(), cx.depth()),
931938
it.name.as_ref().unwrap(),
932939
ty.print(cx.cache(), cx.depth())
933940
);
@@ -947,7 +954,7 @@ fn assoc_type(
947954
w,
948955
"{}type <a href=\"{}\" class=\"type\">{}</a>",
949956
extra,
950-
naive_assoc_href(it, link, cache),
957+
naive_assoc_href(it, link, cache, depth),
951958
it.name.as_ref().unwrap()
952959
);
953960
if !bounds.is_empty() {
@@ -1018,12 +1025,14 @@ fn render_assoc_item(
10181025
ItemType::TyMethod
10191026
};
10201027

1021-
href(did, cx.cache()).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor)
1028+
href(did, cx.cache(), cx.depth())
1029+
.map(|p| format!("{}#{}.{}", p.0, ty, name))
1030+
.unwrap_or(anchor)
10221031
}
10231032
};
10241033
let mut header_len = format!(
10251034
"{}{}{}{}{}{:#}fn {}{:#}",
1026-
meth.visibility.print_with_space(cx.tcx(), meth.def_id, cx.cache()),
1035+
meth.visibility.print_with_space(cx.tcx(), meth.def_id, cx.cache(), cx.depth()),
10271036
header.constness.print_with_space(),
10281037
header.asyncness.print_with_space(),
10291038
header.unsafety.print_with_space(),
@@ -1045,7 +1054,7 @@ fn render_assoc_item(
10451054
"{}{}{}{}{}{}{}fn <a href=\"{href}\" class=\"fnname\">{name}</a>\
10461055
{generics}{decl}{spotlight}{where_clause}",
10471056
if parent == ItemType::Trait { " " } else { "" },
1048-
meth.visibility.print_with_space(cx.tcx(), meth.def_id, cx.cache()),
1057+
meth.visibility.print_with_space(cx.tcx(), meth.def_id, cx.cache(), cx.depth()),
10491058
header.constness.print_with_space(),
10501059
header.asyncness.print_with_space(),
10511060
header.unsafety.print_with_space(),

src/librustdoc/html/render/print_item.rs

+47-17
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,30 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
247247
Some(ref src) => write!(
248248
w,
249249
"<tr><td><code>{}extern crate {} as {};",
250-
myitem.visibility.print_with_space(cx.tcx(), myitem.def_id, cx.cache()),
251-
anchor(myitem.def_id, &*src.as_str(), cx.cache()),
250+
myitem.visibility.print_with_space(
251+
cx.tcx(),
252+
myitem.def_id,
253+
cx.cache(),
254+
cx.depth(),
255+
),
256+
anchor(myitem.def_id, &*src.as_str(), cx.cache(), cx.depth()),
252257
myitem.name.as_ref().unwrap(),
253258
),
254259
None => write!(
255260
w,
256261
"<tr><td><code>{}extern crate {};",
257-
myitem.visibility.print_with_space(cx.tcx(), myitem.def_id, cx.cache()),
258-
anchor(myitem.def_id, &*myitem.name.as_ref().unwrap().as_str(), cx.cache()),
262+
myitem.visibility.print_with_space(
263+
cx.tcx(),
264+
myitem.def_id,
265+
cx.cache(),
266+
cx.depth(),
267+
),
268+
anchor(
269+
myitem.def_id,
270+
&*myitem.name.as_ref().unwrap().as_str(),
271+
cx.cache(),
272+
cx.depth(),
273+
),
259274
),
260275
}
261276
w.write_str("</code></td></tr>");
@@ -265,7 +280,12 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
265280
write!(
266281
w,
267282
"<tr><td><code>{}{}</code></td></tr>",
268-
myitem.visibility.print_with_space(cx.tcx(), myitem.def_id, cx.cache()),
283+
myitem.visibility.print_with_space(
284+
cx.tcx(),
285+
myitem.def_id,
286+
cx.cache(),
287+
cx.depth()
288+
),
269289
import.print(cx.cache(), cx.depth())
270290
);
271291
}
@@ -367,7 +387,7 @@ fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) ->
367387
fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) {
368388
let header_len = format!(
369389
"{}{}{}{}{:#}fn {}{:#}",
370-
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()),
390+
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()),
371391
f.header.constness.print_with_space(),
372392
f.header.asyncness.print_with_space(),
373393
f.header.unsafety.print_with_space(),
@@ -382,7 +402,7 @@ fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::
382402
w,
383403
"{vis}{constness}{asyncness}{unsafety}{abi}fn \
384404
{name}{generics}{decl}{spotlight}{where_clause}</pre>",
385-
vis = it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()),
405+
vis = it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()),
386406
constness = f.header.constness.print_with_space(),
387407
asyncness = f.header.asyncness.print_with_space(),
388408
unsafety = f.header.unsafety.print_with_space(),
@@ -412,7 +432,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
412432
write!(
413433
w,
414434
"{}{}{}trait {}{}{}",
415-
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()),
435+
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()),
416436
t.unsafety.print_with_space(),
417437
if t.is_auto { "auto " } else { "" },
418438
it.name.as_ref().unwrap(),
@@ -813,7 +833,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
813833
write!(
814834
w,
815835
"{}enum {}{}{}",
816-
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()),
836+
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()),
817837
it.name.as_ref().unwrap(),
818838
e.generics.print(cx.cache(), cx.depth()),
819839
WhereClause { gens: &e.generics, indent: 0, end_newline: true }
@@ -988,7 +1008,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean::
9881008
write!(
9891009
w,
9901010
"{vis}const {name}: {typ}",
991-
vis = it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()),
1011+
vis = it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()),
9921012
name = it.name.as_ref().unwrap(),
9931013
typ = c.type_.print(cx.cache(), cx.depth()),
9941014
);
@@ -1072,7 +1092,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
10721092
write!(
10731093
w,
10741094
"{vis}static {mutability}{name}: {typ}</pre>",
1075-
vis = it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()),
1095+
vis = it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()),
10761096
mutability = s.mutability.print_with_space(),
10771097
name = it.name.as_ref().unwrap(),
10781098
typ = s.type_.print(cx.cache(), cx.depth())
@@ -1086,7 +1106,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) {
10861106
write!(
10871107
w,
10881108
" {}type {};\n}}</pre>",
1089-
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()),
1109+
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()),
10901110
it.name.as_ref().unwrap(),
10911111
);
10921112

@@ -1250,7 +1270,7 @@ fn render_union(
12501270
write!(
12511271
w,
12521272
"{}{}{}",
1253-
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()),
1273+
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()),
12541274
if structhead { "union " } else { "" },
12551275
it.name.as_ref().unwrap()
12561276
);
@@ -1269,7 +1289,7 @@ fn render_union(
12691289
write!(
12701290
w,
12711291
" {}{}: {},\n{}",
1272-
field.visibility.print_with_space(cx.tcx(), field.def_id, cx.cache()),
1292+
field.visibility.print_with_space(cx.tcx(), field.def_id, cx.cache(), cx.depth()),
12731293
field.name.as_ref().unwrap(),
12741294
ty.print(cx.cache(), cx.depth()),
12751295
tab
@@ -1296,7 +1316,7 @@ fn render_struct(
12961316
write!(
12971317
w,
12981318
"{}{}{}",
1299-
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()),
1319+
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache(), cx.depth()),
13001320
if structhead { "struct " } else { "" },
13011321
it.name.as_ref().unwrap()
13021322
);
@@ -1321,7 +1341,12 @@ fn render_struct(
13211341
w,
13221342
"\n{} {}{}: {},",
13231343
tab,
1324-
field.visibility.print_with_space(cx.tcx(), field.def_id, cx.cache()),
1344+
field.visibility.print_with_space(
1345+
cx.tcx(),
1346+
field.def_id,
1347+
cx.cache(),
1348+
cx.depth()
1349+
),
13251350
field.name.as_ref().unwrap(),
13261351
ty.print(cx.cache(), cx.depth())
13271352
);
@@ -1353,7 +1378,12 @@ fn render_struct(
13531378
write!(
13541379
w,
13551380
"{}{}",
1356-
field.visibility.print_with_space(cx.tcx(), field.def_id, cx.cache()),
1381+
field.visibility.print_with_space(
1382+
cx.tcx(),
1383+
field.def_id,
1384+
cx.cache(),
1385+
cx.depth()
1386+
),
13571387
ty.print(cx.cache(), cx.depth())
13581388
)
13591389
}

0 commit comments

Comments
 (0)