Project

General

Profile

Bug #9669

Inconsistent SyntaxError in Ruby 2.1 mandatory keyword arguments definition without parentheses.

Added by tejanium (Teja Sophista) over 5 years ago. Updated over 5 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
2.1.1
[ruby-core:61658]

Description

Ruby allowed us to define method with arguments without parentheses.

def foo a:, b:
  'bar'
end
#=> :foo

def foo a:, b:
  puts 'bar'
end
#=> syntax error

Related issues

Related to Ruby master - Bug #10279: Syntax error on Hash with symbol syntax and nested expression: 2.1.3 regression Closed09/22/2014Actions
Has duplicate Ruby master - Bug #9722: Failure with multiple keyword argumentsClosed04/10/2014Actions

Associated revisions

Revision d7442c32
Added by nobu (Nobuyoshi Nakada) over 5 years ago

parse.y: required kwarg without parentheses

  • parse.y (lex_state_e, parser_params, f_arglist, parser_yylex): separate EXPR_LABELARG from EXPR_BEG and let newline significant, so that required keyword argument can place at the end of argument list without parentheses. [ruby-core:61658] [Bug #9669]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45405 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 45405
Added by nobu (Nobuyoshi Nakada) over 5 years ago

parse.y: required kwarg without parentheses

  • parse.y (lex_state_e, parser_params, f_arglist, parser_yylex): separate EXPR_LABELARG from EXPR_BEG and let newline significant, so that required keyword argument can place at the end of argument list without parentheses. [ruby-core:61658] [Bug #9669]

Revision 45405
Added by nobu (Nobuyoshi Nakada) over 5 years ago

parse.y: required kwarg without parentheses

  • parse.y (lex_state_e, parser_params, f_arglist, parser_yylex): separate EXPR_LABELARG from EXPR_BEG and let newline significant, so that required keyword argument can place at the end of argument list without parentheses. [ruby-core:61658] [Bug #9669]

Revision 45405
Added by nobu (Nobuyoshi Nakada) over 5 years ago

parse.y: required kwarg without parentheses

  • parse.y (lex_state_e, parser_params, f_arglist, parser_yylex): separate EXPR_LABELARG from EXPR_BEG and let newline significant, so that required keyword argument can place at the end of argument list without parentheses. [ruby-core:61658] [Bug #9669]

Revision 45405
Added by nobu (Nobuyoshi Nakada) over 5 years ago

parse.y: required kwarg without parentheses

  • parse.y (lex_state_e, parser_params, f_arglist, parser_yylex): separate EXPR_LABELARG from EXPR_BEG and let newline significant, so that required keyword argument can place at the end of argument list without parentheses. [ruby-core:61658] [Bug #9669]

Revision 45405
Added by nobu (Nobuyoshi Nakada) over 5 years ago

parse.y: required kwarg without parentheses

  • parse.y (lex_state_e, parser_params, f_arglist, parser_yylex): separate EXPR_LABELARG from EXPR_BEG and let newline significant, so that required keyword argument can place at the end of argument list without parentheses. [ruby-core:61658] [Bug #9669]

Revision 45405
Added by nobu (Nobuyoshi Nakada) over 5 years ago

parse.y: required kwarg without parentheses

  • parse.y (lex_state_e, parser_params, f_arglist, parser_yylex): separate EXPR_LABELARG from EXPR_BEG and let newline significant, so that required keyword argument can place at the end of argument list without parentheses. [ruby-core:61658] [Bug #9669]

Revision dcd1e5e5
Added by nobu (Nobuyoshi Nakada) over 5 years ago

parse.y: required kwarg without parentheses

  • parse.y (parser_yylex): only a newline after label should be significant. [ruby-core:61658] [Bug #9669]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45408 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 45408
Added by nobu (Nobuyoshi Nakada) over 5 years ago

parse.y: required kwarg without parentheses

  • parse.y (parser_yylex): only a newline after label should be significant. [ruby-core:61658] [Bug #9669]

Revision 45408
Added by nobu (Nobuyoshi Nakada) over 5 years ago

parse.y: required kwarg without parentheses

  • parse.y (parser_yylex): only a newline after label should be significant. [ruby-core:61658] [Bug #9669]

Revision 45408
Added by nobu (Nobuyoshi Nakada) over 5 years ago

parse.y: required kwarg without parentheses

  • parse.y (parser_yylex): only a newline after label should be significant. [ruby-core:61658] [Bug #9669]

Revision 45408
Added by nobu (Nobuyoshi Nakada) over 5 years ago

parse.y: required kwarg without parentheses

  • parse.y (parser_yylex): only a newline after label should be significant. [ruby-core:61658] [Bug #9669]

Revision 45408
Added by nobu (Nobuyoshi Nakada) over 5 years ago

parse.y: required kwarg without parentheses

  • parse.y (parser_yylex): only a newline after label should be significant. [ruby-core:61658] [Bug #9669]

Revision 45408
Added by nobu (Nobuyoshi Nakada) over 5 years ago

parse.y: required kwarg without parentheses

  • parse.y (parser_yylex): only a newline after label should be significant. [ruby-core:61658] [Bug #9669]

Revision 4e71ea3a
Added by nagachika (Tomoyuki Chikanaga) over 5 years ago

merge revision(s) r45405,r45408: [Backport #9669] [Backport #9740]

    * parse.y (lex_state_e, parser_params, f_arglist, parser_yylex):
      separate EXPR_LABELARG from EXPR_BEG and let newline significant,
      so that required keyword argument can place at the end of
      argument list without parentheses.  [ruby-core:61658] [Bug #9669]

    * parse.y (parser_yylex): only a newline after label should be
      significant.  [ruby-core:61658] [Bug #9669]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@46005 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 46005
Added by nagachika (Tomoyuki Chikanaga) over 5 years ago

merge revision(s) r45405,r45408: [Backport #9669] [Backport #9740]

* parse.y (lex_state_e, parser_params, f_arglist, parser_yylex):
  separate EXPR_LABELARG from EXPR_BEG and let newline significant,
  so that required keyword argument can place at the end of
  argument list without parentheses.  [ruby-core:61658] [Bug #9669]

* parse.y (parser_yylex): only a newline after label should be
  significant.  [ruby-core:61658] [Bug #9669]

History

Updated by sawa (Tsuyoshi Sawada) over 5 years ago

That is consistent. In the first example, 'bar' is interpreted as the default value of b. In the second example, the expression puts 'bar' is inappropriate as the default value, hence the error.

Updated by phluid61 (Matthew Kerwin) over 5 years ago

It's because of line continuations. It interpreted the code as:

def foo a:, b: 'bar'
  # returns `nil`
end

def foo a:, b: puts 'bar'  #<< syntax error
end

There are two ways to add parentheses to make it legal:

## CODE           |  ## INTERPRETED AS
                  |
def foo a:, b:    |  def foo a:, b: puts('bar')
  puts('bar')     |    # returns `nil`
end               |  end
                  |
def baz(a:, b:)   |  def foo(a:, b:)
  puts 'bar'      |    puts 'bar'
end               |  end

I think Ruby should drop the line continuation, and interpret all three code samples like the second case above, even though it might be hard to solve with the current parser.

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

Or put a semicolon after b:.

Matthew Kerwin wrote:

I think Ruby should drop the line continuation, and interpret all three code samples like the second case above, even though it might be hard to solve with the current parser.

Do you mean all line continuations?

Updated by phluid61 (Matthew Kerwin) over 5 years ago

Nobuyoshi Nakada wrote:

Do you mean all line continuations?

I don't think so; the only one that ever comes up as a gotcha is a final required keyword argument in a function definition.

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

Applied in changeset r45405.


parse.y: required kwarg without parentheses

  • parse.y (lex_state_e, parser_params, f_arglist, parser_yylex): separate EXPR_LABELARG from EXPR_BEG and let newline significant, so that required keyword argument can place at the end of argument list without parentheses. [ruby-core:61658] [Bug #9669]

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

  • Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN to 2.0.0: DONTNEED, 2.1: REQUIRED

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

  • Status changed from Closed to Open

It doesn't work yet...

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

  • Status changed from Open to Closed

Applied in changeset r45408.


parse.y: required kwarg without parentheses

  • parse.y (parser_yylex): only a newline after label should be significant. [ruby-core:61658] [Bug #9669]

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

  • Has duplicate Bug #9722: Failure with multiple keyword arguments added

Updated by nagachika (Tomoyuki Chikanaga) over 5 years ago

  • Backport changed from 2.0.0: DONTNEED, 2.1: REQUIRED to 2.0.0: DONTNEED, 2.1: DONE

r45405 and r45408 were backported into ruby_2_1 branch at r46005.

Updated by nobu (Nobuyoshi Nakada) about 5 years ago

  • Related to Bug #10279: Syntax error on Hash with symbol syntax and nested expression: 2.1.3 regression added

Also available in: Atom PDF