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 suggestions cause compile error with generics #88442

Closed
ehuss opened this issue Aug 28, 2021 · 1 comment · Fixed by #88504
Closed

rust_2021_prelude_collisions suggestions cause compile error with generics #88442

ehuss opened this issue Aug 28, 2021 · 1 comment · Fixed by #88504
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ehuss
Copy link
Contributor

ehuss commented Aug 28, 2021

Given the following code:

#![allow(unused)]
#![warn(rust_2021_prelude_collisions)]

trait AnnotatableTryInto {
    fn try_into<T>(self) -> Result<T, Self::Error>
    where Self: std::convert::TryInto<T> {
        std::convert::TryInto::try_into(self)
    }
}

impl<T> AnnotatableTryInto for T where T: From<u8> {}

fn main() -> Result<(), &'static str> {
    let x: u64 = 1;
    x.try_into::<usize>().or(Err("foo"))?.checked_sub(1);

    x.try_into::<usize>().or(Err("foo"))?;

    Ok(())
}

The rust_2021_prelude_collisions lint provides two suggestions, both of which fail with different errors. The first:

AnnotatableTryInto::try_into(x).or(Err("foo"))?.checked_sub(1);

fails with "type annotations needed" because the generics in the path ::<usize> are lost.

The second:

    AnnotatableTryInto::try_into(x).or(Err("foo"))?;

fails with "the trait bound (): From<u64> is not satisfied".

Ideally I think the suggestions should be:

    AnnotatableTryInto::try_into::<usize>(x).or(Err("foo"))?.checked_sub(1);
    AnnotatableTryInto::try_into::<usize>(x).or(Err("foo"))?;

Found in the 2021 crate run migrating https://crater-reports.s3.amazonaws.com/pr-87190-3/try%23a7a572ce3edd6d476191fbfe92c9c1986e009b34/reg/kamadak-exif-0.5.4/log.txt

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 A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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 and removed A-edition-2021 Area: The 2021 edition labels Aug 28, 2021
@Mark-Simulacrum Mark-Simulacrum added A-edition-2021 Area: The 2021 edition C-bug Category: This is a bug. and removed A-edition-2021 Area: The 2021 edition labels Aug 28, 2021
@m-ou-se
Copy link
Member

m-ou-se commented Aug 30, 2021

Looks like the turbofishes are swimming away!

grabs turbofish food

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants