Skip to content

rustdoc: Experiment: Inject doctests into the host crate #141083

Open
@fmease

Description

@fmease

From: https://hackmd.io/@fmease/inject-doctests-into-host-crate:

Motivation

  • Have doctest work in binary crate
  • Have better warnings and errors reporting by having correct spans
  • Handle doctests with different editions in the same compilation unit
  • Simplify cross-compilation (deprecating all the --doctest-cross-arg ...)
  • Better Cargo integration (parallel test exec, smarter unused_crate_dependencies, ...)

Implementation

  • Have a hook in the AST->HIR lowering that is only implemented by rustdoc:
    • It would parse the doctests, create the AST nodes (by calling rustc_parse maybe?) and give back to rustc the AST nodes corresponding to #[test] fn doctest_1234 { ... } with the doctest body inside
  • Rustdoc would give spans corresponding to the original doctest code in the doc comment. This could include a different edition for doctests that override that
    • Problem: how would we get the spans to be correct given the presence of /// in the original code but not the new code given to rustc_parse? would it just work or does it need more invasive changes?
  • These AST nodes would then be injected into the crate and lowered into HIR and go through the rest of the compilation
  • Then we run them using the libtest harness.
  • We need to run all rustc passes to make it work

Implementation History

Empty.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-doctestsArea: Documentation tests, run by rustdocC-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-unimplementedStatus: The feature has not been implemented.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions