-
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: check position of usages of variant type parameters (#852)
Closes #743 ### Summary of Changes * Covariant type parameters must only be used in covariant positions. * Contravariant type parameters must only be used in contravariant positions. This PR adds checks for this.
- Loading branch information
1 parent
3362b6c
commit a2672d7
Showing
8 changed files
with
388 additions
and
18 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
74 changes: 74 additions & 0 deletions
74
...ions/type parameters/usage of variant type parameter/contravariant 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,74 @@ | ||
package tests.validation.other.declarations.typeParameters.usageOfVariantTypeParameters | ||
|
||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
class MyClass1<in Contravariant>(p1: »Contravariant«) { | ||
// $TEST$ error "A contravariant type parameter cannot be used in covariant position." | ||
attr a1: »Contravariant« | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
// $TEST$ error "A contravariant type parameter cannot be used in covariant position." | ||
attr a2: (a1: »Contravariant«) -> (r1: »Contravariant«) | ||
// $TEST$ error "A contravariant type parameter cannot be used in covariant position." | ||
attr a3: Producer<»Contravariant«> | ||
// $TEST$ error "A contravariant type parameter cannot be used in invariant position." | ||
attr a4: Middleware<»Contravariant«> | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
attr a5: Consumer<»Contravariant«> | ||
// $TEST$ error "A contravariant type parameter cannot be used in covariant position." | ||
attr a6: Producer<Producer<»Contravariant«>> | ||
// $TEST$ error "A contravariant type parameter cannot be used in invariant position." | ||
attr a7: Middleware<Producer<»Contravariant«>> | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
attr a8: Consumer<Producer<»Contravariant«>> | ||
|
||
fun f( | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
p1: »Contravariant«, | ||
// $TEST$ error "A contravariant type parameter cannot be used in covariant position." | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
p2: (a1: »Contravariant«) -> (r1: »Contravariant«), | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
p3: Producer<»Contravariant«>, | ||
// $TEST$ error "A contravariant type parameter cannot be used in invariant position." | ||
p4: Middleware<»Contravariant«>, | ||
// $TEST$ error "A contravariant type parameter cannot be used in covariant position." | ||
p5: Consumer<»Contravariant«>, | ||
) -> ( | ||
// $TEST$ error "A contravariant type parameter cannot be used in covariant position." | ||
r1: »Contravariant«, | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
// $TEST$ error "A contravariant type parameter cannot be used in covariant position." | ||
r2: (a1: »Contravariant«) -> (r1: »Contravariant«), | ||
// $TEST$ error "A contravariant type parameter cannot be used in covariant position." | ||
r3: Producer<»Contravariant«>, | ||
// $TEST$ error "A contravariant type parameter cannot be used in invariant position." | ||
r4: Middleware<»Contravariant«>, | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
r5: Consumer<»Contravariant«>, | ||
) | ||
} | ||
|
||
fun f1<in Contravariant>( | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
p1: »Contravariant«, | ||
// $TEST$ error "A contravariant type parameter cannot be used in covariant position." | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
p2: (a1: »Contravariant«) -> (r1: »Contravariant«), | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
p3: Producer<»Contravariant«>, | ||
// $TEST$ error "A contravariant type parameter cannot be used in invariant position." | ||
p4: Middleware<»Contravariant«>, | ||
// $TEST$ error "A contravariant type parameter cannot be used in covariant position." | ||
p5: Consumer<»Contravariant«>, | ||
) -> ( | ||
// $TEST$ error "A contravariant type parameter cannot be used in covariant position." | ||
r1: »Contravariant«, | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
// $TEST$ error "A contravariant type parameter cannot be used in covariant position." | ||
r2: (a1: »Contravariant«) -> (r1: »Contravariant«), | ||
// $TEST$ error "A contravariant type parameter cannot be used in covariant position." | ||
r3: Producer<»Contravariant«>, | ||
// $TEST$ error "A contravariant type parameter cannot be used in invariant position." | ||
r4: Middleware<»Contravariant«>, | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
r5: Consumer<»Contravariant«>, | ||
) |
74 changes: 74 additions & 0 deletions
74
...arations/type parameters/usage of variant type parameter/covariant 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,74 @@ | ||
package tests.validation.other.declarations.typeParameters.usageOfVariantTypeParameters | ||
|
||
// $TEST$ error "A covariant type parameter cannot be used in contravariant position." | ||
class MyClass2<out Covariant>(p1: »Covariant«) { | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
attr a1: »Covariant« | ||
// $TEST$ error "A covariant type parameter cannot be used in contravariant position." | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
attr a2: (a1: »Covariant«) -> (r1: »Covariant«) | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
attr a3: Producer<»Covariant«> | ||
// $TEST$ error "A covariant type parameter cannot be used in invariant position." | ||
attr a4: Middleware<»Covariant«> | ||
// $TEST$ error "A covariant type parameter cannot be used in contravariant position." | ||
attr a5: Consumer<»Covariant«> | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
attr a6: Producer<Producer<»Covariant«>> | ||
// $TEST$ error "A covariant type parameter cannot be used in invariant position." | ||
attr a7: Middleware<Producer<»Covariant«>> | ||
// $TEST$ error "A covariant type parameter cannot be used in contravariant position." | ||
attr a8: Consumer<Producer<»Covariant«>> | ||
|
||
fun f( | ||
// $TEST$ error "A covariant type parameter cannot be used in contravariant position." | ||
p1: »Covariant«, | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
// $TEST$ error "A covariant type parameter cannot be used in contravariant position." | ||
p2: (a1: »Covariant«) -> (r1: »Covariant«), | ||
// $TEST$ error "A covariant type parameter cannot be used in contravariant position." | ||
p3: Producer<»Covariant«>, | ||
// $TEST$ error "A covariant type parameter cannot be used in invariant position." | ||
p4: Middleware<»Covariant«>, | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
p5: Consumer<»Covariant«>, | ||
) -> ( | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
r1: »Covariant«, | ||
// $TEST$ error "A covariant type parameter cannot be used in contravariant position." | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
r2: (a1: »Covariant«) -> (r1: »Covariant«), | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
r3: Producer<»Covariant«>, | ||
// $TEST$ error "A covariant type parameter cannot be used in invariant position." | ||
r4: Middleware<»Covariant«>, | ||
// $TEST$ error "A covariant type parameter cannot be used in contravariant position." | ||
r5: Consumer<»Covariant«>, | ||
) | ||
} | ||
|
||
fun f2<out Covariant>( | ||
// $TEST$ error "A covariant type parameter cannot be used in contravariant position." | ||
p1: »Covariant«, | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
// $TEST$ error "A covariant type parameter cannot be used in contravariant position." | ||
p2: (a1: »Covariant«) -> (r1: »Covariant«), | ||
// $TEST$ error "A covariant type parameter cannot be used in contravariant position." | ||
p3: Producer<»Covariant«>, | ||
// $TEST$ error "A covariant type parameter cannot be used in invariant position." | ||
p4: Middleware<»Covariant«>, | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
p5: Consumer<»Covariant«>, | ||
) -> ( | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
r1: »Covariant«, | ||
// $TEST$ error "A covariant type parameter cannot be used in contravariant position." | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
r2: (a1: »Covariant«) -> (r1: »Covariant«), | ||
// $TEST$ no error r"A .*variant type parameter cannot be used in .*variant position." | ||
r3: Producer<»Covariant«>, | ||
// $TEST$ error "A covariant type parameter cannot be used in invariant position." | ||
r4: Middleware<»Covariant«>, | ||
// $TEST$ error "A covariant type parameter cannot be used in contravariant position." | ||
r5: Consumer<»Covariant«>, | ||
) |
Oops, something went wrong.