elves / elvish

Powerful scripting language & versatile interactive shell
https://elv.sh/
BSD 2-Clause "Simplified" License
5.69k stars 300 forks source link

f~ not initialized to $nop~ #1519

Closed gergelyk closed 2 years ago

gergelyk commented 2 years ago

According to https://elv.sh/ref/language.html#variable-suffix Variables with ~ suffix are initialized to nop. As far as I understand, it should be accessible through $nop~, but it seems not. As we can see below, variable was initialized to nop~ instead of nop. Corresponding $nop~~ doesn't exist.

[~]─> var f~
[~]─> is f~ $nop~
▶ $false
[~]─> put $f~
▶ <builtin nop~>
[~]─> $nop~~
compilation error: 0-6 in [tty]: variable $nop~~ not found
krader1961 commented 2 years ago

Good catch. Notice the difference:

0.000245 elvish> put $f~
▶ <builtin nop~>
0.000193 elvish> put $nop~
▶ <builtin nop>

And compare with this formulation:

0.000392 elvish> var n~ = $nop~
0.000158 elvish> put $n~
▶ <builtin nop>
0.000175 elvish> is $n~ $nop~
▶ $true
krader1961 commented 2 years ago

FYI, I have a fix for this but need to sleep on it before creating a pull-request. My fix addresses this specific situation but breaks two existing unit tests: TestAddVar and TestAddVars. My change creates an actual alias to the builtin nop command rather than using the equivalent of fn nop { } (which is why the is $f~ $nop~ test fails). I need to look at what those tests are doing to understand why they are failing with my change.

krader1961 commented 2 years ago

My simple fix for this issue addresses this narrow issue but is incorrect. It simply assigned the read-only $nop~ value as the default value of a command variable. Which is why it broke the TestAddVar and TestAddVars unit tests. A proper fix needs to make var f~ and var f~ = $nop~ equivalent.