1
+ mod markdown;
2
+ mod rust;
3
+
1
4
use rustc_ast as ast;
2
5
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
3
6
use rustc_data_structures:: sync:: Lrc ;
4
7
use rustc_errors:: emitter:: stderr_destination;
5
8
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 ;
8
11
use rustc_interface:: interface;
9
- use rustc_middle:: hir:: map:: Map ;
10
- use rustc_middle:: hir:: nested_filter;
11
- use rustc_middle:: ty:: TyCtxt ;
12
12
use rustc_parse:: new_parser_from_source_str;
13
13
use rustc_parse:: parser:: attr:: InnerAttrPolicy ;
14
- use rustc_resolve:: rustdoc:: span_of_fragments;
15
14
use rustc_session:: config:: { self , CrateType , ErrorOutputType } ;
15
+ use rustc_session:: lint;
16
16
use rustc_session:: parse:: ParseSess ;
17
- use rustc_session:: { lint, Session } ;
18
17
use rustc_span:: edition:: Edition ;
19
18
use rustc_span:: source_map:: SourceMap ;
20
19
use rustc_span:: symbol:: sym;
@@ -33,11 +32,12 @@ use std::sync::{Arc, Mutex};
33
32
34
33
use tempfile:: { Builder as TempFileBuilder , TempDir } ;
35
34
36
- use crate :: clean:: { types:: AttributesExt , Attributes } ;
37
35
use crate :: config:: Options as RustdocOptions ;
38
- use crate :: html:: markdown:: { self , ErrorCodes , Ignore , LangString } ;
36
+ use crate :: html:: markdown:: { ErrorCodes , Ignore , LangString } ;
39
37
use crate :: lint:: init_lints;
40
38
39
+ use self :: rust:: HirCollector ;
40
+
41
41
/// Options that apply to all doctests in a crate or Markdown file (for `rustdoc foo.md`).
42
42
#[ derive( Clone , Default ) ]
43
43
pub ( crate ) struct GlobalTestOptions {
@@ -1293,116 +1293,5 @@ impl DoctestVisitor for Vec<usize> {
1293
1293
}
1294
1294
}
1295
1295
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
-
1407
1296
#[ cfg( test) ]
1408
1297
mod tests;
0 commit comments