-
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: optional type parameters (#829)
Closes #739 ### Summary of Changes Type parameters can now have default values, akin to parameters.
- Loading branch information
1 parent
67ab766
commit 0e9f67a
Showing
25 changed files
with
209 additions
and
70 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
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
23 changes: 23 additions & 0 deletions
23
packages/safe-ds-lang/src/language/validation/other/declarations/typeParameterLists.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,23 @@ | ||
import { ValidationAcceptor } from 'langium'; | ||
import { SdsTypeParameterList } from '../../../generated/ast.js'; | ||
import { TypeParameter } from '../../../helpers/nodeProperties.js'; | ||
|
||
export const CODE_TYPE_PARAMETER_LIST_REQUIRED_AFTER_OPTIONAL = 'type-parameter-list/required-after-optional'; | ||
|
||
export const typeParameterListMustNotHaveRequiredTypeParametersAfterOptionalTypeParameters = ( | ||
node: SdsTypeParameterList, | ||
accept: ValidationAcceptor, | ||
) => { | ||
let foundOptional = false; | ||
for (const typeParameter of node.typeParameters) { | ||
if (TypeParameter.isOptional(typeParameter)) { | ||
foundOptional = true; | ||
} else if (foundOptional) { | ||
accept('error', 'After the first optional type parameter all type parameters must be optional.', { | ||
node: typeParameter, | ||
property: 'name', | ||
code: CODE_TYPE_PARAMETER_LIST_REQUIRED_AFTER_OPTIONAL, | ||
}); | ||
} | ||
} | ||
}; |
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
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
5 changes: 5 additions & 0 deletions
5
...ces/formatting/declarations/type parameters/contravariant optional type parameter.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,5 @@ | ||
fun myFunction< in T = Int >() | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
fun myFunction<in T = Int>() |
File renamed without changes.
5 changes: 5 additions & 0 deletions
5
...sources/formatting/declarations/type parameters/covariant optional type parameter.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,5 @@ | ||
fun myFunction< out T = Int >() | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
fun myFunction<out T = Int>() |
File renamed without changes.
5 changes: 5 additions & 0 deletions
5
...sources/formatting/declarations/type parameters/invariant optional type parameter.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,5 @@ | ||
fun myFunction< T = Int >() | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
fun myFunction<T = Int>() |
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
...s/grammar/declarations/type parameters/good-contravariant optional type parameter.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,3 @@ | ||
// $TEST$ no_syntax_error | ||
|
||
fun myFunction<in T = Int>() |
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
...urces/grammar/declarations/type parameters/good-covariant optional type parameter.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,3 @@ | ||
// $TEST$ no_syntax_error | ||
|
||
fun myFunction<out T = Int>() |
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
...urces/grammar/declarations/type parameters/good-invariant optional type parameter.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,3 @@ | ||
// $TEST$ no_syntax_error | ||
|
||
fun myFunction<T = Int>() |
File renamed without changes.
28 changes: 28 additions & 0 deletions
28
...ion/other/declarations/type parameters/must not have required after optional/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,28 @@ | ||
package tests.validation.other.declarations.typeParameterLists.mustNotHaveRequiredAfterOptional | ||
|
||
// $TEST$ no error "After the first optional type parameter all type parameters must be optional." | ||
// $TEST$ no error "After the first optional type parameter all type parameters must be optional." | ||
// $TEST$ error "After the first optional type parameter all type parameters must be optional." | ||
// $TEST$ no error "After the first optional type parameter all type parameters must be optional." | ||
class MyClass1<»A«, »B« = Int, »C«, »D« = String> | ||
|
||
// $TEST$ no error "After the first optional type parameter all type parameters must be optional." | ||
// $TEST$ no error "After the first optional type parameter all type parameters must be optional." | ||
class MyClass2<»A«, »B« = Int> | ||
|
||
// $TEST$ no error "After the first optional type parameter all type parameters must be optional." | ||
class MyClass3<»A«> | ||
|
||
|
||
// $TEST$ no error "After the first optional type parameter all type parameters must be optional." | ||
// $TEST$ no error "After the first optional type parameter all type parameters must be optional." | ||
// $TEST$ error "After the first optional type parameter all type parameters must be optional." | ||
// $TEST$ no error "After the first optional type parameter all type parameters must be optional." | ||
fun myFunction1<»A«, »B« = Int, »C«, »D« = String>() | ||
|
||
// $TEST$ no error "After the first optional type parameter all type parameters must be optional." | ||
// $TEST$ no error "After the first optional type parameter all type parameters must be optional." | ||
fun myFunction2<»A«, »B« = Int>() | ||
|
||
// $TEST$ no error "After the first optional type parameter all type parameters must be optional." | ||
fun myFunction3<»A«>() |
25 changes: 14 additions & 11 deletions
25
...resources/validation/other/types/type argument lists/too many type arguments/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 |
---|---|---|
@@ -1,17 +1,20 @@ | ||
package tests.validation.other.types.typeArgumentLists.tooManyTypeArguments | ||
|
||
class MyClass1<T> | ||
class MyClass1<A> | ||
class MyClass2<A, B> | ||
class MyClass3<A, B = Int> | ||
|
||
fun myFunction( | ||
// $TEST$ no error r"Expected \d* type arguments? but got \d*\." | ||
f: MyClass1»<>«, | ||
// $TEST$ no error r"Expected \d* type arguments? but got \d*\." | ||
g: MyClass1»<Int>«, | ||
// $TEST$ error "Expected 1 type argument but got 2." | ||
h: MyClass1»<Int, Int>«, | ||
// $TEST$ error "Expected 2 type arguments but got 3." | ||
i: MyClass2»<Int, Int, Int>«, | ||
// $TEST$ no error r"Expected \d* type arguments? but got \d*\." | ||
j: Unresolved»<Int, Int>« | ||
// $TEST$ no error r"Expected .* type arguments? but got \d*\." | ||
a: MyClass1»<>«, | ||
// $TEST$ no error r"Expected .* type arguments? but got \d*\." | ||
b: MyClass1»<Int>«, | ||
// $TEST$ error "Expected exactly 1 type argument but got 2." | ||
c: MyClass1»<Int, Int>«, | ||
// $TEST$ error "Expected exactly 2 type arguments but got 3." | ||
d: MyClass2»<Int, Int, Int>«, | ||
// $TEST$ error "Expected between 1 and 2 type arguments but got 3." | ||
f: MyClass3»<Int, Int, Int>«, | ||
// $TEST$ no error r"Expected .* type arguments? but got \d*\." | ||
g: Unresolved»<Int, Int>« | ||
) |
9 changes: 9 additions & 0 deletions
9
...pe parameter/dont show this error if the type argument list is missing altogether.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,9 @@ | ||
package tests.validation.other.namedTypes.missingRequiredTypeParameter | ||
|
||
// $TEST$ no error r"The type parameters? .* must be set here\." | ||
|
||
class MyClass<T> | ||
|
||
fun myFunction2( | ||
myCallableType: MyClass | ||
) |
32 changes: 32 additions & 0 deletions
32
...tests/resources/validation/types/named types/missing required type parameter/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,32 @@ | ||
|
||
package tests.validation.types.namedTypes.missingRequiredTypeParameter | ||
|
||
class MyClassWithoutTypeParameter | ||
class MyClassWithTypeParameters<T1, T2, T3 = Int> | ||
|
||
fun myFunction1( | ||
// $TEST$ no error r"The type parameters? .* must be set here\." | ||
a1: MyClassWithoutTypeParameter»<>«, | ||
// $TEST$ no error r"The type parameters? .* must be set here\." | ||
a2: MyClassWithoutTypeParameter»<Int>«, | ||
// $TEST$ no error r"The type parameters? .* must be set here\." | ||
a3: MyClassWithoutTypeParameter»<T = Int>«, | ||
|
||
// $TEST$ error "The type parameters 'T1', 'T2' must be set here." | ||
b1: MyClassWithTypeParameters»<>«, | ||
// $TEST$ error "The type parameter 'T2' must be set here." | ||
b2: MyClassWithTypeParameters»<Int>«, | ||
// $TEST$ error "The type parameter 'T1' must be set here." | ||
b3: MyClassWithTypeParameters»<T2 = Int>«, | ||
// $TEST$ no error r"The type parameters? .* must be set here\." | ||
b4: MyClassWithTypeParameters»<Int, T2 = Int>«, | ||
// $TEST$ no error r"The type parameters? .* must be set here\." | ||
b5: MyClassWithTypeParameters»<Int, Int, Int>«, | ||
|
||
// $TEST$ no error r"The type parameters? .* must be set here\." | ||
d1: Unresolved»<>«, | ||
// $TEST$ no error r"The type parameters? .* must be set here\." | ||
d2: Unresolved»<Int>«, | ||
// $TEST$ no error r"The type parameters? .* must be set here\." | ||
d3: Unresolved»<T = Int>«, | ||
) |
29 changes: 17 additions & 12 deletions
29
...lang/tests/resources/validation/types/named types/missing type argument list/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 |
---|---|---|
@@ -1,22 +1,27 @@ | ||
package tests.validation.types.namedTypes.missingTypeArgumentList | ||
|
||
class MyClassWithoutTypeParameters | ||
class MyClassWithTypeParameters<T> | ||
class MyClassWithRequiredTypeParameters<T> | ||
class MyClassWithOptionalTypeParameters<T = Int> | ||
|
||
fun myFunction( | ||
// $TEST$ no error r"The type '\w*' is parameterized, so a type argument list must be added\." | ||
// $TEST$ no error r"The type '\w*' has required type parameters, so a type argument list must be added\." | ||
a1: »MyClassWithoutTypeParameters«, | ||
// $TEST$ no error r"The type '\w*' is parameterized, so a type argument list must be added\." | ||
b1: »MyClassWithoutTypeParameters«<>, | ||
// $TEST$ no error r"The type '\w*' has required type parameters, so a type argument list must be added\." | ||
a2: »MyClassWithoutTypeParameters«<>, | ||
|
||
// $TEST$ error "The type 'MyClassWithTypeParameters' is parameterized, so a type argument list must be added." | ||
c1: »MyClassWithTypeParameters«, | ||
// $TEST$ no error r"The type '\w*' is parameterized, so a type argument list must be added\." | ||
d1: »MyClassWithTypeParameters«<>, | ||
// $TEST$ error "The type 'MyClassWithRequiredTypeParameters' has required type parameters, so a type argument list must be added." | ||
b1: »MyClassWithRequiredTypeParameters«, | ||
// $TEST$ no error r"The type '\w*' has required type parameters, so a type argument list must be added\." | ||
b2: »MyClassWithRequiredTypeParameters«<>, | ||
|
||
// $TEST$ no error r"The type '\w*' has required type parameters, so a type argument list must be added\." | ||
c1: »MyClassWithOptionalTypeParameters«, | ||
// $TEST$ no error r"The type '\w*' has required type parameters, so a type argument list must be added\." | ||
c2: »MyClassWithOptionalTypeParameters«<>, | ||
|
||
// $TEST$ no error r"The type '\w*' is parameterized, so a type argument list must be added\." | ||
e: »UnresolvedClass«, | ||
// $TEST$ no error r"The type '\w*' is parameterized, so a type argument list must be added\." | ||
f: »UnresolvedClass«<>, | ||
// $TEST$ no error r"The type '\w*' has required type parameters, so a type argument list must be added\." | ||
d1: »UnresolvedClass«, | ||
// $TEST$ no error r"The type '\w*' has required type parameters, so a type argument list must be added\." | ||
d2: »UnresolvedClass«<>, | ||
) |
Oops, something went wrong.