Skip to content

rustc fails to see that trait bound is satisfied #92292

Closed
@Freax13

Description

@Freax13

Code

I tried this code:

use std::marker::PhantomData;

pub struct MyGenericType<T> {
    _marker: PhantomData<*const T>,
}

pub struct MyNonGenericType;

impl<T> From<MyGenericType<T>> for MyNonGenericType {
    fn from(_: MyGenericType<T>) -> Self {
        todo!()
    }
}

pub trait MyTrait {
    const MY_CONSTANT: i32;
}

impl<T> MyTrait for MyGenericType<T>
where
    Self: Into<MyNonGenericType>,
{
    const MY_CONSTANT: i32 = 1;
}

impl<T> MyGenericType<T> {
    const MY_OTHER_CONSTANT: i32 = <MyGenericType<T> as MyTrait>::MY_CONSTANT;
}

I expected to see this happen:
It should compile successfully.

Instead, this happened:
The compiler doesn't realize that Into<MyNonGenericType> is implemented for all MyGenericType<T> and reports an error.

error[E0277]: the trait bound `MyNonGenericType: From<MyGenericType<T>>` is not satisfied
  --> src/lib.rs:27:36
   |
27 |     const MY_OTHER_CONSTANT: i32 = <MyGenericType<T> as MyTrait>::MY_CONSTANT;
   |                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<MyGenericType<T>>` is not implemented for `MyNonGenericType`
   |
   = note: required because of the requirements on the impl of `Into<MyNonGenericType>` for `MyGenericType<T>`
note: required because of the requirements on the impl of `MyTrait` for `MyGenericType<T>`
  --> src/lib.rs:19:9
   |
19 | impl<T> MyTrait for MyGenericType<T>
   |         ^^^^^^^     ^^^^^^^^^^^^^^^^
help: consider introducing a `where` bound, but there might be an alternative better way to express this requirement
   |
26 | impl<T> MyGenericType<T> where MyNonGenericType: From<MyGenericType<T>> {
   |                          ++++++++++++++++++++++++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `regression` due to previous error

Interestingly enough, removing the where Self: Into<MyNonGenericType>, bound makes this compile again.

cargo-bisect-rustc report

searched nightlies: from nightly-2021-12-01 to nightly-2021-12-25
regressed nightly: nightly-2021-12-14
searched commit range: 6bda5b3...8f117a7
regressed commit: 22f8bde

bisected with cargo-bisect-rustc v0.6.1

Host triple: x86_64-unknown-linux-gnu
Reproduce with:

cargo bisect-rustc --test-dir=. --start=2021-12-01 --end=2021-12-25 

@rustbot modify labels: +regression-from-stable-to-nightly

Metadata

Metadata

Assignees

Labels

E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions