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

Compiler: make sure to remove literal types before instantiating matches #6284

Merged
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
14 changes: 14 additions & 0 deletions spec/compiler/codegen/double_splat_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,18 @@ describe "Codegen: double splat" do
Global.x
)).to_i.should eq(1)
end

it "removes literal types in all matches (#6239)" do
run(%(
def foo(y : Float64)
y.to_i
end

def bar(x : Float64, **args)
foo(**args)
end

bar(x: 1, y: 2.0)
)).to_i.should eq(2)
end
end
13 changes: 6 additions & 7 deletions src/compiler/crystal/semantic/call.cr
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class Crystal::Call
end

@uses_with_scope = true
instantiate matches, owner, self_type: nil
instantiate matches, owner, self_type: nil, with_literals: with_literals
end

def lookup_matches_in_type(owner, arg_types, named_args_types, self_type, def_name, search_in_parents, search_in_toplevel = true, with_literals = false)
Expand Down Expand Up @@ -280,7 +280,7 @@ class Crystal::Call
attach_subclass_observer instance_type.base_type
end

instantiate matches, owner, self_type
instantiate matches, owner, self_type, with_literals
end

def lookup_matches_checking_expansion(owner, signature, search_in_parents = true, with_literals = false)
Expand Down Expand Up @@ -331,7 +331,9 @@ class Crystal::Call
end
end

def instantiate(matches, owner, self_type)
def instantiate(matches, owner, self_type, with_literals)
matches.each &.remove_literals if with_literals

block = @block

typed_defs = Array(Def).new(matches.size)
Expand Down Expand Up @@ -472,7 +474,6 @@ class Crystal::Call
in_bounds = (0 <= index < instance_type.size)
if nilable || in_bounds
indexer_def = yield instance_type, (in_bounds ? index : -1)
arg_types.map!(&.remove_literal)
indexer_match = Match.new(indexer_def, arg_types, MatchContext.new(owner, owner))
return Matches.new([indexer_match] of Match, true)
elsif instance_type.size == 0
Expand All @@ -496,7 +497,6 @@ class Crystal::Call
index = instance_type.name_index(name)
if index || nilable
indexer_def = yield instance_type, (index || -1)
arg_types.map!(&.remove_literal)
indexer_match = Match.new(indexer_def, arg_types, MatchContext.new(owner, owner))
return Matches.new([indexer_match] of Match, true)
else
Expand Down Expand Up @@ -627,7 +627,6 @@ class Crystal::Call

signature = CallSignature.new(previous.name, arg_types, block, named_args_types)
context = MatchContext.new(scope, scope, def_free_vars: previous.free_vars)
arg_types.map!(&.remove_literal)
match = Match.new(previous, arg_types, context, named_args_types)
matches = Matches.new([match] of Match, true)

Expand All @@ -639,7 +638,7 @@ class Crystal::Call
parent_visitor.check_self_closured
end

typed_defs = instantiate matches, scope, self_type: nil
typed_defs = instantiate matches, scope, self_type: nil, with_literals: with_literals
typed_defs.each do |typed_def|
typed_def.next = parent_visitor.typed_def
end
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/crystal/semantic/match.cr
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ module Crystal

def initialize(@def, @arg_types, @context, @named_arg_types = nil)
end

def remove_literals
@arg_types.map!(&.remove_literal)
@named_arg_types.try &.map! { |arg| NamedArgumentType.new(arg.name, arg.type.remove_literal) }
end
end

struct Matches
Expand Down