Skip to content

Commit

Permalink
Showing 2 changed files with 26 additions and 2 deletions.
11 changes: 11 additions & 0 deletions spec/compiler/type_inference/tuple_spec.cr
Original file line number Diff line number Diff line change
@@ -208,4 +208,15 @@ describe "Type inference: tuples" do
Tuple(Int32, String).types
)) { tuple_of([int32.metaclass, string.metaclass]) }
end

it "can call [] on T" do
assert_type(%(
struct Tuple
def self.types
{{ T[0] }}
end
end
Tuple(Nil, Int32).types
)) { |mod| mod.nil.metaclass }
end
end
17 changes: 15 additions & 2 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
@@ -955,7 +955,8 @@ module Crystal
when "[]"
interpret_one_arg_method(method, args) do |arg|
type = type.instance_type
if type.is_a?(NamedTupleInstanceType)
case type
when NamedTupleInstanceType
case arg
when SymbolLiteral
key = arg.value
@@ -969,8 +970,20 @@ module Crystal
return NilLiteral.new
end
TypeNode.new(type.entries[index].type)
when TupleInstanceType
case arg
when NumberLiteral
index = arg.to_number.to_i
type = type.tuple_types[index]?
unless type
return NilLiteral.new
end
TypeNode.new(type)
else
return NilLiteral.new
end
else
raise "undefined method '[]' for TypeNode of type #{type} (must be a named tuple type)"
raise "undefined method '[]' for TypeNode of type #{type} (must be a tuple or named tuple type)"
end
end
when "class"

0 comments on commit 5fc4f1a

Please sign in to comment.