Skip to content

Commit b50b162

Browse files
committed
Be more strict about doc comments
Previously, `//// foo` and `/*** foo ***/` were accepted as doc comments. This changes that, so that only `/// foo` and `/** foo ***/` are accepted. This confuses many newcomers and it seems weird. Also update the manual for these changes, and modernify the EBNF for comments. Closes #10638
1 parent e147a09 commit b50b162

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

doc/rust.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,19 @@ Some productions are defined by exclusion of particular Unicode characters:
153153
~~~~ {.ebnf .gram}
154154
comment : block_comment | line_comment ;
155155
block_comment : "/*" block_comment_body * '*' + '/' ;
156-
block_comment_body : non_star * | '*' + non_slash_or_star ;
156+
block_comment_body : (block_comment | character) * ;
157157
line_comment : "//" non_eol * ;
158158
~~~~
159159

160160
Comments in Rust code follow the general C++ style of line and block-comment forms,
161161
with no nesting of block-comment delimiters.
162162

163-
Line comments beginning with _three_ slashes (`///`),
164-
and block comments beginning with a repeated asterisk in the block-open sequence (`/**`),
165-
are interpreted as a special syntax for `doc` [attributes](#attributes).
166-
That is, they are equivalent to writing `#[doc "..."]` around the comment's text.
163+
Line comments beginning with exactly _three_ slashes (`///`), and block
164+
comments beginning with a exactly one repeated asterisk in the block-open
165+
sequence (`/**`), are interpreted as a special syntax for `doc`
166+
[attributes](#attributes). That is, they are equivalent to writing
167+
`#[doc="..."]` around the body of the comment (this includes the comment
168+
characters themselves, ie `/// Foo` turns into `#[doc="/// Foo"]`).
167169

168170
Non-doc comments are interpreted as a form of whitespace.
169171

src/etc/vim/syntax/rust.vim

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ syn match rustCharacter /'\([^'\\]\|\\\([nrt0\\'"]\|x\x\{2}\|u\x\{4}\|U\x\{8
187187

188188
syn region rustCommentML start="/\*" end="\*/" contains=rustTodo
189189
syn region rustComment start="//" end="$" contains=rustTodo keepend
190-
syn region rustCommentMLDoc start="/\*\%(!\|\*/\@!\)" end="\*/" contains=rustTodo
191-
syn region rustCommentDoc start="//[/!]" end="$" contains=rustTodo keepend
190+
syn region rustCommentMLDoc start="/\*\%(!\|\*[*/]\@!\)" end="\*/" contains=rustTodo
191+
syn region rustCommentDoc start="//\%(//\@!\|!\)" end="$" contains=rustTodo keepend
192192

193193
syn keyword rustTodo contained TODO FIXME XXX NB NOTE
194194

src/libsyntax/parse/lexer.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,7 @@ fn consume_whitespace_and_comments(rdr: @mut StringReader)
317317
}
318318

319319
pub fn is_line_non_doc_comment(s: &str) -> bool {
320-
let s = s.trim_right();
321-
s.len() > 3 && s.chars().all(|ch| ch == '/')
320+
s.starts_with("////")
322321
}
323322

324323
// PRECONDITION: rdr.curr is not whitespace
@@ -378,8 +377,7 @@ fn consume_any_line_comment(rdr: @mut StringReader)
378377
}
379378

380379
pub fn is_block_non_doc_comment(s: &str) -> bool {
381-
assert!(s.len() >= 1u);
382-
s.slice(1u, s.len() - 1u).chars().all(|ch| ch == '*')
380+
s.starts_with("/***")
383381
}
384382

385383
// might return a sugared-doc-attr

src/test/run-pass/issue-10638.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn main() {
12+
//// I am not a doc comment!
13+
////////////////// still not a doc comment
14+
/////**** nope, me neither */
15+
/*** And neither am I! */
16+
5;
17+
/*****! certainly not I */
18+
}

0 commit comments

Comments
 (0)