Skip to content
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

Calling Range(_, Nil)#size hangs in runtime #8151

Closed
Sija opened this issue Sep 5, 2019 · 10 comments · Fixed by #8829
Closed

Calling Range(_, Nil)#size hangs in runtime #8151

Sija opened this issue Sep 5, 2019 · 10 comments · Fixed by #8829

Comments

@Sija
Copy link
Contributor

Sija commented Sep 5, 2019

Code below is enough to kill the compiler:

(0..).size

Infinite Iterators are having the same issue btw:

[1, 2, 3].cycle.size
@asterite
Copy link
Member

asterite commented Sep 5, 2019

It looks like it hangs at runtime, which I guess is expected, though we could try to detect it somehow... it's really hard, though.

@asterite
Copy link
Member

asterite commented Sep 5, 2019

Advice: always do crystal build foo.cr and then ./foo to see whether it's a compiler bug or just hangs at runtime.

@Sija
Copy link
Contributor Author

Sija commented Sep 5, 2019

I guess we could overload #size in these 2 cases.

@asterite
Copy link
Member

asterite commented Sep 5, 2019

We could. It would also hang when doing map, or select or any of the other enumerable methods.

@jkthorne
Copy link
Contributor

jkthorne commented Sep 5, 2019

could we have another class called InfiniteIterator subclassed from Iterator for cases like this?

@Sija Sija changed the title Calling Range(_, Nil)#size crashes the compiler Calling Range(_, Nil)#size hangs in runtime Sep 7, 2019
@RX14
Copy link
Contributor

RX14 commented Sep 16, 2019

This is expected behaviour. Every language will hang if you try and perform an infinite amount of work. What would #size return? Infinity isn't an integer.

@RX14 RX14 closed this as completed Sep 16, 2019
@straight-shoota
Copy link
Member

For this specific case, Range#size could at least raise when end is nil.

It would probably make sense to override this method anyway, because for the most common use case with integers, the size can be calculated directly. It doesn't need to iterate through the elements as Enumerable#size does.

@jkthorne
Copy link
Contributor

It would be nice to have an Infinity to return. I think Float is the object that has an infinity but maybe the Int classes should have one for cases like this.

@Sija
Copy link
Contributor Author

Sija commented Sep 17, 2019

@wontruefree IMO if anything Infinity should be moved into the Number struct instead.

@straight-shoota
Copy link
Member

@wontruefree Int has no concept of infinity, so it can't be a valid return value. And it's not really useful here either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants