Union type produced by nested subject/let (memoize)
Using a memoized value with the same name in a nested context can produce a union type for the outer context.
Spectator.context "outer" do
subject { 42 }
specify { is_expected.to compile_as(Int32) } # Fails, is actually (Int32 | String).
context "inner" do
subject { "foo" }
specify { is_expected.to compile_as(String) } # Correct, is not a union with Int32.
end
end
Testing inside the memoized methods shows they are compiled to the correct type (no union). Immediately outside, in the test method, the value appears as a union. I don't know if this is a bug in the crystal compiler or some strange behavior with inheritance. I am unable to reproduce this issue in reduced code.