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

**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:

```bash
> 10 ** -1
Error: nu:🐚: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



---------

Co-authored-by: Kumar Ujjawal <kumar.ujjawal@greenpista.com>
This commit is contained in:
Kumar Ujjawal 2025-06-04 13:36:41 +05:30 committed by GitHub
parent b83aa17c96
commit fc813af4c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3293,8 +3293,16 @@ impl Value {
pub fn pow(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> { pub fn pow(&self, op: Span, rhs: &Value, span: Span) -> Result<Value, ShellError> {
match (self, rhs) { match (self, rhs) {
(Value::Int { val: lhs, .. }, Value::Int { val: rhs, .. }) => { (Value::Int { val: lhs, .. }, Value::Int { val: rhsv, .. }) => {
if let Some(val) = lhs.checked_pow(*rhs as u32) { 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) {
Ok(Value::int(val, span)) Ok(Value::int(val, span))
} else { } else {
Err(ShellError::OperatorOverflow { Err(ShellError::OperatorOverflow {