Project

General

Profile

Bug #16248

Unclear semantics of the "numbered parameter is already used" error

Added by ibylich (Ilya Bylich) 6 months ago. Updated 6 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.0dev (2019-10-02T14:56:28Z trunk ef697388be) [x86_64-darwin18]
[ruby-core:95294]

Description

Numbered parameters now have an underscore as a prefix:

->{ _1 }.call(1)
 => 1

And it's not allowed to use them in a block if an outer block also uses them:

->{ _1; ->{_2} }
SyntaxError ((irb):4: numbered parameter is already used in)
(irb):4: outer block here
->{ _1; ->{_2} }
    ^~

even if the usage goes after declaring an inner block with numbered params:

->{ ->{_2}; _1 }
SyntaxError ((irb):5: numbered parameter is already used in)
(irb):5: inner block here
->{ ->{_2}; _1 }
       ^~

So far it's clear. Then comes the second part: if you wrap an inner block into a method you get a valid syntax:

->{ def m; ->{_2}; end; _1 }
 => #<Proc:0x00007f8666a25168 (irb):7 (lambda)>

but if you swap block statements you get an error:

>{ _1; def m; ->{_2}; end }
SyntaxError ((irb):6: numbered parameter is already used in)
(irb):6: outer block here
->{ _1; def m; ->{_2}; end }
    ^~

It looks like a consistency issue to me. Is there a strict rule that defines what is valid what is not?

#1

Updated by nobu (Nobuyoshi Nakada) 6 months ago

  • Status changed from Open to Closed

Applied in changeset git|c2065c64cb198d5b53a5ecdf9638d064f6fec2e2.


Fixed numbered parameter check

  • parse.y (struct local_vars): moved numbered parameter NODEs for nesting check to separate per local variable scopes, as numbered parameters should belong to local variable scopes. [Bug #16248]
#2

Updated by nobu (Nobuyoshi Nakada) 6 months ago

  • Backport changed from 2.5: UNKNOWN, 2.6: UNKNOWN to 2.5: DONTNEED, 2.6: DONTNEED

Also available in: Atom PDF