Skip to content

Commit

Permalink
Add Rustdoc book link to scrape examples help. Remove remaining panic
Browse files Browse the repository at this point in the history
locations in scrape examples.
  • Loading branch information
willcrichton committed Apr 12, 2022
1 parent b9ecdca commit 6a18b68
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
12 changes: 9 additions & 3 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ use crate::html::sources;
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>);
Expand Down Expand Up @@ -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,
Expand Down
61 changes: 42 additions & 19 deletions src/librustdoc/scrape_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ crate struct SyntaxRange {
}

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())?),
})
}
}

Expand All @@ -95,12 +95,12 @@ impl CallLocation {
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)?,
})
}
}

Expand Down Expand Up @@ -178,7 +178,7 @@ where
// 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;
}

Expand All @@ -188,7 +188,7 @@ where
.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;
}

Expand Down Expand Up @@ -224,11 +224,27 @@ where
};

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 }
Expand All @@ -240,7 +256,14 @@ where
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);
}
}
Expand Down Expand Up @@ -274,8 +297,8 @@ crate fn run(
.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();
Expand Down

0 comments on commit 6a18b68

Please sign in to comment.