Skip to content

Commit 68193a3

Browse files
committed
Start moving format-specific code into doctest submodule
1 parent 1518bf0 commit 68193a3

File tree

3 files changed

+136
-120
lines changed

3 files changed

+136
-120
lines changed

src/librustdoc/doctest.rs

Lines changed: 9 additions & 120 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,116 +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-
);
1347-
}
1348-
1349-
nested(self);
1350-
1351-
if has_name {
1352-
self.collector.names.pop();
1353-
}
1354-
}
1355-
}
1356-
1357-
impl<'a, 'hir, 'tcx> intravisit::Visitor<'hir> for HirCollector<'a, 'hir, 'tcx> {
1358-
type NestedFilter = nested_filter::All;
1359-
1360-
fn nested_visit_map(&mut self) -> Self::Map {
1361-
self.map
1362-
}
1363-
1364-
fn visit_item(&mut self, item: &'hir hir::Item<'_>) {
1365-
let name = match &item.kind {
1366-
hir::ItemKind::Impl(impl_) => {
1367-
rustc_hir_pretty::id_to_string(&self.map, impl_.self_ty.hir_id)
1368-
}
1369-
_ => item.ident.to_string(),
1370-
};
1371-
1372-
self.visit_testable(name, item.owner_id.def_id, item.span, |this| {
1373-
intravisit::walk_item(this, item);
1374-
});
1375-
}
1376-
1377-
fn visit_trait_item(&mut self, item: &'hir hir::TraitItem<'_>) {
1378-
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
1379-
intravisit::walk_trait_item(this, item);
1380-
});
1381-
}
1382-
1383-
fn visit_impl_item(&mut self, item: &'hir hir::ImplItem<'_>) {
1384-
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
1385-
intravisit::walk_impl_item(this, item);
1386-
});
1387-
}
1388-
1389-
fn visit_foreign_item(&mut self, item: &'hir hir::ForeignItem<'_>) {
1390-
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
1391-
intravisit::walk_foreign_item(this, item);
1392-
});
1393-
}
1394-
1395-
fn visit_variant(&mut self, v: &'hir hir::Variant<'_>) {
1396-
self.visit_testable(v.ident.to_string(), v.def_id, v.span, |this| {
1397-
intravisit::walk_variant(this, v);
1398-
});
1399-
}
1400-
1401-
fn visit_field_def(&mut self, f: &'hir hir::FieldDef<'_>) {
1402-
self.visit_testable(f.ident.to_string(), f.def_id, f.span, |this| {
1403-
intravisit::walk_field_def(this, f);
1404-
});
1405-
}
1406-
}
1407-
14081297
#[cfg(test)]
14091298
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: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
);
67+
}
68+
69+
nested(self);
70+
71+
if has_name {
72+
self.collector.names.pop();
73+
}
74+
}
75+
}
76+
77+
impl<'a, 'hir, 'tcx> intravisit::Visitor<'hir> for HirCollector<'a, 'hir, 'tcx> {
78+
type NestedFilter = nested_filter::All;
79+
80+
fn nested_visit_map(&mut self) -> Self::Map {
81+
self.map
82+
}
83+
84+
fn visit_item(&mut self, item: &'hir hir::Item<'_>) {
85+
let name = match &item.kind {
86+
hir::ItemKind::Impl(impl_) => {
87+
rustc_hir_pretty::id_to_string(&self.map, impl_.self_ty.hir_id)
88+
}
89+
_ => item.ident.to_string(),
90+
};
91+
92+
self.visit_testable(name, item.owner_id.def_id, item.span, |this| {
93+
intravisit::walk_item(this, item);
94+
});
95+
}
96+
97+
fn visit_trait_item(&mut self, item: &'hir hir::TraitItem<'_>) {
98+
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
99+
intravisit::walk_trait_item(this, item);
100+
});
101+
}
102+
103+
fn visit_impl_item(&mut self, item: &'hir hir::ImplItem<'_>) {
104+
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
105+
intravisit::walk_impl_item(this, item);
106+
});
107+
}
108+
109+
fn visit_foreign_item(&mut self, item: &'hir hir::ForeignItem<'_>) {
110+
self.visit_testable(item.ident.to_string(), item.owner_id.def_id, item.span, |this| {
111+
intravisit::walk_foreign_item(this, item);
112+
});
113+
}
114+
115+
fn visit_variant(&mut self, v: &'hir hir::Variant<'_>) {
116+
self.visit_testable(v.ident.to_string(), v.def_id, v.span, |this| {
117+
intravisit::walk_variant(this, v);
118+
});
119+
}
120+
121+
fn visit_field_def(&mut self, f: &'hir hir::FieldDef<'_>) {
122+
self.visit_testable(f.ident.to_string(), f.def_id, f.span, |this| {
123+
intravisit::walk_field_def(this, f);
124+
});
125+
}
126+
}

0 commit comments

Comments
 (0)