Skip to content

Commit 7e75e7c

Browse files
committed
Start moving format-specific code into doctest submodule
1 parent 1821029 commit 7e75e7c

File tree

3 files changed

+137
-121
lines changed

3 files changed

+137
-121
lines changed

src/librustdoc/doctest.rs

Lines changed: 9 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1+
mod markdown;
2+
mod rust;
3+
14
use rustc_ast as ast;
25
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
36
use rustc_data_structures::sync::Lrc;
47
use rustc_errors::emitter::stderr_destination;
58
use rustc_errors::{ColorConfig, ErrorGuaranteed, FatalError};
6-
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
7-
use rustc_hir::{self as hir, intravisit, CRATE_HIR_ID};
9+
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
10+
use rustc_hir::CRATE_HIR_ID;
811
use rustc_interface::interface;
9-
use rustc_middle::hir::map::Map;
10-
use rustc_middle::hir::nested_filter;
11-
use rustc_middle::ty::TyCtxt;
1212
use rustc_parse::maybe_new_parser_from_source_str;
1313
use rustc_parse::parser::attr::InnerAttrPolicy;
14-
use rustc_resolve::rustdoc::span_of_fragments;
1514
use rustc_session::config::{self, CrateType, ErrorOutputType};
15+
use rustc_session::lint;
1616
use rustc_session::parse::ParseSess;
17-
use rustc_session::{lint, Session};
1817
use rustc_span::edition::Edition;
1918
use rustc_span::source_map::SourceMap;
2019
use rustc_span::symbol::sym;
@@ -33,11 +32,12 @@ use std::sync::{Arc, Mutex};
3332

3433
use tempfile::{Builder as TempFileBuilder, TempDir};
3534

36-
use crate::clean::{types::AttributesExt, Attributes};
3735
use crate::config::Options as RustdocOptions;
38-
use crate::html::markdown::{self, ErrorCodes, Ignore, LangString};
36+
use crate::html::markdown::{ErrorCodes, Ignore, LangString};
3937
use crate::lint::init_lints;
4038

39+
use self::rust::HirCollector;
40+
4141
/// Options that apply to all doctests in a crate or Markdown file (for `rustdoc foo.md`).
4242
#[derive(Clone, Default)]
4343
pub(crate) struct GlobalTestOptions {
@@ -1294,117 +1294,5 @@ impl DoctestVisitor for Vec<usize> {
12941294
}
12951295
}
12961296

1297-
struct HirCollector<'a, 'hir, 'tcx> {
1298-
sess: &'a Session,
1299-
collector: &'a mut Collector,
1300-
map: Map<'hir>,
1301-
codes: ErrorCodes,
1302-
tcx: TyCtxt<'tcx>,
1303-
}
1304-
1305-
impl<'a, 'hir, 'tcx> HirCollector<'a, 'hir, 'tcx> {
1306-
fn visit_testable<F: FnOnce(&mut Self)>(
1307-
&mut self,
1308-
name: String,
1309-
def_id: LocalDefId,
1310-
sp: Span,
1311-
nested: F,
1312-
) {
1313-
let ast_attrs = self.tcx.hir().attrs(self.tcx.local_def_id_to_hir_id(def_id));
1314-
if let Some(ref cfg) = ast_attrs.cfg(self.tcx, &FxHashSet::default()) {
1315-
if !cfg.matches(&self.sess.psess, Some(self.tcx.features())) {
1316-
return;
1317-
}
1318-
}
1319-
1320-
let has_name = !name.is_empty();
1321-
if has_name {
1322-
self.collector.names.push(name);
1323-
}
1324-
1325-
// The collapse-docs pass won't combine sugared/raw doc attributes, or included files with
1326-
// anything else, this will combine them for us.
1327-
let attrs = Attributes::from_ast(ast_attrs);
1328-
if let Some(doc) = attrs.opt_doc_value() {
1329-
// Use the outermost invocation, so that doctest names come from where the docs were written.
1330-
let span = ast_attrs
1331-
.iter()
1332-
.find(|attr| attr.doc_str().is_some())
1333-
.map(|attr| attr.span.ctxt().outer_expn().expansion_cause().unwrap_or(attr.span))
1334-
.unwrap_or(DUMMY_SP);
1335-
self.collector.set_position(span);
1336-
markdown::find_testable_code(
1337-
&doc,
1338-
self.collector,
1339-
self.codes,
1340-
self.collector.enable_per_target_ignores,
1341-
Some(&crate::html::markdown::ExtraInfo::new(
1342-
self.tcx,
1343-
def_id.to_def_id(),
1344-
span_of_fragments(&attrs.doc_strings).unwrap_or(sp),
1345-
)),
1346-
self.tcx.features().custom_code_classes_in_docs,
1347-
);
1348-
}
1349-
1350-
nested(self);
1351-
1352-
if has_name {
1353-
self.collector.names.pop();
1354-
}
1355-
}
1356-
}
1357-
1358-
impl<'a, 'hir, 'tcx> intravisit::Visitor<'hir> for HirCollector<'a, 'hir, 'tcx> {
1359-
type NestedFilter = nested_filter::All;
1360-
1361-
fn nested_visit_map(&mut self) -> Self::Map {
1362-
self.map
1363-
}
1364-
1365-
fn visit_item(&mut self, item: &'hir hir::Item<'_>) {
1366-
let name = match &item.kind {
1367-
hir::ItemKind::Impl(impl_) => {
1368-
rustc_hir_pretty::id_to_string(&self.map, impl_.self_ty.hir_id)
1369-
}
1370-
_ => item.ident.to_string(),
1371-
};
1372-
1373-
self.visit_testable(name, item.owner_id.def_id, item.span, |this| {
1374-
intravisit::walk_item(this, item);
1375-
});
1376-
}
1377-
1378-
fn visit_trait_item(&mut self, item: &'hir hir::TraitItem<'_>) {
1379-
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
1380-
intravisit::walk_trait_item(this, item);
1381-
});
1382-
}
1383-
1384-
fn visit_impl_item(&mut self, item: &'hir hir::ImplItem<'_>) {
1385-
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
1386-
intravisit::walk_impl_item(this, item);
1387-
});
1388-
}
1389-
1390-
fn visit_foreign_item(&mut self, item: &'hir hir::ForeignItem<'_>) {
1391-
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
1392-
intravisit::walk_foreign_item(this, item);
1393-
});
1394-
}
1395-
1396-
fn visit_variant(&mut self, v: &'hir hir::Variant<'_>) {
1397-
self.visit_testable(v.ident.to_string(), v.def_id, v.span, |this| {
1398-
intravisit::walk_variant(this, v);
1399-
});
1400-
}
1401-
1402-
fn visit_field_def(&mut self, f: &'hir hir::FieldDef<'_>) {
1403-
self.visit_testable(f.ident.to_string(), f.def_id, f.span, |this| {
1404-
intravisit::walk_field_def(this, f);
1405-
});
1406-
}
1407-
}
1408-
14091297
#[cfg(test)]
14101298
mod tests;

src/librustdoc/doctest/markdown.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//! Doctest functionality used only for doctests in `.md` Markdown files.

src/librustdoc/doctest/rust.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//! Doctest functionality used only for doctests in `.rs` source files.
2+
3+
use rustc_data_structures::fx::FxHashSet;
4+
use rustc_hir::def_id::LocalDefId;
5+
use rustc_hir::{self as hir, intravisit};
6+
use rustc_middle::hir::map::Map;
7+
use rustc_middle::hir::nested_filter;
8+
use rustc_middle::ty::TyCtxt;
9+
use rustc_resolve::rustdoc::span_of_fragments;
10+
use rustc_session::Session;
11+
use rustc_span::{Span, DUMMY_SP};
12+
13+
use super::Collector;
14+
use crate::clean::{types::AttributesExt, Attributes};
15+
use crate::html::markdown::{self, ErrorCodes};
16+
17+
pub(super) struct HirCollector<'a, 'hir, 'tcx> {
18+
pub(super) sess: &'a Session,
19+
pub(super) collector: &'a mut Collector,
20+
pub(super) map: Map<'hir>,
21+
pub(super) codes: ErrorCodes,
22+
pub(super) tcx: TyCtxt<'tcx>,
23+
}
24+
25+
impl<'a, 'hir, 'tcx> HirCollector<'a, 'hir, 'tcx> {
26+
pub(super) fn visit_testable<F: FnOnce(&mut Self)>(
27+
&mut self,
28+
name: String,
29+
def_id: LocalDefId,
30+
sp: Span,
31+
nested: F,
32+
) {
33+
let ast_attrs = self.tcx.hir().attrs(self.tcx.local_def_id_to_hir_id(def_id));
34+
if let Some(ref cfg) = ast_attrs.cfg(self.tcx, &FxHashSet::default()) {
35+
if !cfg.matches(&self.sess.psess, Some(self.tcx.features())) {
36+
return;
37+
}
38+
}
39+
40+
let has_name = !name.is_empty();
41+
if has_name {
42+
self.collector.names.push(name);
43+
}
44+
45+
// The collapse-docs pass won't combine sugared/raw doc attributes, or included files with
46+
// anything else, this will combine them for us.
47+
let attrs = Attributes::from_ast(ast_attrs);
48+
if let Some(doc) = attrs.opt_doc_value() {
49+
// Use the outermost invocation, so that doctest names come from where the docs were written.
50+
let span = ast_attrs
51+
.iter()
52+
.find(|attr| attr.doc_str().is_some())
53+
.map(|attr| attr.span.ctxt().outer_expn().expansion_cause().unwrap_or(attr.span))
54+
.unwrap_or(DUMMY_SP);
55+
self.collector.set_position(span);
56+
markdown::find_testable_code(
57+
&doc,
58+
self.collector,
59+
self.codes,
60+
self.collector.enable_per_target_ignores,
61+
Some(&crate::html::markdown::ExtraInfo::new(
62+
self.tcx,
63+
def_id.to_def_id(),
64+
span_of_fragments(&attrs.doc_strings).unwrap_or(sp),
65+
)),
66+
self.tcx.features().custom_code_classes_in_docs,
67+
);
68+
}
69+
70+
nested(self);
71+
72+
if has_name {
73+
self.collector.names.pop();
74+
}
75+
}
76+
}
77+
78+
impl<'a, 'hir, 'tcx> intravisit::Visitor<'hir> for HirCollector<'a, 'hir, 'tcx> {
79+
type NestedFilter = nested_filter::All;
80+
81+
fn nested_visit_map(&mut self) -> Self::Map {
82+
self.map
83+
}
84+
85+
fn visit_item(&mut self, item: &'hir hir::Item<'_>) {
86+
let name = match &item.kind {
87+
hir::ItemKind::Impl(impl_) => {
88+
rustc_hir_pretty::id_to_string(&self.map, impl_.self_ty.hir_id)
89+
}
90+
_ => item.ident.to_string(),
91+
};
92+
93+
self.visit_testable(name, item.owner_id.def_id, item.span, |this| {
94+
intravisit::walk_item(this, item);
95+
});
96+
}
97+
98+
fn visit_trait_item(&mut self, item: &'hir hir::TraitItem<'_>) {
99+
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
100+
intravisit::walk_trait_item(this, item);
101+
});
102+
}
103+
104+
fn visit_impl_item(&mut self, item: &'hir hir::ImplItem<'_>) {
105+
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
106+
intravisit::walk_impl_item(this, item);
107+
});
108+
}
109+
110+
fn visit_foreign_item(&mut self, item: &'hir hir::ForeignItem<'_>) {
111+
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
112+
intravisit::walk_foreign_item(this, item);
113+
});
114+
}
115+
116+
fn visit_variant(&mut self, v: &'hir hir::Variant<'_>) {
117+
self.visit_testable(v.ident.to_string(), v.def_id, v.span, |this| {
118+
intravisit::walk_variant(this, v);
119+
});
120+
}
121+
122+
fn visit_field_def(&mut self, f: &'hir hir::FieldDef<'_>) {
123+
self.visit_testable(f.ident.to_string(), f.def_id, f.span, |this| {
124+
intravisit::walk_field_def(this, f);
125+
});
126+
}
127+
}

0 commit comments

Comments
 (0)