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

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

Open
wdv4758h opened this issue Apr 28, 2024 · 2 comments
Open

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

wdv4758h opened this issue Apr 28, 2024 · 2 comments
Labels
🐛 bug Something isn't working regression Something that worked does not work anymore. scoping/name-resolution How Nu finds which variables/functions are in scope and to what they are bound

Comments

@wdv4758h
Copy link

wdv4758h commented Apr 28, 2024

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

@wdv4758h wdv4758h added the needs-triage An issue that hasn't had any proper look label Apr 28, 2024
@wdv4758h wdv4758h 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) Apr 28, 2024
@dedebenui
Copy link

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?

@sholderbach sholderbach added 🐛 bug Something isn't working scoping/name-resolution How Nu finds which variables/functions are in scope and to what they are bound regression Something that worked does not work anymore. and removed needs-triage An issue that hasn't had any proper look labels May 2, 2024
@WindSoilder
Copy link
Collaborator

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working regression Something that worked does not work anymore. scoping/name-resolution How Nu finds which variables/functions are in scope and to what they are bound
Projects
None yet
Development

No branches or pull requests

4 participants