Skip to content

Commit 516010b

Browse files
camelidGuillaumeGomez
authored andcommitted
Start moving format-specific code into doctest submodule
1 parent f9e12ef commit 516010b

File tree

3 files changed

+136
-120
lines changed

3 files changed

+136
-120
lines changed

src/librustdoc/doctest.rs

+9-120
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::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 {
@@ -1293,116 +1293,5 @@ impl DoctestVisitor for Vec<usize> {
12931293
}
12941294
}
12951295

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

src/librustdoc/doctest/markdown.rs

+1
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

+126
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)