C#: General type constraints tests.

This commit is contained in:
Michael Nebel
2025-01-02 14:43:34 +01:00
parent 7a7d8e40a7
commit c0974f364e
3 changed files with 70 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
public class TestClass
{
public void M1<T1>(T1 x) where T1 : class { }
public void M2<T2>(T2 x) where T2 : struct { }
public void M3<T3>(T3 x) where T3 : unmanaged { }
public void M4<T4>(T4 x) where T4 : new() { }
public void M5<T5>(T5 x) where T5 : notnull { }
public void M6<T6>(T6 x) where T6 : IList<object> { }
public void M7<T7>(T7 x) where T7 : allows ref struct { }
}

View File

@@ -0,0 +1,20 @@
typeParameterContraints
| TypeParameterConstraints.cs:6:20:6:21 | T1 | file://:0:0:0:0 | where T1: ... |
| TypeParameterConstraints.cs:8:20:8:21 | T2 | file://:0:0:0:0 | where T2: ... |
| TypeParameterConstraints.cs:10:20:10:21 | T3 | file://:0:0:0:0 | where T3: ... |
| TypeParameterConstraints.cs:12:20:12:21 | T4 | file://:0:0:0:0 | where T4: ... |
| TypeParameterConstraints.cs:14:20:14:21 | T5 | file://:0:0:0:0 | where T5: ... |
| TypeParameterConstraints.cs:16:20:16:21 | T6 | file://:0:0:0:0 | where T6: ... |
| TypeParameterConstraints.cs:18:20:18:21 | T7 | file://:0:0:0:0 | where T7: ... |
specificParameterConstraints
| TypeParameterConstraints.cs:16:20:16:21 | T6 | IList<object> |
hasConstructorConstraint
| TypeParameterConstraints.cs:12:20:12:21 | T4 | file://:0:0:0:0 | where T4: ... |
hasRefTypeConstraint
| TypeParameterConstraints.cs:6:20:6:21 | T1 | file://:0:0:0:0 | where T1: ... |
hasValueTypeConstraint
| TypeParameterConstraints.cs:8:20:8:21 | T2 | file://:0:0:0:0 | where T2: ... |
| TypeParameterConstraints.cs:10:20:10:21 | T3 | file://:0:0:0:0 | where T3: ... |
hasUnmanagedTypeConstraint
| TypeParameterConstraints.cs:10:20:10:21 | T3 | file://:0:0:0:0 | where T3: ... |
hasNullableRefTypeConstraint

View File

@@ -0,0 +1,31 @@
import csharp
query predicate typeParameterContraints(TypeParameter tp, TypeParameterConstraints tpc) {
tp.fromSource() and tp.getConstraints() = tpc
}
query predicate specificParameterConstraints(TypeParameter tp, string type) {
exists(TypeParameterConstraints tpc |
typeParameterContraints(tp, tpc) and type = tpc.getATypeConstraint().toStringWithTypes()
)
}
query predicate hasConstructorConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
typeParameterContraints(tp, tpc) and tpc.hasConstructorConstraint()
}
query predicate hasRefTypeConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
typeParameterContraints(tp, tpc) and tpc.hasRefTypeConstraint()
}
query predicate hasValueTypeConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
typeParameterContraints(tp, tpc) and tpc.hasValueTypeConstraint()
}
query predicate hasUnmanagedTypeConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
typeParameterContraints(tp, tpc) and tpc.hasUnmanagedTypeConstraint()
}
query predicate hasNullableRefTypeConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
typeParameterContraints(tp, tpc) and tpc.hasNullableRefTypeConstraint()
}