-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[rustdoc] Give more information into extracted doctest information #141399
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
base: master
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
cd34db0
to
4e95830
Compare
Fixed |
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.
Core logic looks correct (mostly just exposing existing variables), just have a few nits about naming to hopefully increase readability.
let mut prog = String::new(); | ||
let everything_else = self.everything_else.trim(); | ||
let mut crate_level_code = String::new(); | ||
let code = self.everything_else.trim().to_string(); |
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.
Nit: could we call this inner_code
to disambiguate?
/// Contains a doctest information. Can be converted into code with the `to_string()` method. | ||
pub(crate) enum DocTestWrapper { | ||
Valid { crate_level_code: String, wrapper: Option<WrapperInfo>, code: String }, | ||
SyntaxError(String), |
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.
SyntaxError(String), | |
/// syntactically invalid code that should be passed through verbatim. | |
SyntaxError(String), |
It wasn't immediately obvious that the String
here is source code and not a description of the error, as errstrs are a fairly common pattern in rust.
I assume we do this partially for compile_fail tests and also because rustc provides better diagnostics?
pub(crate) enum DocTestWrapper { | ||
Valid { crate_level_code: String, wrapper: Option<WrapperInfo>, code: String }, |
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.
- The name
DocTestWrapper
seems a bit confusing since we already haveDocTest
andWrapperInfo
(and unlikeWrapperInfo
, this also includes the code itself), maybe this could beDocTestWrapResult
as it is the result of wrapping the doctest, and it has an error variant likeResult
? - couldn't we avoid specifying all the fields twice by leveraging the existing struct? like so:
Valid(DocTest)
let prog_len = code.len() | ||
+ crate_level_code.len() | ||
+ wrapper.as_ref().map(|w| w.len()).unwrap_or(0); |
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.
If we're doing all this to estimate capacity, should we also do + 4 * code.lines().count()
, or is that counterproductive? how much do we care about avoiding allocations?
let (wrapper, _) = doctest.generate_unique_doctest(&test, false, &opts, krate); | ||
let test = wrapper.to_string(); |
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.
let (wrapper, _) = doctest.generate_unique_doctest(&test, false, &opts, krate); | |
let test = wrapper.to_string(); | |
let (wrapped, _) = doctest.generate_unique_doctest(&test, false, &opts, krate); | |
let test = wrapped.to_string(); |
Nit: it's not just a wrapper, but the wrapper in combination with the inner code that has been wrapped. we make this distinction in the JSON output, so maybe we should stick to that terminology here.
Follow-up of #134531.
This update fragment the doctest code into its sub-parts to give more control to the end users on how they want to use it.
The new JSON looks like this:
for this doctest:
With this, I think it matches what you need @ojeda? If so, once merged I'll update the patch I sent to RfL.
r? @aDotInTheVoid