Skip to content

Fix type checking for assignment operators#16107

Merged
ysthakur merged 3 commits intonushell:mainfrom
132ikl:rework-assignment-typecheck
Jul 4, 2025
Merged

Fix type checking for assignment operators#16107
ysthakur merged 3 commits intonushell:mainfrom
132ikl:rework-assignment-typecheck

Conversation

@132ikl
Copy link
Copy Markdown
Member

@132ikl 132ikl commented Jul 3, 2025

Description

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:

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:

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:

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:

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

  • 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

Adds some tests for the examples given above

After Submitting

N/A

@132ikl 132ikl added notes:breaking-changes This PR implies a change affecting users and has to be noted in the release notes notes:fixes Include the release notes summary in the "Bug fixes" section deprecated:pr-language (deprecated: this label is too vague) This PR makes some language changes labels Jul 3, 2025
@github-actions github-actions bot added the A:parser Issues related to parsing label Jul 3, 2025
@132ikl 132ikl changed the title Rework type checking for assignment operators Fix type checking for assignment operators Jul 3, 2025
@132ikl
Copy link
Copy Markdown
Member Author

132ikl commented Jul 3, 2025

the PR title originally said "rework", but to be honest this is more of a fix than a rework since it aligns the behavior of $x = <...> to be the same as let = <...> /mut = <...> for assignments and makes compound operators work the same as their "long" versions

Copy link
Copy Markdown
Member

@ysthakur ysthakur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, this is a surprisingly simple change

@ysthakur ysthakur merged commit a4bd51a into nushell:main Jul 4, 2025
16 checks passed
@github-actions github-actions bot added this to the v0.106.0 milestone Jul 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:parser Issues related to parsing deprecated:pr-language (deprecated: this label is too vague) This PR makes some language changes notes:breaking-changes This PR implies a change affecting users and has to be noted in the release notes notes:fixes Include the release notes summary in the "Bug fixes" section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants