2021-08-26 21:43:12 +00:00
|
|
|
//! This module analyzes crates to find call sites that can serve as examples in the documentation.
|
2021-05-09 23:22:22 +00:00
|
|
|
|
2021-06-03 00:21:48 +00:00
|
|
|
use crate::clean;
|
|
|
|
use crate::config;
|
|
|
|
use crate::formats;
|
|
|
|
use crate::formats::renderer::FormatRenderer;
|
|
|
|
use crate::html::render::Context;
|
|
|
|
|
2021-05-09 23:22:22 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
|
|
|
use rustc_hir::{
|
|
|
|
self as hir,
|
|
|
|
intravisit::{self, Visitor},
|
|
|
|
};
|
|
|
|
use rustc_interface::interface;
|
2021-09-17 01:12:45 +00:00
|
|
|
use rustc_macros::{Decodable, Encodable};
|
2021-05-09 23:22:22 +00:00
|
|
|
use rustc_middle::hir::map::Map;
|
2021-11-03 23:03:12 +00:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2021-05-30 17:00:44 +00:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2021-09-17 01:12:45 +00:00
|
|
|
use rustc_serialize::{
|
2022-06-14 04:52:01 +00:00
|
|
|
opaque::{FileEncoder, MemDecoder},
|
2022-06-16 06:00:25 +00:00
|
|
|
Decodable, Encodable,
|
2021-09-17 01:12:45 +00:00
|
|
|
};
|
2022-11-27 19:11:21 +00:00
|
|
|
use rustc_session::{config::CrateType, getopts};
|
2021-09-20 21:08:33 +00:00
|
|
|
use rustc_span::{
|
2021-10-13 01:26:39 +00:00
|
|
|
def_id::{CrateNum, DefPathHash, LOCAL_CRATE},
|
2021-09-20 21:08:33 +00:00
|
|
|
edition::Edition,
|
|
|
|
BytePos, FileName, SourceFile,
|
|
|
|
};
|
2021-09-17 01:12:45 +00:00
|
|
|
|
2021-06-01 21:02:09 +00:00
|
|
|
use std::fs;
|
2021-06-03 00:21:48 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2021-09-20 21:08:33 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2022-05-21 01:06:44 +00:00
|
|
|
pub(crate) struct ScrapeExamplesOptions {
|
2021-09-20 21:08:33 +00:00
|
|
|
output_path: PathBuf,
|
|
|
|
target_crates: Vec<String>,
|
2022-05-21 01:06:44 +00:00
|
|
|
pub(crate) scrape_tests: bool,
|
2021-09-20 21:08:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ScrapeExamplesOptions {
|
2022-05-21 01:06:44 +00:00
|
|
|
pub(crate) fn new(
|
2021-09-20 21:08:33 +00:00
|
|
|
matches: &getopts::Matches,
|
|
|
|
diag: &rustc_errors::Handler,
|
|
|
|
) -> Result<Option<Self>, i32> {
|
|
|
|
let output_path = matches.opt_str("scrape-examples-output-path");
|
|
|
|
let target_crates = matches.opt_strs("scrape-examples-target-crate");
|
2022-02-12 05:48:59 +00:00
|
|
|
let scrape_tests = matches.opt_present("scrape-tests");
|
|
|
|
match (output_path, !target_crates.is_empty(), scrape_tests) {
|
|
|
|
(Some(output_path), true, _) => Ok(Some(ScrapeExamplesOptions {
|
2021-09-20 21:08:33 +00:00
|
|
|
output_path: PathBuf::from(output_path),
|
|
|
|
target_crates,
|
2022-02-12 05:48:59 +00:00
|
|
|
scrape_tests,
|
2021-09-20 21:08:33 +00:00
|
|
|
})),
|
2022-02-12 05:48:59 +00:00
|
|
|
(Some(_), false, _) | (None, true, _) => {
|
2021-11-05 21:06:17 +00:00
|
|
|
diag.err("must use --scrape-examples-output-path and --scrape-examples-target-crate together");
|
2021-09-20 21:08:33 +00:00
|
|
|
Err(1)
|
|
|
|
}
|
2022-02-12 05:48:59 +00:00
|
|
|
(None, false, true) => {
|
|
|
|
diag.err("must use --scrape-examples-output-path and --scrape-examples-target-crate with --scrape-tests");
|
|
|
|
Err(1)
|
|
|
|
}
|
|
|
|
(None, false, false) => Ok(None),
|
2021-09-20 21:08:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 01:12:45 +00:00
|
|
|
#[derive(Encodable, Decodable, Debug, Clone)]
|
2022-05-21 01:06:44 +00:00
|
|
|
pub(crate) struct SyntaxRange {
|
|
|
|
pub(crate) byte_span: (u32, u32),
|
|
|
|
pub(crate) line_span: (usize, usize),
|
2021-08-26 21:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SyntaxRange {
|
2022-04-12 18:05:07 +00:00
|
|
|
fn new(span: rustc_span::Span, file: &SourceFile) -> Option<Self> {
|
2021-08-26 21:43:12 +00:00
|
|
|
let get_pos = |bytepos: BytePos| file.original_relative_byte_pos(bytepos).0;
|
2022-04-12 18:05:07 +00:00
|
|
|
let get_line = |bytepos: BytePos| file.lookup_line(bytepos);
|
2021-08-26 21:43:12 +00:00
|
|
|
|
2022-04-12 18:05:07 +00:00
|
|
|
Some(SyntaxRange {
|
2021-08-26 21:43:12 +00:00
|
|
|
byte_span: (get_pos(span.lo()), get_pos(span.hi())),
|
2022-04-12 18:05:07 +00:00
|
|
|
line_span: (get_line(span.lo())?, get_line(span.hi())?),
|
|
|
|
})
|
2021-08-26 21:43:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 01:12:45 +00:00
|
|
|
#[derive(Encodable, Decodable, Debug, Clone)]
|
2022-05-21 01:06:44 +00:00
|
|
|
pub(crate) struct CallLocation {
|
|
|
|
pub(crate) call_expr: SyntaxRange,
|
|
|
|
pub(crate) call_ident: SyntaxRange,
|
|
|
|
pub(crate) enclosing_item: SyntaxRange,
|
2021-08-26 21:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CallLocation {
|
|
|
|
fn new(
|
|
|
|
expr_span: rustc_span::Span,
|
2022-01-25 01:32:33 +00:00
|
|
|
ident_span: rustc_span::Span,
|
2021-11-04 20:57:39 +00:00
|
|
|
enclosing_item_span: rustc_span::Span,
|
2021-09-17 01:12:45 +00:00
|
|
|
source_file: &SourceFile,
|
2022-04-12 18:05:07 +00:00
|
|
|
) -> 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)?,
|
|
|
|
})
|
2021-08-26 21:43:12 +00:00
|
|
|
}
|
2021-06-03 00:21:48 +00:00
|
|
|
}
|
2021-05-09 23:22:22 +00:00
|
|
|
|
2021-09-17 01:12:45 +00:00
|
|
|
#[derive(Encodable, Decodable, Debug, Clone)]
|
2022-05-21 01:06:44 +00:00
|
|
|
pub(crate) struct CallData {
|
|
|
|
pub(crate) locations: Vec<CallLocation>,
|
|
|
|
pub(crate) url: String,
|
|
|
|
pub(crate) display_name: String,
|
|
|
|
pub(crate) edition: Edition,
|
2022-11-27 19:11:21 +00:00
|
|
|
pub(crate) is_bin: bool,
|
2021-06-03 00:21:48 +00:00
|
|
|
}
|
2021-09-17 01:12:45 +00:00
|
|
|
|
2022-05-21 01:06:44 +00:00
|
|
|
pub(crate) type FnCallLocations = FxHashMap<PathBuf, CallData>;
|
|
|
|
pub(crate) type AllCallLocations = FxHashMap<DefPathHash, FnCallLocations>;
|
2021-05-09 23:22:22 +00:00
|
|
|
|
|
|
|
/// Visitor for traversing a crate and finding instances of function calls.
|
|
|
|
struct FindCalls<'a, 'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
map: Map<'tcx>,
|
2021-06-03 00:21:48 +00:00
|
|
|
cx: Context<'tcx>,
|
2021-09-20 21:08:33 +00:00
|
|
|
target_crates: Vec<CrateNum>,
|
2021-05-09 23:22:22 +00:00
|
|
|
calls: &'a mut AllCallLocations,
|
2022-11-27 19:11:21 +00:00
|
|
|
crate_types: Vec<CrateType>,
|
2021-05-09 23:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for FindCalls<'a, 'tcx>
|
|
|
|
where
|
|
|
|
'tcx: 'a,
|
|
|
|
{
|
2021-11-03 23:03:12 +00:00
|
|
|
type NestedFilter = nested_filter::OnlyBodies;
|
2021-05-09 23:22:22 +00:00
|
|
|
|
2021-11-03 23:03:12 +00:00
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
|
|
|
self.map
|
2021-05-09 23:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
|
|
|
|
intravisit::walk_expr(self, ex);
|
|
|
|
|
2021-09-17 01:12:45 +00:00
|
|
|
let tcx = self.tcx;
|
2021-10-29 20:21:50 +00:00
|
|
|
|
|
|
|
// If we visit an item that contains an expression outside a function body,
|
|
|
|
// then we need to exit before calling typeck (which will panic). See
|
|
|
|
// test/run-make/rustdoc-scrape-examples-invalid-expr for an example.
|
|
|
|
let hir = tcx.hir();
|
2022-09-20 05:11:23 +00:00
|
|
|
if hir.maybe_body_owned_by(ex.hir_id.owner.def_id).is_none() {
|
2021-10-29 20:21:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get type of function if expression is a function call
|
2022-01-25 01:32:33 +00:00
|
|
|
let (ty, call_span, ident_span) = match ex.kind {
|
2021-06-01 21:02:09 +00:00
|
|
|
hir::ExprKind::Call(f, _) => {
|
2022-09-20 05:11:23 +00:00
|
|
|
let types = tcx.typeck(ex.hir_id.owner.def_id);
|
2021-10-29 20:21:50 +00:00
|
|
|
|
2021-11-05 14:37:33 +00:00
|
|
|
if let Some(ty) = types.node_type_opt(f.hir_id) {
|
2022-01-25 01:32:33 +00:00
|
|
|
(ty, ex.span, f.span)
|
2021-11-05 14:37:33 +00:00
|
|
|
} else {
|
2021-11-05 16:57:57 +00:00
|
|
|
trace!("node_type_opt({}) = None", f.hir_id);
|
2021-11-05 14:37:33 +00:00
|
|
|
return;
|
2021-10-29 20:21:50 +00:00
|
|
|
}
|
2021-06-01 21:02:09 +00:00
|
|
|
}
|
2022-09-01 04:27:31 +00:00
|
|
|
hir::ExprKind::MethodCall(path, _, _, call_span) => {
|
2022-09-20 05:11:23 +00:00
|
|
|
let types = tcx.typeck(ex.hir_id.owner.def_id);
|
2022-02-15 04:58:25 +00:00
|
|
|
let Some(def_id) = types.type_dependent_def_id(ex.hir_id) else {
|
2021-11-05 16:57:57 +00:00
|
|
|
trace!("type_dependent_def_id({}) = None", ex.hir_id);
|
2021-11-05 14:37:33 +00:00
|
|
|
return;
|
|
|
|
};
|
2022-01-25 01:32:33 +00:00
|
|
|
|
2022-01-25 20:11:44 +00:00
|
|
|
let ident_span = path.ident.span;
|
2022-01-25 01:32:33 +00:00
|
|
|
(tcx.type_of(def_id), call_span, ident_span)
|
2021-05-09 23:22:22 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-08 22:32:22 +00:00
|
|
|
// 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.
|
2022-01-25 01:32:33 +00:00
|
|
|
if call_span.from_expansion() {
|
2022-04-12 18:05:07 +00:00
|
|
|
trace!("Rejecting expr from macro: {call_span:?}");
|
2021-10-08 22:32:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-06-03 00:21:48 +00:00
|
|
|
|
2021-11-04 20:57:39 +00:00
|
|
|
// If the enclosing item has a span coming from a proc macro, then we also don't want to include
|
|
|
|
// the example.
|
2022-09-20 05:11:23 +00:00
|
|
|
let enclosing_item_span =
|
|
|
|
tcx.hir().span_with_body(tcx.hir().get_parent_item(ex.hir_id).into());
|
2021-11-04 20:57:39 +00:00
|
|
|
if enclosing_item_span.from_expansion() {
|
2022-04-12 18:05:07 +00:00
|
|
|
trace!("Rejecting expr ({call_span:?}) from macro item: {enclosing_item_span:?}");
|
2021-11-04 20:57:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:36:22 +00:00
|
|
|
// If the enclosing item doesn't actually enclose the call, this means we probably have a weird
|
|
|
|
// macro issue even though the spans aren't tagged as being from an expansion.
|
|
|
|
if !enclosing_item_span.contains(call_span) {
|
|
|
|
warn!(
|
|
|
|
"Attempted to scrape call at [{call_span:?}] whose enclosing item [{enclosing_item_span:?}] doesn't contain the span of the call."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Similarly for the call w/ the function ident.
|
|
|
|
if !call_span.contains(ident_span) {
|
|
|
|
warn!(
|
|
|
|
"Attempted to scrape call at [{call_span:?}] whose identifier [{ident_span:?}] was not contained in the span of the call."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-11-04 20:57:39 +00:00
|
|
|
|
2021-06-01 21:02:09 +00:00
|
|
|
// Save call site if the function resolves to a concrete definition
|
2021-05-30 17:00:44 +00:00
|
|
|
if let ty::FnDef(def_id, _) = ty.kind() {
|
2021-09-20 21:08:33 +00:00
|
|
|
if self.target_crates.iter().all(|krate| *krate != def_id.krate) {
|
2022-01-25 01:32:33 +00:00
|
|
|
trace!("Rejecting expr from crate not being documented: {call_span:?}");
|
2021-09-20 21:08:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-02 01:03:22 +00:00
|
|
|
let source_map = tcx.sess.source_map();
|
2022-01-25 01:32:33 +00:00
|
|
|
let file = source_map.lookup_char_pos(call_span.lo()).file;
|
2021-06-03 00:21:48 +00:00
|
|
|
let file_path = match file.name.clone() {
|
|
|
|
FileName::Real(real_filename) => real_filename.into_local_path(),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(file_path) = file_path {
|
2022-04-12 18:05:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-03 00:21:48 +00:00
|
|
|
let cx = &self.cx;
|
2022-04-12 18:05:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-17 01:12:45 +00:00
|
|
|
let mk_call_data = || {
|
|
|
|
let display_name = file_path.display().to_string();
|
2022-01-25 01:32:33 +00:00
|
|
|
let edition = call_span.edition();
|
2022-11-27 19:11:21 +00:00
|
|
|
let is_bin = self.crate_types.contains(&CrateType::Executable);
|
|
|
|
|
|
|
|
CallData { locations: Vec::new(), url, display_name, edition, is_bin }
|
2021-09-17 01:12:45 +00:00
|
|
|
};
|
|
|
|
|
2021-09-21 22:49:36 +00:00
|
|
|
let fn_key = tcx.def_path_hash(*def_id);
|
2021-09-17 01:12:45 +00:00
|
|
|
let fn_entries = self.calls.entry(fn_key).or_default();
|
|
|
|
|
2022-01-25 01:32:33 +00:00
|
|
|
trace!("Including expr: {:?}", call_span);
|
2022-02-02 01:03:22 +00:00
|
|
|
let enclosing_item_span =
|
|
|
|
source_map.span_extend_to_prev_char(enclosing_item_span, '\n', false);
|
2022-04-12 18:05:07 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
2021-09-17 01:12:45 +00:00
|
|
|
fn_entries.entry(abs_path).or_insert_with(mk_call_data).locations.push(location);
|
2021-06-03 00:21:48 +00:00
|
|
|
}
|
2021-05-09 23:22:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 01:06:44 +00:00
|
|
|
pub(crate) fn run(
|
2021-06-03 00:21:48 +00:00
|
|
|
krate: clean::Crate,
|
2021-12-11 18:13:16 +00:00
|
|
|
mut renderopts: config::RenderOptions,
|
2021-06-03 00:21:48 +00:00
|
|
|
cache: formats::cache::Cache,
|
2021-09-17 01:12:45 +00:00
|
|
|
tcx: TyCtxt<'_>,
|
2021-09-20 21:08:33 +00:00
|
|
|
options: ScrapeExamplesOptions,
|
2022-11-27 19:11:21 +00:00
|
|
|
crate_types: Vec<CrateType>,
|
2021-06-03 00:21:48 +00:00
|
|
|
) -> interface::Result<()> {
|
2021-09-17 01:12:45 +00:00
|
|
|
let inner = move || -> Result<(), String> {
|
2021-06-03 00:21:48 +00:00
|
|
|
// Generates source files for examples
|
2021-12-11 18:13:16 +00:00
|
|
|
renderopts.no_emit_shared = true;
|
2021-09-17 01:12:45 +00:00
|
|
|
let (cx, _) = Context::init(krate, renderopts, cache, tcx).map_err(|e| e.to_string())?;
|
2021-06-03 00:21:48 +00:00
|
|
|
|
2021-09-20 21:08:33 +00:00
|
|
|
// Collect CrateIds corresponding to provided target crates
|
|
|
|
// If two different versions of the crate in the dependency tree, then examples will be collcted from both.
|
2021-10-13 01:26:39 +00:00
|
|
|
let all_crates = tcx
|
|
|
|
.crates(())
|
|
|
|
.iter()
|
|
|
|
.chain([&LOCAL_CRATE])
|
|
|
|
.map(|crate_num| (crate_num, tcx.crate_name(*crate_num)))
|
|
|
|
.collect::<Vec<_>>();
|
2021-09-20 21:08:33 +00:00
|
|
|
let target_crates = options
|
|
|
|
.target_crates
|
|
|
|
.into_iter()
|
2022-02-03 21:28:19 +00:00
|
|
|
.flat_map(|target| all_crates.iter().filter(move |(_, name)| name.as_str() == target))
|
2021-10-13 01:26:39 +00:00
|
|
|
.map(|(crate_num, _)| **crate_num)
|
2021-09-20 21:08:33 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2022-04-12 18:05:07 +00:00
|
|
|
debug!("All crates in TyCtxt: {all_crates:?}");
|
|
|
|
debug!("Scrape examples target_crates: {target_crates:?}");
|
2021-10-13 01:26:39 +00:00
|
|
|
|
2021-06-03 00:21:48 +00:00
|
|
|
// Run call-finder on all items
|
|
|
|
let mut calls = FxHashMap::default();
|
2022-11-27 19:11:21 +00:00
|
|
|
let mut finder =
|
|
|
|
FindCalls { calls: &mut calls, tcx, map: tcx.hir(), cx, target_crates, crate_types };
|
2022-07-03 13:28:57 +00:00
|
|
|
tcx.hir().visit_all_item_likes_in_crate(&mut finder);
|
2021-06-03 00:21:48 +00:00
|
|
|
|
2022-07-31 04:12:04 +00:00
|
|
|
// The visitor might have found a type error, which we need to
|
|
|
|
// promote to a fatal error
|
|
|
|
if tcx.sess.diagnostic().has_errors_or_lint_errors().is_some() {
|
|
|
|
return Err(String::from("Compilation failed, aborting rustdoc"));
|
|
|
|
}
|
|
|
|
|
2021-11-04 20:57:09 +00:00
|
|
|
// Sort call locations within a given file in document order
|
|
|
|
for fn_calls in calls.values_mut() {
|
|
|
|
for file_calls in fn_calls.values_mut() {
|
|
|
|
file_calls.locations.sort_by_key(|loc| loc.call_expr.byte_span.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 01:12:45 +00:00
|
|
|
// Save output to provided path
|
2021-09-20 21:08:33 +00:00
|
|
|
let mut encoder = FileEncoder::new(options.output_path).map_err(|e| e.to_string())?;
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 03:30:45 +00:00
|
|
|
calls.encode(&mut encoder);
|
|
|
|
encoder.finish().map_err(|e| e.to_string())?;
|
2021-06-03 00:21:48 +00:00
|
|
|
|
|
|
|
Ok(())
|
2021-06-01 21:02:09 +00:00
|
|
|
};
|
2021-05-09 23:22:22 +00:00
|
|
|
|
2021-09-17 01:12:45 +00:00
|
|
|
if let Err(e) = inner() {
|
|
|
|
tcx.sess.fatal(&e);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2021-05-09 23:22:22 +00:00
|
|
|
}
|
2021-08-26 03:15:46 +00:00
|
|
|
|
2021-10-07 17:27:09 +00:00
|
|
|
// Note: the Handler must be passed in explicitly because sess isn't available while parsing options
|
2022-05-21 01:06:44 +00:00
|
|
|
pub(crate) fn load_call_locations(
|
2021-08-26 03:15:46 +00:00
|
|
|
with_examples: Vec<String>,
|
|
|
|
diag: &rustc_errors::Handler,
|
2021-09-17 01:12:45 +00:00
|
|
|
) -> Result<AllCallLocations, i32> {
|
|
|
|
let inner = || {
|
|
|
|
let mut all_calls: AllCallLocations = FxHashMap::default();
|
|
|
|
for path in with_examples {
|
2021-08-26 03:15:46 +00:00
|
|
|
let bytes = fs::read(&path).map_err(|e| format!("{} (for path {})", e, path))?;
|
2022-06-14 04:52:01 +00:00
|
|
|
let mut decoder = MemDecoder::new(&bytes, 0);
|
Make `Decodable` and `Decoder` infallible.
`Decoder` has two impls:
- opaque: this impl is already partly infallible, i.e. in some places it
currently panics on failure (e.g. if the input is too short, or on a
bad `Result` discriminant), and in some places it returns an error
(e.g. on a bad `Option` discriminant). The number of places where
either happens is surprisingly small, just because the binary
representation has very little redundancy and a lot of input reading
can occur even on malformed data.
- json: this impl is fully fallible, but it's only used (a) for the
`.rlink` file production, and there's a `FIXME` comment suggesting it
should change to a binary format, and (b) in a few tests in
non-fundamental ways. Indeed #85993 is open to remove it entirely.
And the top-level places in the compiler that call into decoding just
abort on error anyway. So the fallibility is providing little value, and
getting rid of it leads to some non-trivial performance improvements.
Much of this commit is pretty boring and mechanical. Some notes about
a few interesting parts:
- The commit removes `Decoder::{Error,error}`.
- `InternIteratorElement::intern_with`: the impl for `T` now has the same
optimization for small counts that the impl for `Result<T, E>` has,
because it's now much hotter.
- Decodable impls for SmallVec, LinkedList, VecDeque now all use
`collect`, which is nice; the one for `Vec` uses unsafe code, because
that gave better perf on some benchmarks.
2022-01-18 02:22:50 +00:00
|
|
|
let calls = AllCallLocations::decode(&mut decoder);
|
2021-09-17 01:12:45 +00:00
|
|
|
|
|
|
|
for (function, fn_calls) in calls.into_iter() {
|
|
|
|
all_calls.entry(function).or_default().extend(fn_calls.into_iter());
|
2021-08-26 03:15:46 +00:00
|
|
|
}
|
2021-09-17 01:12:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(all_calls)
|
|
|
|
};
|
|
|
|
|
|
|
|
inner().map_err(|e: String| {
|
|
|
|
diag.err(&format!("failed to load examples: {}", e));
|
|
|
|
1
|
|
|
|
})
|
2021-08-26 03:15:46 +00:00
|
|
|
}
|