rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

feat: type inference for generators

Open lowr opened this issue 3 years ago • 5 comments

This PR implements basic type inference for generator and yield expressions.

Things not included in this PR:

  • Generator upvars and generator witnesses are not implemented. They are only used to determine auto trait impls, so basic type inference should be fine without them, but method resolutions with auto trait bounds may not be resolved correctly.

Open questions:

  • I haven't (yet) implemented HirDisplay for TyKind::Generator, so generator types are just shown as "{{generator}}" (in tests, inlay hints, hovers, etc.), which is not really nice. How should we show them?
  • I added moderate amount of stuffs to minicore. I especially didn't want to add impl<T> Deref for &T and impl<T> Deref for &mut T exclusively for tests for generators; should I move them into the test fixtures or can they be placed in minicore?

cc #4309

lowr avatar Sep 09 '22 08:09 lowr

I deliberately did not link #4309 for automatic close because there are still some features missing as mentioned in the PR description. However I think this PR enables type inference in most of the real-world use cases and we might want to close #4309 and create a new issue for the missing features (only if this PR is merged, of course).

lowr avatar Sep 09 '22 08:09 lowr

I added moderate amount of stuffs to minicore. I especially didn't want to add impl<T> Deref for &T and impl<T> Deref for &mut T exclusively for tests for generators; should I move them into the test fixtures or can they be placed in minicore?

I think those are some key implementations so adding them to minicore seems fine to me

I haven't (yet) implemented HirDisplay for TyKind::Generator, so generator types are just shown as "{{generator}}" (in tests, inlay hints, hovers, etc.), which is not really nice. How should we show them?

Probably just like closures but maybe with gen prefixed or something along those lines?

Veykril avatar Sep 12 '22 14:09 Veykril

I haven't (yet) implemented HirDisplay for TyKind::Generator, so generator types are just shown as "{{generator}}" (in tests, inlay hints, hovers, etc.), which is not really nice. How should we show them?

Probably just like closures but maybe with gen prefixed or something along those lines?

I'd love to reuse the closure syntax, but I'm not sure how we can encode yield type of a generator. Another option I thought of was impl Generator<ResumeType, Yield = YieldType, Return = ReturnType>, it's clear but too long to show imo (maybe it's suitable for DisplayTarget::Test though).

lowr avatar Sep 12 '22 17:09 lowr

I'd love to reuse the closure syntax, but I'm not sure how we can encode yield type of a generator. Another option I thought of was impl Generator<ResumeType, Yield = YieldType, Return = ReturnType>, it's clear but too long to show imo (maybe it's suitable for DisplayTarget::Test though).

We can probably just make something up here like gen<YieldTy> |ResumeType| -> ReturnType or yield<YieldTy> |ResumeType| -> ReturnType?

Veykril avatar Sep 13 '22 15:09 Veykril

Sorry for the delay! I decided to go for the initial implementation like so: |ResumeType| yields YieldType -> ReturnType.

This is now ready for (another?) full review.

note to self: some parts of generator substitution handling need to be adjusted when we change the generic parameter ordering.

lowr avatar Sep 21 '22 13:09 lowr

@bors r+

Veykril avatar Sep 26 '22 07:09 Veykril

:pushpin: Commit 9ede5f073564f140194229546fc2bf5eb6267b62 has been approved by Veykril

It is now in the queue for this repository.

bors avatar Sep 26 '22 07:09 bors

:hourglass: Testing commit 9ede5f073564f140194229546fc2bf5eb6267b62 with merge 1f929659acff378adad85fcea747f8b19a0f819f...

bors avatar Sep 26 '22 09:09 bors

:sunny: Test successful - checks-actions Approved by: Veykril Pushing 1f929659acff378adad85fcea747f8b19a0f819f to master...

bors avatar Sep 26 '22 09:09 bors