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

Rustdoc should not show impls with a where clause that is always false #80481

Open
jyn514 opened this issue Dec 29, 2020 · 5 comments
Open

Rustdoc should not show impls with a where clause that is always false #80481

jyn514 opened this issue Dec 29, 2020 · 5 comments
Labels
A-traits Area: Trait system 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

@jyn514
Copy link
Member

jyn514 commented Dec 29, 2020

pin_project_lite generates code that looks like this:

        impl <'__pin> ::pin_project_lite::__private::Unpin for Sleep<> where
         __Origin<'__pin>: ::pin_project_lite::__private::Unpin {
        }
Full output of cargo expand
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
use pin_project_lite::pin_project;

struct TimerEntry {
}


// The link between the `Sleep` instance and the timer that drives it.
#[doc = r" Future returned by [`sleep`](sleep) and"]
#[doc = r" [`sleep_until`](sleep_until)."]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Sleep {
    deadline: std::time::Duration,
    entry: TimerEntry,
}
#[automatically_derived]
#[allow(unused_qualifications)]
impl ::core::fmt::Debug for Sleep {
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match *self {
            Sleep { deadline: ref __self_0_0, entry: ref __self_0_1 } => {
                let mut debug_trait_builder = f.debug_struct("Sleep");
                let _ =
                    debug_trait_builder.field("deadline", &&(*__self_0_0));
                let _ = debug_trait_builder.field("entry", &&(*__self_0_1));
                debug_trait_builder.finish()
            }
        }
    }
}
#[allow(explicit_outlives_requirements)]
#[allow(single_use_lifetimes)]
#[allow(clippy :: redundant_pub_crate)]
#[allow(clippy :: used_underscore_binding)]
const _: () =
    {
        #[allow(dead_code)]
        #[allow(single_use_lifetimes)]
        #[allow(clippy :: mut_mut)]
        #[allow(clippy :: redundant_pub_crate)]
        #[allow(clippy :: type_repetition_in_bounds)]
        pub(crate) struct Projection<'__pin> where Sleep<>: '__pin {
            deadline: &'__pin mut (std::time::Duration),
            entry: ::pin_project_lite::__private::Pin<&'__pin mut (TimerEntry)>,
        }
        #[allow(dead_code)]
        #[allow(single_use_lifetimes)]
        #[allow(clippy :: mut_mut)]
        #[allow(clippy :: redundant_pub_crate)]
        #[allow(clippy :: type_repetition_in_bounds)]
        pub(crate) struct ProjectionRef<'__pin> where Sleep<>: '__pin {
            deadline: &'__pin (std::time::Duration),
            entry: ::pin_project_lite::__private::Pin<&'__pin (TimerEntry)>,
        }
        impl Sleep<> {
            pub(crate) fn project<'__pin>(self:
                                              ::pin_project_lite::__private::Pin<&'__pin mut Self>)
             -> Projection<'__pin> {
                unsafe {
                    let Self { deadline, entry } = self.get_unchecked_mut();
                    Projection{deadline: deadline,
                               entry:
                                   ::pin_project_lite::__private::Pin::new_unchecked(entry),}
                }
            }
            pub(crate) fn project_ref<'__pin>(self:
                                                  ::pin_project_lite::__private::Pin<&'__pin Self>)
             -> ProjectionRef<'__pin> {
                unsafe {
                    let Self { deadline, entry } = self.get_ref();
                    ProjectionRef{deadline: deadline,
                                  entry:
                                      ::pin_project_lite::__private::Pin::new_unchecked(entry),}
                }
            }
        }
        #[allow(non_snake_case)]
        pub struct __Origin<'__pin> {
            __dummy_lifetime: ::pin_project_lite::__private::PhantomData<&'__pin ()>,
            deadline: ::pin_project_lite::__private::AlwaysUnpin<std::time::Duration>,
            entry: TimerEntry,
        }
        impl <'__pin> ::pin_project_lite::__private::Unpin for Sleep<> where
         __Origin<'__pin>: ::pin_project_lite::__private::Unpin {
        }
        trait MustNotImplDrop { }
        #[allow(clippy :: drop_bounds, drop_bounds)]
        impl <T: ::pin_project_lite::__private::Drop> MustNotImplDrop for T {
        }
        impl MustNotImplDrop for Sleep<> { }
        #[forbid(safe_packed_borrows)]
        fn __assert_not_repr_packed(this: &Sleep<>) {
            let _ = &this.deadline;
            let _ = &this.entry;
        }
    };

Rustdoc in turn generates documentation like this:

impl<'__pin> Unpin for Sleep where
    __Origin<'__pin>: Unpin, 

image

This is really unfortunate, because __Origin does not in fact implement Unpin. Rustdoc shouldn't show trait implementations that can never be applied.

Originally posted by @jyn514 in tokio-rs/tokio#3330 (comment)

@jyn514 jyn514 added A-traits Area: Trait system 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. labels Dec 29, 2020
@Aaron1011
Copy link
Member

Aaron1011 commented Dec 29, 2020

I disagree. The impl<'__pin> Unpin for Sleep where __Origin<'__pin>: Unpin impl really exists - hiding it could be very confusing, and will make debugging code more difficult when proc-macros are involved.

Instead, I think we could display a note on any impl that can be proven to never apply.

Also, this is not a blanket impl - it's a macro-generated impl for Sleep specifically.

@jyn514
Copy link
Member Author

jyn514 commented Dec 29, 2020

Instead, I think we could display a note on any impl that can be proven to never apply.

Sure, that seems reasonable to me. I think

impl Clean<GenericBound> for hir::GenericBound<'_> {
is roughly the relevant code.

Also, this is not a blanket impl - it's a macro-generated impl for Sleep specifically.

Oops, good catch, thanks.

@Darksonn
Copy link
Contributor

Darksonn commented Jan 7, 2021

I was just reminded by this because another person was bitten by it. It would definitely be useful to add a note on impls that are always false, although it would of course be better doc-wise if we can somehow convince it to display impl !Unpin for Sleep.

I don't think this can be fixed in pin-project-lite, because in this case, it has to emit an always-false impl Unpin impl to prevent the user from adding their own impl Unpin on structs for which it would be unsound for them to be Unpin.

Right now, the only way to get the impl displayed properly in Tokio is to redo the unsafe code inside the macro, giving up the soundness guarantees of the macro.

@jyn514 jyn514 changed the title Rustdoc should not show blanket impls with a where clause that is always false Rustdoc should not show impls with a where clause that is always false May 15, 2021
@compiler-errors compiler-errors self-assigned this Aug 6, 2022
@jendrikw
Copy link
Contributor

Isn't this undecidable in general?

@compiler-errors
Copy link
Member

In general, yea, but we have a pretty good way of telling when an where clause is trivially false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system 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

No branches or pull requests

6 participants