Skip to content

rustc fails to see that trait bound is satisfied #92292

Closed

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

Activity

  1. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Dec 26, 2021
  2. inquisitivecrystal commented on Dec 28, 2021

    @inquisitivecrystal
    Contributor

    CC @fee1-dead, as the author of the relevant PR.

  3. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Dec 28, 2021
  4. fee1-dead commented on Dec 28, 2021

    @fee1-dead
    Member

    Fixed by #92257, this one is more interesting than the test I added in that PR, as it is stable code (but uses impl const Into<U> for T where U: ~const From<T>), so I think we could add this one as an ui test as well.

  5. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Dec 28, 2021
  6. Freax13 commented on Dec 29, 2021

    @Freax13
    ContributorAuthor

    I can confirm that this is fixed on the latest nightly.

  7. added
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    on Dec 29, 2021
  8. fee1-dead commented on Dec 29, 2021

    @fee1-dead
    Member

    It's E-needs-test because someone should add an ui test for this issue

  9. weirane commented on Dec 29, 2021

    @weirane
    Contributor

    It's E-needs-test because someone should add an ui test for this issue

    I'm interested in working on this. Could you tell me where should the test be added? It seems to be somewhere inside src/test/ui.

  10. inquisitivecrystal commented on Dec 29, 2021

    @inquisitivecrystal
    Contributor

    It's E-needs-test because someone should add an ui test for this issue

    I'm interested in working on this. Could you tell me where should the test be added? It seems to be somewhere inside src/test/ui.

    I might go with src/test/ui/traits/issue-92292.rs. There isn't really much dark magic to picking which directory to file something under; there are a few directories that aren't really used anymore, but other than that, it's just a matter of picking something that makes sense.

  11. weirane commented on Dec 29, 2021

    @weirane
    Contributor

    @rustbot claim

  12. added a commit that references this issue on Dec 30, 2021
  13. added a commit that references this issue on Dec 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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