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

Highlight default enum variant in rustdoc output #115438

Open
omid opened this issue Sep 1, 2023 · 8 comments
Open

Highlight default enum variant in rustdoc output #115438

omid opened this issue Sep 1, 2023 · 8 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@omid
Copy link

omid commented Sep 1, 2023

Problem

Since Rust has introduced #[default], can we hightlight it in the doc?
Like this one:

#[derive(Default)]
enum Status {
    #[default]
    Active,
    Inactive,
}

For other type of default definition of enums, it can be hard! For example, the following code, can be hard:

#[derive(...)]
enum Status {
    Active,
    Inactive,
}

impl Default for Status {
    fn default() -> Self {
        serde::from_str("active").unwrap()
    }
}

or:

#[derive(...)]
enum Status {
    Active,
    Inactive,
}

impl Default for Status {
    fn default() -> Self {
        if env!("ENV") == "prod" {
            Self::Inactive
        } else {
            Self::Active
        }
    }
}

We can ignore these cases.

Proposed Solution

No response

Notes

No response

@omid omid added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Sep 1, 2023
@epage
Copy link
Contributor

epage commented Sep 1, 2023

To double check, you are asking for changes in the documentation that cargo doc generates? If so, that would actually be with the underlying rustdoc tool and we can move this issue to the appropriate repo.

@omid
Copy link
Author

omid commented Sep 1, 2023

Yes, thanks

@epage epage changed the title Highlight default enum variant Highlight default enum variant in rustdoc output Sep 1, 2023
@epage epage transferred this issue from rust-lang/cargo Sep 1, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 1, 2023
@GuillaumeGomez
Copy link
Member

What do you mean by highlighted?

@GuillaumeGomez GuillaumeGomez added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 4, 2023
@omid
Copy link
Author

omid commented Sep 4, 2023

@GuillaumeGomez any way to show it or make it visible in docs. It can be shown as a flag in front of the default variant or whatever.

@fmease
Copy link
Member

fmease commented Sep 6, 2023

I'm not super convinced that there is any benefit in displaying #[default] at all in the docs. It's a niche feature and in most other cases (structs, variants with payloads), the crate author needs to keep documenting the default value of their type manually (if they choose to disclose it). All in all, is it worth adding extra code for this corner case?

What do y'all think?

@Nemo157
Copy link
Member

Nemo157 commented Oct 2, 2023

It also feels like showing this attribute in the docs is similar to #90435, it surfaces something that might not be considered stable API by the author and implies that it is stable.

@Valentin271
Copy link

Valentin271 commented Dec 16, 2023

Just giving my opinion here.

From a crate maintainability point of view this would be great.

We use #[default] a lot in Ratatui.

Documenting every enum like below is kind of cumbersome, plus we have to keep it in sync with the actual attribute (which, granted, shouldn't be that often).
image

I feel like this is more of a robustness thing, where a comment can't be out of sync with the actual code (which matches the philosophy of compile checking doc-comment imo).

I also feel like this is a similar request as https://doc.rust-lang.org/rustdoc/unstable-features.html#extensions-to-the-doc-attribute.
I don't know the codebase at all so I could be completely wrong here, but treating #[default] seems like adding a condition somewhere to show a banner on the variant. Not a lot of extra code?

image

it surfaces something that might not be considered stable

I disagree, a default derive has the visibility of its enum, if the enum is public then it should be stable. As such, the default is documented only if the enum, and in tern default, are public.


Also, if this ever makes it https://rust-lang.github.io/rfcs/3107-derive-default-enum.html#overriding-default-fields, this request would make even more sense.

In the example, if beta is public and the default is 1, I want to know that as a lib user.
Of course if the field is private it shouldn't be documented and neither should its default.


Again this is just my opinion, I understand you guys have high stability constraints and all sort of other constraints :)

@Nemo157
Copy link
Member

Nemo157 commented Dec 16, 2023

I disagree, a default derive has the visibility of its enum, if the enum is public then it should be stable. As such, the default is documented only if the enum, and in tern default, are public.

The fact that Default is implemented is public and must be stable, which exact value it returns might not be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants