From e4414b56f560961babe626460b23626daf8eab3d Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Tue, 23 Apr 2024 10:19:17 +0200 Subject: [PATCH] feat: do not check whether optional type parameters of classes can be inferred --- .../validation/other/declarations/typeParameters.ts | 8 ++++++-- .../type parameters/insufficient context/main.sdsdev | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/safe-ds-lang/src/language/validation/other/declarations/typeParameters.ts b/packages/safe-ds-lang/src/language/validation/other/declarations/typeParameters.ts index 48bfbca37..62797ce40 100644 --- a/packages/safe-ds-lang/src/language/validation/other/declarations/typeParameters.ts +++ b/packages/safe-ds-lang/src/language/validation/other/declarations/typeParameters.ts @@ -32,8 +32,12 @@ export const typeParameterMustHaveSufficientContext = (node: SdsTypeParameter, a } /* c8 ignore stop */ - // Classes without constructor can only be used as named types, where type arguments are manifest - if (isSdsClass(containingCallable) && !containingCallable.parameterList) { + // Optional type parameters of classes get initialized to their default value if there is insufficient context in + // the constructor. Elsewhere, the class might be used as a named type, where type arguments are manifest, so having + // the type parameter is beneficial. This is not the case for functions, where type parameters only get inferred. + // + // Classes without constructor can only be used as named types, where type arguments are manifest. + if (isSdsClass(containingCallable) && (TypeParameter.isOptional(node) || !containingCallable.parameterList)) { return; } diff --git a/packages/safe-ds-lang/tests/resources/validation/other/declarations/type parameters/insufficient context/main.sdsdev b/packages/safe-ds-lang/tests/resources/validation/other/declarations/type parameters/insufficient context/main.sdsdev index be989fcdb..d024409c6 100644 --- a/packages/safe-ds-lang/tests/resources/validation/other/declarations/type parameters/insufficient context/main.sdsdev +++ b/packages/safe-ds-lang/tests/resources/validation/other/declarations/type parameters/insufficient context/main.sdsdev @@ -22,6 +22,8 @@ class MyClass8<»T«>(param: () -> (r: (p: T) -> ())) class MyClass9<»T«>(param: union) // $TEST$ error "Insufficient context to infer this type parameter." class MyClass10<»T«>(param: union) +// $TEST$ no error "Insufficient context to infer this type parameter." +class MyClass11<»T« = Int>() // $TEST$ error "Insufficient context to infer this type parameter." fun myFunction1<»T« sub Int>() @@ -39,3 +41,5 @@ fun myFunction6<»T«>(param: () -> (r: (p: T) -> ())) fun myFunction7<»T«>(param: union) // $TEST$ error "Insufficient context to infer this type parameter." fun myFunction8<»T«>(param: union) +// $TEST$ error "Insufficient context to infer this type parameter." +fun myFunction9<»T« = Int>()