Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: type checking for list & map literals #751

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ import {
indexedAccessIndexMustHaveCorrectType,
indexedAccessReceiverMustBeListOrMap,
infixOperationOperandsMustHaveCorrectType,
listMustNotContainNamedTuples,
mapMustNotContainNamedTuples,
namedTypeMustSetAllTypeParameters,
parameterDefaultValueTypeMustMatchParameterType,
parameterMustHaveTypeHint,
Expand Down Expand Up @@ -253,14 +255,15 @@ export const registerValidationChecks = function (services: SafeDsServices) {
lambdaParametersMustNotBeAnnotated,
lambdaParameterMustNotHaveConstModifier,
],
SdsList: [listMustNotContainNamedTuples(services)],
SdsLiteralType: [
literalTypeMustHaveLiterals,
literalTypeMustNotContainListLiteral,
literalTypeMustNotContainMapLiteral,
literalTypesShouldBeUsedWithCaution,
literalTypeShouldNotHaveDuplicateLiteral(services),
],
SdsMap: [mapsShouldBeUsedWithCaution],
SdsMap: [mapMustNotContainNamedTuples(services), mapsShouldBeUsedWithCaution],
SdsMemberAccess: [
memberAccessMustBeNullSafeIfReceiverIsNullable(services),
memberAccessNullSafetyShouldBeNeeded(services),
Expand Down
45 changes: 45 additions & 0 deletions packages/safe-ds-lang/src/language/validation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
SdsCall,
SdsIndexedAccess,
SdsInfixOperation,
SdsList,
SdsMap,
SdsNamedType,
SdsParameter,
SdsPrefixOperation,
Expand All @@ -23,6 +25,7 @@ import {
} from '../generated/ast.js';
import { getTypeArguments, getTypeParameters } from '../helpers/nodeProperties.js';
import { SafeDsServices } from '../safe-ds-module.js';
import { NamedTupleType } from '../typing/model.js';

export const CODE_TYPE_CALLABLE_RECEIVER = 'type/callable-receiver';
export const CODE_TYPE_MISMATCH = 'type/mismatch';
Expand Down Expand Up @@ -191,6 +194,48 @@ export const infixOperationOperandsMustHaveCorrectType = (services: SafeDsServic
};
};

export const listMustNotContainNamedTuples = (services: SafeDsServices) => {
const typeComputer = services.types.TypeComputer;

return (node: SdsList, accept: ValidationAcceptor): void => {
for (const element of node.elements) {
const elementType = typeComputer.computeType(element);
if (elementType instanceof NamedTupleType) {
accept('error', `Cannot add a value of type '${elementType}' to a list.`, {
node: element,
code: CODE_TYPE_MISMATCH,
});
}
}
};
};

export const mapMustNotContainNamedTuples = (services: SafeDsServices) => {
const typeComputer = services.types.TypeComputer;

return (node: SdsMap, accept: ValidationAcceptor): void => {
for (const entry of node.entries) {
const keyType = typeComputer.computeType(entry.key);
if (keyType instanceof NamedTupleType) {
accept('error', `Cannot use a value of type '${keyType}' as a map key.`, {
node: entry,
property: 'key',
code: CODE_TYPE_MISMATCH,
});
}

const valueKey = typeComputer.computeType(entry.value);
if (valueKey instanceof NamedTupleType) {
accept('error', `Cannot use a value of type '${valueKey}' as a map value.`, {
node: entry,
property: 'value',
code: CODE_TYPE_MISMATCH,
});
}
}
};
};

export const parameterDefaultValueTypeMustMatchParameterType = (services: SafeDsServices) => {
const typeChecker = services.types.TypeChecker;
const typeComputer = services.types.TypeComputer;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tests.validation.types.checking.lists

fun noResults()
fun oneResult() -> r: Int
fun twoResults() -> (r1: Int, r2: Int)

segment mySegment() {
[
// $TEST$ error "Cannot add a value of type '()' to a list."
»noResults()«,
// $TEST$ no error r"Cannot add a value of type '.*' to a list\."
»oneResult()«,
// $TEST$ error "Cannot add a value of type '(r1: Int, r2: Int)' to a list."
»twoResults()«
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tests.validation.types.checking.maps

fun noResults()
fun oneResult() -> r: Int
fun twoResults() -> (r1: Int, r2: Int)

segment mySegment() {
{
// $TEST$ error "Cannot use a value of type '()' as a map key."
// $TEST$ error "Cannot use a value of type '()' as a map value."
»noResults()«: »noResults()«,
// $TEST$ no error r"Cannot use a value of type '.*' as a map key\."
// $TEST$ no error r"Cannot use a value of type '.*' as a map value\."
»oneResult()«: »oneResult()«,
// $TEST$ error "Cannot use a value of type '(r1: Int, r2: Int)' as a map key."
// $TEST$ error "Cannot use a value of type '(r1: Int, r2: Int)' as a map value."
»twoResults()«: »twoResults()«
};
}