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

Don't use named argument key names as parameters for method_missing calls #10388

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
13 changes: 7 additions & 6 deletions spec/compiler/codegen/method_missing_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,12 @@ describe "Code gen: method_missing" do
)).to_string.should eq("bar")
end

it "works with named arguments, using names (#3654)" do
it "works with named arguments (#3654)" do
run(%(
class A
macro method_missing(call)
x &+ y
{{call.named_args[0].value}} &+
{{call.named_args[1].value}}
end
end

Expand All @@ -395,17 +396,17 @@ describe "Code gen: method_missing" do
)).to_i.should eq(3)
end

it "works with named arguments, named args in call (#3654)" do
it "works with named arguments that aren't legal variable names (#10381)" do
run(%(
class A
macro method_missing(call)
{{call.named_args[0].name}} &+
{{call.named_args[1].name}}
{{call.named_args[0].value}} &+
{{call.named_args[1].value}}
end
end

a = A.new
a.b(x: 1, y: 2)
a.b("@x": 1, Y: 2)
)).to_i.should eq(3)
end

Expand Down
19 changes: 11 additions & 8 deletions src/compiler/crystal/semantic/method_missing.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,19 @@ module Crystal
name_node = StringLiteral.new(signature.name)
args_nodes = [] of ASTNode
named_args_nodes = nil
args_nodes_names = Set(String).new
args_nodes_names = [] of {String?, String} # external <-> internal name
signature.arg_types.each_index do |index|
arg_node_name = "_arg#{index}"
args_nodes << MacroId.new(arg_node_name)
args_nodes_names << arg_node_name
args_nodes_names << {nil, arg_node_name}
end
if named_args = signature.named_args
args_nodes_names << ""
named_args.each do |named_arg|
args_nodes_names << {nil, ""}
named_args.each_with_index do |named_arg, index|
named_arg_node_name = "_named_arg#{index}"
named_args_nodes ||= [] of NamedArgument
named_args_nodes << NamedArgument.new(named_arg.name, MacroId.new(named_arg.name))
args_nodes_names << named_arg.name
named_args_nodes << NamedArgument.new(named_arg.name, MacroId.new(named_arg_node_name))
args_nodes_names << {named_arg.name, named_arg_node_name}
end
end
if block = signature.block
Expand All @@ -56,7 +57,7 @@ module Crystal
block_node = Nop.new
end

a_def = Def.new(signature.name, args_nodes_names.map { |name| Arg.new(name) })
a_def = Def.new(signature.name, args_nodes_names.map { |ext_name, name| Arg.new(name, external_name: ext_name) })
a_def.splat_index = signature.arg_types.size if signature.named_args

call = Call.new(nil, signature.name,
Expand All @@ -70,9 +71,11 @@ module Crystal
# Check if the expanded macro is a def. We do this
# by just lexing the result and seeing if the first
# token is `def`
macro_vars = Set(String).new
args_nodes_names.each { |_, name| macro_vars << name }
expands_to_def = starts_with_def?(expanded_macro)
generated_nodes =
program.parse_macro_source(expanded_macro, macro_expansion_pragmas, method_missing, method_missing, args_nodes_names) do |parser|
program.parse_macro_source(expanded_macro, macro_expansion_pragmas, method_missing, method_missing, macro_vars) do |parser|
if expands_to_def
parser.parse
else
Expand Down