Skip to content

Commit 603ce81

Browse files
committed
C#: General type constraints tests.
1 parent 68ca307 commit 603ce81

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
public class TestClass
5+
{
6+
public void M1<T1>(T1 x) where T1 : class { }
7+
8+
public void M2<T2>(T2 x) where T2 : struct { }
9+
10+
public void M3<T3>(T3 x) where T3 : unmanaged { }
11+
12+
public void M4<T4>(T4 x) where T4 : new() { }
13+
14+
public void M5<T5>(T5 x) where T5 : notnull { }
15+
16+
public void M6<T6>(T6 x) where T6 : IList<object> { }
17+
18+
public void M7<T7>(T7 x) where T7 : allows ref struct { }
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
typeParameterContraints
2+
| TypeParameterConstraints.cs:6:20:6:21 | T1 | file://:0:0:0:0 | where T1: ... |
3+
| TypeParameterConstraints.cs:8:20:8:21 | T2 | file://:0:0:0:0 | where T2: ... |
4+
| TypeParameterConstraints.cs:10:20:10:21 | T3 | file://:0:0:0:0 | where T3: ... |
5+
| TypeParameterConstraints.cs:12:20:12:21 | T4 | file://:0:0:0:0 | where T4: ... |
6+
| TypeParameterConstraints.cs:14:20:14:21 | T5 | file://:0:0:0:0 | where T5: ... |
7+
| TypeParameterConstraints.cs:16:20:16:21 | T6 | file://:0:0:0:0 | where T6: ... |
8+
| TypeParameterConstraints.cs:18:20:18:21 | T7 | file://:0:0:0:0 | where T7: ... |
9+
specificParameterConstraints
10+
| TypeParameterConstraints.cs:16:20:16:21 | T6 | IList<object> |
11+
hasConstructorConstraint
12+
| TypeParameterConstraints.cs:12:20:12:21 | T4 | file://:0:0:0:0 | where T4: ... |
13+
hasRefTypeConstraint
14+
| TypeParameterConstraints.cs:6:20:6:21 | T1 | file://:0:0:0:0 | where T1: ... |
15+
hasValueTypeConstraint
16+
| TypeParameterConstraints.cs:8:20:8:21 | T2 | file://:0:0:0:0 | where T2: ... |
17+
| TypeParameterConstraints.cs:10:20:10:21 | T3 | file://:0:0:0:0 | where T3: ... |
18+
hasUnmanagedTypeConstraint
19+
| TypeParameterConstraints.cs:10:20:10:21 | T3 | file://:0:0:0:0 | where T3: ... |
20+
hasNullableRefTypeConstraint
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import csharp
2+
3+
query predicate typeParameterContraints(TypeParameter tp, TypeParameterConstraints tpc) {
4+
tp.fromSource() and tp.getConstraints() = tpc
5+
}
6+
7+
query predicate specificParameterConstraints(TypeParameter tp, string type) {
8+
exists(TypeParameterConstraints tpc |
9+
typeParameterContraints(tp, tpc) and type = tpc.getATypeConstraint().toStringWithTypes()
10+
)
11+
}
12+
13+
query predicate hasConstructorConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
14+
typeParameterContraints(tp, tpc) and tpc.hasConstructorConstraint()
15+
}
16+
17+
query predicate hasRefTypeConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
18+
typeParameterContraints(tp, tpc) and tpc.hasRefTypeConstraint()
19+
}
20+
21+
query predicate hasValueTypeConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
22+
typeParameterContraints(tp, tpc) and tpc.hasValueTypeConstraint()
23+
}
24+
25+
query predicate hasUnmanagedTypeConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
26+
typeParameterContraints(tp, tpc) and tpc.hasUnmanagedTypeConstraint()
27+
}
28+
29+
query predicate hasNullableRefTypeConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
30+
typeParameterContraints(tp, tpc) and tpc.hasNullableRefTypeConstraint()
31+
}

0 commit comments

Comments
 (0)