From 23d960f5e5f1b9c9db1698db05cbdcbcbf22d659 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Thu, 12 Oct 2023 19:45:24 +0200 Subject: [PATCH] feat: error if call receiver is class without constructor --- src/language/validation/types.ts | 7 ++++++- .../main.sdstest | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/resources/validation/types/checking/called class must have constructor/main.sdstest diff --git a/src/language/validation/types.ts b/src/language/validation/types.ts index 3170c0260..b44007ddb 100644 --- a/src/language/validation/types.ts +++ b/src/language/validation/types.ts @@ -1,7 +1,7 @@ import { getContainerOfType, ValidationAcceptor } from 'langium'; import { isSdsAnnotation, - isSdsCallable, + isSdsCallable, isSdsClass, isSdsLambda, isSdsMemberAccess, isSdsPipeline, isSdsReference, isSdsSchema, SdsAttribute, SdsCall, @@ -46,6 +46,11 @@ export const callReceiverMustBeCallable = (services: SafeDsServices) => { node: node.receiver, code: CODE_TYPE_CALLABLE_RECEIVER, }); + } else if (isSdsClass(callable) && !callable.parameterList) { + accept('error', "Cannot instantiate a class that has no constructor.", { + node: node.receiver, + code: CODE_TYPE_CALLABLE_RECEIVER, + }); } }; }; diff --git a/tests/resources/validation/types/checking/called class must have constructor/main.sdstest b/tests/resources/validation/types/checking/called class must have constructor/main.sdstest new file mode 100644 index 000000000..d42292ebe --- /dev/null +++ b/tests/resources/validation/types/checking/called class must have constructor/main.sdstest @@ -0,0 +1,14 @@ +package tests.validation.types.checking.calledClassMustHaveConstructor + +class A + +class B() + +pipeline test { + + // $TEST$ error "Cannot instantiate a class that has no constructor." + val a = »A«(); + + // $TEST$ no error "Cannot instantiate a class that has no constructor." + val b = »B«(); +}