-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Closed
Labels
E-has-instructionsIssue has some instructions and pointers to code to get startedIssue has some instructions and pointers to code to get startedE-mediumgood first issue
Description
We need to infer the result types of binary operations, like a + b or a && b. In general, this will involve the following steps:
- we probably want to wait for Introduce hir::Expr #386 to have
hir::Expr::BinOp - add a test in
ra_hir/src/ty/tests.rsfor the binary operation - in
infer_exprinty.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:
- short-circuiting bool operations
&&and||: These aren't overloadable and always have result typebool, so the inference is easy - comparison / equality:
==,<etc.: These are overloadable, but the result type is still alwaysbool, so it's still easy 😄 - assignment:
=. 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. - operation + assignment:
+=,*=etc.. Overloadable, but result type is still always(). - arithmetical and bitwise operations:
+,-,|,&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. - ranges
..and..=: Not overloadable, but we need to find the variousRangestructs to construct the type.
I'd suggest taking these on one bullet point at a time 😄
matklad, h-michael, DJMcNab and marcusklaas
Metadata
Metadata
Assignees
Labels
E-has-instructionsIssue has some instructions and pointers to code to get startedIssue has some instructions and pointers to code to get startedE-mediumgood first issue