-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Added type restrictions to many collections methods. Fixes #1764 #1851
Conversation
@@ -46,7 +46,7 @@ module HTTP | |||
bytesize = line.bytesize | |||
|
|||
# Get the colon index and name | |||
colon_index = cstr.to_slice(bytesize).index(':'.ord) || 0 | |||
colon_index = cstr.to_slice(bytesize).index(':'.ord.to_u8) || 0 |
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.
here was bug or not?
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.
@kostya Not a bug because index
checks for equality and it works with different kinds of integers. With this change I have to put the specific type, but I think these conversions will be rare, so it's ok.
i checked my relative big project, find 2 bugs in specs, which test nothing, because always pass. |
I just found one boring case (not a deal breaker): [1, "1", true, "true"].includes?(1_i64)
|
Yes, the only cases I found it "broke" was when different int/float types, so it shouldn't be that terrible (you can always |
i think better extend restriction to example: # seems better
if v = bla[x]?
p v
end
# than
if x && (v = bla[x]?)
p v
end
# checking for x here make no sense in my project i was need to add 10 times this check. |
@kostya Yes, in a project of mine I also had this issue. |
also spend some time to find this bug: b = BitArray.new(10000)
if b[100] == 0
...
end condition always false because comparing different types. i think crystal at least can show the warning. |
Bump? |
@kirbyfan64 Not sure what the best thing to do here would be. Maybe |
Sorry I've been playing around with clojure lately.
We could write If we accept Maybe is ok to use |
:( |
@kirbyfan64 #2996 ;-) |
This fixes #1764 and #988 by adding type restrictions to methods like
includes?
anddelete
.Just adding this helped me find a bug in the compiler: a case wasn't handled and a variable remained
nil
, and it could leak into thesedelete
andincludes?
checks just fine.I had to remove some
Set
specs that involved sets of different types, likeSet{1, 2, 3} - Set{1, 'a'}
. I checked some local projects against this PR and they all compiled fine, except one where I had to be a bit more strict, but it definitely helped me rethink those "unexpected" cases a bit more, so it didn't feel like something annoying. So removing thoseSet
specs should be OK, in practice those cases don't seem to appear. If they do, we can think of alternative methods.It would be nice if you could test the compiler with this PR applied against your projects and see if it breaks code, or if you find a case where you definitely need the old functionality.