]> git.lizzy.rs Git - rust.git/commitdiff
Add error code to inner doc comment attribute error
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Wed, 22 Apr 2020 09:08:50 +0000 (11:08 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Wed, 22 Apr 2020 09:08:50 +0000 (11:08 +0200)
src/librustc_error_codes/error_codes.rs
src/librustc_error_codes/error_codes/E0753.md [new file with mode: 0644]
src/librustc_parse/parser/attr.rs

index bc04809eaa1dfd59e97927d5a90eedf1224dda9b..9f4b5fd85fd4d381f275670e413ea119fcaf4557 100644 (file)
 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 (file)
index 0000000..a69da96
--- /dev/null
@@ -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() {
+    // ...
+}
+```
index b56dd30739dae76ee75262c76e26663779ab7362..803f14a2a228a6789915694fe27ee9ec38541dbb 100644 (file)
@@ -4,7 +4,7 @@
 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 @@ pub(super) fn parse_outer_attributes(&mut self) -> PResult<'a, Vec<ast::Attribut
             } 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();
                 }