Skip to content

Commit

Permalink
Treat untyped element in tuple and named tuple
Browse files Browse the repository at this point in the history
Fixed #6718

The reason of #6718 is calling `node.update` with untyped `TupleLiteral` or
`NamedTupleLiteral`. An untyped node has no `node.program`, so it fails nil
assertion.

I think this `node.update` is not needed because `CleanupTransformer`
does not change the type of `node` execpt for untyped and unreachable
node.  However such nodes should be transformed to other node.
In other words `node.update` does nothing in general.

Thus, this replaces `node.update` with treating untyped element.
  • Loading branch information
makenowjust committed Sep 14, 2018
1 parent ee71249 commit 0e03875
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
19 changes: 19 additions & 0 deletions spec/compiler/semantic/named_tuple_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,23 @@ describe "Semantic: named tuples" do
meta
end
end

it "doesn't crash on named tuple in not executed block (#6718)" do
assert_type(%(
require "prelude"
def pending(&block)
end
def untyped(x = nil)
end
# To reproduce this bug, it is needed to the expression that is
# not typed on main phase but is typed on cleanup phase.
# `untyped(untyped)` is just one.
pending do
{s: untyped(untyped)}
end
)) { nil_type }
end
end
19 changes: 19 additions & 0 deletions spec/compiler/semantic/tuple_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,23 @@ describe "Semantic: tuples" do
)
}
end

it "doesn't crash on tuple in not executed block (#6718)" do
assert_type(%(
require "prelude"
def pending(&block)
end
def untyped(x = nil)
end
# To reproduce this bug, it is needed to the expression that is
# not typed on main phase but is typed on cleanup phase.
# `untyped(untyped)` is just one.
pending do
{untyped(untyped)}
end
)) { nil_type }
end
end
10 changes: 8 additions & 2 deletions src/compiler/crystal/semantic/cleanup_transformer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,10 @@ module Crystal

def transform(node : TupleLiteral)
super
node.update

unless node.elements.all? &.type?
return untyped_expression node
end

no_return_index = node.elements.index &.no_returns?
if no_return_index
Expand All @@ -743,7 +746,10 @@ module Crystal

def transform(node : NamedTupleLiteral)
super
node.update

unless node.entries.all? &.value.type?
return untyped_expression node
end

no_return_index = node.entries.index &.value.no_returns?
if no_return_index
Expand Down

0 comments on commit 0e03875

Please sign in to comment.