-
Notifications
You must be signed in to change notification settings - Fork 35
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
Binary comprehension #201
Binary comprehension #201
Conversation
Codecov Report
@@ Coverage Diff @@
## master #201 +/- ##
==========================================
- Coverage 83.91% 83.64% -0.27%
==========================================
Files 15 15
Lines 2381 2403 +22
==========================================
+ Hits 1998 2010 +12
- Misses 383 393 +10
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Although not covering the general case, this is a clear improvement. Adding a TODO comment for covering the general case (unions containing all bitstring types) would be good IMO.
src/typechecker.erl
Outdated
@@ -2783,7 +2783,11 @@ type_check_comprehension_in(Env, ResTy, bc, Expr, P, []) -> | |||
[{integer, erl_anno:new(0), 0}, | |||
{integer, erl_anno:new(0), 1}]}; | |||
_ -> | |||
throw({type_error, bc, P, ResTy}) | |||
case subtype({type, erl_anno:new(0), binary, []}, ResTy, Env) of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This covers ResTy iodata()
, but what if ResTy is a union containing bitstring()
or another bit type such as <<_:7, _:_*3>>
? I think the general solution would be do like in the list comprehension case and create a function expect_bitstring_type
.
Another solution: I think we can get rid of all the expect_
functions though if we split any union (e.g. iodata()
after normalizing it) before this function is called. I'm not sure if there is a performance concern for doing this, @josefs? Personally I'm more concerned about the code complexity than the performance for these things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, thinking about that, I came up with this other testcase, which fails for my current solution:
-spec bitstring_fun() -> <<_:7, _:_*3>> | string().
bitstring_fun() -> << <<1:N>> || N <- lists:seq(3, 12)>>.
I think I'm coming for a solution for that, with the idea of splitting the unions, but the code is getting a bit uglier at least from my perspective. I'll push it here when I have it, but maybe I could put it on a different PR so that you can choose the best solution, maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An even crazier example
-type nested() :: string() | bitstring() | binary().
-spec nested_bitstrings() -> nested() | boolean() | bitstring().
nested_bitstrings() ->
<< <<1:N>> || N <- lists:seq(3, 12)>>.
😃
|
||
-spec iodata_binary() -> iodata(). | ||
iodata_binary() -> | ||
<<<<"A">> || _ <- lists:seq(1, 10)>>. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good test case.
f2aebf4
to
7bbc7f9
Compare
@zuiderkwast I moved that test to I also changed the matching on the throws, let me know what do you think about it by now and what else can I do to improve this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so the idea is good in general, but the code needs improvements. :-)
19c6ecb
to
22b62cc
Compare
It's nice to see binary comprehensions getting some love because they're almost completely forgotten right now. In order to type check them properly we need a new function |
As we've been talking with @zuiderkwast, the fact that there are all these |
22b62cc
to
f52426a
Compare
Yes, I agree that the is room for improvement in the code. Patches are most welcome. I'm afraid it's not very high on my priority list at the moment. |
I agree with you @NelsonVides! I didn't mean to tell you to rewrite the code into the style of the old stuff. I just wanted you to be aware of it, so that we can refactor it as whole. Folding higher up on the call stack is exactly what I've been tying to suggest, but then we need to take the error messages into account, perhaps not simply re-throwing the one for one element in the union. |
f52426a
to
8b71ac8
Compare
@zuiderkwast It's all rewritten now, following the style of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this works, at least most of it. I find these expect functions a bit hard to read. Perhaps @josefs (who wrote the other expect functions) can have a look?
There are some other functions for taking types apart, such as list_view/1
(and some similar ones), that I find more elegant. It seems to serve the same purpose as expect_list_type
, at least partially, but it doesn't handle unions, type variables and a bunch of other things. (Maybe one day, these can be unified...)
Also, we should really try to add specs and some description for new functions. It helps.
8b71ac8
to
c56f145
Compare
This pull request has unfortunately lost momentum. What are the current blocking items? How can we help get this merged? |
00351ef
to
0bdbccb
Compare
Codecov ReportPatch coverage:
❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more. Additional details and impacted files@@ Coverage Diff @@
## master #201 +/- ##
==========================================
- Coverage 87.46% 83.64% -3.82%
==========================================
Files 18 15 -3
Lines 2760 2403 -357
==========================================
- Hits 2414 2010 -404
- Misses 346 393 +47
☔ View full report in Codecov by Sentry. |
0bdbccb
to
e44a85b
Compare
I've rebased this on top of the current master and made sure all the tests pass - they do, together with the self-check. I'll merge it now, as otherwise it might linger forever. |
Trying to solve #199 by checking that
binary
is a subtype of the expected result type of the binary comprehension.