The following code (playground link):
fn main() {
if true /*!*/ {}
}
Generates this error:
Compiling playground v0.0.1 (/playground)
error: expected `{`, found `/*!*/`
--> src/main.rs:2:13
|
2 | if true /*!*/ {}
| -- ^^^^^
| |
| this `if` statement has a condition, but no block
Deleting the exclamation mark or replacing it with another symbol seems to make the error go away.
The exclamation point causes the comment to be parsed as a documentation comment (same as //!) which have special meaning, so I'd say this is an error in the code. The compiler error message could perhaps be made better, for example by mentioning that this location is illegal for a documentation comment.
But a comment is a comment, right? I would like to place comments wherever I want, even if they are doc comments. I would appreciate a warning maybe, but not an error at all!
@hellow554
But a comment is a comment, right?
right except doc comments, a doc comment is an attribute in rust actually.
cc https://doc.rust-lang.org/reference/comments.html#doc-comments
cc https://doc.rust-lang.org/reference/attributes.html
(btw, doc comment is kind of special for tools like rustdoc/rustfmt, arbitrary doc comments will make these tools very twisted.)
I'd be in favor of having the error message changed to:
error: expected `{`, found doc comment
Changing how a DocComment token is printed is easy, just modify this line:
Into something like:
token::DocComment(s) => format!("doc comment: {}", s),
But since doc comments are only allowed in certain places, it would be nice to show an error with a clarification. And it looks like that's already the case, this line:
let x = /*!*/ 0;
Shows the following error:
error: expected outer doc comment
--> src/main.rs:2:13
|
2 | let x = /*!*/ 0;
| ^^^^^
|
= note: inner doc comments like this (starting with `//!` or `/*!`) can only appear before items
error: aborting due to previous error
So I'm not sure why it doesn't work in this case.
Most helpful comment
The exclamation point causes the comment to be parsed as a documentation comment (same as
//!) which have special meaning, so I'd say this is an error in the code. The compiler error message could perhaps be made better, for example by mentioning that this location is illegal for a documentation comment.