about summary refs log tree commit diff
diff options
context:
space:
mode:
authorWill Crichton <wcrichto@cs.stanford.edu>2021-09-21 15:49:36 -0700
committerWill Crichton <wcrichto@cs.stanford.edu>2021-10-06 19:45:25 -0700
commitdf5e3a6e40b5e70f3e869f97a7ce800385913c28 (patch)
treeb7d16a97452cf2e35ccac68cd369d952fa304a28
parent5c05b3c03da5d37e544589e42fe9738728b27b40 (diff)
downloadrust-df5e3a6e40b5e70f3e869f97a7ce800385913c28.tar.gz
rust-df5e3a6e40b5e70f3e869f97a7ce800385913c28.zip
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
-rw-r--r--src/librustdoc/html/highlight.rs6
-rw-r--r--src/librustdoc/html/highlight/tests.rs2
-rw-r--r--src/librustdoc/html/render/mod.rs41
-rw-r--r--src/librustdoc/scrape_examples.rs14
-rw-r--r--src/test/run-make/rustdoc-scrape-examples-remap/Makefile2
-rw-r--r--src/test/rustdoc-ui/scrape-examples-wrong-options.rs (renamed from src/test/rustdoc/scrape-examples-wrong-options.rs)1
-rw-r--r--src/test/rustdoc-ui/scrape-examples-wrong-options.stderr2
7 files changed, 27 insertions, 41 deletions
diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs
index a97dd95dcb6..8ef5fabb901 100644
--- a/src/librustdoc/html/highlight.rs
+++ b/src/librustdoc/html/highlight.rs
@@ -258,11 +258,7 @@ impl Iterator for PeekIter<'a> {
     type Item = (TokenKind, &'a str);
     fn next(&mut self) -> Option<Self::Item> {
         self.peek_pos = 0;
-        if let Some(first) = self.stored.pop_front() {
-            Some(first)
-        } else {
-            self.iter.next()
-        }
+        if let Some(first) = self.stored.pop_front() { Some(first) } else { self.iter.next() }
     }
 }
 
diff --git a/src/librustdoc/html/highlight/tests.rs b/src/librustdoc/html/highlight/tests.rs
index 405bdf0d810..3fa386dded9 100644
--- a/src/librustdoc/html/highlight/tests.rs
+++ b/src/librustdoc/html/highlight/tests.rs
@@ -60,7 +60,7 @@ fn test_union_highlighting() {
     create_default_session_globals_then(|| {
         let src = include_str!("fixtures/union.rs");
         let mut html = Buffer::new();
-        write_code(&mut html, src, Edition::Edition2018, None);
+        write_code(&mut html, src, Edition::Edition2018, None, None);
         expect_file!["fixtures/union.html"].assert_eq(&html.into_inner());
     });
 }
diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs
index cd5316240d0..2454ae492b2 100644
--- a/src/librustdoc/html/render/mod.rs
+++ b/src/librustdoc/html/render/mod.rs
@@ -2465,7 +2465,7 @@ const MAX_FULL_EXAMPLES: usize = 5;
 /// Generates the HTML for example call locations generated via the --scrape-examples flag.
 fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item: &clean::Item) {
     let tcx = cx.tcx();
-    let key = crate::scrape_examples::def_id_call_key(tcx, def_id);
+    let key = tcx.def_path_hash(def_id);
     let call_locations = match cx.shared.call_locations.get(&key) {
         Some(call_locations) => call_locations,
         _ => {
@@ -2474,13 +2474,14 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item:
     };
 
     // Generate a unique ID so users can link to this section for a given method
+    // FIXME: this should use init_id_map instead of derive
     let id = cx.id_map.borrow_mut().derive("scraped-examples");
     write!(
         w,
-        r##"<div class="docblock scraped-example-list">
-          <h1 id="scraped-examples" class="small-section-header">
-             <a href="#{}">Examples found in repository</a>
-          </h1>"##,
+        "<div class=\"docblock scraped-example-list\">\
+          <h1 id=\"scraped-examples\" class=\"small-section-header\">\
+             <a href=\"#{}\">Examples found in repository</a>\
+          </h1>",
         id
     );
 
@@ -2533,11 +2534,11 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item:
 
         write!(
             w,
-            r#"<div class="scraped-example" data-locs="{locations}" data-offset="{offset}">
-                <div class="scraped-example-title">
-                   {name} (<a href="{root}{url}" target="_blank">{line_range}</a>)
-                </div>
-                <div class="code-wrapper">"#,
+            "<div class=\"scraped-example\" data-locs=\"{locations}\" data-offset=\"{offset}\">\
+                <div class=\"scraped-example-title\">\
+                   {name} (<a href=\"{root}{url}\" target=\"_blank\">{line_range}</a>)\
+                </div>\
+                <div class=\"code-wrapper\">",
             root = cx.root_path(),
             url = call_data.url,
             name = call_data.display_name,
@@ -2625,14 +2626,13 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, def_id: DefId, item:
     if it.peek().is_some() {
         write!(
             w,
-            r#"<details class="rustdoc-toggle more-examples-toggle">
-                  <summary class="hideme">
-                     <span>More examples</span>
-                  </summary>
-                  <div class="more-scraped-examples">
-                    <div class="toggle-line"><div class="toggle-line-inner"></div></div>
-                    <div class="more-scraped-examples-inner">
-"#
+            "<details class=\"rustdoc-toggle more-examples-toggle\">\
+                  <summary class=\"hideme\">\
+                     <span>More examples</span>\
+                  </summary>\
+                  <div class=\"more-scraped-examples\">\
+                    <div class=\"toggle-line\"><div class=\"toggle-line-inner\"></div></div>\
+                    <div class=\"more-scraped-examples-inner\">"
         );
 
         // 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:
 
         // For the remaining examples, generate a <ul> containing links to the source files.
         if it.peek().is_some() {
-            write!(
-                w,
-                r#"<div class="example-links">Additional examples can be found in:<br /><ul>"#
-            );
+            write!(w, r#"<div class="example-links">Additional examples can be found in:<br><ul>"#);
             it.for_each(|(_, call_data)| {
                 write!(
                     w,
diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs
index 62557fa5c7d..bb3cb016e2e 100644
--- a/src/librustdoc/scrape_examples.rs
+++ b/src/librustdoc/scrape_examples.rs
@@ -22,7 +22,7 @@ use rustc_serialize::{
 };
 use rustc_session::getopts;
 use rustc_span::{
-    def_id::{CrateNum, DefId},
+    def_id::{CrateNum, DefPathHash},
     edition::Edition,
     BytePos, FileName, SourceFile,
 };
@@ -108,7 +108,7 @@ crate struct CallData {
 }
 
 crate type FnCallLocations = FxHashMap<PathBuf, CallData>;
-crate type AllCallLocations = FxHashMap<String, FnCallLocations>;
+crate type AllCallLocations = FxHashMap<DefPathHash, FnCallLocations>;
 
 /// Visitor for traversing a crate and finding instances of function calls.
 struct FindCalls<'a, 'tcx> {
@@ -119,14 +119,6 @@ struct FindCalls<'a, 'tcx> {
     calls: &'a mut AllCallLocations,
 }
 
-crate fn def_id_call_key(tcx: TyCtxt<'_>, def_id: DefId) -> String {
-    format!(
-        "{}{}",
-        tcx.crate_name(def_id.krate).to_ident_string(),
-        tcx.def_path(def_id).to_string_no_crate_verbose()
-    )
-}
-
 impl<'a, 'tcx> Visitor<'tcx> for FindCalls<'a, 'tcx>
 where
     'tcx: 'a,
@@ -185,7 +177,7 @@ where
                     CallData { locations: Vec::new(), url, display_name, edition }
                 };
 
-                let fn_key = def_id_call_key(tcx, *def_id);
+                let fn_key = tcx.def_path_hash(*def_id);
                 let fn_entries = self.calls.entry(fn_key).or_default();
 
                 let location = CallLocation::new(tcx, span, ex.hir_id, &file);
diff --git a/src/test/run-make/rustdoc-scrape-examples-remap/Makefile b/src/test/run-make/rustdoc-scrape-examples-remap/Makefile
index 823ae37f6ef..9903c87be23 100644
--- a/src/test/run-make/rustdoc-scrape-examples-remap/Makefile
+++ b/src/test/run-make/rustdoc-scrape-examples-remap/Makefile
@@ -7,7 +7,7 @@ all:
 	$(RUSTC) src/lib.rs --crate-name foobar --crate-type lib --emit=metadata
 
 # 2. scrape examples from the reverse-dependency into an ex.calls file
-	$(RUSTDOC) examples/ex.rs --crate-name ex --crate-type bin \
+	$(RUSTDOC) examples/ex.rs --crate-name ex --crate-type bin --output $(OUTPUT_DIR) \
 	  --extern foobar=$(TMPDIR)/libfoobar.rmeta \
 		-Z unstable-options \
 		--scrape-examples-output-path $(TMPDIR)/ex.calls \
diff --git a/src/test/rustdoc/scrape-examples-wrong-options.rs b/src/test/rustdoc-ui/scrape-examples-wrong-options.rs
index 419234d21b1..b18d4715453 100644
--- a/src/test/rustdoc/scrape-examples-wrong-options.rs
+++ b/src/test/rustdoc-ui/scrape-examples-wrong-options.rs
@@ -1,2 +1 @@
-// should-fail
 // compile-flags: --scrape-examples-target-crate foobar
diff --git a/src/test/rustdoc-ui/scrape-examples-wrong-options.stderr b/src/test/rustdoc-ui/scrape-examples-wrong-options.stderr
new file mode 100644
index 00000000000..b4ad28f92da
--- /dev/null
+++ b/src/test/rustdoc-ui/scrape-examples-wrong-options.stderr
@@ -0,0 +1,2 @@
+error: the `-Z unstable-options` flag must also be passed to enable the flag `scrape-examples-target-crate`
+