Skip to content

Doctests should not inherit features #149583

@peter-lyons-kehl

Description

@peter-lyons-kehl

Code

Have a crate that

  • has feature(s), like with Cargo.toml:
    ❯ cat Cargo.toml 
     [package]
     name = "macro_def"
     version = "0.1.0"
     edition = "2024"
      
     [dependencies]
      
     [features]
     macro_def_cfg = []
  • exports a macro_rules! that (incorrectly, of course) generates code conditional on such feature(s), like
       #[macro_export]
       macro_rules! generate_unknown_cfg {
         () => {
             #[cfg(feature="macro_def_cfg")]
             let _ = {};
          }
       }
  • have a doctest in that crate
    • containing #![forbid(unexpected_cfgs)] (or a deny), and
    • invoking that macro
      /// ```
      /// #![forbid(unexpected_cfgs)]
      ///
      /// use macro_def::generate_unknown_cfg;
      ///
      /// let _: () = {
      ///     generate_unknown_cfg!();
      /// };
      /// ```    
      
  • OR, if the doctest itself doesn't define forbid or deny, then have the crate's src/lib.rs contain: #![doc(test(attr(forbid(unexpected_cfgs))))] (or a deny)
```

Reproduction Steps

cargo test

Expected Outcome

---- src/lib.rs - generate_unknown_cfg (line 3) stdout ----
error: unexpected `cfg` condition value: `macro_def_cfg`
 --> src/lib.rs:8:5
  |
8 |     generate_unknown_cfg!();
  |     ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: no expected values for `feature`

---- YES, expecting NO features to be inherited from the crate

  = note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate
  = help: try referring to `generate_unknown_cfg` crate for guidance on how handle this unexpected cfg
  = help: the macro `generate_unknown_cfg` may come from an old version of the `macro_def` crate, try updating your dependency with `cargo update -p macro_def`
  = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
note: the lint level is defined here
 --> src/lib.rs:1:11
  |
1 | #![forbid(unexpected_cfgs)]

Expecting this error, because as https://doc.rust-lang.org/nightly/rustdoc/write-documentation/the-doc-attribute.html#testno_crate_inject clearly implies that doctests are build in a separate crate. PLUS, since doctests can't test non-public functionality, they shouldn't inherit any features at all.

Actual Output

Compiles fine - so it hides the error in the macro.

Version

1.89.0 (and probably also older); 1.91.1; and December 1, 2025 nightly

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-cfgArea: `cfg` conditional compilationA-doctestsArea: Documentation tests, run by rustdocC-discussionCategory: Discussion or questions that doesn't represent real issues.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