Greatly simplify doctest parsing and information extraction#138104
Greatly simplify doctest parsing and information extraction#138104bors merged 12 commits intorust-lang:masterfrom
Conversation
70b66bc to
4d9a209
Compare
543cc23 to
7e403e8
Compare
fe7ed38 to
5b53cea
Compare
|
This PR modifies cc @jieyouxu |
5b53cea to
0aa9752
Compare
|
CI finally passed! \o/ |
src/librustdoc/doctest/make.rs
Outdated
| debug!("crates:\n{crates}"); | ||
| debug!("after:\n{everything_else}"); | ||
|
|
||
| // If the AST returned an error, we don't want this doctest to be merged with the |
There was a problem hiding this comment.
Outdated sentence: On parse errors, we already return early above yielding invalid which has has !can_be_merged and failed_ast.
| } | ||
|
|
||
| #[test] | ||
| fn comment_in_attrs() { |
There was a problem hiding this comment.
Wait, what has this test to do with comments? There are no comments, only an inner doc comment – which also isn't "in [an] attr"
src/librustdoc/doctest/tests.rs
Outdated
|
|
||
| #[test] | ||
| fn comment_in_attrs() { | ||
| // if input already has a fn main, it should insert a space before it |
There was a problem hiding this comment.
I'm confused what does this comment have to do with the input string? There is no fn main in it and there's also no space getting inserted before anything (I can only see an extra empty line in the generated main fn)
There was a problem hiding this comment.
I'll rewrite the comment to explain better the problem.
There was a problem hiding this comment.
Hum also the comment is wrong. XD
src/librustdoc/doctest/make.rs
Outdated
| // We need to shift by 1 because we added `{` at the beginning of the source.we provided | ||
| // to the parser. |
There was a problem hiding this comment.
| // We need to shift by 1 because we added `{` at the beginning of the source.we provided | |
| // to the parser. | |
| // We need to shift by 1 because we added `{` at the beginning of the | |
| // source we provided to the parser. |
src/librustdoc/doctest/make.rs
Outdated
| prev_span_hi: &mut Option<usize>, | ||
| ) { | ||
| let extra_len = DOCTEST_CODE_WRAPPER.len(); | ||
| // We need to shift by 1 because we added `{` at the beginning of the source.we provided |
There was a problem hiding this comment.
Outdated comment? We added more than { and we don't shift by 1
src/librustdoc/doctest/make.rs
Outdated
| Ok, | ||
| } | ||
|
|
||
| fn cancel_error_count(psess: &ParseSess) { |
There was a problem hiding this comment.
(preexisting) This would ideally also be called reset_error_count canceling counting and resetting counting are different things >.<
src/librustdoc/doctest/make.rs
Outdated
| && (!info.maybe_crate_attrs.is_empty() || !info.crate_attrs.is_empty()) | ||
| { | ||
| PartitionState::Crates | ||
| // We add potential backlines/comments if there are some in items generated |
There was a problem hiding this comment.
Reading my comment destroyed my brain. What the hell happened when I wrote it? O.o
There was a problem hiding this comment.
Ok got it, I definitely need to improve this...
| while let Some(token) = iter.next() { | ||
| if let TokenTree::Token(token, _) = token | ||
| && let TokenKind::Ident(name, _) = token.kind | ||
| && name == kw::Fn | ||
| && let Some(TokenTree::Token(fn_token, _)) = iter.peek() | ||
| && let TokenKind::Ident(fn_name, _) = fn_token.kind | ||
| && fn_name == sym::main | ||
| && let Some(TokenTree::Delimited(_, _, Delimiter::Parenthesis, _)) = { | ||
| iter.next(); | ||
| iter.peek() | ||
| } | ||
| { | ||
| info.has_main_fn = true; | ||
| break; | ||
| } | ||
| } |
There was a problem hiding this comment.
I appreciate the effort to try and get rod of the string-based fallback logic. It's a bit overkill maybe but I guess it's fine.
Tho note that this check will run a lot more often than the other check. E.g., it will check all macro calls given
//! ````
//! first!(...);
//! second!(...);
//! fn main() {}
//! ```
because we go in source order. The old string-based fallback check only kicked in when it couldn't find fn main anywhere else. I guess in practice it doesn't really matter and doing two passes would be even more overkill
src/librustdoc/doctest/make.rs
Outdated
| s: &mut String, | ||
| source: &str, | ||
| span: rustc_span::Span, | ||
| prev_span_hi: &mut Option<usize>, |
There was a problem hiding this comment.
| prev_span_hi: &mut Option<usize>, | |
| prev_span_hi: &mut usize, |
this could just be an usize right? starting off at 0 instead of None in parse_source
There was a problem hiding this comment.
Fair enough! I do .unwrap_or(0) with it so it's definitely useless to keep it in an Option...
src/librustdoc/doctest/make.rs
Outdated
|
|
||
| fn push_to_s( | ||
| s: &mut String, | ||
| source: &str, |
There was a problem hiding this comment.
Wait, if you just passed wrapped_source we wouldn't need to fix the any of the byte offsets, right? Could we do that instead please
There was a problem hiding this comment.
It would force to clone wrapped_source since it's passed by value to new_parser_from_source_str. So I'd rather not.
outside of wrapping function
0aa9752 to
796b8d6
Compare
|
Applied suggestions. Since it was approved, let's go! :D @bors r=fmease rollup |
…rsing, r=fmease Greatly simplify doctest parsing and information extraction The original process was pretty terrible, as it tried to extract information such as attributes by performing matches over tokens like `#!`, which doesn't work very well considering you can have `# ! [`, which is valid. Also, it now does it in one pass: if the parser is happy, then we try to extract information, otherwise we return early. r? `@fmease`
Rollup of 5 pull requests Successful merges: - rust-lang#138104 (Greatly simplify doctest parsing and information extraction) - rust-lang#139010 (Improve `xcrun` error handling) - rust-lang#139021 (std: get rid of pre-Vista fallback code) - rust-lang#139026 (Use `abs_diff` where applicable) - rust-lang#139030 (saethlin goes on vacation) r? `@ghost` `@rustbot` modify labels: rollup
|
@bors r=fmease rollup |
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#137889 (update outdated doc with new example) - rust-lang#138104 (Greatly simplify doctest parsing and information extraction) - rust-lang#138678 (rustc_resolve: fix instability in lib.rmeta contents) - rust-lang#138986 (feat(config): Add ChangeId enum for suppressing warnings) - rust-lang#139038 (Update target maintainers for thumb targets to reflect new REWG Arm team name) - rust-lang#139045 (bootstrap: update `test_find` test) - rust-lang#139047 (Remove ScopeDepth) Failed merges: - rust-lang#139044 (bootstrap: Avoid cloning `change-id` list) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#137889 (update outdated doc with new example) - rust-lang#138104 (Greatly simplify doctest parsing and information extraction) - rust-lang#138678 (rustc_resolve: fix instability in lib.rmeta contents) - rust-lang#138986 (feat(config): Add ChangeId enum for suppressing warnings) - rust-lang#139038 (Update target maintainers for thumb targets to reflect new REWG Arm team name) - rust-lang#139045 (bootstrap: update `test_find` test) - rust-lang#139047 (Remove ScopeDepth) Failed merges: - rust-lang#139044 (bootstrap: Avoid cloning `change-id` list) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#138104 - GuillaumeGomez:simplify-doctest-parsing, r=fmease Greatly simplify doctest parsing and information extraction The original process was pretty terrible, as it tried to extract information such as attributes by performing matches over tokens like `#!`, which doesn't work very well considering you can have `# ! [`, which is valid. Also, it now does it in one pass: if the parser is happy, then we try to extract information, otherwise we return early. r? `@fmease`
The original process was pretty terrible, as it tried to extract information such as attributes by performing matches over tokens like
#!, which doesn't work very well considering you can have# ! [, which is valid.Also, it now does it in one pass: if the parser is happy, then we try to extract information, otherwise we return early.
r? @fmease