Skip to content

Commit

Permalink
Syntax: disallow named argument as return/break/next/yield arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust committed Jul 17, 2018
1 parent c6f4fd9 commit f1cc13e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,11 @@ module Crystal

assert_syntax_error "case #{keyword}; when 1; end; end", "void value expression"
assert_syntax_error "case 1; when #{keyword}; end; end", "void value expression"

assert_syntax_error "#{keyword} foo: 1", "named argument is not allowed here", line: 1, column: keyword.size + 2
assert_syntax_error %(#{keyword} "foo bar": 1), "named argument is not allowed here", line: 1, column: keyword.size + 2
assert_syntax_error "#{keyword} 1, foo: 2", "named argument is not allowed here", line: 1, column: keyword.size + 5
assert_syntax_error %(#{keyword} 1, "foo bar": 2), "named argument is not allowed here", line: 1, column: keyword.size + 5
end

it_parses "yield", Yield.new
Expand All @@ -662,6 +667,11 @@ module Crystal
it_parses "yield 1 if true", If.new(true.bool, Yield.new([1.int32] of ASTNode))
it_parses "yield if true", If.new(true.bool, Yield.new)

assert_syntax_error "yield foo: 1", "named argument is not allowed here", line: 1, column: 7
assert_syntax_error %(yield "foo bar": 1), "named argument is not allowed here", line: 1, column: 7
assert_syntax_error "yield 1, foo: 2", "named argument is not allowed here", line: 1, column: 10
assert_syntax_error %(yield 1, "foo bar": 2), "named argument is not allowed here", line: 1, column: 10

it_parses "Int", "Int".path

it_parses "Int[]", Call.new("Int".path, "[]")
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4150,10 +4150,12 @@ module Crystal
end

if @token.type == :IDENT && current_char == ':'
unexpected_named_arg if control
return parse_call_args_named_args(@token.location, args, first_name: nil, allow_newline: true)
else
arg = parse_call_arg(found_double_splat)
if @token.type == :":" && arg.is_a?(StringLiteral)
unexpected_named_arg(arg.location) if control
return parse_call_args_named_args(arg.location.not_nil!, args, first_name: arg.value, allow_newline: true)
else
args << arg
Expand Down Expand Up @@ -4261,10 +4263,12 @@ module Crystal
end

if @token.type == :IDENT && current_char == ':'
unexpected_named_arg if control
return parse_call_args_named_args(@token.location, args, first_name: nil, allow_newline: false)
else
arg = parse_call_arg(found_double_splat)
if @token.type == :":" && arg.is_a?(StringLiteral)
unexpected_named_arg(arg.location) if control
return parse_call_args_named_args(arg.location.not_nil!, args, first_name: arg.value, allow_newline: false)
else
args << arg
Expand All @@ -4290,6 +4294,10 @@ module Crystal
@call_args_nest -= 1
end

def unexpected_named_arg(loc = @token.location)
raise "named argument is not allowed here", loc.not_nil!
end

def parse_call_args_named_args(location, args, first_name, allow_newline)
named_args = parse_named_args(location, first_name: first_name, allow_newline: allow_newline)

Expand Down

0 comments on commit f1cc13e

Please sign in to comment.