Skip to content

Commit

Permalink
Semantic: fix behavior of union and proc notation with untyped expres…
Browse files Browse the repository at this point in the history
…sion

Fixed crystal-lang#5805

The reason of crystal-lang#5805 is missing to check untyped in union types.
And `ProcNotation` has the same problem. It is fixed also.
  • Loading branch information
makenowjust committed Mar 11, 2018
1 parent 1b4261c commit 195cf68
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spec/compiler/semantic/proc_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -880,4 +880,14 @@ describe "Semantic: proc" do
Foo.new.x
)) { proc_of(int32) }
end

it "does not raise an error if proc notation contains untyped" do
assert_type(%(
x = 1
if x.is_a?(String)
[] of typeof(x) ->
end
x
)) { int32 }
end
end
10 changes: 10 additions & 0 deletions spec/compiler/semantic/union_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,14 @@ describe "Semantic: union" do
{foo(1), foo("hi")}
)) { tuple_of([int32, string]) }
end

it "does not raise an error if union contains untyped" do
assert_type(%(
x = 1
if x.is_a?(String)
[] of typeof(x) | typeof(1)
end
x
)) { int32 }
end
end
4 changes: 4 additions & 0 deletions src/compiler/crystal/semantic/main_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,14 @@ module Crystal
@in_type_args -= 1

if inputs = node.inputs
return false unless inputs.all? &.type?
types = inputs.map &.type.instance_type.virtual_type
else
types = [] of Type
end

if output = node.output
return false unless output.type?
types << output.type.instance_type.virtual_type
else
types << program.void
Expand All @@ -307,6 +309,8 @@ module Crystal
node.types.each &.accept self
@in_type_args -= 1

return false unless node.types.all? &.type?

old_in_is_a, @in_is_a = @in_is_a, false

types = node.types.map do |subtype|
Expand Down

0 comments on commit 195cf68

Please sign in to comment.