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

Variables Declared In () Or [] context produce unhelpful error message #12808

Open
lace-wing opened this issue May 8, 2024 · 2 comments
Open
Labels
parser Issues related to parsing unhelpful-error The error message you observe is not helpful to identify the problem

Comments

@lace-wing
Copy link

Describe the bug

As title says, mut variables declared in () or [] closures are not accessible even right in the same closure.

How to reproduce

# create a () or [] clousure
(
    # declare a mutable variable
    mut m = ""
    # try accessing the variable
    $m # Variable not found!
)

Expected behavior

Mutable variable should be accessible just as they are in non-()[] environments:

mut m = ""
$m = "Yes!"

Screenshots

image

Configuration

key value
version 0.93.0
major 0
minor 93
patch 0
branch
commit_hash
build_os macos-aarch64
build_target aarch64-apple-darwin
rust_version rustc 1.77.2 (25ef9e3d8 2024-04-09) (Homebrew)
cargo_version cargo 1.77.2
build_time 2024-04-30 22:51:13 +00:00
build_rust_channel release
allocator mimalloc
features dataframe, default, sqlite, system-clipboard, trash, which
installed_plugins gstat

Additional context

No response

@lace-wing lace-wing added the needs-triage An issue that hasn't had any proper look label May 8, 2024
@sholderbach sholderbach added scoping/name-resolution How Nu finds which variables/functions are in scope and to what they are bound 🐛 bug Something isn't working parser Issues related to parsing and removed 🐛 bug Something isn't working labels May 8, 2024
@sholderbach
Copy link
Member

To clarify () declares a subexpression and you can use a variable declared inside if the statements are sepearated with ;:

(
  let x = 10;
  $x
)  
# returns: 10

Worth diving in if we can infer that the ; is not needed here after a line break.

[] is declaring a list.
Each of the entries of the list is its own expression (with restrictions on what is allowed without parens, as our lists permit whitespace as a separator). So to me it would be surprising or a deliberate design choice if the scope of a previous expression would be valid for the next entry.

No closures involved. That term is reserved for anonymous functions that are permitted to capture values from the outside scope as their own (close over them). In Nushell their syntax is { code }/{ || code}/{ | args | code }

@sholderbach
Copy link
Member

I think the takeaway for this issue is that we should make the parsing rules of let/mut binding/assignment stricter so they produce a helpful error:

  • when placed inside a list: here the assignment expression doesn't return a useful value to be added to the list
  • When extra tokens are supplied after the complete right hand side expression of the assignment (let x = 10 extra) so we could provide a "did you forget a ;" in subexpression.

@sholderbach sholderbach added unhelpful-error The error message you observe is not helpful to identify the problem and removed needs-triage An issue that hasn't had any proper look scoping/name-resolution How Nu finds which variables/functions are in scope and to what they are bound labels May 10, 2024
@sholderbach sholderbach changed the title Mutable Variables Declared In () Or [] Closures Are Not Accessible In Their Scope Variables Declared In () Or [] context produce unhelpful error message May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parser Issues related to parsing unhelpful-error The error message you observe is not helpful to identify the problem
Projects
None yet
Development

No branches or pull requests

2 participants