-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Specific error message for missplaced doc comments #33922
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @sfackler (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
@@ -12,5 +12,6 @@ | |||
|
|||
extern { | |||
/// hi | |||
//~^ ERROR expected item after doc comment | |||
//~^^ HELP May be you wanted a comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Maybe"
Thanks for your PR! You need to update some tests which failed (like |
@GuillaumeGomez I just pushed a new commit with the requested changes. The tests are running ATM. |
Please squash your commits. Once done and travis says it's ok, I'll r+ it. |
I'm away for the weekend, I'll do it on Monday night. |
@GuillaumeGomez I have rebased+push. Travis is running now, but I don't see why it'd fail :) |
Better sure than sorry! Thanks for this PR. :) @bors: r+ |
📌 Commit ffd9efc has been approved by |
⌛ Testing commit ffd9efc with merge 91dd180... |
💔 Test failed - auto-win-gnu-32-opt-rustbuild |
@bors: retry On Tue, May 31, 2016 at 3:07 AM, bors notifications@github.com wrote:
|
Of course!
My pleasure. I see the build failure in win, and I don't see how it was affected by my change. Do you have any ideas on what caused this? ---- sync::atomic::AtomicU8::new_0 stdout ----
error: could not exec the linker `gcc`: The filename or extension is too long. (os error 206)
thread 'sync::atomic::AtomicU8::new_0' panicked at 'Box<Any>', C:\bot\slave\auto-win-gnu-32-opt-rustbuild\build\src\librustc\session/mod.rs:171
note: Run with `RUST_BACKTRACE=1` for a backtrace.
thread 'sync::atomic::AtomicU8::new_0' panicked at 'couldn't compile the test', C:\bot\slave\auto-win-gnu-32-opt-rustbuild\build\src\librustdoc\test.rs:285
---- sync::atomic::AtomicUsize::compare_exchange_0 stdout ----
thread 'sync::atomic::AtomicUsize::compare_exchange_0' panicked at 'couldn't run the test: The filename or extension is too long. (os error 206)', C:\bot\slave\auto-win-gnu-32-opt-rustbuild\build\src\librustdoc\test.rs:308
---- sync::atomic::AtomicUsize::compare_and_swap_0 stdout ----
thread 'sync::atomic::AtomicUsize::compare_and_swap_0' panicked at 'couldn't run the test: The filename or extension is too long. (os error 206)', C:\bot\slave\auto-win-gnu-32-opt-rustbuild\build\src\librustdoc\test.rs:308
---- unimplemented!_0 stdout ----
error: could not exec the linker `gcc`: The filename or extension is too long. (os error 206)
thread 'unimplemented!_0' panicked at 'Box<Any>', C:\bot\slave\auto-win-gnu-32-opt-rustbuild\build\src\librustc\session/mod.rs:171
thread 'unimplemented!_0' panicked at 'couldn't compile the test', C:\bot\slave\auto-win-gnu-32-opt-rustbuild\build\src\librustdoc\test.rs:285
failures:
sync::atomic::AtomicU8::new_0
sync::atomic::AtomicUsize::compare_and_swap_0
sync::atomic::AtomicUsize::compare_exchange_0
unimplemented!_0
test result: FAILED. 944 passed; 4 failed; 9 ignored; 0 measured |
@GuillaumeGomez @alexcrichton I fixed a typo in the commit description and repushed, which I believe will require a new r+, right? It didn't occur to me before I did it :-/ |
No worries! Taking a look at this I've got a few comments as well, but I'll leave them inline. |
token_str))) | ||
let err = self.fatal(&format!("expected `{}`, found `{}`", "}", | ||
token_str)); | ||
return Err(err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like no change happened here, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted.
@@ -3521,7 +3535,7 @@ impl<'a> Parser<'a> { | |||
if self.token != token::CloseDelim(token::Brace) { | |||
let token_str = self.this_token_to_string(); | |||
return Err(self.fatal(&format!("expected `{}`, found `{}`", "}", | |||
token_str))) | |||
token_str))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This semicolon addition can probably also be reverted
Thanks @estebank! I wonder if we can perhaps get more targeted with the error messages here, though? It seems unfortunate that a blanket |
I'm not sure I follow, @alexcrichton. Are you proposing pushing the logic slightly earlier in the processing of the AST? Identifying doc comments and looking ahead to check wether they are the last element? |
self.this_token_to_string())); | ||
if self.token == token::Underscore { | ||
err.note("`_` is a wildcard pattern, not an identifier"); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah this semicolon can be left off
Ah yeah sure, I'll elaborate. I think we've definitely got a few particular scenarios in mind of what we'd like to warn about here (e.g. the tests you've written). For example adding this clause to check struct Foo {
foo: u32 /// dox
} Another location, when an item isn't found but attributes are, also makes sense to me as we can lint against something like: mod foo {
/// dox
} The other case, however, in fn /// foo
foo() {} Where the previous error message of "expected identifier, found comment" would perhaps make more sense. This is basically replacing the previous error message so we need to be sure that the error is always more informative than the previous one, or otherwise we're going to have regressions in some cases. Could you elaborate on which tests require the modification to |
parse-fail/issue-27429-2.rs fails if I remove the check in struct X {
a: u8,
/// document
} ---- [parse-fail] parse-fail/issue-27429-2.rs stdout ----
error: /Users/ekuber/personal/rust/src/test/parse-fail/issue-27429-2.rs:14: unexpected "error": '14:5: 14:17: expected identifier, found `/// document`'
error: /Users/ekuber/personal/rust/src/test/parse-fail/issue-27429-2.rs:14: expected error not found: found a documentation comment that doesn't document anything Added a few new tests (without changing the code so far). This one goes through the same code path as the one above: struct X {
a: u8 /// document
//~^ ERROR found a documentation comment that doesn't document anything
//~^^ HELP maybe a comment was intended
} I feel that the behaviour in the following tests is good acceptable and clear enough: /// PASSES
fn /// document
foo() {} //~ expected identifier, found `/// document` mod Foo {
/// document
//~^ ERROR expected item after doc comment
} I'm updating the names of the tests to be descriptive of behaviour and not tied to the issue that prompted this PR. |
74ba52c
to
92e8799
Compare
&format!("expected `,`, or `}}`, found `{}`", | ||
token_str), | ||
"struct fields should be separated by commas")) | ||
if self.is_doc_comment(span) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking through the list of tokens again, we actually have token::DocComment
which may be more applicable than adding a new method here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushing a new commit that removes is_doc_comment
completely.
189da4f
to
9e0afa8
Compare
Good for me. I'll let @alexcrichton handles the r+ if you don't mind. 😉 |
} | ||
Err(err) | ||
let last_token = self.last_token.clone().map(|t| *t); | ||
Err(match last_token { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this the same as self.token
which is being matched on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this point last_token
is the actual doc comment, but the failure is raised by the current self.token
not being a valid element at this location, that's why we have to look back to check wether the failure is because the previous element was a doc comment or something else. This is also why we set error to self.last_span
instead of self.span
.
For example, with the following input file.rs
:
fn foo(a: u8, b: u64) {
/// document
}
The type of self.last_token
is Some(DocComment(/// document(61)))
while self.token
is CloseDelim(Brace)
and you get the following output with the current code:
% ./rustc file.rs
file.rs:2:5: 2:17 error: found a documentation comment that doesn't document anything
file.rs:2 /// document
^~~~~~~~~~~~
file.rs:2:5: 2:17 help: doc comments must come before what they document, maybe a comment was intended with `//`?
☔ The latest upstream changes (presumably #34424) made this pull request unmergeable. Please resolve the merge conflicts. |
Identify when documetation comments have been missplaced in the following places: * After a struct element: ```rust // file.rs: struct X { a: u8 /** document a */, } ``` ```bash $ rustc file.rs file.rs:2:11: 2:28 error: found documentation comment that doesn't document anything file.rs:2 a: u8 /** document a */, ^~~~~~~~~~~~~~~~~ file.rs:2:11: 2:28 help: doc comments must come before what they document, maybe a comment was intended with `//`? ``` * As the last line of a struct: ```rust // file.rs: struct X { a: u8, /// incorrect documentation } ``` ```bash $ rustc file.rs file.rs:3:5: 3:27 error: found a documentation comment that doesn't document anything file.rs:3 /// incorrect documentation ^~~~~~~~~~~~~~~~~~~~~~ file.rs:3:5: 3:27 help: doc comments must come before what they document, maybe a comment was intended with `//`? ``` * As the last line of a `fn`: ```rust // file.rs: fn main() { let x = 1; /// incorrect documentation } ``` ```bash $ rustc file.rs file.rs:3:5: 3:27 error: found a documentation comment that doesn't document anything file.rs:3 /// incorrect documentation ^~~~~~~~~~~~~~~~~~~~~~ file.rs:3:5: 3:27 help: doc comments must come before what they document, maybe a comment was intended with `//`? ``` Fix rust-lang#27429, rust-lang#30322
Ping @alexcrichton this seems to be ready for a final check |
Specific error message for missplaced doc comments Identify when documetation comments have been missplaced in the following places: * After a struct element: ```rust // file.rs: struct X { a: u8 /** document a */, } ``` ```bash $ rustc file.rs file.rs:2:11: 2:28 error: found documentation comment that doesn't document anything file.rs:2 a: u8 /** document a */, ^~~~~~~~~~~~~~~~~ file.rs:2:11: 2:28 help: doc comments must come before what they document, maybe a comment was intended with `//`? ``` * As the last line of a struct: ```rust // file.rs: struct X { a: u8, /// incorrect documentation } ``` ```bash $ rustc file.rs file.rs:3:5: 3:27 error: found a documentation comment that doesn't document anything file.rs:3 /// incorrect documentation ^~~~~~~~~~~~~~~~~~~~~~ file.rs:3:5: 3:27 help: doc comments must come before what they document, maybe a comment was intended with `//`? ``` * As the last line of a `fn`: ```rust // file.rs: fn main() { let x = 1; /// incorrect documentation } ``` ```bash $ rustc file.rs file.rs:3:5: 3:27 error: found a documentation comment that doesn't document anything file.rs:3 /// incorrect documentation ^~~~~~~~~~~~~~~~~~~~~~ file.rs:3:5: 3:27 help: doc comments must come before what they document, maybe a comment was intended with `//`? ``` Fix #27429, #30322
Identify when documetation comments have been missplaced in the following places:
After a struct element:
As the last line of a struct:
As the last line of a
fn
:Fix #27429, #30322