Skip to content

Commit 3390ff9

Browse files
Add error code to inner doc comment attribute error
1 parent 2dc5b60 commit 3390ff9

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/librustc_error_codes/error_codes.rs

Lines changed: 1 addition & 0 deletions
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
Lines changed: 31 additions & 0 deletions
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

Lines changed: 9 additions & 3 deletions
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
}

0 commit comments

Comments
 (0)