Better error handling for negative integer exponents in ** operator#15882
Merged
sholderbach merged 4 commits intonushell:mainfrom Jun 4, 2025
Merged
Better error handling for negative integer exponents in ** operator#15882sholderbach merged 4 commits intonushell:mainfrom
sholderbach merged 4 commits intonushell:mainfrom
Conversation
Contributor
Contributor
Author
Contributor
|
I think it looks better. |
Contributor
Author
|
so rhs implementation? |
Contributor
|
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) {
... |
Contributor
Author
|
This look good, I will make the changes. |
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


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 anOperatorOverflowerror. 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::powto:ShellError::IncorrectValuewith a helpful message guiding users to use floating point values instead.Example:
🔪 Testing
Manual testing:
10 ** -1→ now returns a clear and appropriateIncorrectValueerror.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!