Skip to content

rust_2021_prelude_collisions bad suggestion with lifetime generics #88470

Closed

Description

@ehuss

I tried this code:

#![warn(rust_2021_prelude_collisions)]
pub struct PyAny;

pub trait PyTryFrom<'v>: Sized {
    fn try_from<V>(value: V) -> Result<&'v Self, ()>;
}

pub trait PyTryInto<T>: Sized {
    fn try_into(&self) -> Result<&T, ()>;
}

struct Foo;

impl<U> PyTryInto<U> for Foo
where
    U: for<'v> PyTryFrom<'v>,
{
    fn try_into(&self) -> Result<&U, ()> {
        U::try_from(self)
    }
}

This generates a suggestion to avoid the prelude collision with:

        <U as PyTryFrom<_>>::try_from(self)

However, that does not compile because the generics for PyTryFrom is a lifetime parameter which should not be specified with _. I believe the correct suggestion would be <U as PyTryFrom>::try_from(self) without the generic args.

Found in the 2021 crater run for:

Meta

rustc --version --verbose:

rustc 1.56.0-nightly (ac50a5335 2021-08-27)
binary: rustc
commit-hash: ac50a53359328a5d7f2f558833e63d59d372e4f7
commit-date: 2021-08-27
host: x86_64-apple-darwin
release: 1.56.0-nightly
LLVM version: 13.0.0

Activity

  1. added
    C-bugCategory: This is a bug.
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
    on Aug 29, 2021
  2. ehuss commented on Aug 29, 2021

    @ehuss
    ContributorAuthor

    cc #88442 which is similar where rust_2021_prelude_collisions is having issues with generics.

  3. m-ou-se commented on Aug 30, 2021

    @m-ou-se
    Member

    Similar problem (not found in the crater results):

    #![warn(rust_2021_prelude_collisions)]
    
    pub trait A {
        fn try_from(self) -> i32;
    }
    
    struct Foo<'a>(&'a i32);
    
    impl<'a> A for Foo<'a> {
        fn try_from(self) -> i32 {
            0
        }
    }
    
    fn main() {
        Foo::try_from(todo!());
    }
    warning: trait-associated function `try_from` will become ambiguous in Rust 2021
      --> src/main.rs:16:5
       |
    16 |     Foo::try_from(todo!());
       |     ^^^^^^^^^^^^^ help: disambiguate the associated function: `<Foo<_> as A>::try_from`
       |
    

    (The lifetime is now on the Self type, not on the trait.)

  4. m-ou-se commented on Aug 30, 2021

    @m-ou-se
    Member

    The problem also exists for const generics. But I don't think we can solve that without #![feature(generic_arg_infer)].

  5. added a commit that references this issue on Aug 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-edition-2021Area: The 2021 editionA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions