1+ mod markdown;
2+ mod rust;
3+
14use rustc_ast as ast;
25use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
36use rustc_data_structures:: sync:: Lrc ;
47use rustc_errors:: emitter:: stderr_destination;
58use 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 ;
811use rustc_interface:: interface;
9- use rustc_middle:: hir:: map:: Map ;
10- use rustc_middle:: hir:: nested_filter;
11- use rustc_middle:: ty:: TyCtxt ;
1212use rustc_parse:: maybe_new_parser_from_source_str;
1313use rustc_parse:: parser:: attr:: InnerAttrPolicy ;
14- use rustc_resolve:: rustdoc:: span_of_fragments;
1514use rustc_session:: config:: { self , CrateType , ErrorOutputType } ;
15+ use rustc_session:: lint;
1616use rustc_session:: parse:: ParseSess ;
17- use rustc_session:: { lint, Session } ;
1817use rustc_span:: edition:: Edition ;
1918use rustc_span:: source_map:: SourceMap ;
2019use rustc_span:: symbol:: sym;
@@ -33,11 +32,12 @@ use std::sync::{Arc, Mutex};
3332
3433use tempfile:: { Builder as TempFileBuilder , TempDir } ;
3534
36- use crate :: clean:: { types:: AttributesExt , Attributes } ;
3735use crate :: config:: Options as RustdocOptions ;
38- use crate :: html:: markdown:: { self , ErrorCodes , Ignore , LangString } ;
36+ use crate :: html:: markdown:: { ErrorCodes , Ignore , LangString } ;
3937use 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 ) ]
4343pub ( 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) ]
14101298mod tests;
0 commit comments