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

rustc fails to see that trait bound is satisfied #92292

Closed
Freax13 opened this issue Dec 26, 2021 · 7 comments · Fixed by #92423
Closed

rustc fails to see that trait bound is satisfied #92292

Freax13 opened this issue Dec 26, 2021 · 7 comments · Fixed by #92423
Assignees
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Freax13
Copy link
Contributor

Freax13 commented Dec 26, 2021

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

@rustbot rustbot added regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Dec 26, 2021
@inquisitivecrystal
Copy link
Contributor

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

@inquisitivecrystal inquisitivecrystal added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Dec 28, 2021
@fee1-dead
Copy link
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.

@fee1-dead fee1-dead added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Dec 28, 2021
@Freax13
Copy link
Contributor Author

Freax13 commented Dec 29, 2021

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

@Freax13 Freax13 closed this as completed Dec 29, 2021
@fee1-dead fee1-dead added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Dec 29, 2021
@fee1-dead
Copy link
Member

fee1-dead commented Dec 29, 2021

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

@fee1-dead fee1-dead reopened this Dec 29, 2021
@weirane
Copy link
Contributor

weirane commented Dec 29, 2021

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.

@inquisitivecrystal
Copy link
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.

@weirane
Copy link
Contributor

weirane commented Dec 29, 2021

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 30, 2021
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 30, 2021
…askrgr

Rollup of 4 pull requests

Successful merges:

 - rust-lang#91519 (ast: Avoid aborts on fatal errors thrown from mutable AST visitor)
 - rust-lang#92414 (Fix spacing of pretty printed const item without body)
 - rust-lang#92423 (Add UI test for rust-lang#92292)
 - rust-lang#92427 (Use `UnsafeCell::get_mut()` in `core::lazy::OnceCell::get_mut()`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in 12b59b4 Dec 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. 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.

5 participants