about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2020-04-22 12:18:34 +0200
committerGitHub <noreply@github.com>2020-04-22 12:18:34 +0200
commit372d37b106f4385e28eeeffb60bdfc666ef9f59d (patch)
tree8416ff6e34f75d26d5a99780fcf855a84dd88c86 /src
parent7b1ce6e98ddb97bc70281a6dbaf3c9b2c938917a (diff)
parent038f5b74336f310f495af0c15e4a2b4d0750cfee (diff)
downloadrust-372d37b106f4385e28eeeffb60bdfc666ef9f59d.tar.gz
rust-372d37b106f4385e28eeeffb60bdfc666ef9f59d.zip
Rollup merge of #71214 - GuillaumeGomez:add-error-code-inner-doc-error, r=Dylan-DPC
Add error code for inner doc error

r? @Dylan-DPC

cc @oli-obk
Diffstat (limited to 'src')
-rw-r--r--src/librustc_error_codes/error_codes.rs1
-rw-r--r--src/librustc_error_codes/error_codes/E0753.md31
-rw-r--r--src/librustc_parse/parser/attr.rs12
-rw-r--r--src/test/ui/parser/doc-comment-in-if-statement.stderr3
-rw-r--r--src/test/ui/parser/issue-30318.stderr3
5 files changed, 45 insertions, 5 deletions
diff --git a/src/librustc_error_codes/error_codes.rs b/src/librustc_error_codes/error_codes.rs
index bc04809eaa1..9f4b5fd85fd 100644
--- a/src/librustc_error_codes/error_codes.rs
+++ b/src/librustc_error_codes/error_codes.rs
@@ -432,6 +432,7 @@ E0749: include_str!("./error_codes/E0749.md"),
 E0750: include_str!("./error_codes/E0750.md"),
 E0751: include_str!("./error_codes/E0751.md"),
 E0752: include_str!("./error_codes/E0752.md"),
+E0753: include_str!("./error_codes/E0753.md"),
 ;
 //  E0006, // merged with E0005
 //  E0008, // cannot bind by-move into a pattern guard
diff --git a/src/librustc_error_codes/error_codes/E0753.md b/src/librustc_error_codes/error_codes/E0753.md
new file mode 100644
index 00000000000..a69da964aee
--- /dev/null
+++ b/src/librustc_error_codes/error_codes/E0753.md
@@ -0,0 +1,31 @@
+An inner doc comment was used in an invalid context.
+
+Erroneous code example:
+
+```compile_fail,E0753
+fn foo() {}
+//! foo
+// ^ error!
+fn main() {}
+```
+
+Inner document can only be used before items. For example:
+
+```
+//! A working comment applied to the module!
+fn foo() {
+    //! Another working comment!
+}
+fn main() {}
+```
+
+In case you want to document the item following the doc comment, you might want
+to use outer doc comment:
+
+```
+/// I am an outer doc comment
+#[doc = "I am also an outer doc comment!"]
+fn foo() {
+    // ...
+}
+```
diff --git a/src/librustc_parse/parser/attr.rs b/src/librustc_parse/parser/attr.rs
index b56dd30739d..803f14a2a22 100644
--- a/src/librustc_parse/parser/attr.rs
+++ b/src/librustc_parse/parser/attr.rs
@@ -4,7 +4,7 @@ use rustc_ast::attr;
 use rustc_ast::token::{self, Nonterminal};
 use rustc_ast::util::comments;
 use rustc_ast_pretty::pprust;
-use rustc_errors::PResult;
+use rustc_errors::{error_code, PResult};
 use rustc_span::{Span, Symbol};
 
 use log::debug;
@@ -50,10 +50,16 @@ impl<'a> Parser<'a> {
             } else if let token::DocComment(s) = self.token.kind {
                 let attr = self.mk_doc_comment(s);
                 if attr.style != ast::AttrStyle::Outer {
-                    self.struct_span_err(self.token.span, "expected outer doc comment")
+                    self.sess
+                        .span_diagnostic
+                        .struct_span_err_with_code(
+                            self.token.span,
+                            "expected outer doc comment",
+                            error_code!(E0753),
+                        )
                         .note(
                             "inner doc comments like this (starting with \
-                              `//!` or `/*!`) can only appear before items",
+                             `//!` or `/*!`) can only appear before items",
                         )
                         .emit();
                 }
diff --git a/src/test/ui/parser/doc-comment-in-if-statement.stderr b/src/test/ui/parser/doc-comment-in-if-statement.stderr
index af21b78733f..be52a0afd46 100644
--- a/src/test/ui/parser/doc-comment-in-if-statement.stderr
+++ b/src/test/ui/parser/doc-comment-in-if-statement.stderr
@@ -1,4 +1,4 @@
-error: expected outer doc comment
+error[E0753]: expected outer doc comment
   --> $DIR/doc-comment-in-if-statement.rs:2:13
    |
 LL |     if true /*!*/ {}
@@ -17,3 +17,4 @@ LL |     if true /*!*/ {}
 
 error: aborting due to 2 previous errors
 
+For more information about this error, try `rustc --explain E0753`.
diff --git a/src/test/ui/parser/issue-30318.stderr b/src/test/ui/parser/issue-30318.stderr
index 489451bb5dc..b3a27f19851 100644
--- a/src/test/ui/parser/issue-30318.stderr
+++ b/src/test/ui/parser/issue-30318.stderr
@@ -1,4 +1,4 @@
-error: expected outer doc comment
+error[E0753]: expected outer doc comment
   --> $DIR/issue-30318.rs:3:1
    |
 LL | //! Misplaced comment...
@@ -8,3 +8,4 @@ LL | //! Misplaced comment...
 
 error: aborting due to previous error
 
+For more information about this error, try `rustc --explain E0753`.