Skip to content

Better error handling for negative integer exponents in ** operator#15882

Merged
sholderbach merged 4 commits intonushell:mainfrom
kumarUjjawal:clean-fix
Jun 4, 2025
Merged

Better error handling for negative integer exponents in ** operator#15882
sholderbach merged 4 commits intonushell:mainfrom
kumarUjjawal:clean-fix

Conversation

@kumarUjjawal
Copy link
Copy Markdown
Contributor

Title: Better error handling for negative integer exponents in ** operator


🐛 Bug Fix

This PR addresses an issue where attempting to raise an integer to a negative power (e.g. 10 ** -1) incorrectly triggered an OperatorOverflow error. This behavior was misleading since the overflow isn't actually the root problem — it's the unsupported operation of raising integers to negative powers.


✅ Fix Summary

  • Updated Value::pow to:

    • Check for negative exponents when both operands are integers.
    • Return a ShellError::IncorrectValue with a helpful message guiding users to use floating point values instead.

Example:

> 10 ** -1
Error: nu::shell::incorrect_value

  × Incorrect value.
   ╝─[entry #2:1:4]
 1 │ 10 ** -1
   ·    ─┬┬
   ·     │╰── encountered here
   ·     ╰── Negative exponent for integer power is unsupported; use floats instead.

🔪 Testing

Manual testing:

  • 10 ** -1 → now returns a clear and appropriate IncorrectValue error.
  • 10.0 ** -1, 10 ** -1.0, etc. continue to work as expected.

🧹 Related

Fixes #15860


Let me know if you'd like a test case or doc update included in the PR too!

@fdncred
Copy link
Copy Markdown
Contributor

fdncred commented Jun 3, 2025

I wonder if it's better to use the value's rhs span for the error message.
image

@kumarUjjawal
Copy link
Copy Markdown
Contributor Author

I wonder if it's better to use the value's rhs span for the error message. image

we can use rhs span for better pinpointing the exact cause of issues. Do you want me to work on that?

@fdncred
Copy link
Copy Markdown
Contributor

fdncred commented Jun 3, 2025

I think it looks better.

@kumarUjjawal
Copy link
Copy Markdown
Contributor Author

so rhs implementation?

@fdncred
Copy link
Copy Markdown
Contributor

fdncred commented Jun 3, 2025

This is all I did. Not sure if you want to go further.

    pub fn pow(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
        match (self, rhs) {
            (Value::Int { val: lhs, .. }, Value::Int { val: rhsv, .. }) => {
                if *rhsv < 0 {
                    return Err(ShellError::IncorrectValue {
                        msg: "Negative exponent for integer power is unsupported; use floats instead.".into(),
                        val_span: rhs.span(),
                        call_span: op,
                    });
                }
                if let Some(val) = lhs.checked_pow(*rhsv as u32) {
...

@kumarUjjawal
Copy link
Copy Markdown
Contributor Author

This look good, I will make the changes.

Copy link
Copy Markdown
Member

@sholderbach sholderbach left a comment

Choose a reason for hiding this comment

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

Thanks!

@sholderbach sholderbach merged commit fc813af into nushell:main Jun 4, 2025
16 checks passed
@github-actions github-actions bot added this to the v0.105.0 milestone Jun 4, 2025
kumarUjjawal added a commit to kumarUjjawal/nushell that referenced this pull request Jun 5, 2025
…ushell#15882)

**Title**: Better error handling for negative integer exponents in `**`
operator

---

This PR addresses an issue where attempting to raise an integer to a
negative power (e.g. `10 ** -1`) incorrectly triggered an
`OperatorOverflow` error. This behavior was misleading since the
overflow isn't actually the root problem — it's the unsupported
operation of raising integers to negative powers.

---

* Updated `Value::pow` to:

  * Check for negative exponents when both operands are integers.
* Return a `ShellError::IncorrectValue` with a helpful message guiding
users to use floating point values instead.

```bash
> 10 ** -1
Error: nu::shell::incorrect_value

  × Incorrect value.
   ╝─[entry nushell#2:1:4]
 1 │ 10 ** -1
   ·    ─┬┬
   ·     │╰── encountered here
   ·     ╰── Negative exponent for integer power is unsupported; use floats instead.
```

---

Manual testing:

* `10 ** -1` → now returns a clear and appropriate `IncorrectValue`
error.
* `10.0 ** -1`, `10 ** -1.0`, etc. continue to work as expected.

---

Fixes nushell#15860

---------

Co-authored-by: Kumar Ujjawal <kumar.ujjawal@greenpista.com>
kumarUjjawal added a commit to kumarUjjawal/nushell that referenced this pull request Jun 5, 2025
…ushell#15882)

**Title**: Better error handling for negative integer exponents in `**`
operator

---

This PR addresses an issue where attempting to raise an integer to a
negative power (e.g. `10 ** -1`) incorrectly triggered an
`OperatorOverflow` error. This behavior was misleading since the
overflow isn't actually the root problem — it's the unsupported
operation of raising integers to negative powers.

---

* Updated `Value::pow` to:

  * Check for negative exponents when both operands are integers.
* Return a `ShellError::IncorrectValue` with a helpful message guiding
users to use floating point values instead.

```bash
> 10 ** -1
Error: nu::shell::incorrect_value

  × Incorrect value.
   ╝─[entry nushell#2:1:4]
 1 │ 10 ** -1
   ·    ─┬┬
   ·     │╰── encountered here
   ·     ╰── Negative exponent for integer power is unsupported; use floats instead.
```

---

Manual testing:

* `10 ** -1` → now returns a clear and appropriate `IncorrectValue`
error.
* `10.0 ** -1`, `10 ** -1.0`, etc. continue to work as expected.

---

Fixes nushell#15860

---------

Co-authored-by: Kumar Ujjawal <kumar.ujjawal@greenpista.com>
kumarUjjawal added a commit to kumarUjjawal/nushell that referenced this pull request Jun 5, 2025
…ushell#15882)

**Title**: Better error handling for negative integer exponents in `**`
operator

---

This PR addresses an issue where attempting to raise an integer to a
negative power (e.g. `10 ** -1`) incorrectly triggered an
`OperatorOverflow` error. This behavior was misleading since the
overflow isn't actually the root problem — it's the unsupported
operation of raising integers to negative powers.

---

* Updated `Value::pow` to:

  * Check for negative exponents when both operands are integers.
* Return a `ShellError::IncorrectValue` with a helpful message guiding
users to use floating point values instead.

```bash
> 10 ** -1
Error: nu::shell::incorrect_value

  × Incorrect value.
   ╝─[entry nushell#2:1:4]
 1 │ 10 ** -1
   ·    ─┬┬
   ·     │╰── encountered here
   ·     ╰── Negative exponent for integer power is unsupported; use floats instead.
```

---

Manual testing:

* `10 ** -1` → now returns a clear and appropriate `IncorrectValue`
error.
* `10.0 ** -1`, `10 ** -1.0`, etc. continue to work as expected.

---

Fixes nushell#15860

---------

Co-authored-by: Kumar Ujjawal <kumar.ujjawal@greenpista.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Confusing error when raising an integer to a negative power

3 participants