Skip to content

Add error code for inner doc error #71214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/librustc_error_codes/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions src/librustc_error_codes/error_codes/E0753.md
Original file line number Diff line number Diff line change
@@ -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() {
// ...
}
```
12 changes: 9 additions & 3 deletions src/librustc_parse/parser/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/parser/doc-comment-in-if-statement.stderr
Original file line number Diff line number Diff line change
@@ -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 /*!*/ {}
Expand All @@ -17,3 +17,4 @@ LL | if true /*!*/ {}

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0753`.
3 changes: 2 additions & 1 deletion src/test/ui/parser/issue-30318.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: expected outer doc comment
error[E0753]: expected outer doc comment
--> $DIR/issue-30318.rs:3:1
|
LL | //! Misplaced comment...
Expand All @@ -8,3 +8,4 @@ LL | //! Misplaced comment...

error: aborting due to previous error

For more information about this error, try `rustc --explain E0753`.