Skip to content

rust_2021_prelude_collisions suggestions cause compile error with generics #88442

Closed
@ehuss

Description

@ehuss

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-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.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions