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

Type checker does not eagerly select predicates from where-clauses #5514

Open
jonas-schievink opened this issue Jul 24, 2020 · 7 comments
Open
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug S-unactionable Issue requires feedback, design decisions or is blocked on other work

Comments

@jonas-schievink
Copy link
Contributor

The following code fails to infer the type of x, as well as the call to the inherent method as_str:

fn f<T: Into<String>>(u: T) {
    let x = u.into();
    x.as_str();
}

rustc has no problem with this code, since the .into() call will eagerly use the T: Into<String> bound from the where clause. This happens despite plenty of other Into impls existing in the wild and is an intentional Rust feature.

@jonas-schievink jonas-schievink added the A-ty type system / type inference / traits / method resolution label Jul 24, 2020
@flodiebold
Copy link
Member

Interesting, I didn't realize this exists. I think this needs to be solved in Chalk, since it works even if the where clause doesn't directly match the goal (e.g. if you have a trait Into2 that's blanket-impl'd for Into). I think there actually was code for this in the old recursive solver; we removed it because we didn't come up with a test case for it.

@flodiebold flodiebold added the S-unactionable Issue requires feedback, design decisions or is blocked on other work label Dec 21, 2020
@ANtlord
Copy link

ANtlord commented May 20, 2021

Is there any progress on the issue? I don't want to push, I just would like to know.

@ANtlord
Copy link

ANtlord commented Dec 9, 2022

@flodiebold I browsed the code, trying to understand what is executed when a type for such case is inferred.

I made the test:

#[test]
fn infer_into() {
    check_types(
        r#"
fn call(v: impl Into<u32>) {
    let n = v.into();
    n;
} //^ u32

fn test() {
    call(0u32);
}
"#,
    );
}

Of cause it fails. The thing is that it fails because the inferred type is {unknown}.

In the beginning I thought that the type inference should be fixed in
InferenceContext::infer_body. Then I took a look at InferenceContext::resolve_all. It has a
comment FIXME resolve obligations as well (use Guidance if necessary). It reminded me about the
related issue rust-lang/chalk#584 in the Chalk project. So I'm trying to
understand a couple of things:

  1. Does Chalk provides everything to implement a solution for the issue?
  2. Should InferenceContext::resolve_all infer the type somehow? If so, could you describe the
    idea which involved Guidance?

@flodiebold
Copy link
Member

Your test doesn't work because neither Into nor the blanket impl for Into exist in the test, you need to either define them yourself or add //- minicore: from to the beginning of the test code. (Also, you can remove the test function.)

As to your question, no, this needs to be solved in Chalk, i.e. the issue you linked needs to be fixed. This is unrelated to InferenceContext::resolve_all.

@ANtlord
Copy link

ANtlord commented Dec 10, 2022

Alright, thank you. I'll take a look at Chalk.

Speaking of the test, you're right, the test works after removing the test function. Although, adding //- minicore: from makes it failing still.

@Throne3d
Copy link
Contributor

Throne3d commented Jul 27, 2024

I'm also hitting into this (via #13761 / #17170) - there's a use of a borrow that isn't resolved in DataFusion, and then a destructuring that causes a later type error (because RA isn't inferring the real use of ref bindings when matching the borrow).

It looks like quite a long-standing issue, both here and in Chalk upstream (rust-lang/chalk#584). I've seen some of the discussions about the next-gen trait solver in rustc, and a few different issues suggesting RA plans to take advantage of it when it's more stabilized. Would that be expected to solve this? Also, is there an RA issue/epic for the new trait solver somewhere?

@Veykril
Copy link
Member

Veykril commented Jul 28, 2024

Would that be expected to solve this?

Yes

Also, is there an RA issue/epic for the new trait solver somewhere?

No not yet, we are still trying to figure out how to tackle adopting the new trait solver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug S-unactionable Issue requires feedback, design decisions or is blocked on other work
Projects
None yet
Development

No branches or pull requests

5 participants