-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Implement type inference for binary operators #390
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
Comments
I'm working on type inference for binary operations on primitive numbers now. |
@marcusklaas I've got the refactoring for #386 coming up, which changes a lot of code in type inference... So you're going to get a lot of conflicts if you start now :) I'll rebase and push my branch when #440 is merged, so maybe it'd be a good idea to wait for that. |
Sounds good, I'll fix #440 and then wait for your code. Thanks for the heads-up :-) |
440: Implement type inference for boolean operators r=flodiebold a=marcusklaas Tried implementing the easiest part of #390. Hope this is somewhat close to what the intent of the issue was. Found it surprisingly easy to find my way around the repository - it's well organized! Very grateful for any pointers. Co-authored-by: Marcus Klaas de Vries <[email protected]>
Ok, the PR is #446. |
451: More type inference for more binary expressions r=flodiebold a=marcusklaas Implements more of #390. Just works for primitive (numeric) types for now. Found an issue where `let x: Ty = expr;` doesn't actually propagate the type information unless `Ty` is primitive and numeric. I'll open an issue for this. Co-authored-by: Marcus Klaas de Vries <[email protected]>
What is missing before this can be closed? |
Searching for lang items and trait resolution :P. The former probably also requires a better api for handling attributes. |
Uh oh!
There was an error while loading. Please reload this page.
We need to infer the result types of binary operations, like
a + b
ora && b
. In general, this will involve the following steps:hir::Expr::BinOp
ra_hir/src/ty/tests.rs
for the binary operationinfer_expr
inty.rs
, add the code to infer the type of both operands and then calculate the result type.One complication is that many operators are overloadable via traits (e.g.
Add
). To implement these correctly, we would have to be able to do associated type projections (i.e. calculate<T1 as Add<T2>>::Output
). Since we're still quite a bit away from doing that, we might hard-code the result types for the primitive operations at least (and maybe it makes sense to guess that the output type is the same as the input type, but that could result in wrong inferences).Specifically, we've got these groups of binary operations:
&&
and||
: These aren't overloadable and always have result typebool
, so the inference is easy==
,<
etc.: These are overloadable, but the result type is still alwaysbool
, so it's still easy 😄=
. This isn't overloadable, and the return type is always()
(Ty::unit()
). The right side needs to be coercible to the left; we don't have coercion yet, but we can already pass down the type of the left side as the expectation for the right side.+=
,*=
etc.. Overloadable, but result type is still always()
.+
,-
,|
,&
etc.. These are overloadable with arbitrary result types, so we can't handle this in general without trait resolution, but we could still handle primitive types...
and..=
: Not overloadable, but we need to find the variousRange
structs to construct the type.I'd suggest taking these on one bullet point at a time 😄
The text was updated successfully, but these errors were encountered: