diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2023-10-20 02:54:45 +0000 | 
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2023-10-20 02:54:45 +0000 | 
| commit | 20de5c762db2a1fa637c25bd76772490eecc7a6f (patch) | |
| tree | 46d9c6ebf07c1ea86555ed1208fb8231b68b6cc4 /compiler/rustc_parse/src | |
| parent | cc705b801236d064260bb67b3a0a25e4747fa7ec (diff) | |
| download | rust-20de5c762db2a1fa637c25bd76772490eecc7a6f.tar.gz rust-20de5c762db2a1fa637c25bd76772490eecc7a6f.zip | |
Move where doc comment meant as comment check
The new place makes more sense and covers more cases beyond individual statements. ``` error: expected one of `.`, `;`, `?`, `else`, or an operator, found doc comment `//!foo --> $DIR/doc-comment-in-stmt.rs:25:22 | LL | let y = x.max(1) //!foo | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected one of `.`, `;`, `?`, `else`, or an operator | help: add a space before `!` to write a regular comment | LL | let y = x.max(1) // !foo | + ``` Fix #65329.
Diffstat (limited to 'compiler/rustc_parse/src')
| -rw-r--r-- | compiler/rustc_parse/src/parser/diagnostics.rs | 22 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/stmt.rs | 17 | 
2 files changed, 21 insertions, 18 deletions
| diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 75e3393aa96..9f8361a4b1e 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -39,7 +39,7 @@ use rustc_errors::{ use rustc_session::errors::ExprParenthesesNeeded; use rustc_span::source_map::Spanned; use rustc_span::symbol::{kw, sym, Ident}; -use rustc_span::{Span, SpanSnippetError, Symbol, DUMMY_SP}; +use rustc_span::{BytePos, Span, SpanSnippetError, Symbol, DUMMY_SP}; use std::mem::take; use std::ops::{Deref, DerefMut}; use thin_vec::{thin_vec, ThinVec}; @@ -648,6 +648,26 @@ impl<'a> Parser<'a> { ); } + if let token::DocComment(kind, style, _) = self.token.kind { + // We have something like `expr //!val` where the user likely meant `expr // !val` + let pos = self.token.span.lo() + BytePos(2); + let span = self.token.span.with_lo(pos).with_hi(pos); + err.span_suggestion_verbose( + span, + format!( + "add a space before {} to write a regular comment", + match (kind, style) { + (token::CommentKind::Line, ast::AttrStyle::Inner) => "`!`", + (token::CommentKind::Block, ast::AttrStyle::Inner) => "`!`", + (token::CommentKind::Line, ast::AttrStyle::Outer) => "the last `/`", + (token::CommentKind::Block, ast::AttrStyle::Outer) => "the last `*`", + }, + ), + " ".to_string(), + Applicability::MachineApplicable, + ); + } + // Add suggestion for a missing closing angle bracket if '>' is included in expected_tokens // there are unclosed angle brackets if self.unmatched_angle_bracket_count > 0 diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index e2f59cb2071..8e3c4568694 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -632,23 +632,6 @@ impl<'a> Parser<'a> { // Recover from parser, skip type error to avoid extra errors. Ok(true) => true, Err(mut e) => { - if let TokenKind::DocComment(..) = self.token.kind - && let Ok(snippet) = self.span_to_snippet(self.token.span) - { - let sp = self.token.span; - let marker = &snippet[..3]; - let (comment_marker, doc_comment_marker) = marker.split_at(2); - - e.span_suggestion( - sp.with_hi(sp.lo() + BytePos(marker.len() as u32)), - format!( - "add a space before `{doc_comment_marker}` to use a regular comment", - ), - format!("{comment_marker} {doc_comment_marker}"), - Applicability::MaybeIncorrect, - ); - } - if self.recover_colon_as_semi() { // recover_colon_as_semi has already emitted a nicer error. e.delay_as_bug(); | 
