Skip to content

Commit df5e3a6

Browse files
committed
Change serialized format to use DefPathHash instead of custom String
Move test to rustdoc-ui Fix test writing to wrong directory Formatting Fix test Add FIXME Remove raw multiline strings
1 parent 5c05b3c commit df5e3a6

File tree

7 files changed

+27
-41
lines changed

7 files changed

+27
-41
lines changed

src/librustdoc/html/highlight.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,7 @@ impl Iterator for PeekIter<'a> {
258258
type Item = (TokenKind, &'a str);
259259
fn next(&mut self) -> Option<Self::Item> {
260260
self.peek_pos = 0;
261-
if let Some(first) = self.stored.pop_front() {
262-
Some(first)
263-
} else {
264-
self.iter.next()
265-
}
261+
if let Some(first) = self.stored.pop_front() { Some(first) } else { self.iter.next() }
266262
}
267263
}
268264

src/librustdoc/html/highlight/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn test_union_highlighting() {
6060
create_default_session_globals_then(|| {
6161
let src = include_str!("fixtures/union.rs");
6262
let mut html = Buffer::new();
63-
write_code(&mut html, src, Edition::Edition2018, None);
63+
write_code(&mut html, src, Edition::Edition2018, None, None);
6464
expect_file!["fixtures/union.html"].assert_eq(&html.into_inner());
6565
});
6666
}

src/librustdoc/html/render/mod.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2465,7 +2465,7 @@ const MAX_FULL_EXAMPLES: usize = 5;
24652465
/// Generates the HTML for example call locations generated via the --scrape-examples flag.
24662466
fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item: &clean::Item) {
24672467
let tcx = cx.tcx();
2468-
let key = crate::scrape_examples::def_id_call_key(tcx, def_id);
2468+
let key = tcx.def_path_hash(def_id);
24692469
let call_locations = match cx.shared.call_locations.get(&key) {
24702470
Some(call_locations) => call_locations,
24712471
_ => {
@@ -2474,13 +2474,14 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item:
24742474
};
24752475

24762476
// Generate a unique ID so users can link to this section for a given method
2477+
// FIXME: this should use init_id_map instead of derive
24772478
let id = cx.id_map.borrow_mut().derive("scraped-examples");
24782479
write!(
24792480
w,
2480-
r##"<div class="docblock scraped-example-list">
2481-
<h1 id="scraped-examples" class="small-section-header">
2482-
<a href="#{}">Examples found in repository</a>
2483-
</h1>"##,
2481+
"<div class=\"docblock scraped-example-list\">\
2482+
<h1 id=\"scraped-examples\" class=\"small-section-header\">\
2483+
<a href=\"#{}\">Examples found in repository</a>\
2484+
</h1>",
24842485
id
24852486
);
24862487

@@ -2533,11 +2534,11 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item:
25332534

25342535
write!(
25352536
w,
2536-
r#"<div class="scraped-example" data-locs="{locations}" data-offset="{offset}">
2537-
<div class="scraped-example-title">
2538-
{name} (<a href="{root}{url}" target="_blank">{line_range}</a>)
2539-
</div>
2540-
<div class="code-wrapper">"#,
2537+
"<div class=\"scraped-example\" data-locs=\"{locations}\" data-offset=\"{offset}\">\
2538+
<div class=\"scraped-example-title\">\
2539+
{name} (<a href=\"{root}{url}\" target=\"_blank\">{line_range}</a>)\
2540+
</div>\
2541+
<div class=\"code-wrapper\">",
25412542
root = cx.root_path(),
25422543
url = call_data.url,
25432544
name = call_data.display_name,
@@ -2625,14 +2626,13 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item:
26252626
if it.peek().is_some() {
26262627
write!(
26272628
w,
2628-
r#"<details class="rustdoc-toggle more-examples-toggle">
2629-
<summary class="hideme">
2630-
<span>More examples</span>
2631-
</summary>
2632-
<div class="more-scraped-examples">
2633-
<div class="toggle-line"><div class="toggle-line-inner"></div></div>
2634-
<div class="more-scraped-examples-inner">
2635-
"#
2629+
"<details class=\"rustdoc-toggle more-examples-toggle\">\
2630+
<summary class=\"hideme\">\
2631+
<span>More examples</span>\
2632+
</summary>\
2633+
<div class=\"more-scraped-examples\">\
2634+
<div class=\"toggle-line\"><div class=\"toggle-line-inner\"></div></div>\
2635+
<div class=\"more-scraped-examples-inner\">"
26362636
);
26372637

26382638
// Only generate inline code for MAX_FULL_EXAMPLES number of examples. Otherwise we could
@@ -2643,10 +2643,7 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item:
26432643

26442644
// For the remaining examples, generate a <ul> containing links to the source files.
26452645
if it.peek().is_some() {
2646-
write!(
2647-
w,
2648-
r#"<div class="example-links">Additional examples can be found in:<br /><ul>"#
2649-
);
2646+
write!(w, r#"<div class="example-links">Additional examples can be found in:<br><ul>"#);
26502647
it.for_each(|(_, call_data)| {
26512648
write!(
26522649
w,

src/librustdoc/scrape_examples.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_serialize::{
2222
};
2323
use rustc_session::getopts;
2424
use rustc_span::{
25-
def_id::{CrateNum, DefId},
25+
def_id::{CrateNum, DefPathHash},
2626
edition::Edition,
2727
BytePos, FileName, SourceFile,
2828
};
@@ -108,7 +108,7 @@ crate struct CallData {
108108
}
109109

110110
crate type FnCallLocations = FxHashMap<PathBuf, CallData>;
111-
crate type AllCallLocations = FxHashMap<String, FnCallLocations>;
111+
crate type AllCallLocations = FxHashMap<DefPathHash, FnCallLocations>;
112112

113113
/// Visitor for traversing a crate and finding instances of function calls.
114114
struct FindCalls<'a, 'tcx> {
@@ -119,14 +119,6 @@ struct FindCalls<'a, 'tcx> {
119119
calls: &'a mut AllCallLocations,
120120
}
121121

122-
crate fn def_id_call_key(tcx: TyCtxt<'_>, def_id: DefId) -> String {
123-
format!(
124-
"{}{}",
125-
tcx.crate_name(def_id.krate).to_ident_string(),
126-
tcx.def_path(def_id).to_string_no_crate_verbose()
127-
)
128-
}
129-
130122
impl<'a, 'tcx> Visitor<'tcx> for FindCalls<'a, 'tcx>
131123
where
132124
'tcx: 'a,
@@ -185,7 +177,7 @@ where
185177
CallData { locations: Vec::new(), url, display_name, edition }
186178
};
187179

188-
let fn_key = def_id_call_key(tcx, *def_id);
180+
let fn_key = tcx.def_path_hash(*def_id);
189181
let fn_entries = self.calls.entry(fn_key).or_default();
190182

191183
let location = CallLocation::new(tcx, span, ex.hir_id, &file);

src/test/run-make/rustdoc-scrape-examples-remap/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ all:
77
$(RUSTC) src/lib.rs --crate-name foobar --crate-type lib --emit=metadata
88

99
# 2. scrape examples from the reverse-dependency into an ex.calls file
10-
$(RUSTDOC) examples/ex.rs --crate-name ex --crate-type bin \
10+
$(RUSTDOC) examples/ex.rs --crate-name ex --crate-type bin --output $(OUTPUT_DIR) \
1111
--extern foobar=$(TMPDIR)/libfoobar.rmeta \
1212
-Z unstable-options \
1313
--scrape-examples-output-path $(TMPDIR)/ex.calls \
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
// should-fail
21
// compile-flags: --scrape-examples-target-crate foobar
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: the `-Z unstable-options` flag must also be passed to enable the flag `scrape-examples-target-crate`
2+

0 commit comments

Comments
 (0)