Skip to content

[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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

GuillaumeGomez
Copy link
Member

@GuillaumeGomez GuillaumeGomez commented May 22, 2025

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:

{
  "format_version":2,
  "doctests":[
    {
      "file":"$DIR/extract-doctests-result.rs",
      "line":8,
      "doctest_attributes":{
        "original":"",
        "should_panic":false,
        "no_run":false,
        "ignore":"None",
        "rust":true,
        "test_harness":false,
        "compile_fail":false,
        "standalone_crate":false,
        "error_codes":[],
        "edition":null,
        "added_css_classes":[],
        "unknown":[]
      },
      "original_code":"let x = 12;\nOk(())",
      "doctest_code":{
        "crate_level":"#![allow(unused)]\n",
        "code":"let x = 12;\nOk(())",
        "wrapper":{
          "before":"fn main() { fn _inner() -> core::result::Result<(), impl core::fmt::Debug> {\n",
          "after":"\n} _inner().unwrap() }",
          "returns_result":true
        }
      },
      "name":"$DIR/extract-doctests-result.rs - (line 8)"
    }
  ]
}

for this doctest:

let x = 12;
Ok(())

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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels May 22, 2025
@GuillaumeGomez GuillaumeGomez changed the title Extracted doctest [rustdoc] Give more information into extracted doctest information May 22, 2025
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez
Copy link
Member Author

Fixed fmt.

Copy link
Contributor

@lolbinarycat lolbinarycat left a 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();
Copy link
Contributor

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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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?

Comment on lines +202 to +203
pub(crate) enum DocTestWrapper {
Valid { crate_level_code: String, wrapper: Option<WrapperInfo>, code: String },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The name DocTestWrapper seems a bit confusing since we already have DocTest and WrapperInfo (and unlike WrapperInfo, this also includes the code itself), maybe this could be DocTestWrapResult as it is the result of wrapping the doctest, and it has an error variant like Result?
  2. couldn't we avoid specifying all the fields twice by leveraging the existing struct? like so: Valid(DocTest)

Comment on lines +212 to +214
let prog_len = code.len()
+ crate_level_code.len()
+ wrapper.as_ref().map(|w| w.len()).unwrap_or(0);
Copy link
Contributor

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?

Comment on lines +311 to +312
let (wrapper, _) = doctest.generate_unique_doctest(&test, false, &opts, krate);
let test = wrapper.to_string();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants