Skip to content

Commit 372d37b

Browse files
authored
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
2 parents 7b1ce6e + 038f5b7 commit 372d37b

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

src/librustc_error_codes/error_codes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ E0749: include_str!("./error_codes/E0749.md"),
432432
E0750: include_str!("./error_codes/E0750.md"),
433433
E0751: include_str!("./error_codes/E0751.md"),
434434
E0752: include_str!("./error_codes/E0752.md"),
435+
E0753: include_str!("./error_codes/E0753.md"),
435436
;
436437
// E0006, // merged with E0005
437438
// E0008, // cannot bind by-move into a pattern guard
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
An inner doc comment was used in an invalid context.
2+
3+
Erroneous code example:
4+
5+
```compile_fail,E0753
6+
fn foo() {}
7+
//! foo
8+
// ^ error!
9+
fn main() {}
10+
```
11+
12+
Inner document can only be used before items. For example:
13+
14+
```
15+
//! A working comment applied to the module!
16+
fn foo() {
17+
//! Another working comment!
18+
}
19+
fn main() {}
20+
```
21+
22+
In case you want to document the item following the doc comment, you might want
23+
to use outer doc comment:
24+
25+
```
26+
/// I am an outer doc comment
27+
#[doc = "I am also an outer doc comment!"]
28+
fn foo() {
29+
// ...
30+
}
31+
```

src/librustc_parse/parser/attr.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_ast::attr;
44
use rustc_ast::token::{self, Nonterminal};
55
use rustc_ast::util::comments;
66
use rustc_ast_pretty::pprust;
7-
use rustc_errors::PResult;
7+
use rustc_errors::{error_code, PResult};
88
use rustc_span::{Span, Symbol};
99

1010
use log::debug;
@@ -50,10 +50,16 @@ impl<'a> Parser<'a> {
5050
} else if let token::DocComment(s) = self.token.kind {
5151
let attr = self.mk_doc_comment(s);
5252
if attr.style != ast::AttrStyle::Outer {
53-
self.struct_span_err(self.token.span, "expected outer doc comment")
53+
self.sess
54+
.span_diagnostic
55+
.struct_span_err_with_code(
56+
self.token.span,
57+
"expected outer doc comment",
58+
error_code!(E0753),
59+
)
5460
.note(
5561
"inner doc comments like this (starting with \
56-
`//!` or `/*!`) can only appear before items",
62+
`//!` or `/*!`) can only appear before items",
5763
)
5864
.emit();
5965
}

src/test/ui/parser/doc-comment-in-if-statement.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: expected outer doc comment
1+
error[E0753]: expected outer doc comment
22
--> $DIR/doc-comment-in-if-statement.rs:2:13
33
|
44
LL | if true /*!*/ {}
@@ -17,3 +17,4 @@ LL | if true /*!*/ {}
1717

1818
error: aborting due to 2 previous errors
1919

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

src/test/ui/parser/issue-30318.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: expected outer doc comment
1+
error[E0753]: expected outer doc comment
22
--> $DIR/issue-30318.rs:3:1
33
|
44
LL | //! Misplaced comment...
@@ -8,3 +8,4 @@ LL | //! Misplaced comment...
88

99
error: aborting due to previous error
1010

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

0 commit comments

Comments
 (0)