Skip to content

Allow #![doc(test(attr(..)))] at module level #140560

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 4 commits into
base: master
Choose a base branch
from

Conversation

Urgau
Copy link
Member

@Urgau Urgau commented May 1, 2025

This PR adds the ability to specify #![doc(test(attr(..)))] at module level in addition to allowing it at crate-root.

This is motivated by a recent PR #140323 (by @tgross35) where we have to duplicate 2 attributes to every single f16 and f128 doctests, by allowing #![doc(test(attr(..)))] at module level we can omit them entirely and just have (in both module):

#![doc(test(attr(feature(cfg_target_has_reliable_f16_f128))))]
#![doc(test(attr(expect(internal_features))))]

Those new attributes are appended to the one found at crate-root or at a previous module. Those "global" attributes are compatible with merged doctests (they already were before).

Given the small addition that this is, I'm proposing to insta-stabilize it, but I can feature-gate it if preferred.

Best reviewed commit by commit.

r? @GuillaumeGomez

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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 1, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 1, 2025

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

@rust-log-analyzer

This comment has been minimized.

@Urgau Urgau force-pushed the test_attr-module-level branch from 516b680 to 715421c Compare May 1, 2025 19:57
@rust-log-analyzer

This comment has been minimized.

@Urgau Urgau force-pushed the test_attr-module-level branch from 715421c to 7dbac96 Compare May 1, 2025 21:13
@rust-log-analyzer

This comment has been minimized.

@Urgau Urgau force-pushed the test_attr-module-level branch from 7dbac96 to ebd7505 Compare May 1, 2025 22:46
@rustbot rustbot added the A-run-make Area: port run-make Makefiles to rmake.rs label May 1, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 1, 2025

This PR modifies run-make tests.

cc @jieyouxu

@GuillaumeGomez GuillaumeGomez added needs-fcp This change is insta-stable, so needs a completed FCP to proceed. A-doctests Area: Documentation tests, run by rustdoc and removed T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels May 2, 2025
Copy link
Member

@GuillaumeGomez GuillaumeGomez left a comment

Choose a reason for hiding this comment

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

Some small changes needed but otherwise it's pretty good as is. Once done, I'll start the FCP.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 2, 2025
@Urgau Urgau removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label May 2, 2025
@Urgau Urgau force-pushed the test_attr-module-level branch from ebd7505 to 746ceb9 Compare May 2, 2025 14:04
@rustbot rustbot added the T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. label May 2, 2025
@GuillaumeGomez
Copy link
Member

Just one last nit, I'll start the FCP afterward. Please note that I will add a concern about whether it should overload existing doc(test(attr(...))) (from the parent module) or if the current approach (appending to parent module's) is the one we want.

@Urgau Urgau force-pushed the test_attr-module-level branch from 746ceb9 to 5a3cad2 Compare May 2, 2025 14:32
@Urgau Urgau added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 2, 2025
@Urgau
Copy link
Member Author

Urgau commented May 2, 2025

I think all crate attributes support being overridden, so if a module doesn't want one from the parent (or crate-root) they can just revert to the old/default value, it's how #![allow], #![deny], ... works and what makes the most sense to me.

If we were to overload the current list with the one from the current module alone, we would need to duplicate (for the core crate) the one currently at the crate-root, which seems prone to be out-of-sync and is IMO redundant.

test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))

@Urgau Urgau force-pushed the test_attr-module-level branch from 5a3cad2 to 6a84c99 Compare May 2, 2025 14:46
@rustbot rustbot added the T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. label May 2, 2025
@Urgau Urgau removed the T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. label May 2, 2025
@GuillaumeGomez
Copy link
Member

Not always. Some attributes like cfg_attr or deprecated cannot be overloaded for example. Not sure how important or if it is important though.

@GuillaumeGomez
Copy link
Member

Thanks! Let's start the FCP. I'll add my concern as well.

@rfcbot fcp merge
@rfcbot concern non-overloadable attributes

@rfcbot
Copy link
Collaborator

rfcbot commented May 2, 2025

Team member @GuillaumeGomez has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels May 2, 2025
@Urgau
Copy link
Member Author

Urgau commented May 2, 2025

Not always. Some attributes like cfg_attr or deprecated cannot be overloaded for example. Not sure how important or if it is important though.

Hum, indeed, didn't think of those.

Currently both would be applied to every doctests in the crate, so to me adding a #![doc(test(attr(..)))] at module level shouldn't overload/reset the list of attributes, at least not implicitly.

I could see rustdoc having an explicit way, like #![doc(test(attr(reset())))] (to be bikeshed) that would explicitly overload the list of attributes to add to the doctests and therefore not take the one from the parents.

This would have the advantage of supporting both strategy: appending and overloading.

@Nemo157
Copy link
Member

Nemo157 commented May 4, 2025

Why is this limited to the module level? If it's being extended from crate level I would expect it to just work anywhere like most attributes.

@Urgau
Copy link
Member Author

Urgau commented May 4, 2025

Allowing it at item level, that is for struct, enum, function, const, union, trait, impl, ... would be equivalent to put in directly inside the doctest. Doing,

#[doc(test(attr(feature(my_feature))))]
/// ```
/// ```
pub struct A;

would be identical to putting the attribute directly inside the doctest (which IMO is clearer),

/// ```
/// #![feature(my_feature)]
/// ```
pub struct A;

Then there is the case fields and variants, putting it directly on them would be the same as above; but putting it at the struct or enum level might be useful; but I don't have a usecase nor motivation for it.

#[doc(test(attr(feature(my_feature))))]
pub struct A {
    /// ```
    /// # this doctest would receive #[feature(my_feature)] from the parent
    /// # not sure how useful this would be
    /// ```
    pub field1: u32,
    /// ```
    /// # same here   
    /// ```
    pub field2: u32,
}

@Nemo157
Copy link
Member

Nemo157 commented May 4, 2025

You can have multiple doctests on one item. The more relevant location I was thinking of was impls, that could apply to dozens of methods.

I do feel like modules would cover all usecases, with a little code rearranging for weird edgecases, it just seems like an artificial restriction to me. Are there other inherited attributes that have a similar restriction? All I'm aware of can be applied either at crate-level or anywhere.

@GuillaumeGomez
Copy link
Member

It's a good point indeed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-doctests Area: Documentation tests, run by rustdoc A-run-make Area: port run-make Makefiles to rmake.rs disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants