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

Warn on unused #[doc(hidden)] attributes on trait impl items #96008

Merged

Conversation

fmease
Copy link
Member

@fmease fmease commented Apr 13, 2022

Zulip conversation.

Whether an associated item in a trait impl is shown or hidden in the documentation entirely depends on the corresponding item in the trait declaration. Rustdoc completely ignores #[doc(hidden)] attributes on impl items. No error or warning is emitted:

pub trait Tr { fn f(); }
pub struct Ty;
impl Tr for Ty { #[doc(hidden)] fn f() {} }
//               ^^^^^^^^^^^^^^ ignored by rustdoc and currently
//                              no error or warning issued

This may lead users to the wrong belief that the attribute has an effect. In fact, several such cases are found in the standard library (I've removed all of them in this PR).
There does not seem to exist any incentive to allow this in the future either: Impl'ing a trait for a type means the type fully conforms to its API. Users can add #[doc(hidden)] to the whole impl if they want to hide the implementation or add the attribute to the corresponding associated item in the trait declaration to hide the specific item. Hiding an implementation of an associated item does not make much sense: The associated item can still be found on the trait page.

This PR emits the warn-by-default lint unused_attribute for this case with a future-incompat warning.

@rustbot label T-compiler T-rustdoc A-lint

@rust-highfive
Copy link
Collaborator

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with r? rust-lang/libs-api @rustbot label +T-libs-api to request review from a libs-api team reviewer. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • a stabilization of a library feature
  • introducing new or changes existing unstable library APIs
  • changes to public documentation in ways that create new stability guarantees

@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 13, 2022
@rust-highfive
Copy link
Collaborator

r? @lcnr

(rust-highfive has picked a reviewer for you, use r? to override)

@rustbot rustbot added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Apr 13, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 13, 2022
@GuillaumeGomez
Copy link
Member

For the bug you detected, please open an issue (and assign it to yourself) and fix it in another PR.

Otherwise, this PR looks good to me, thanks!

@fmease fmease force-pushed the warn-on-useless-doc-hidden-on-assoc-impl-items branch from 070aca7 to 8b19067 Compare April 13, 2022 15:25
@fmease fmease marked this pull request as ready for review April 13, 2022 15:25
@fmease

This comment was marked as outdated.

@fmease fmease force-pushed the warn-on-useless-doc-hidden-on-assoc-impl-items branch from 8b19067 to 9c81d0c Compare April 13, 2022 18:17
@lcnr
Copy link
Contributor

lcnr commented Apr 13, 2022

I personally think we should treat this similar to other, previously silently ignored attributes. E.g.

struct Foo(#[inline] ());
warning: `#[inline]` is ignored on struct fields, match arms and macro defs
 --> src/lib.rs:1:12
  |
1 | struct Foo(#[inline] ());
  |            ^^^^^^^^^
  |
  = note: `#[warn(unused_attributes)]` on by default
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: see issue #80564 <https://github.com/rust-lang/rust/issues/80564> for more information

Imo this should be a unused_attributes future compat lint.

Though that's for @rust-lang/lang @rust-lang/docs to decide.

@bors
Copy link
Contributor

bors commented May 3, 2022

☔ The latest upstream changes (presumably #92566) made this pull request unmergeable. Please resolve the merge conflicts.

@fmease
Copy link
Member Author

fmease commented May 4, 2022

Since nobody else voiced their opinion, I will integrate the lint unused_doc_hidden into unused_attributes.

@scottmcm
Copy link
Member

scottmcm commented May 4, 2022

👍 to treating it like the other attributes -- there have been a ton of these, like #[repr] on fields, etc.

@fmease
Copy link
Member Author

fmease commented May 6, 2022

@rustbot author

@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 6, 2022
@fmease fmease force-pushed the warn-on-useless-doc-hidden-on-assoc-impl-items branch from 9c81d0c to 02e490e Compare May 8, 2022 19:40
@fmease fmease force-pushed the warn-on-useless-doc-hidden-on-assoc-impl-items branch from 02e490e to 9d157ad Compare May 8, 2022 20:54
@fmease
Copy link
Member Author

fmease commented May 8, 2022

@rustbot ready
@rustbot label -T-libs

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 8, 2022
@lcnr
Copy link
Contributor

lcnr commented May 9, 2022

cnsidering that this is

  1. a pretty straightforward bug fix
  2. only adds a new lint, so it isn't a breaking change
  3. i've already notified t-lang and got an unofficial signoff by @scottmcm

@bors r+ rollup

@bors
Copy link
Contributor

bors commented May 9, 2022

📌 Commit 9d157ad has been approved by lcnr

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 9, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request May 9, 2022
…askrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#95483 (Improve floating point documentation)
 - rust-lang#96008 (Warn on unused `#[doc(hidden)]` attributes on trait impl items)
 - rust-lang#96841 (Revert "Implement [OsStr]::join", which was merged without FCP.)
 - rust-lang#96844 (Actually fix ICE from rust-lang#96583)
 - rust-lang#96854 (Some subst cleanup)
 - rust-lang#96858 (Remove unused param from search.js::checkPath)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 6c8001b into rust-lang:master May 9, 2022
@rustbot rustbot added this to the 1.62.0 milestone May 9, 2022
@scottmcm
Copy link
Member

scottmcm commented May 9, 2022

only adds a new lint, so it isn't a breaking change

👍. So long as (like here) reverting it doesn't break people, it doesn't need a full FCP.

@fmease fmease deleted the warn-on-useless-doc-hidden-on-assoc-impl-items branch May 9, 2022 20:06
@dtolnay
Copy link
Member

dtolnay commented May 10, 2022

This may lead users to the wrong belief that the attribute has an effect. In fact, several such cases are found in the standard library (I've removed all of them in this PR).

There does not seem to exist any incentive to allow this in the future either

FWIW the rustdoc that shipped with 1.56.0 and older versions of Rust does look at doc(hidden) on impl items and it does affect the rendered output, so you might find that these attributes exist (for good reason) in the ecosystem.

A typical case looks like:

pub trait Trait {
    fn public_fn();
    #[doc(hidden)]
    fn __private_fn() {}
}

impl Trait for Struct {
    fn public_fn() {}
    #[doc(hidden)]
    fn __private_fn() {}
}

Without doc(hidden) in the impl, old rustdoc will render __private_fn on Struct's page. Versions 1.54.0 and older will additionally render it in the impl section of Trait's page.

Rust 1.56.0 is only 6 months old so it's not unreasonable that libraries would want to support it.

@dtolnay
Copy link
Member

dtolnay commented May 10, 2022

I noticed in a different one of my crates that the doc(hidden) attribute on an associated type is still used by rustdoc, so this lint is a false positive in some cases.

I filed #96890 to follow up.

@fmease
Copy link
Member Author

fmease commented May 10, 2022

Oh, I didn't know older versions of rustdoc behave differently in this scenario.
@dtolnay Do you suggest reverting this PR and re-introducing this change “later” like in a year? How can we make sure this won't be forgotten?
As a middle ground, what do you think about only linting this case in the 2021 edition (stabilized in 1.56) as a temporary measure? With the rationale that people who successfully migrated to the latest edition potentially do not care so much about supporting older versions of rustc (edit: only at the time of this writing, obviously, since the edition is relatively recent)? Or is this utter nonsense?
And adding a FIXME to backport it to all editions eventually?

Is 1.56 the last version where rustdoc behaved differently? (Well, for 1.62 I even extended the new behavior, see #95769)

davidhewitt pushed a commit to PyO3/pyo3 that referenced this pull request May 10, 2022
davidhewitt pushed a commit to PyO3/pyo3 that referenced this pull request May 15, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request May 20, 2022
Do not emit the lint `unused_attributes` for *inherent* `#[doc(hidden)]` associated items

Fixes rust-lang#97205 (embarrassing oversight from rust-lang#96008).

`@rustbot` label A-lint
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jun 21, 2022
…t, r=GuillaumeGomez

Remove the unused-`#[doc(hidden)]` logic from the `unused_attributes` lint

Fixes rust-lang#96890.

It was found out that `#[doc(hidden)]` on trait impl items does indeed have an effect on the generated documentation (see the linked issue). In my opinion and the one of [others](https://rust-lang.zulipchat.com/#narrow/stream/266220-rustdoc/topic/Validy.20checks.20for.20.60.23.5Bdoc.28hidden.29.5D.60/near/281846219), rustdoc's output is actually a bit flawed in that regard but that should be tracked in a new issue I suppose (I will open an issue for that in the near future).

The check was introduced in rust-lang#96008 which is marked to be part of version `1.62` (current `beta`). As far as I understand, this means that **this PR needs to be backported** to `beta` to fix rust-lang#96890 on time. Correct me if I am wrong.

CC `@dtolnay` (in case you would like to agree or disagree with my decision to fully remove this check)

`@rustbot` label A-lint T-compiler T-rustdoc

r? `@rust-lang/compiler`
ehuss pushed a commit to ehuss/rust that referenced this pull request Jun 24, 2022
…t, r=GuillaumeGomez

Remove the unused-`#[doc(hidden)]` logic from the `unused_attributes` lint

Fixes rust-lang#96890.

It was found out that `#[doc(hidden)]` on trait impl items does indeed have an effect on the generated documentation (see the linked issue). In my opinion and the one of [others](https://rust-lang.zulipchat.com/#narrow/stream/266220-rustdoc/topic/Validy.20checks.20for.20.60.23.5Bdoc.28hidden.29.5D.60/near/281846219), rustdoc's output is actually a bit flawed in that regard but that should be tracked in a new issue I suppose (I will open an issue for that in the near future).

The check was introduced in rust-lang#96008 which is marked to be part of version `1.62` (current `beta`). As far as I understand, this means that **this PR needs to be backported** to `beta` to fix rust-lang#96890 on time. Correct me if I am wrong.

CC `@dtolnay` (in case you would like to agree or disagree with my decision to fully remove this check)

`@rustbot` label A-lint T-compiler T-rustdoc

r? `@rust-lang/compiler`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants