@@ -370,32 +370,22 @@ impl<I: IntoIterator<Item = ast::NestedMetaItem>> NestedAttributesExt for I {
370
370
/// information can be given when a doctest fails. Sugared doc comments and "raw" doc comments are
371
371
/// kept separate because of issue #42760.
372
372
#[ derive( Clone , PartialEq , Eq , Debug , Hash ) ]
373
- pub enum DocFragment {
373
+ pub struct DocFragment {
374
+ pub line : usize ,
375
+ pub span : rustc_span:: Span ,
376
+ pub doc : String ,
377
+ pub kind : DocFragmentKind ,
378
+ }
379
+
380
+ #[ derive( Clone , PartialEq , Eq , Debug , Hash ) ]
381
+ pub enum DocFragmentKind {
374
382
/// A doc fragment created from a `///` or `//!` doc comment.
375
- SugaredDoc ( usize , rustc_span :: Span , String ) ,
383
+ SugaredDoc ,
376
384
/// A doc fragment created from a "raw" `#[doc=""]` attribute.
377
- RawDoc ( usize , rustc_span :: Span , String ) ,
385
+ RawDoc ,
378
386
/// A doc fragment created from a `#[doc(include="filename")]` attribute. Contains both the
379
387
/// given filename and the file contents.
380
- Include ( usize , rustc_span:: Span , String , String ) ,
381
- }
382
-
383
- impl DocFragment {
384
- pub fn as_str ( & self ) -> & str {
385
- match * self {
386
- DocFragment :: SugaredDoc ( _, _, ref s) => & s[ ..] ,
387
- DocFragment :: RawDoc ( _, _, ref s) => & s[ ..] ,
388
- DocFragment :: Include ( _, _, _, ref s) => & s[ ..] ,
389
- }
390
- }
391
-
392
- pub fn span ( & self ) -> rustc_span:: Span {
393
- match * self {
394
- DocFragment :: SugaredDoc ( _, span, _)
395
- | DocFragment :: RawDoc ( _, span, _)
396
- | DocFragment :: Include ( _, span, _, _) => span,
397
- }
398
- }
388
+ Include { filename : String } ,
399
389
}
400
390
401
391
impl < ' a > FromIterator < & ' a DocFragment > for String {
@@ -407,12 +397,7 @@ impl<'a> FromIterator<&'a DocFragment> for String {
407
397
if !acc. is_empty ( ) {
408
398
acc. push ( '\n' ) ;
409
399
}
410
- match * frag {
411
- DocFragment :: SugaredDoc ( _, _, ref docs)
412
- | DocFragment :: RawDoc ( _, _, ref docs)
413
- | DocFragment :: Include ( _, _, _, ref docs) => acc. push_str ( docs) ,
414
- }
415
-
400
+ acc. push_str ( & frag. doc ) ;
416
401
acc
417
402
} )
418
403
}
@@ -547,15 +532,15 @@ impl Attributes {
547
532
. filter_map ( |attr| {
548
533
if let Some ( value) = attr. doc_str ( ) {
549
534
let value = beautify_doc_string ( value) ;
550
- let mk_fragment : fn ( _ , _ , _ ) -> _ = if attr. is_doc_comment ( ) {
551
- DocFragment :: SugaredDoc
535
+ let kind = if attr. is_doc_comment ( ) {
536
+ DocFragmentKind :: SugaredDoc
552
537
} else {
553
- DocFragment :: RawDoc
538
+ DocFragmentKind :: RawDoc
554
539
} ;
555
540
556
541
let line = doc_line;
557
542
doc_line += value. lines ( ) . count ( ) ;
558
- doc_strings. push ( mk_fragment ( line, attr. span , value) ) ;
543
+ doc_strings. push ( DocFragment { line, span : attr. span , doc : value, kind } ) ;
559
544
560
545
if sp. is_none ( ) {
561
546
sp = Some ( attr. span ) ;
@@ -575,9 +560,12 @@ impl Attributes {
575
560
{
576
561
let line = doc_line;
577
562
doc_line += contents. lines ( ) . count ( ) ;
578
- doc_strings. push ( DocFragment :: Include (
579
- line, attr. span , filename, contents,
580
- ) ) ;
563
+ doc_strings. push ( DocFragment {
564
+ line,
565
+ span : attr. span ,
566
+ doc : contents,
567
+ kind : DocFragmentKind :: Include { filename } ,
568
+ } ) ;
581
569
}
582
570
}
583
571
}
@@ -621,7 +609,7 @@ impl Attributes {
621
609
/// Finds the `doc` attribute as a NameValue and returns the corresponding
622
610
/// value found.
623
611
pub fn doc_value ( & self ) -> Option < & str > {
624
- self . doc_strings . first ( ) . map ( |s| s. as_str ( ) )
612
+ self . doc_strings . first ( ) . map ( |s| s. doc . as_str ( ) )
625
613
}
626
614
627
615
/// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
0 commit comments