mirror of
https://github.com/nushell/nushell.git
synced 2025-07-08 10:27:47 +02:00
<!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> Rel: #14429, #16079 Finishes up a TODO in the assignment type checking. - For regular assignment operations (only applies to `mut`), type checking is now done using `type_compatible` (which is what `let` uses) - This allows some mutable assignments to work which weren't allowed before Before: ```nushell let x: glob = "" # => ok, no error mut x: glob = ""; $x = "" # => Error: nu::parser::operator_incompatible_types # => # => × Types 'glob' and 'string' are not compatible for the '=' operator. # => ╭─[entry #6:1:19] # => 1 │ mut x: glob = ""; $x = "" # => · ─┬ ┬ ─┬ # => · │ │ ╰── string # => · │ ╰── does not operate between 'glob' and 'string' # => · ╰── glob # => ╰──── let x: number = 1 # ok, no error mut x: number = 1; $x = 2 # => Error: nu::parser::operator_incompatible_types # => # => × Types 'number' and 'int' are not compatible for the '=' operator. # => ╭─[source:1:20] # => 1 │ mut x: number = 1; $x = 2 # => · ─┬ ┬ ┬ # => · │ │ ╰── int # => · │ ╰── does not operate between 'number' and 'int' # => · ╰── number # => ╰──── ``` After: ```nushell let x: glob = "" # ok, no error (same as before) mut x: glob = ""; $x = "" # ok, no error let x: number = 1 # ok, no error (same as before) mut x: number = 1; $x = 2 # ok, no error ``` - Properly type check compound operations. First checks if the operation (eg. `+` for `+=`) type checks successfully, and then checks if the assignment type checks successfully (also using `type_compatible`) - This fixes some issues where the "long version" of a compound assignment operator would error, but the compound assignment operator itself would not Before: ```nushell mut x = 1; $x = $x / 2 # => Error: nu::parser::operator_incompatible_types # => # => × Types 'int' and 'float' are not compatible for the '=' operator. # => ╭─[entry #15:1:12] # => 1 │ mut x = 1; $x = $x / 2 # => · ─┬ ┬ ───┬── # => · │ │ ╰── float # => · │ ╰── does not operate between 'int' and 'float' # => · ╰── int # => ╰──── mut x = 1; $x /= 2 # uh oh, no error... mut x = (date now); $x = $x - 2019-05-10 # => Error: nu::parser::operator_incompatible_types # => # => × Types 'datetime' and 'duration' are not compatible for the '=' operator. # => ╭─[entry #1:1:21] # => 1 │ mut x = (date now); $x = $x - 2019-05-10 # => · ─┬ ┬ ───────┬─────── # => · │ │ ╰── duration # => · │ ╰── does not operate between 'datetime' and 'duration' # => · ╰── datetime # => ╰──── mut x = (date now); $x -= 2019-05-10 # uh oh, no error... (the result of this is a duration, not a datetime) ``` After: ```nushell mut x = 1; $x = $x / 2 # => Error: nu::parser::operator_incompatible_types # => # => × Types 'int' and 'float' are not compatible for the '=' operator. # => ╭─[entry #5:1:12] # => 1 │ mut x = 1; $x = $x / 2 # => · ─┬ ┬ ───┬── # => · │ │ ╰── float # => · │ ╰── does not operate between 'int' and 'float' # => · ╰── int # => ╰──── mut x = (date now); $x -= 2019-05-10 # => Error: nu::parser::operator_incompatible_types # => # => × Types 'datetime' and 'datetime' are not compatible for the '-=' operator. # => ╭─[entry #11:1:21] # => 1 │ mut x = (date now); $x -= 2019-05-10 # => · ─┬ ─┬ ─────┬──── # => · │ │ ╰── datetime # => · │ ╰── does not operate between 'datetime' and 'datetime' # => · ╰── datetime # => ╰──── # => help: The result type of this operation is not compatible with the type of the variable. ``` This is technically a breaking change if you relied on the old behavior (for example, there was a test that broke after this change because it relied on `/=` improperly type checking) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> * Mutable assignment operations now use the same type checking rules as normal assignments * For example, `$x = 123` now uses the same type checking rules as `let x = 123` or `mut x = 123` * Compound assignment operations now type check using the same rules as the operation they use * Assignment errors will also now highlight the invalid assignment operator in red # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> Adds some tests for the examples given above # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --> N/A