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

rust_2021_prelude_collisions bad suggestion with lifetime generics #88470

Closed
ehuss opened this issue Aug 29, 2021 · 3 comments · Fixed by #88496
Closed

rust_2021_prelude_collisions bad suggestion with lifetime generics #88470

ehuss opened this issue Aug 29, 2021 · 3 comments · Fixed by #88496
Labels
A-edition-2021 Area: The 2021 edition A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code.

Comments

@ehuss
Copy link
Contributor

ehuss commented Aug 29, 2021

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
@ehuss ehuss added C-bug Category: This is a bug. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. A-edition-2021 Area: The 2021 edition labels Aug 29, 2021
@ehuss
Copy link
Contributor Author

ehuss commented Aug 29, 2021

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

@m-ou-se
Copy link
Member

m-ou-se commented Aug 30, 2021

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.)

@m-ou-se
Copy link
Member

m-ou-se commented Aug 30, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edition-2021 Area: The 2021 edition A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants