Skip to content

Commit

Permalink
Merge pull request #6045 from crystal-lang/bug/5717-typeof-regression
Browse files Browse the repository at this point in the history
Compiler: don't fail when typeof argument doesn't have a type
  • Loading branch information
asterite committed May 2, 2018
2 parents 73d3a85 + b53067f commit 0d61684
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
8 changes: 8 additions & 0 deletions spec/compiler/semantic/primitives_spec.cr
Expand Up @@ -65,10 +65,18 @@ describe "Semantic: primitives" do
assert_type("sizeof(Float64)") { int32 }
end

it "types sizeof NoReturn (missing type) (#5717)" do
assert_type("x = nil; x ? sizeof(typeof(x)) : 1") { int32 }
end

it "types instance_sizeof" do
assert_type("instance_sizeof(Reference)") { int32 }
end

it "types instance_sizeof NoReturn (missing type) (#5717)" do
assert_type("x = nil; x ? instance_sizeof(typeof(x)) : 1") { int32 }
end

it "errors when comparing void (#225)" do
assert_error %(
lib LibFoo
Expand Down
14 changes: 6 additions & 8 deletions src/compiler/crystal/semantic/main_visitor.cr
Expand Up @@ -2461,19 +2461,18 @@ module Crystal
@in_type_args -= 1

type = node.exp.type?
unless type
raise "BUG: #{node} at #{node.location} should receive a type"
end

# Try to resolve the sizeof right now to a number literal
# (useful for sizeof inside as a generic type argument, but also
# to make it easier for LLVM to optimize things)
if !node.exp.is_a?(TypeOf)
if type && !node.exp.is_a?(TypeOf)
expanded = NumberLiteral.new(@program.size_of(type.sizeof_type))
expanded.type = @program.int32
node.expanded = expanded
end

node.type = @program.int32

false
end

Expand All @@ -2483,9 +2482,6 @@ module Crystal
@in_type_args -= 1

type = node.exp.type?
unless type
raise "BUG: #{node} at #{node.location} should receive a type"
end

if type.is_a? GenericType
node.raise "can't calculate instance_sizeof of generic class #{type} without specifying its type vars"
Expand All @@ -2494,12 +2490,14 @@ module Crystal
# Try to resolve the instance_sizeof right now to a number literal
# (useful for sizeof inside as a generic type argument, but also
# to make it easier for LLVM to optimize things)
if type.instance_type.devirtualize.class? && !node.exp.is_a?(TypeOf)
if type && type.instance_type.devirtualize.class? && !node.exp.is_a?(TypeOf)
expanded = NumberLiteral.new(@program.instance_size_of(type.sizeof_type))
expanded.type = @program.int32
node.expanded = expanded
end

node.type = @program.int32

false
end

Expand Down

0 comments on commit 0d61684

Please sign in to comment.