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

fix: NPEs during validation #727

Merged
merged 1 commit into from
Nov 6, 2023
Merged
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
15 changes: 9 additions & 6 deletions packages/safe-ds-lang/src/language/validation/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { AstNode, getContainerOfType, ValidationAcceptor } from 'langium';
import { isEmpty } from '../../helpers/collectionUtils.js';
import { pluralize } from '../../helpers/stringUtils.js';
import {
isSdsAnnotation,
isSdsCallable,
Expand All @@ -21,8 +23,6 @@ import {
} from '../generated/ast.js';
import { getTypeArguments, getTypeParameters } from '../helpers/nodeProperties.js';
import { SafeDsServices } from '../safe-ds-module.js';
import { pluralize } from '../../helpers/stringUtils.js';
import { isEmpty } from '../../helpers/collectionUtils.js';

export const CODE_TYPE_CALLABLE_RECEIVER = 'type/callable-receiver';
export const CODE_TYPE_MISMATCH = 'type/mismatch';
Expand Down Expand Up @@ -76,12 +76,12 @@ export const callReceiverMustBeCallable = (services: SafeDsServices) => {
}

const callable = nodeMapper.callToCallable(node);
if (!callable || isSdsAnnotation(callable)) {
if (node.receiver && (!callable || isSdsAnnotation(callable))) {
accept('error', 'This expression is not callable.', {
node: node.receiver,
code: CODE_TYPE_CALLABLE_RECEIVER,
});
} else if (isSdsClass(callable) && !callable.parameterList) {
} else if (node.receiver && isSdsClass(callable) && !callable.parameterList) {
accept('error', 'Cannot instantiate a class that has no constructor.', {
node: node.receiver,
code: CODE_TYPE_CALLABLE_RECEIVER,
Expand All @@ -98,6 +98,7 @@ export const indexedAccessReceiverMustBeListOrMap = (services: SafeDsServices) =
return (node: SdsIndexedAccess, accept: ValidationAcceptor): void => {
const receiverType = typeComputer.computeType(node.receiver);
if (
node.receiver &&
!typeChecker.isAssignableTo(receiverType, coreTypes.List) &&
!typeChecker.isAssignableTo(receiverType, coreTypes.Map)
) {
Expand Down Expand Up @@ -140,13 +141,13 @@ export const infixOperationOperandsMustHaveCorrectType = (services: SafeDsServic
switch (node.operator) {
case 'or':
case 'and':
if (!typeChecker.isAssignableTo(leftType, coreTypes.Boolean)) {
if (node.leftOperand && !typeChecker.isAssignableTo(leftType, coreTypes.Boolean)) {
accept('error', `Expected type '${coreTypes.Boolean}' but got '${leftType}'.`, {
node: node.leftOperand,
code: CODE_TYPE_MISMATCH,
});
}
if (!typeChecker.isAssignableTo(rightType, coreTypes.Boolean)) {
if (node.rightOperand && !typeChecker.isAssignableTo(rightType, coreTypes.Boolean)) {
accept('error', `Expected type '${coreTypes.Boolean}' but got '${rightType}'.`, {
node: node.rightOperand,
code: CODE_TYPE_MISMATCH,
Expand All @@ -162,6 +163,7 @@ export const infixOperationOperandsMustHaveCorrectType = (services: SafeDsServic
case '*':
case '/':
if (
node.leftOperand &&
!typeChecker.isAssignableTo(leftType, coreTypes.Float) &&
!typeChecker.isAssignableTo(leftType, coreTypes.Int)
) {
Expand All @@ -171,6 +173,7 @@ export const infixOperationOperandsMustHaveCorrectType = (services: SafeDsServic
});
}
if (
node.rightOperand &&
!typeChecker.isAssignableTo(rightType, coreTypes.Float) &&
!typeChecker.isAssignableTo(rightType, coreTypes.Int)
) {
Expand Down