1+ use rustc_ast:: ast:: AttrStyle ;
12use rustc_ast:: token:: { self , CommentKind , Token , TokenKind } ;
2- use rustc_ast:: util:: comments;
33use rustc_data_structures:: sync:: Lrc ;
44use rustc_errors:: { error_code, Applicability , DiagnosticBuilder , FatalError } ;
55use rustc_lexer:: Base ;
@@ -15,7 +15,7 @@ mod tokentrees;
1515mod unescape_error_reporting;
1616mod unicode_chars;
1717
18- use rustc_lexer:: unescape:: Mode ;
18+ use rustc_lexer:: { unescape:: Mode , DocStyle } ;
1919use unescape_error_reporting:: { emit_unescape_error, push_escaped_char} ;
2020
2121#[ derive( Clone , Debug ) ]
@@ -168,25 +168,23 @@ impl<'a> StringReader<'a> {
168168 /// symbols and runs additional validation.
169169 fn cook_lexer_token ( & self , token : rustc_lexer:: TokenKind , start : BytePos ) -> TokenKind {
170170 match token {
171- rustc_lexer:: TokenKind :: LineComment => {
172- let string = self . str_from ( start) ;
173- if let Some ( attr_style) = comments:: line_doc_comment_style ( string) {
174- self . forbid_bare_cr ( start, string, "bare CR not allowed in doc-comment" ) ;
175- // Opening delimiter of the length 3 is not included into the symbol.
176- token:: DocComment ( CommentKind :: Line , attr_style, Symbol :: intern ( & string[ 3 ..] ) )
177- } else {
178- token:: Comment
171+ rustc_lexer:: TokenKind :: LineComment { doc_style } => {
172+ match doc_style {
173+ Some ( doc_style) => {
174+ // Opening delimiter of the length 3 is not included into the symbol.
175+ let content_start = start + BytePos ( 3 ) ;
176+ let content = self . str_from ( content_start) ;
177+
178+ self . cook_doc_comment ( content_start, content, CommentKind :: Line , doc_style)
179+ }
180+ None => token:: Comment ,
179181 }
180182 }
181- rustc_lexer:: TokenKind :: BlockComment { terminated } => {
182- let string = self . str_from ( start) ;
183- let attr_style = comments:: block_doc_comment_style ( string, terminated) ;
184-
183+ rustc_lexer:: TokenKind :: BlockComment { doc_style, terminated } => {
185184 if !terminated {
186- let msg = if attr_style. is_some ( ) {
187- "unterminated block doc-comment"
188- } else {
189- "unterminated block comment"
185+ let msg = match doc_style {
186+ Some ( _) => "unterminated block doc-comment" ,
187+ None => "unterminated block comment" ,
190188 } ;
191189 let last_bpos = self . pos ;
192190 self . sess
@@ -199,18 +197,17 @@ impl<'a> StringReader<'a> {
199197 . emit ( ) ;
200198 FatalError . raise ( ) ;
201199 }
202-
203- if let Some ( attr_style) = attr_style {
204- self . forbid_bare_cr ( start, string, "bare CR not allowed in block doc-comment" ) ;
205- // Opening delimiter of the length 3 and closing delimiter of the length 2
206- // are not included into the symbol.
207- token:: DocComment (
208- CommentKind :: Block ,
209- attr_style,
210- Symbol :: intern ( & string[ 3 ..string. len ( ) - if terminated { 2 } else { 0 } ] ) ,
211- )
212- } else {
213- token:: Comment
200+ match doc_style {
201+ Some ( doc_style) => {
202+ // Opening delimiter of the length 3 and closing delimiter of the length 2
203+ // are not included into the symbol.
204+ let content_start = start + BytePos ( 3 ) ;
205+ let content_end = self . pos - BytePos ( if terminated { 2 } else { 0 } ) ;
206+ let content = self . str_from_to ( content_start, content_end) ;
207+
208+ self . cook_doc_comment ( content_start, content, CommentKind :: Block , doc_style)
209+ }
210+ None => token:: Comment ,
214211 }
215212 }
216213 rustc_lexer:: TokenKind :: Whitespace => token:: Whitespace ,
@@ -319,6 +316,34 @@ impl<'a> StringReader<'a> {
319316 }
320317 }
321318
319+ fn cook_doc_comment (
320+ & self ,
321+ content_start : BytePos ,
322+ content : & str ,
323+ comment_kind : CommentKind ,
324+ doc_style : DocStyle ,
325+ ) -> TokenKind {
326+ if content. contains ( '\r' ) {
327+ for ( idx, _) in content. char_indices ( ) . filter ( |& ( _, c) | c == '\r' ) {
328+ self . err_span_ (
329+ content_start + BytePos ( idx as u32 ) ,
330+ content_start + BytePos ( idx as u32 + 1 ) ,
331+ match comment_kind {
332+ CommentKind :: Line => "bare CR not allowed in doc-comment" ,
333+ CommentKind :: Block => "bare CR not allowed in block doc-comment" ,
334+ } ,
335+ ) ;
336+ }
337+ }
338+
339+ let attr_style = match doc_style {
340+ DocStyle :: Outer => AttrStyle :: Outer ,
341+ DocStyle :: Inner => AttrStyle :: Inner ,
342+ } ;
343+
344+ token:: DocComment ( comment_kind, attr_style, Symbol :: intern ( content) )
345+ }
346+
322347 fn cook_lexer_literal (
323348 & self ,
324349 start : BytePos ,
@@ -472,17 +497,6 @@ impl<'a> StringReader<'a> {
472497 & self . src [ self . src_index ( start) ..self . src_index ( end) ]
473498 }
474499
475- fn forbid_bare_cr ( & self , start : BytePos , s : & str , errmsg : & str ) {
476- let mut idx = 0 ;
477- loop {
478- idx = match s[ idx..] . find ( '\r' ) {
479- None => break ,
480- Some ( it) => idx + it + 1 ,
481- } ;
482- self . err_span_ ( start + BytePos ( idx as u32 - 1 ) , start + BytePos ( idx as u32 ) , errmsg) ;
483- }
484- }
485-
486500 fn report_raw_str_error ( & self , start : BytePos , opt_err : Option < RawStrError > ) {
487501 match opt_err {
488502 Some ( RawStrError :: InvalidStarter { bad_char } ) => {
0 commit comments