Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type names for generic instances with empty splat type vars #13189

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,55 @@ module Crystal
end
end

describe "generic instance" do
it "prints generic type arguments" do
assert_macro("{{klass.name}}", "Foo(Int32, 3)") do |program|
generic_type = GenericClassType.new(program, program, "Foo", program.reference, ["T", "U"])
{klass: TypeNode.new(generic_type.instantiate([program.int32, 3.int32] of TypeVar))}
end
end

it "prints empty splat type var" do
assert_macro("{{klass.name}}", "Foo()") do |program|
generic_type = GenericClassType.new(program, program, "Foo", program.reference, ["T"])
generic_type.splat_index = 0
{klass: TypeNode.new(generic_type.instantiate([] of TypeVar))}
end
end

it "prints multiple arguments for splat type var" do
assert_macro("{{klass.name}}", "Foo(Int32, String)") do |program|
generic_type = GenericClassType.new(program, program, "Foo", program.reference, ["T"])
generic_type.splat_index = 0
{klass: TypeNode.new(generic_type.instantiate([program.int32, program.string] of TypeVar))}
end
end

it "does not print extra commas for empty splat type var (1)" do
assert_macro("{{klass.name}}", "Foo(Int32)") do |program|
generic_type = GenericClassType.new(program, program, "Foo", program.reference, ["T", "U"])
generic_type.splat_index = 1
{klass: TypeNode.new(generic_type.instantiate([program.int32] of TypeVar))}
end
end

it "does not print extra commas for empty splat type var (2)" do
assert_macro("{{klass.name}}", "Foo(Int32)") do |program|
generic_type = GenericClassType.new(program, program, "Foo", program.reference, ["T", "U"])
generic_type.splat_index = 0
{klass: TypeNode.new(generic_type.instantiate([program.int32] of TypeVar))}
end
end

it "does not print extra commas for empty splat type var (3)" do
assert_macro("{{klass.name}}", "Foo(Int32, String)") do |program|
generic_type = GenericClassType.new(program, program, "Foo", program.reference, ["T", "U", "V"])
generic_type.splat_index = 1
{klass: TypeNode.new(generic_type.instantiate([program.int32, program.string] of TypeVar))}
end
end
end

describe :generic_args do
describe true do
it "includes the generic_args of the type" do
Expand Down
12 changes: 9 additions & 3 deletions src/compiler/crystal/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2072,21 +2072,27 @@ module Crystal
generic_type.append_full_name(io)
if generic_args
io << '('
type_vars.each_value.with_index do |type_var, i|
io << ", " if i > 0
first = true
type_vars.each_with_index do |(_, type_var), i|
if type_var.is_a?(Var)
if i == splat_index
tuple = type_var.type.as(TupleInstanceType)
tuple.tuple_types.join(io, ", ") do |tuple_type|
tuple.tuple_types.each do |tuple_type|
io << ", " unless first
first = false
tuple_type = tuple_type.devirtualize unless codegen
tuple_type.to_s_with_options(io, codegen: codegen)
end
else
io << ", " unless first
first = false
type_var_type = type_var.type
type_var_type = type_var_type.devirtualize unless codegen
type_var_type.to_s_with_options(io, skip_union_parens: true, codegen: codegen)
end
else
io << ", " unless first
first = false
type_var.to_s(io)
end
end
Expand Down