Skip to content

it is somehow a special variable in closure (since 0.92) #12690

Closed

Description

@wdv4758h

Describe the bug

See more info in discussion link.

Discussed in #12440

Originally posted by wdv4758h April 7, 2024
With 0.92, it seems that it is treated differently.

This print 42 in 0.91, but gets error in 0.92.

let tmp = {
  let it = 42
  print $it
}

do -c $tmp

errors in 0.92:

  × Missing parameter: $it.
   ╭─[example.nu:6:1]
 5 │ 
 6 │ do -c $tmp
   · ─┬
   ·  ╰── missing parameter: $it
   ╰────

This print 42 in both 0.91 and 0.92.

let tmp = {
  let x = 42
  print $x
}

do -c $tmp

How to reproduce

See above code.

Expected behavior

Works without error.

Screenshots

No response

Configuration

key value
version 0.92.3
major 0
minor 92
patch 3
branch nightly
commit_hash 24ecb84
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.77.2 (25ef9e3d8 2024-04-09)
rust_channel 1.77.2-x86_64-unknown-linux-gnu
cargo_version cargo 1.77.2 (e52e36006 2024-03-26)
build_time 2024-04-28 01:44:34 +00:00
build_rust_channel release
allocator mimalloc
features dataframe, default, sqlite, static-link-openssl, system-clipboard, trash, which
installed_plugins query

Additional context

No response

Activity

  1. added
    status:needs-triage
    on Apr 28, 2024
  2. changed the title [-]`it` is somehow a special variable in closure (in 0.92)[/-] [+]`it` is somehow a special variable in closure (since 0.92)[/+] on Apr 28, 2024
  3. dedebenui commented on Apr 29, 2024

    @dedebenui

    In do_.rs in the nu-cmd-lang crate, we get a reference to the do block with the following line (l. 78 I think):

    let block = engine_state.get_block(block.block_id);

    With a debug print, we see that somehow, the block has a positional argument in its signature when $it is declared and referenced inside. If I add a positional parameter like so

    do {|x| print $x; let $it = 2; print $it} 5

    it works as expected (it prints 5, then 2) and the block only has one positional argument x

    This behavior seems to come parser.rs:4451 in nu-parser. If a signature already exists (like the |x| above), it takes it, but if not, it creates one if $it is in the block, not sure why. Could we get rid of the else block there?

  4. added
    category:bug
    A:scoping/name-resolution
    category:regression
    and removed
    status:needs-triage
    on May 2, 2024
  5. WindSoilder commented on May 15, 2024

    @WindSoilder
    Contributor

    @dedebenui Excellent hack! I think nushell once allowed such syntax before(yeah version 0.44):

    let x = { echo $it };

    In this way we need to run do $x with additional argument. But it's not allowed anymore, so I think we can get rid of else block there.

  6. added a commit that references this issue on May 17, 2024
    8adf340
  7. added this to the v0.94.0 milestone on May 17, 2024
  8. added a commit that references this issue on May 18, 2024
    6186da8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:scoping/name-resolutioncategory:bugcategory:regression

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions