Add Rustdoc book link to scrape examples help. Remove remaining panic

locations in scrape examples.
This commit is contained in:
Will Crichton 2022-04-12 11:05:07 -07:00
parent b9ecdca0d5
commit 6a18b68655
2 changed files with 51 additions and 22 deletions

View file

@ -79,6 +79,7 @@
use crate::html::static_files::SCRAPE_EXAMPLES_HELP_MD;
use crate::scrape_examples::{CallData, CallLocation};
use crate::try_none;
use crate::DOC_RUST_LANG_ORG_CHANNEL;
/// A pair of name and its optional document.
crate type NameDoc = (String, Option<String>);
@ -463,17 +464,22 @@ fn settings(root_path: &str, suffix: &str, theme_names: Vec<String>) -> Result<S
}
fn scrape_examples_help(shared: &SharedContext<'_>) -> String {
let content = SCRAPE_EXAMPLES_HELP_MD;
let mut content = SCRAPE_EXAMPLES_HELP_MD.to_owned();
content.push_str(&format!(
"## More information\n\n\
If you want more information about this feature, please read the [corresponding chapter in the Rustdoc book]({}/rustdoc/scraped-examples.html).",
DOC_RUST_LANG_ORG_CHANNEL));
let mut ids = IdMap::default();
format!(
"<div class=\"main-heading\">
"<div class=\"main-heading\">\
<h1 class=\"fqn\">\
<span class=\"in-band\">About scraped examples</span>\
</h1>\
</div>\
<div>{}</div>",
Markdown {
content,
content: &content,
links: &[],
ids: &mut ids,
error_codes: shared.codes,

View file

@ -71,14 +71,14 @@ impl ScrapeExamplesOptions {
}
impl SyntaxRange {
fn new(span: rustc_span::Span, file: &SourceFile) -> Self {
fn new(span: rustc_span::Span, file: &SourceFile) -> Option<Self> {
let get_pos = |bytepos: BytePos| file.original_relative_byte_pos(bytepos).0;
let get_line = |bytepos: BytePos| file.lookup_line(bytepos).unwrap();
let get_line = |bytepos: BytePos| file.lookup_line(bytepos);
SyntaxRange {
Some(SyntaxRange {
byte_span: (get_pos(span.lo()), get_pos(span.hi())),
line_span: (get_line(span.lo()), get_line(span.hi())),
}
line_span: (get_line(span.lo())?, get_line(span.hi())?),
})
}
}
@ -95,12 +95,12 @@ fn new(
ident_span: rustc_span::Span,
enclosing_item_span: rustc_span::Span,
source_file: &SourceFile,
) -> Self {
CallLocation {
call_expr: SyntaxRange::new(expr_span, source_file),
call_ident: SyntaxRange::new(ident_span, source_file),
enclosing_item: SyntaxRange::new(enclosing_item_span, source_file),
}
) -> Option<Self> {
Some(CallLocation {
call_expr: SyntaxRange::new(expr_span, source_file)?,
call_ident: SyntaxRange::new(ident_span, source_file)?,
enclosing_item: SyntaxRange::new(enclosing_item_span, source_file)?,
})
}
}
@ -178,7 +178,7 @@ fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
// If this span comes from a macro expansion, then the source code may not actually show
// a use of the given item, so it would be a poor example. Hence, we skip all uses in macros.
if call_span.from_expansion() {
trace!("Rejecting expr from macro: {:?}", call_span);
trace!("Rejecting expr from macro: {call_span:?}");
return;
}
@ -188,7 +188,7 @@ fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
.hir()
.span_with_body(tcx.hir().local_def_id_to_hir_id(tcx.hir().get_parent_item(ex.hir_id)));
if enclosing_item_span.from_expansion() {
trace!("Rejecting expr ({:?}) from macro item: {:?}", call_span, enclosing_item_span);
trace!("Rejecting expr ({call_span:?}) from macro item: {enclosing_item_span:?}");
return;
}
@ -224,11 +224,27 @@ fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
};
if let Some(file_path) = file_path {
let abs_path = fs::canonicalize(file_path.clone()).unwrap();
let abs_path = match fs::canonicalize(file_path.clone()) {
Ok(abs_path) => abs_path,
Err(_) => {
trace!("Could not canonicalize file path: {}", file_path.display());
return;
}
};
let cx = &self.cx;
let clean_span = crate::clean::types::Span::new(call_span);
let url = match cx.href_from_span(clean_span, false) {
Some(url) => url,
None => {
trace!(
"Rejecting expr ({call_span:?}) whose clean span ({clean_span:?}) cannot be turned into a link"
);
return;
}
};
let mk_call_data = || {
let clean_span = crate::clean::types::Span::new(call_span);
let url = cx.href_from_span(clean_span, false).unwrap();
let display_name = file_path.display().to_string();
let edition = call_span.edition();
CallData { locations: Vec::new(), url, display_name, edition }
@ -240,7 +256,14 @@ fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
trace!("Including expr: {:?}", call_span);
let enclosing_item_span =
source_map.span_extend_to_prev_char(enclosing_item_span, '\n', false);
let location = CallLocation::new(call_span, ident_span, enclosing_item_span, &file);
let location =
match CallLocation::new(call_span, ident_span, enclosing_item_span, &file) {
Some(location) => location,
None => {
trace!("Could not get serializable call location for {call_span:?}");
return;
}
};
fn_entries.entry(abs_path).or_insert_with(mk_call_data).locations.push(location);
}
}
@ -274,8 +297,8 @@ fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
.map(|(crate_num, _)| **crate_num)
.collect::<Vec<_>>();
debug!("All crates in TyCtxt: {:?}", all_crates);
debug!("Scrape examples target_crates: {:?}", target_crates);
debug!("All crates in TyCtxt: {all_crates:?}");
debug!("Scrape examples target_crates: {target_crates:?}");
// Run call-finder on all items
let mut calls = FxHashMap::default();