-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: error if divisor is zero (#644)
Closes partially #543 ### Summary of Changes Show an error if we know that the divisor of a division is zero. Once the partial evaluator gets fully implemented, this will work in more cases. --------- Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
- Loading branch information
1 parent
8b076e7
commit 9af3b81
Showing
5 changed files
with
149 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
40 changes: 40 additions & 0 deletions
40
src/language/validation/other/expressions/infixOperations.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { SdsInfixOperation } from '../../../generated/ast.js'; | ||
import { ValidationAcceptor } from 'langium'; | ||
import { SafeDsServices } from '../../../safe-ds-module.js'; | ||
import { toConstantExpressionOrUndefined } from '../../../partialEvaluation/toConstantExpressionOrUndefined.js'; | ||
import { SdsConstantFloat, SdsConstantInt } from '../../../partialEvaluation/model.js'; | ||
import { UnknownType } from '../../../typing/model.js'; | ||
|
||
export const CODE_INFIX_OPERATION_DIVISION_BY_ZERO = 'infix-operation/division-by-zero'; | ||
|
||
export const divisionDivisorMustNotBeZero = (services: SafeDsServices) => { | ||
const typeComputer = services.types.TypeComputer; | ||
const zeroInt = new SdsConstantInt(BigInt(0)); | ||
const zeroFloat = new SdsConstantFloat(0.0); | ||
const minusZeroFloat = new SdsConstantFloat(-0.0); | ||
|
||
return (node: SdsInfixOperation, accept: ValidationAcceptor): void => { | ||
if (node.operator !== '/') { | ||
return; | ||
} | ||
|
||
const dividendType = typeComputer.computeType(node.leftOperand); | ||
if ( | ||
dividendType === UnknownType || | ||
(!dividendType.equals(typeComputer.Int) && !dividendType.equals(typeComputer.Float)) | ||
) { | ||
return; | ||
} | ||
|
||
const divisorValue = toConstantExpressionOrUndefined(node.rightOperand); | ||
if ( | ||
divisorValue && | ||
(divisorValue.equals(zeroInt) || divisorValue.equals(zeroFloat) || divisorValue.equals(minusZeroFloat)) | ||
) { | ||
accept('error', 'Division by zero.', { | ||
node, | ||
code: CODE_INFIX_OPERATION_DIVISION_BY_ZERO, | ||
}); | ||
} | ||
}; | ||
}; |
This file contains 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
38 changes: 38 additions & 0 deletions
38
tests/resources/validation/other/expressions/infix operations/division by zero/main.sdstest
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package tests.validation.other.expressions.infixOperations.divisionByZero | ||
|
||
pipeline test { | ||
// $TEST$ error "Division by zero." | ||
»1.0 / 0.0«; | ||
// $TEST$ error "Division by zero." | ||
»1.0 / -0.0«; | ||
// $TEST$ no error "Division by zero." | ||
»1.0 / 1.0«; | ||
|
||
// $TEST$ error "Division by zero." | ||
»1.0 / 0«; | ||
// $TEST$ no error "Division by zero." | ||
»1.0 / 1«; | ||
|
||
// $TEST$ error "Division by zero." | ||
»1 / 0.0«; | ||
// $TEST$ error "Division by zero." | ||
»1 / -0.0«; | ||
// $TEST$ no error "Division by zero." | ||
»1 / 1.0«; | ||
|
||
// $TEST$ error "Division by zero." | ||
»1 / 0«; | ||
// $TEST$ no error "Division by zero." | ||
»1 / 1«; | ||
|
||
// $TEST$ no error "Division by zero." | ||
»null / 0.0«; | ||
// $TEST$ no error "Division by zero." | ||
»null / -0.0«; | ||
// $TEST$ no error "Division by zero." | ||
»null / 1.0«; | ||
// $TEST$ no error "Division by zero." | ||
»null / 0«; | ||
// $TEST$ no error "Division by zero." | ||
»null / 1«; | ||
} |