Skip to content
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

Doc comments only apply to the first #[cfg] item, even if another is ultimately selected. #91147

Closed
umanwizard opened this issue Nov 22, 2021 · 3 comments
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@umanwizard
Copy link
Contributor

Given the following code:

#![warn(missing_docs)]
//! my test crate

/// foo.
#[cfg(debug_assertions)]
pub const FOO: () = ();
#[cfg(not(debug_assertions))]
pub const FOO: () = ();

fn main() {}

The current output is:

warning: missing documentation for a constant
 --> src/main.rs:8:1
  |
8 | pub const FOO: () = ();
  | ^^^^^^^^^^^^^^^^^^^^^^^
  |
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![warn(missing_docs)]
  |         ^^^^^^^^^^^^

warning: `test_rust` (bin "test_rust") generated 1 warning
    Finished release [optimized] target(s) in 0.00s

The above is only seen on release builds; on debug builds, no warning is output.

The doc comment should apply to whichever piece of code is actually compiled. Is there a way to achieve this without duplicating the comment?

@umanwizard umanwizard added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 22, 2021
@umanwizard
Copy link
Contributor Author

Maybe a dupe of #1998

@jyn514
Copy link
Member

jyn514 commented Nov 23, 2021

@umanwizard you seem to be imagining that doc-comments apply like this (using braces to mean precedence since rust doesn't have a syntax for this):

/// foo.
{
  #[cfg(debug_assertions)]
  pub const FOO: () = ();
  #[cfg(not(debug_assertions))]
  pub const FOO: () = ();
}

But they don't - they apply like this:

{
  // foo.
  #[cfg(debug_assertions)]
  pub const FOO: () = ();
}
{
  #[cfg(not(debug_assertions))]
  pub const FOO: () = ();
}

The doc-comment is just another attribute - you could also write it as #[doc = " foo."] if you like. For consistency, it behaves the same as any other attribute in that position.

If you want to share the docs between two items, you could put them in an extern file and use #[doc = include_str!("my_docs.md")] above both items.

@jyn514 jyn514 added C-discussion Category: Discussion or questions that doesn't represent real issues. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. and removed A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 23, 2021
@umanwizard
Copy link
Contributor Author

umanwizard commented Nov 23, 2021

For posterity, I ended up solving it like this:

MaterializeInc/materialize#9238

@jyn514 jyn514 closed this as completed Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants