From 1ab23bda9c44989a7d5b4bab557eb2dab18eb509 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Tue, 1 Feb 2022 17:03:22 -0800 Subject: [PATCH] Include all contents of first line of scraped item --- src/librustdoc/scrape_examples.rs | 5 ++++- .../run-make/rustdoc-scrape-examples-whitespace/Makefile | 5 +++++ .../rustdoc-scrape-examples-whitespace/examples/ex.rs | 8 ++++++++ .../rustdoc-scrape-examples-whitespace/src/lib.rs | 3 +++ 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/test/run-make/rustdoc-scrape-examples-whitespace/Makefile create mode 100644 src/test/run-make/rustdoc-scrape-examples-whitespace/examples/ex.rs create mode 100644 src/test/run-make/rustdoc-scrape-examples-whitespace/src/lib.rs diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs index 899c9e4c62956..68b3e18275137 100644 --- a/src/librustdoc/scrape_examples.rs +++ b/src/librustdoc/scrape_examples.rs @@ -196,7 +196,8 @@ where return; } - let file = tcx.sess.source_map().lookup_char_pos(span.lo()).file; + let source_map = tcx.sess.source_map(); + let file = source_map.lookup_char_pos(span.lo()).file; let file_path = match file.name.clone() { FileName::Real(real_filename) => real_filename.into_local_path(), _ => None, @@ -217,6 +218,8 @@ where let fn_entries = self.calls.entry(fn_key).or_default(); trace!("Including expr: {:?}", span); + let enclosing_item_span = + source_map.span_extend_to_prev_char(enclosing_item_span, '\n', false); let location = CallLocation::new(span, enclosing_item_span, &file); fn_entries.entry(abs_path).or_insert_with(mk_call_data).locations.push(location); } diff --git a/src/test/run-make/rustdoc-scrape-examples-whitespace/Makefile b/src/test/run-make/rustdoc-scrape-examples-whitespace/Makefile new file mode 100644 index 0000000000000..dce8b83eefe4e --- /dev/null +++ b/src/test/run-make/rustdoc-scrape-examples-whitespace/Makefile @@ -0,0 +1,5 @@ +deps := ex + +-include ../rustdoc-scrape-examples-multiple/scrape.mk + +all: scrape diff --git a/src/test/run-make/rustdoc-scrape-examples-whitespace/examples/ex.rs b/src/test/run-make/rustdoc-scrape-examples-whitespace/examples/ex.rs new file mode 100644 index 0000000000000..44ff689dfc876 --- /dev/null +++ b/src/test/run-make/rustdoc-scrape-examples-whitespace/examples/ex.rs @@ -0,0 +1,8 @@ +struct Foo; +impl Foo { + fn bar() { foobar::ok(); } +} + +fn main() { + Foo::bar(); +} diff --git a/src/test/run-make/rustdoc-scrape-examples-whitespace/src/lib.rs b/src/test/run-make/rustdoc-scrape-examples-whitespace/src/lib.rs new file mode 100644 index 0000000000000..28c34716c2f3b --- /dev/null +++ b/src/test/run-make/rustdoc-scrape-examples-whitespace/src/lib.rs @@ -0,0 +1,3 @@ +// @has foobar/fn.ok.html '//*[@class="docblock scraped-example-list"]//code' ' ' + +pub fn ok() {}