-
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 index of indexed access is invalid (#796)
Closes #16 ### Summary of Changes * Show an error if we know that a map key does not exist. * Show an error if we know that a list index is out of bounds.
- Loading branch information
1 parent
2d1abc9
commit 5017759
Showing
6 changed files
with
209 additions
and
40 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
59 changes: 59 additions & 0 deletions
59
packages/safe-ds-lang/src/language/validation/other/expressions/indexedAccess.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,59 @@ | ||
import { SdsIndexedAccess } from '../../../generated/ast.js'; | ||
import { ValidationAcceptor } from 'langium'; | ||
import { SafeDsServices } from '../../../safe-ds-module.js'; | ||
import { EvaluatedList, EvaluatedMap, IntConstant } from '../../../partialEvaluation/model.js'; | ||
|
||
export const CODE_INDEXED_ACCESS_INVALID_INDEX = 'indexed-access/invalid-index'; | ||
|
||
export const indexedAccessIndexMustBeValid = (services: SafeDsServices) => { | ||
const coreTypes = services.types.CoreTypes; | ||
const partialEvaluator = services.evaluation.PartialEvaluator; | ||
const typeComputer = services.types.TypeComputer; | ||
|
||
return (node: SdsIndexedAccess, accept: ValidationAcceptor): void => { | ||
const indexValue = partialEvaluator.evaluate(node.index); | ||
if (!indexValue.isFullyEvaluated) { | ||
return; | ||
} | ||
|
||
const receiverValue = partialEvaluator.evaluate(node.receiver); | ||
|
||
// Check map key | ||
if (receiverValue instanceof EvaluatedMap) { | ||
if (!receiverValue.has(indexValue)) { | ||
accept('error', `Map key '${indexValue}' does not exist.`, { | ||
node, | ||
property: 'index', | ||
code: CODE_INDEXED_ACCESS_INVALID_INDEX, | ||
}); | ||
} | ||
return; | ||
} | ||
|
||
// Check list index | ||
if (!(indexValue instanceof IntConstant)) { | ||
return; | ||
} | ||
|
||
if (receiverValue instanceof EvaluatedList) { | ||
if (indexValue.value < 0 || indexValue.value >= receiverValue.size) { | ||
accept('error', `List index '${indexValue}' is out of bounds.`, { | ||
node, | ||
property: 'index', | ||
code: CODE_INDEXED_ACCESS_INVALID_INDEX, | ||
}); | ||
} | ||
} | ||
|
||
const receiverType = typeComputer.computeType(node.receiver); | ||
if (receiverType.equals(coreTypes.List)) { | ||
if (indexValue.value < 0) { | ||
accept('error', `List index '${indexValue}' is out of bounds.`, { | ||
node, | ||
property: 'index', | ||
code: CODE_INDEXED_ACCESS_INVALID_INDEX, | ||
}); | ||
} | ||
} | ||
}; | ||
}; |
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
...sources/validation/other/expressions/indexed access/list index out of bounds/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,40 @@ | ||
package tests.validation.other.indexedAccess.listIndexOutOfBounds | ||
|
||
@Pure | ||
fun myFunction() -> resultList: List<Int> | ||
|
||
segment mySegment(parameterList: List<Int>, parameterIndex: Int) { | ||
val placeholderList = [1]; | ||
|
||
// $TEST$ error "List index '-1' is out of bounds." | ||
myFunction()[»-1«]; | ||
// $TEST$ no error r"List index '.*' is out of bounds\." | ||
myFunction()[»0«]; | ||
// $TEST$ no error r"List index '.*' is out of bounds\." | ||
myFunction()[»1«]; | ||
// $TEST$ no error r"List index '.*' is out of bounds\." | ||
myFunction()[»parameterIndex«]; | ||
|
||
// $TEST$ error "List index '-1' is out of bounds." | ||
parameterList[»-1«]; | ||
// $TEST$ no error r"List index '.*' is out of bounds\." | ||
parameterList[»0«]; | ||
// $TEST$ no error r"List index '.*' is out of bounds\." | ||
parameterList[»1«]; | ||
// $TEST$ no error r"List index '.*' is out of bounds\." | ||
parameterList[»parameterIndex«]; | ||
|
||
// $TEST$ error "List index '-1' is out of bounds." | ||
placeholderList[»-1«]; | ||
// $TEST$ no error r"List index '.*' is out of bounds\." | ||
placeholderList[»0«]; | ||
// $TEST$ error "List index '1' is out of bounds." | ||
placeholderList[»1«]; | ||
// $TEST$ no error r"List index '.*' is out of bounds\." | ||
placeholderList[»parameterIndex«]; | ||
|
||
// $TEST$ no error r"List index '.*' is out of bounds\." | ||
unresolved[»-1«]; | ||
// $TEST$ no error r"List index '.*' is out of bounds\." | ||
placeholderList[»unresolved«]; | ||
} |
34 changes: 34 additions & 0 deletions
34
...resources/validation/other/expressions/indexed access/map key does not exist/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,34 @@ | ||
package tests.validation.other.indexedAccess.mapKeyDoesNotExist | ||
|
||
@Pure | ||
fun myFunction() -> resultMap: Map<String, Int> | ||
|
||
segment mySegment(parameterMap: Map<String, Int>, parameterKey: String) { | ||
val placeholderMap = {"key": 1}; | ||
|
||
// $TEST$ no error r"Map key '.*' does not exist\." | ||
myFunction()[»"unknown"«]; | ||
// $TEST$ no error r"Map key '.*' does not exist\." | ||
myFunction()[»"key"«]; | ||
// $TEST$ no error r"Map key '.*' does not exist\." | ||
myFunction()[»parameterKey«]; | ||
|
||
// $TEST$ no error r"Map key '.*' does not exist\." | ||
parameterMap[»"unknown"«]; | ||
// $TEST$ no error r"Map key '.*' does not exist\." | ||
parameterMap[»"key"«]; | ||
// $TEST$ no error r"Map key '.*' does not exist\." | ||
parameterMap[»parameterKey«]; | ||
|
||
// $TEST$ error "Map key '"unknown"' does not exist." | ||
placeholderMap[»"unknown"«]; | ||
// $TEST$ no error r"Map key '.*' does not exist\." | ||
placeholderMap[»"key"«]; | ||
// $TEST$ no error r"Map key '.*' does not exist\." | ||
placeholderMap[»parameterKey«]; | ||
|
||
// $TEST$ no error r"Map key '.*' does not exist\." | ||
unresolved[»"unknown"«]; | ||
// $TEST$ no error r"Map key '.*' does not exist\." | ||
placeholderMap[»unresolved«]; | ||
} |