Skip to content

Commit

Permalink
Raise error when using sizeof or similar as generic type argument
Browse files Browse the repository at this point in the history
  • Loading branch information
keidax committed Nov 27, 2022
1 parent 870725a commit 2ff261b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
24 changes: 24 additions & 0 deletions spec/compiler/semantic/static_array_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,28 @@ describe "Semantic: static array" do
),
"expected argument #1 to 'fn' to be StaticArray(Int32, 11), not StaticArray(Int32, 10)"
end

it "doesn't crash on sizeof (#8858)" do
assert_error %(
alias BadArray = Int32[sizeof(Int32)]
),
"can't use sizeof(Int32) as a generic type argument"
end

it "doesn't crash on instance_sizeof (#8858)" do
assert_error %(
alias BadArray = Int32[instance_sizeof(String)]
),
"can't use instance_sizeof(String) as a generic type argument"
end

it "doesn't crash on offsetof (#8858)" do
assert_error %(
class Foo
@foo : Int32 = 0
end
alias BadArray = Int32[offsetof(Foo, @foo)]
),
"can't use offsetof(Foo, @foo) as a generic type argument"
end
end
4 changes: 4 additions & 0 deletions src/compiler/crystal/semantic/type_lookup.cr
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ class Crystal::Type
type_var.raise "can only splat tuple type, not #{splat_type}"
end
next
when SizeOf, InstanceSizeOf, OffsetOf
next unless @raise

type_var.raise "can't use #{type_var} as a generic type argument"
end

# Check the case of T resolving to a number
Expand Down

0 comments on commit 2ff261b

Please sign in to comment.