test = nil
test = JSON.parse %[
{
"test": {
"test_array": [
{
"test_hash": 1
}
]
}
}
]
# pp JSON::Any.new(test.try &.["test"]?.try &.as_h?.try &.["test_array"]?).try &.as_a?
pp test.try &.["test"]?.try &.as_h?.try &.["test_array"]?.try &.as?(Array(JSON::Type))
Workaround in the commented line with JSON::Any.new()
Invalid memory access (signal 11) at address 0x7fff00000006
[4521026555] *CallStack::print_backtrace:Int32 +107
[4521010759] __crystal_sigfault_handler +55
[140735671387434] _sigtramp +26
[4521182122] *Pointer(Array(JSON::Type) | Bool | Float64 | Hash(String, JSON::Type) | Int64 | String | Nil)@Pointer(T)#[]<Int32>:(Array(JSON::Type) | Bool | Float64 | Hash(String, JSON::Type) | Int64 | String | Nil) +10
[4521181823] *Array(JSON::Type)@Array(T)#unsafe_at<Int32>:(Array(JSON::Type) | Bool | Float64 | Hash(String, JSON::Type) | Int64 | String | Nil) +31
[4521181221] *Array(JSON::Type)@Array(T)#to_s<String::Builder>:(String::Builder | Nil) +357
[4521180857] *Array(JSON::Type)@Array(T)#inspect<String::Builder>:(String::Builder | Nil) +9
[4521180818] *Array(JSON::Type)@Object#inspect:String +66
[4520976408] __crystal_main +2088
[4521010504] main +40
Env:
MacOs X
Crystal 0.19.2 (2016-09-16)
No idea what's going on here or how you managed to get this error (does not happen for me).
Here is an easier way to deal with reading the JSON structure:
test["test"]["test_array"].as_a
Sorry, I cannot reproduce your bug either. Does that _exact_ code snippet reproduce the bug for you?
@RX14, @BlaXpirit, please see the environment part of the "issue". You are not using the new release of crystal:
https://play.crystal-lang.org/#/r/1anv
https://play.crystal-lang.org/#/r/1anu
@BlaXpirit , in comments (#) of the "issue" code there's a parsing similar to your code, but with the safety of nil checking, and i stated it like a workaround.
Still there's an error in the type casting code in the last release that worked in previous versions.
@raydf I can assure you I was using 0.19.2
when I tested that snippet and it ran fine.
Segfaulting only on specific OSes and architectures points to a codegen issue. I don't have a mac to see if this happens on all macs.
Thanks @RX14, @BlaXpirit for reporting that in linux 64bit is working correctly. I'll keep using the workaround.
I was able to reproduce it, and found related bugs. I'll probably debug and fix these next week.
Reduced:
class Foo
end
x = Foo.new.as(Int32 | Foo)
x.try &.as(Foo)
It gives a codegen error in this case, but it's essentially the same bug (fixing the above fixes this)
Thanks @asterite.
Most helpful comment
I was able to reproduce it, and found related bugs. I'll probably debug and fix these next week.