Skip to content

Commit 6a18b68

Browse files
committed
Add Rustdoc book link to scrape examples help. Remove remaining panic
locations in scrape examples.
1 parent b9ecdca commit 6a18b68

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

src/librustdoc/html/render/mod.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ use crate::html::sources;
7979
use crate::html::static_files::SCRAPE_EXAMPLES_HELP_MD;
8080
use crate::scrape_examples::{CallData, CallLocation};
8181
use crate::try_none;
82+
use crate::DOC_RUST_LANG_ORG_CHANNEL;
8283

8384
/// A pair of name and its optional document.
8485
crate type NameDoc = (String, Option<String>);
@@ -463,17 +464,22 @@ fn settings(root_path: &str, suffix: &str, theme_names: Vec<String>) -> Result<S
463464
}
464465

465466
fn scrape_examples_help(shared: &SharedContext<'_>) -> String {
466-
let content = SCRAPE_EXAMPLES_HELP_MD;
467+
let mut content = SCRAPE_EXAMPLES_HELP_MD.to_owned();
468+
content.push_str(&format!(
469+
"## More information\n\n\
470+
If you want more information about this feature, please read the [corresponding chapter in the Rustdoc book]({}/rustdoc/scraped-examples.html).",
471+
DOC_RUST_LANG_ORG_CHANNEL));
472+
467473
let mut ids = IdMap::default();
468474
format!(
469-
"<div class=\"main-heading\">
475+
"<div class=\"main-heading\">\
470476
<h1 class=\"fqn\">\
471477
<span class=\"in-band\">About scraped examples</span>\
472478
</h1>\
473479
</div>\
474480
<div>{}</div>",
475481
Markdown {
476-
content,
482+
content: &content,
477483
links: &[],
478484
ids: &mut ids,
479485
error_codes: shared.codes,

src/librustdoc/scrape_examples.rs

+42-19
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ crate struct SyntaxRange {
7171
}
7272

7373
impl SyntaxRange {
74-
fn new(span: rustc_span::Span, file: &SourceFile) -> Self {
74+
fn new(span: rustc_span::Span, file: &SourceFile) -> Option<Self> {
7575
let get_pos = |bytepos: BytePos| file.original_relative_byte_pos(bytepos).0;
76-
let get_line = |bytepos: BytePos| file.lookup_line(bytepos).unwrap();
76+
let get_line = |bytepos: BytePos| file.lookup_line(bytepos);
7777

78-
SyntaxRange {
78+
Some(SyntaxRange {
7979
byte_span: (get_pos(span.lo()), get_pos(span.hi())),
80-
line_span: (get_line(span.lo()), get_line(span.hi())),
81-
}
80+
line_span: (get_line(span.lo())?, get_line(span.hi())?),
81+
})
8282
}
8383
}
8484

@@ -95,12 +95,12 @@ impl CallLocation {
9595
ident_span: rustc_span::Span,
9696
enclosing_item_span: rustc_span::Span,
9797
source_file: &SourceFile,
98-
) -> Self {
99-
CallLocation {
100-
call_expr: SyntaxRange::new(expr_span, source_file),
101-
call_ident: SyntaxRange::new(ident_span, source_file),
102-
enclosing_item: SyntaxRange::new(enclosing_item_span, source_file),
103-
}
98+
) -> Option<Self> {
99+
Some(CallLocation {
100+
call_expr: SyntaxRange::new(expr_span, source_file)?,
101+
call_ident: SyntaxRange::new(ident_span, source_file)?,
102+
enclosing_item: SyntaxRange::new(enclosing_item_span, source_file)?,
103+
})
104104
}
105105
}
106106

@@ -178,7 +178,7 @@ where
178178
// If this span comes from a macro expansion, then the source code may not actually show
179179
// a use of the given item, so it would be a poor example. Hence, we skip all uses in macros.
180180
if call_span.from_expansion() {
181-
trace!("Rejecting expr from macro: {:?}", call_span);
181+
trace!("Rejecting expr from macro: {call_span:?}");
182182
return;
183183
}
184184

@@ -188,7 +188,7 @@ where
188188
.hir()
189189
.span_with_body(tcx.hir().local_def_id_to_hir_id(tcx.hir().get_parent_item(ex.hir_id)));
190190
if enclosing_item_span.from_expansion() {
191-
trace!("Rejecting expr ({:?}) from macro item: {:?}", call_span, enclosing_item_span);
191+
trace!("Rejecting expr ({call_span:?}) from macro item: {enclosing_item_span:?}");
192192
return;
193193
}
194194

@@ -224,11 +224,27 @@ where
224224
};
225225

226226
if let Some(file_path) = file_path {
227-
let abs_path = fs::canonicalize(file_path.clone()).unwrap();
227+
let abs_path = match fs::canonicalize(file_path.clone()) {
228+
Ok(abs_path) => abs_path,
229+
Err(_) => {
230+
trace!("Could not canonicalize file path: {}", file_path.display());
231+
return;
232+
}
233+
};
234+
228235
let cx = &self.cx;
236+
let clean_span = crate::clean::types::Span::new(call_span);
237+
let url = match cx.href_from_span(clean_span, false) {
238+
Some(url) => url,
239+
None => {
240+
trace!(
241+
"Rejecting expr ({call_span:?}) whose clean span ({clean_span:?}) cannot be turned into a link"
242+
);
243+
return;
244+
}
245+
};
246+
229247
let mk_call_data = || {
230-
let clean_span = crate::clean::types::Span::new(call_span);
231-
let url = cx.href_from_span(clean_span, false).unwrap();
232248
let display_name = file_path.display().to_string();
233249
let edition = call_span.edition();
234250
CallData { locations: Vec::new(), url, display_name, edition }
@@ -240,7 +256,14 @@ where
240256
trace!("Including expr: {:?}", call_span);
241257
let enclosing_item_span =
242258
source_map.span_extend_to_prev_char(enclosing_item_span, '\n', false);
243-
let location = CallLocation::new(call_span, ident_span, enclosing_item_span, &file);
259+
let location =
260+
match CallLocation::new(call_span, ident_span, enclosing_item_span, &file) {
261+
Some(location) => location,
262+
None => {
263+
trace!("Could not get serializable call location for {call_span:?}");
264+
return;
265+
}
266+
};
244267
fn_entries.entry(abs_path).or_insert_with(mk_call_data).locations.push(location);
245268
}
246269
}
@@ -274,8 +297,8 @@ crate fn run(
274297
.map(|(crate_num, _)| **crate_num)
275298
.collect::<Vec<_>>();
276299

277-
debug!("All crates in TyCtxt: {:?}", all_crates);
278-
debug!("Scrape examples target_crates: {:?}", target_crates);
300+
debug!("All crates in TyCtxt: {all_crates:?}");
301+
debug!("Scrape examples target_crates: {target_crates:?}");
279302

280303
// Run call-finder on all items
281304
let mut calls = FxHashMap::default();

0 commit comments

Comments
 (0)