2024-05-30 22:36:52 -07:00
|
|
|
//! Doctest functionality used only for doctests in `.rs` source files.
|
|
|
|
|
2024-05-31 00:31:26 -07:00
|
|
|
use std::env;
|
2025-02-03 06:44:41 +03:00
|
|
|
use std::sync::Arc;
|
2024-05-31 00:31:26 -07:00
|
|
|
|
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
|
|
|
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
|
|
|
|
use rustc_hir::{self as hir, CRATE_HIR_ID, intravisit};
|
2024-05-30 22:36:52 -07:00
|
|
|
use rustc_middle::hir::nested_filter;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
|
|
|
use rustc_resolve::rustdoc::span_of_fragments;
|
2024-05-31 00:31:26 -07:00
|
|
|
use rustc_span::source_map::SourceMap;
|
|
|
|
use rustc_span::{BytePos, DUMMY_SP, FileName, Pos, Span};
|
2024-05-30 22:36:52 -07:00
|
|
|
|
2024-07-09 12:07:18 +02:00
|
|
|
use super::{DocTestVisitor, ScrapedDocTest};
|
2025-01-12 20:09:51 -08:00
|
|
|
use crate::clean::{Attributes, extract_cfg_from_attrs};
|
2024-05-31 16:56:04 -07:00
|
|
|
use crate::html::markdown::{self, ErrorCodes, LangString, MdRelLine};
|
2024-05-31 00:31:26 -07:00
|
|
|
|
|
|
|
struct RustCollector {
|
2025-02-03 06:44:41 +03:00
|
|
|
source_map: Arc<SourceMap>,
|
2024-07-09 12:07:18 +02:00
|
|
|
tests: Vec<ScrapedDocTest>,
|
2024-05-31 00:31:26 -07:00
|
|
|
cur_path: Vec<String>,
|
|
|
|
position: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RustCollector {
|
|
|
|
fn get_filename(&self) -> FileName {
|
|
|
|
let filename = self.source_map.span_to_filename(self.position);
|
2024-06-09 10:17:21 -07:00
|
|
|
if let FileName::Real(ref filename) = filename {
|
|
|
|
let path = filename.remapped_path_if_available();
|
|
|
|
// Strip the cwd prefix from the path. This will likely exist if
|
|
|
|
// the path was not remapped.
|
|
|
|
let path = env::current_dir()
|
|
|
|
.map(|cur_dir| path.strip_prefix(&cur_dir).unwrap_or(path))
|
|
|
|
.unwrap_or(path);
|
2024-05-31 00:31:26 -07:00
|
|
|
return path.to_owned().into();
|
|
|
|
}
|
|
|
|
filename
|
|
|
|
}
|
2024-05-31 16:56:04 -07:00
|
|
|
|
|
|
|
fn get_base_line(&self) -> usize {
|
|
|
|
let sp_lo = self.position.lo().to_usize();
|
|
|
|
let loc = self.source_map.lookup_char_pos(BytePos(sp_lo as u32));
|
|
|
|
loc.line
|
|
|
|
}
|
2024-05-31 00:31:26 -07:00
|
|
|
}
|
|
|
|
|
2024-07-09 12:07:18 +02:00
|
|
|
impl DocTestVisitor for RustCollector {
|
2024-05-31 16:56:04 -07:00
|
|
|
fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine) {
|
|
|
|
let line = self.get_base_line() + rel_line.offset();
|
2024-07-09 12:07:18 +02:00
|
|
|
self.tests.push(ScrapedDocTest::new(
|
2024-06-08 22:55:52 +02:00
|
|
|
self.get_filename(),
|
2024-05-31 00:31:26 -07:00
|
|
|
line,
|
2024-06-08 22:55:52 +02:00
|
|
|
self.cur_path.clone(),
|
|
|
|
config,
|
|
|
|
test,
|
|
|
|
));
|
2024-05-31 00:31:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_header(&mut self, _name: &str, _level: u32) {}
|
|
|
|
}
|
|
|
|
|
2024-09-16 09:11:30 +10:00
|
|
|
pub(super) struct HirCollector<'tcx> {
|
2024-05-31 00:31:26 -07:00
|
|
|
codes: ErrorCodes,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
enable_per_target_ignores: bool,
|
|
|
|
collector: RustCollector,
|
|
|
|
}
|
|
|
|
|
2024-09-16 09:11:30 +10:00
|
|
|
impl<'tcx> HirCollector<'tcx> {
|
2024-09-16 09:24:22 +10:00
|
|
|
pub fn new(codes: ErrorCodes, enable_per_target_ignores: bool, tcx: TyCtxt<'tcx>) -> Self {
|
2024-05-31 00:31:26 -07:00
|
|
|
let collector = RustCollector {
|
2024-09-16 09:11:30 +10:00
|
|
|
source_map: tcx.sess.psess.clone_source_map(),
|
2024-05-31 00:31:26 -07:00
|
|
|
cur_path: vec![],
|
|
|
|
position: DUMMY_SP,
|
|
|
|
tests: vec![],
|
|
|
|
};
|
2024-09-16 09:24:22 +10:00
|
|
|
Self { codes, enable_per_target_ignores, tcx, collector }
|
2024-05-31 00:31:26 -07:00
|
|
|
}
|
|
|
|
|
2024-07-09 12:07:18 +02:00
|
|
|
pub fn collect_crate(mut self) -> Vec<ScrapedDocTest> {
|
2024-05-31 00:31:26 -07:00
|
|
|
let tcx = self.tcx;
|
|
|
|
self.visit_testable("".to_string(), CRATE_DEF_ID, tcx.hir().span(CRATE_HIR_ID), |this| {
|
2025-02-17 14:17:57 +11:00
|
|
|
tcx.hir_walk_toplevel_module(this)
|
2024-05-31 00:31:26 -07:00
|
|
|
});
|
|
|
|
self.collector.tests
|
|
|
|
}
|
2024-05-30 22:36:52 -07:00
|
|
|
}
|
|
|
|
|
2024-11-27 15:44:10 +01:00
|
|
|
impl HirCollector<'_> {
|
2024-05-31 00:31:26 -07:00
|
|
|
fn visit_testable<F: FnOnce(&mut Self)>(
|
2024-05-30 22:36:52 -07:00
|
|
|
&mut self,
|
|
|
|
name: String,
|
|
|
|
def_id: LocalDefId,
|
|
|
|
sp: Span,
|
|
|
|
nested: F,
|
|
|
|
) {
|
|
|
|
let ast_attrs = self.tcx.hir().attrs(self.tcx.local_def_id_to_hir_id(def_id));
|
2025-01-12 20:31:53 -08:00
|
|
|
if let Some(ref cfg) =
|
|
|
|
extract_cfg_from_attrs(ast_attrs.iter(), self.tcx, &FxHashSet::default())
|
|
|
|
{
|
2024-09-16 09:11:30 +10:00
|
|
|
if !cfg.matches(&self.tcx.sess.psess, Some(self.tcx.features())) {
|
2024-05-30 22:36:52 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let has_name = !name.is_empty();
|
|
|
|
if has_name {
|
2024-05-31 00:31:26 -07:00
|
|
|
self.collector.cur_path.push(name);
|
2024-05-30 22:36:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The collapse-docs pass won't combine sugared/raw doc attributes, or included files with
|
|
|
|
// anything else, this will combine them for us.
|
2024-10-17 01:14:01 +02:00
|
|
|
let attrs = Attributes::from_hir(ast_attrs);
|
2024-05-30 22:36:52 -07:00
|
|
|
if let Some(doc) = attrs.opt_doc_value() {
|
2024-09-19 13:26:47 -07:00
|
|
|
let span = span_of_fragments(&attrs.doc_strings).unwrap_or(sp);
|
2024-10-26 17:46:22 -07:00
|
|
|
self.collector.position = if span.edition().at_least_rust_2024() {
|
|
|
|
span
|
|
|
|
} else {
|
|
|
|
// this span affects filesystem path resolution,
|
|
|
|
// so we need to keep it the same as it was previously
|
|
|
|
ast_attrs
|
|
|
|
.iter()
|
|
|
|
.find(|attr| attr.doc_str().is_some())
|
|
|
|
.map(|attr| {
|
2025-02-09 22:49:57 +01:00
|
|
|
attr.span().ctxt().outer_expn().expansion_cause().unwrap_or(attr.span())
|
2024-10-26 17:46:22 -07:00
|
|
|
})
|
|
|
|
.unwrap_or(DUMMY_SP)
|
|
|
|
};
|
2024-05-30 22:36:52 -07:00
|
|
|
markdown::find_testable_code(
|
|
|
|
&doc,
|
2024-05-31 00:31:26 -07:00
|
|
|
&mut self.collector,
|
2024-05-30 22:36:52 -07:00
|
|
|
self.codes,
|
2024-05-31 00:31:26 -07:00
|
|
|
self.enable_per_target_ignores,
|
2024-09-19 13:26:47 -07:00
|
|
|
Some(&crate::html::markdown::ExtraInfo::new(self.tcx, def_id, span)),
|
2024-05-30 22:36:52 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
nested(self);
|
|
|
|
|
|
|
|
if has_name {
|
2024-05-31 00:31:26 -07:00
|
|
|
self.collector.cur_path.pop();
|
2024-05-30 22:36:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 09:11:30 +10:00
|
|
|
impl<'tcx> intravisit::Visitor<'tcx> for HirCollector<'tcx> {
|
2024-05-30 22:36:52 -07:00
|
|
|
type NestedFilter = nested_filter::All;
|
|
|
|
|
2025-02-03 14:42:01 +11:00
|
|
|
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
|
|
|
|
self.tcx
|
2024-05-30 22:36:52 -07:00
|
|
|
}
|
|
|
|
|
2024-05-31 00:31:26 -07:00
|
|
|
fn visit_item(&mut self, item: &'tcx hir::Item<'_>) {
|
2024-05-30 22:36:52 -07:00
|
|
|
let name = match &item.kind {
|
|
|
|
hir::ItemKind::Impl(impl_) => {
|
2025-02-03 14:42:01 +11:00
|
|
|
rustc_hir_pretty::id_to_string(&self.tcx, impl_.self_ty.hir_id)
|
2024-05-30 22:36:52 -07:00
|
|
|
}
|
|
|
|
_ => item.ident.to_string(),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.visit_testable(name, item.owner_id.def_id, item.span, |this| {
|
|
|
|
intravisit::walk_item(this, item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-31 00:31:26 -07:00
|
|
|
fn visit_trait_item(&mut self, item: &'tcx hir::TraitItem<'_>) {
|
2024-05-30 22:36:52 -07:00
|
|
|
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
|
|
|
|
intravisit::walk_trait_item(this, item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-31 00:31:26 -07:00
|
|
|
fn visit_impl_item(&mut self, item: &'tcx hir::ImplItem<'_>) {
|
2024-05-30 22:36:52 -07:00
|
|
|
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
|
|
|
|
intravisit::walk_impl_item(this, item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-31 00:31:26 -07:00
|
|
|
fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'_>) {
|
2024-05-30 22:36:52 -07:00
|
|
|
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
|
|
|
|
intravisit::walk_foreign_item(this, item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-31 00:31:26 -07:00
|
|
|
fn visit_variant(&mut self, v: &'tcx hir::Variant<'_>) {
|
2024-05-30 22:36:52 -07:00
|
|
|
self.visit_testable(v.ident.to_string(), v.def_id, v.span, |this| {
|
|
|
|
intravisit::walk_variant(this, v);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-31 00:31:26 -07:00
|
|
|
fn visit_field_def(&mut self, f: &'tcx hir::FieldDef<'_>) {
|
2024-05-30 22:36:52 -07:00
|
|
|
self.visit_testable(f.ident.to_string(), f.def_id, f.span, |this| {
|
|
|
|
intravisit::walk_field_def(this, f);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|