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

fix example codes (2019-03) #7569

Merged
merged 15 commits into from
Mar 25, 2019

Conversation

maiha
Copy link
Contributor

@maiha maiha commented Mar 20, 2019

Hi all, it is time to catch up the examples code once a year.

For convenience, commits are divided file by file, so please apply squash merge.

NOTE

Aside from PR, there are three points that I was worried about.

1. humanize
1073741824.humanize_bytes(format: :IEC)
ambiguous call, implicit cast of :IEC matches all of
Int::BinaryPrefixFormat
2. Hash#dig?
h = {"a" => {"b" => [10, 20, 30]}}
h.dig? "a", "b"                # => [10, 20, 30]
h.dig? "a", "b", "c", "d", "e" # => nil
Error in tmp/hash/all.cr:71: instantiating 'Hash(String, Hash(String, Array(Int32)))#dig?(String, String, String, String, String)'

h.dig? "a", "b", "c", "d", "e" # => nil
  ^~~~

in crystal/src/hash.cr:169: instantiating 'Hash(String, Array(Int32))#dig?(Tuple(String, String, String, String))'

      value.dig?(*subkeys)
            ^~~~

in crystal/src/hash.cr:169: no overload matches 'Array(Int32)#dig?' with types String, String, String
Overloads are:
 - Indexable(T)#dig?(index : Int)
 - Indexable(T)#dig?(index : Int, *subindexes)

      value.dig?(*subkeys)
            ^~~~
3. Int32::MAX + 1 doesn't raise Overflow

Although the compiled compiler was build with default setting -D preview_overflow -D compiler_rt ..., I can't find the overflow action.

$ crystal --version
Crystal 0.28.0-dev [a17a656] (2019-03-20)

LLVM: 4.0.0
Default target: x86_64-pc-linux-gnu

$ cat test.cr
pp! Int32::MAX + 1

$ crystal test.cr
Using compiled compiler at `.build/crystal'
Int32::MAX + 1 # => -2147483648

Best regards,

maiha added 14 commits March 21, 2019 02:14
- Fix missing end quote on the code example
- Fix typo
- TODO: hash.cr:162
```
no overload matches 'Array(Int32)#dig?' with types String, String,
String
Overloads are:
 - Indexable(T)#dig?(index : Int)
 - Indexable(T)#dig?(index : Int, *subindexes)

      value.dig?(*subkeys)
```
- Fix typo
- 1212341515125412412412421 doesn't fit in an UInt64
- Fix to use comment rather than spec
- Fix undefined constant Params
- Fix expecting token ')', not '127.0'
- Fix humanize_XXX
- Fix to work
- Add missing `each`
- Fix typo
- Fix Index out of bounds (IndexError)
- Fix for strict typing
- Catch up the latest API of JSON
@jkthorne
Copy link
Contributor

That overflow behavior seems worrying.

@straight-shoota
Copy link
Member

The error with Int#humanize_bytes looks odd, too. Something wrong with autocasting symbols? Using :JEDEC (the other enum value) instead of :IEC results in the same message: ambiguous call, implicit cast of :JEDEC matches all of Int::BinaryPrefixFormat

@asterite
Copy link
Member

There's a bug with named arguments and autocasting but I can't remember what was it or how to reproduce it.

src/io.cr Outdated Show resolved Hide resolved
@bcardiff
Copy link
Member

  1. Int32::MAX + 1 doesn't raise Overflow
    Although the compiled compiler was build with default setting -D preview_overflow -D compiler_rt ..., I can't find the overflow action.

You need to compiler the snippet with -D preview_overflow in https://github.com/maiha/crystal-examples/blob/bf2ac63fcbd51dce97065d21de3163e18111609d/src/job/compile.cr#L109 to let the compiled program have overflow behavior.

The flag that you mention is for the compiler to overflow on runtime, but does not mean that the generated program have that. It's an opt-in preview feature.

@bcardiff
Copy link
Member

I extracted a bug from Hash#dig? 🕵️ 🐛

Thanks a lot @maiha this is great.

I guess that making all the examples a full crystal program (Ref: #7564) is something that benefits the tool you made, right?

revert this because it was a wrong change

Co-Authored-By: maiha <[email protected]>
@maiha
Copy link
Contributor Author

maiha commented Mar 21, 2019

I guess that making all the examples a full crystal program (Ref: #7564) is something that benefits the tool you made, right?

Yep, it helps to reduce the heuristics built in here. 😺
https://github.com/maiha/crystal-examples/blob/v0.5.1/bundled/heuristic.jnl#L110-L133

maiha added a commit to maiha/crystal-examples that referenced this pull request Mar 22, 2019
@straight-shoota
Copy link
Member

I've created an issue for the autocasting bug (#7578). Hash.dig? is #7570.

@maiha Have you checked the overflow issue with correct flags? Then this should be good to go! 🚀

@maiha
Copy link
Contributor Author

maiha commented Mar 23, 2019

@maiha Have you checked the overflow issue with correct flags? Then this should be good to go! 🚀

Yep, it worked! 😺

@straight-shoota straight-shoota merged commit da26669 into crystal-lang:master Mar 25, 2019
@straight-shoota
Copy link
Member

Thank you very much @maiha 👍

@straight-shoota straight-shoota added kind:bug A bug in the code. Does not apply to documentation, specs, etc. kind:docs topic:stdlib labels Mar 25, 2019
@straight-shoota straight-shoota added this to the 0.28.0 milestone Mar 25, 2019
urde-graven pushed a commit to urde-graven/crystal that referenced this pull request Mar 26, 2019
* upstream/master:
  fix example codes (2019-03) (crystal-lang#7569)
  Change bsearch's `/ 2` to `>> 1` for faster performance (crystal-lang#7531)
  Don't include private constants in the docs  (crystal-lang#7575)
  Add missing `require` statements for the API (A-N) (crystal-lang#7564)
  Implement Int#{leading,trailing}_zeros_count (crystal-lang#7520)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. kind:docs topic:stdlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants