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

Check that def, macro, and block parameters don't end with ? or ! #12197

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,25 @@ module Crystal
assert_syntax_error "foo { |(#{kw}))| }", "cannot use '#{kw}' as a block parameter name", 1, 9
end

# #10917
%w(
bar? bar!
).each do |name|
assert_syntax_error "def foo(#{name}); end", "invalid param name", 1, 14
assert_syntax_error "def foo(foo #{name}); end", "invalid param name", 1, 17
it_parses "def foo(#{name} foo); end", Def.new("foo", [Arg.new("foo", external_name: name.to_s)])

assert_syntax_error "macro foo(#{name}); end", "invalid param name", 1, 16
assert_syntax_error "macro foo(foo #{name}); end", "invalid param name", 1, 19
it_parses "macro foo(#{name} foo); end", Macro.new("foo", [Arg.new("foo", external_name: name.to_s)], body: MacroLiteral.new(" "))

it_parses "foo(#{name})", Call.new(nil, "foo", [name.call] of ASTNode)
it_parses "foo #{name}", Call.new(nil, "foo", [name.call] of ASTNode)

assert_syntax_error "foo { |#{name})| }", "invalid param name", 1, 12
assert_syntax_error "foo { |(#{name}))| }", "invalid param name", 1, 13
end

it_parses "def self.foo\n1\nend", Def.new("foo", body: 1.int32, receiver: "self".var)
it_parses "def self.foo()\n1\nend", Def.new("foo", body: 1.int32, receiver: "self".var)
it_parses "def self.foo=\n1\nend", Def.new("foo=", body: 1.int32, receiver: "self".var)
Expand Down
11 changes: 11 additions & 0 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3921,6 +3921,8 @@ module Crystal
raise "when specified, external name must be different than internal name", @token
end

check_valid_param_name(arg_name)

uses_arg = false
do_next_token = true
when .instance_var?
Expand Down Expand Up @@ -3984,6 +3986,7 @@ module Crystal
raise "cannot use '#{invalid_internal_name}' as a parameter name", invalid_internal_name
end
arg_name = external_name
check_valid_param_name(arg_name)
else
unexpected_token
end
Expand Down Expand Up @@ -4033,6 +4036,12 @@ module Crystal
end
end

def check_valid_param_name(param_name)
if param_name.ends_with?(/\?|!/)
raise "invalid param name"
end
Copy link
Contributor

@Sija Sija Sep 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small optimization (no regex) + rephrased error message to make it more clear about the name in question:

Suggested change
if param_name.ends_with?(/\?|!/)
raise "invalid param name"
end
if param_name[-1]?.in?('?', '!')
raise "invalid parameter name: #{param_name}"
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I use this version I get a bunch of Index out of bounds (IndexError) errors.

I guess I should first check if the param_name string is not empty, what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can go with this:

Suggested change
if param_name.ends_with?(/\?|!/)
raise "invalid param name"
end
if param_name.ends_with?("?") || param_name.ends_with?("!")
raise "invalid param name"
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've edited the suggestion

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so the choice is between:

param_name[-1]?.in?('?', '!')
!param_name.empty? && param_name[-1].in?('?', '!')
param_name.ends_with?("?") || param_name.ends_with?("!")

I think the last one is the most clear.

Is there an advantage in using version 1 or 2?
Which one is the most idiomatic?

end

def parse_if(check_end = true)
location = @token.location

Expand Down Expand Up @@ -4351,6 +4360,7 @@ module Crystal
end

arg_name = @token.value.to_s
check_valid_param_name(arg_name)

if all_names.includes?(arg_name)
raise "duplicated block parameter name: #{arg_name}", @token
Expand All @@ -4372,6 +4382,7 @@ module Crystal
end

sub_arg_name = @token.value.to_s
check_valid_param_name(sub_arg_name)

if all_names.includes?(sub_arg_name)
raise "duplicated block parameter name: #{sub_arg_name}", @token
Expand Down