Diagnostic and cause
LinkNegative integer exponent
- Family
- topaz
- Phase
- run
- Code
TPZ4005
Cause and conditions
For two int operands, exponentiation rejects a negative exponent at runtime with TPZ4005. A nonnegative exponent is accepted when the exponent and result fit their required integer ranges.
Related rule: Integer exponentiation requires a nonnegative exponentResponse
Use a nonnegative integer exponent whose result fits int, or use float operands for a fractional result.
Source evidence
- compiler/crates/topaz_diag/src/explain.rs at
151141d7bb44· 544-552 - compiler/crates/topaz_product_runtime/src/diagnostic.rs at
151141d7bb44· 58-58 - compiler/crates/topaz_value/src/lib.rs at
151141d7bb44· 165-165 - compiler/crates/topaz_value/src/value/operators.rs at
151141d7bb44· 69-94 - compiler/crates/topaz_value/src/value/operators.rs at
151141d7bb44· 204-215
Compiler phase: runtime.
Compiler explanation: Integer exponentiation received a negative exponent.
Message templates are Pending. The explanation above is not an observed CLI message.
Observed for the candidate in Topaz 5.21.3. Execution
- error[TPZ4005]: integer exponent must be non-negative; use float operands
Integer exponentiation with two int operands rejects a negative exponent at runtime with TPZ4005. The guard runs before conversion to u32 and checked exponentiation.
Integer exponentiation requires a nonnegative exponent
For two int operands, exponentiation rejects a negative exponent at runtime with TPZ4005. A nonnegative exponent is accepted when the exponent and result fit their required integer ranges.
Source evidence
- compiler/crates/topaz_value/src/value/operators.rs at
151141d7bb44· 69-94 - compiler/crates/topaz_value/src/value/operators.rs at
151141d7bb44· 204-215
Response
LinkUse a nonnegative integer exponent whose result fits int, or use float operands for a fractional result.
Reproduction and verification
LinkRejected program
Linkfunction power(exponent: int) -> int {2 ** exponent}power(-1)
No stdout.
Expected result
Expected result
Rejection during run. Nonzero exit.
TPZ4005negative-integer-exponent atexample.tpz:2:5
Run these runtime diagnostic assertions with TOPAZ_BIN set using node docs/topaz-reference/templates/diagnostics-p5-runtime.product.mjs. The shared example observer currently evaluates check diagnostics.
Source evidence
- compiler/crates/topaz_value/src/value/operators.rs at
151141d7bb44· 69-94 - compiler/crates/topaz_value/src/value/operators.rs at
151141d7bb44· 204-215
Repaired program
Linkfunction power(exponent: int) -> int {2 ** exponent}power(0)
No stdout.
Expected result
Expected result
Execution completes. Exit code 0.
Run these runtime diagnostic assertions with TOPAZ_BIN set using node docs/topaz-reference/templates/diagnostics-p5-runtime.product.mjs. The shared example observer currently evaluates check diagnostics.
Source evidence
- compiler/crates/topaz_value/src/value/operators.rs at
151141d7bb44· 69-94 - compiler/crates/topaz_value/src/value/operators.rs at
151141d7bb44· 204-215