Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit abf43da

Browse files
committed
C#: Address review comments. Fix up toStringWithTypes(), and deprecate predicates in TypeParameterConstraints.
1 parent 620ecc8 commit abf43da

7 files changed

Lines changed: 67 additions & 68 deletions

File tree

csharp/ql/src/semmle/code/csharp/AnnotatedType.qll

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import csharp
1010

1111
private module Annotations {
12-
private newtype TAnnotation =
12+
newtype TAnnotation =
1313
TNotNullableRefType() or
1414
TNullableRefType() or
1515
TReadonlyRefType() or
@@ -182,16 +182,10 @@ class AnnotatedType extends TAnnotatedType {
182182

183183
/** Gets a textual representation of this annotated type. */
184184
string toString() {
185-
result = annotations.getTypePrefix() + underlyingType.toString() + annotations.getTypeSuffix()
185+
result = annotations.getTypePrefix() + underlyingType.toStringWithTypes() + annotations.getTypeSuffix()
186186
}
187187

188-
/** Gets a textual representation of this annotated type, with full type information. */
189-
string toStringWithTypes() {
190-
result = annotations.getTypePrefix() + underlyingType.toStringWithTypes() +
191-
annotations.getTypeSuffix()
192-
}
193-
194-
/** Gets the location of this annotated type, if any. */
188+
/** Gets the location of this annotated type. */
195189
Location getLocation() { result = underlyingType.getLocation() }
196190

197191
/**
@@ -201,26 +195,26 @@ class AnnotatedType extends TAnnotatedType {
201195
Type getUnderlyingType() { result = underlyingType }
202196

203197
/** Gets the type annotation set of this annotated type. */
204-
Annotations::TypeAnnotations getAnnotations() { result = annotations }
198+
private Annotations::TypeAnnotations getAnnotations() { result = annotations }
205199

206200
/** Gets a type annotation of this annotated type. */
207-
Annotations::TypeAnnotation getAnAnnotation() { result = getAnnotations().getAnAnnotation() }
201+
private Annotations::TypeAnnotation getAnAnnotation() { result = getAnnotations().getAnAnnotation() }
208202

209-
/** Holds if the type is a non-nullable reference. */
203+
/** Holds if the type is a non-nullable reference, for example, `string` in a nullable-enabled context. */
210204
predicate isNonNullableRefType() {
211205
this.getAnAnnotation() instanceof Annotations::NonNullableRefType
212206
}
213207

214-
/** Holds if the type is a nullable reference. */
208+
/** Holds if the type is a nullable reference, for example `string?`. */
215209
predicate isNullableRefType() { this.getAnAnnotation() instanceof Annotations::NullableRefType }
216210

217-
/** Holds if the type is a `ref`. */
211+
/** Holds if the type is a `ref`, for example the return type of `ref int F()`. */
218212
predicate isRef() { this.getAnAnnotation() instanceof Annotations::RefTypeAnnotation }
219213

220-
/** Holds if the type is a `readonly ref`. */
214+
/** Holds if the type is a `ref readonly`, for example the return type of `ref readonly int F()`. */
221215
predicate isReadonlyRef() { this.getAnAnnotation() instanceof Annotations::ReadonlyRefType }
222216

223-
/** Holds if the type is an `out`. */
217+
/** Holds if the type is an `out`, for example parameter p in `void F(out int p)`. */
224218
predicate isOut() { this.getAnAnnotation() instanceof Annotations::OutType }
225219

226220
/** Holds if this annotated type applies to element `e`. */

csharp/ql/src/semmle/code/csharp/Conversion.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -683,9 +683,9 @@ private Class effectiveBaseClassCandidate(TypeParameter tp) {
683683
exists(TypeParameterConstraints tpc | tpc = tp.getConstraints() |
684684
tpc.hasValueTypeConstraint() and result instanceof SystemValueTypeClass
685685
or
686-
result = tpc.getClassConstraint()
686+
result = tpc.getATypeConstraint()
687687
or
688-
result = effectiveBaseClassCandidate(tpc.getATypeParameterConstraint())
688+
result = effectiveBaseClassCandidate(tpc.getATypeConstraint())
689689
or
690690
tpc.hasRefTypeConstraint() and result instanceof ObjectType
691691
)
@@ -694,7 +694,7 @@ private Class effectiveBaseClassCandidate(TypeParameter tp) {
694694
/** 10.1.5: Whether type parameter `tp` has primary constraints. */
695695
private predicate hasPrimaryConstraints(TypeParameter tp) {
696696
exists(TypeParameterConstraints tpc | tpc = tp.getConstraints() |
697-
exists(tpc.getClassConstraint())
697+
tpc.getATypeConstraint() instanceof Class
698698
or
699699
tpc.hasRefTypeConstraint()
700700
or
@@ -705,14 +705,14 @@ private predicate hasPrimaryConstraints(TypeParameter tp) {
705705
/** 10.1.5: The effective interface set of a type parameter `tp` */
706706
private Interface effectiveInterfaceSet(TypeParameter tp) {
707707
exists(TypeParameterConstraints tpc | tpc = tp.getConstraints() |
708-
result = tpc.getAnInterfaceConstraint()
708+
result = tpc.getATypeConstraint()
709709
or
710-
result = effectiveInterfaceSet(tpc.getATypeParameterConstraint())
710+
result = effectiveInterfaceSet(tpc.getATypeConstraint())
711711
)
712712
}
713713

714714
private TypeParameter getATypeParameterFromConstraints(TypeParameter tp) {
715-
result = tp.getConstraints().getATypeParameterConstraint()
715+
result = tp.getConstraints().getATypeConstraint()
716716
}
717717

718718
/**

csharp/ql/src/semmle/code/csharp/Generics.qll

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ class ConstructedGeneric extends DotNet::ConstructedGeneric, Generic {
7171
override Type getATypeArgument() { result = getTypeArgument(_) }
7272

7373
/** Gets the annotated type of type argument `i`. */
74-
final AnnotatedType getAnnotatedTypeArgument(int i) {
75-
result.appliesToTypeArgument(this, i)
76-
}
74+
final AnnotatedType getAnnotatedTypeArgument(int i) { result.appliesToTypeArgument(this, i) }
7775
}
7876

7977
/**
@@ -134,16 +132,16 @@ class TypeParameter extends DotNet::TypeParameter, Type, @type_parameter {
134132
override predicate isRefType() {
135133
exists(TypeParameterConstraints tpc | tpc = getConstraints() |
136134
tpc.hasRefTypeConstraint() or
137-
exists(tpc.getClassConstraint()) or
138-
tpc.getATypeParameterConstraint().isRefType()
135+
tpc.getATypeConstraint() instanceof Class or
136+
tpc.getATypeConstraint().(TypeParameter).isRefType()
139137
// NB: an interface constraint is not a guarantee, as structs can implement interfaces
140138
)
141139
}
142140

143141
override predicate isValueType() {
144142
exists(TypeParameterConstraints tpc | tpc = getConstraints() |
145143
tpc.hasValueTypeConstraint() or
146-
tpc.getATypeParameterConstraint().isValueType()
144+
tpc.getATypeConstraint().(TypeParameter).isValueType()
147145
)
148146
}
149147

@@ -201,23 +199,29 @@ class TypeParameter extends DotNet::TypeParameter, Type, @type_parameter {
201199
* ```
202200
*/
203201
class TypeParameterConstraints extends Element, @type_parameter_constraints {
204-
/** Gets a specific interface constraint, if any. */
205-
Interface getAnInterfaceConstraint() {
206-
specific_type_parameter_constraints(this, getTypeRef(result))
207-
}
202+
/**
203+
* DEPRECATED: Use `getATypeConstraint()` instead.
204+
* Gets a specific interface constraint, if any.
205+
*/
206+
deprecated Interface getAnInterfaceConstraint() { result = getATypeConstraint() }
208207

209-
/** Gets a specific type parameter constraint, if any. */
210-
TypeParameter getATypeParameterConstraint() {
211-
specific_type_parameter_constraints(this, getTypeRef(result))
212-
}
208+
/**
209+
* DEPRECATED: Use `getATypeConstraint()` instead.
210+
* Gets a specific type parameter constraint, if any.
211+
*/
212+
deprecated TypeParameter getATypeParameterConstraint() { result = getATypeConstraint() }
213213

214-
/** Gets the specific class constraint, if any. */
215-
Class getClassConstraint() { specific_type_parameter_constraints(this, getTypeRef(result)) }
214+
/**
215+
* DEPRECATED: Use `getATypeConstraint()` instead.
216+
* Gets the specific class constraint, if any.
217+
*/
218+
deprecated Class getClassConstraint() { result = getATypeConstraint() }
219+
220+
/** Gets a type constraint, if any. */
221+
Type getATypeConstraint() { specific_type_parameter_constraints(this, getTypeRef(result)) }
216222

217223
/** Gets an annotated specific type constraint, if any. */
218-
AnnotatedType getAnAnnotatedTypeConstraint() {
219-
result.appliesToTypeConstraint(this)
220-
}
224+
AnnotatedType getAnAnnotatedTypeConstraint() { result.appliesToTypeConstraint(this) }
221225

222226
override Location getALocation() { type_parameter_constraints_location(this, result) }
223227

@@ -366,8 +370,18 @@ class ConstructedType extends ValueOrRefType, ConstructedGeneric {
366370

367371
override UnboundGenericType getUnboundGeneric() { constructed_generic(this, getTypeRef(result)) }
368372

373+
language[monotonicAggregates]
374+
string annotatedTypeArgumentsToString() {
375+
result = concat(int i |
376+
exists(this.getAnnotatedTypeArgument(i))
377+
|
378+
this.getAnnotatedTypeArgument(i).toString(), ", " order by i
379+
)
380+
}
381+
369382
override string toStringWithTypes() {
370-
result = getUnboundGeneric().getNameWithoutBrackets() + "<" + this.typeArgumentsToString() + ">"
383+
result = getUnboundGeneric().getNameWithoutBrackets() + "<" +
384+
this.annotatedTypeArgumentsToString() + ">"
371385
}
372386

373387
final override Type getChild(int n) { result = getTypeArgument(n) }

csharp/ql/src/semmle/code/csharp/Type.qll

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -808,22 +808,13 @@ class ArrayType extends DotNet::ArrayType, RefType, @array_type {
808808
if i = getRank() - 1 then result = "" else result = "," + getRankString(i + 1)
809809
}
810810

811-
private string getDimensionString(AnnotatedType elementType) {
812-
exists(AnnotatedType et, string res |
813-
et = getAnnotatedElementType() and
814-
res = "[" + getRankString(0) + "]" and
815-
if et.getUnderlyingType() instanceof ArrayType
816-
then result = res + et.getUnderlyingType().(ArrayType).getDimensionString(elementType)
817-
else (
818-
result = res and elementType = et
819-
)
820-
)
811+
private string getDimensionString()
812+
{
813+
result = "[" + getRankString(0) + "]"
821814
}
822815

823816
override string toStringWithTypes() {
824-
exists(AnnotatedType elementType |
825-
result = elementType.toStringWithTypes() + this.getDimensionString(elementType)
826-
)
817+
result = this.getAnnotatedElementType().toString() + this.getDimensionString()
827818
}
828819

829820
override Type getChild(int n) { result = getElementType() and n = 0 }

csharp/ql/test/library-tests/csharp8/NullableRefTypes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void Locals()
4747
delegate MyClass? Del(MyClass x);
4848
event Del? P;
4949

50-
// Nullable method type arguments
50+
// Nullable method type parameters
5151
object Q<T>(T t) where T: MyClass? => null;
5252

5353
// Nullable type parameters

csharp/ql/test/library-tests/csharp8/NullableRefTypes.expected

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ assignableTypes
5656
| NullableRefTypes.cs:19:13:19:16 | Item | NullableRefTypes.cs:6:7:6:13 | MyClass! |
5757
| NullableRefTypes.cs:19:27:19:27 | i | NullableRefTypes.cs:6:7:6:13 | MyClass? |
5858
| NullableRefTypes.cs:19:27:19:27 | i | NullableRefTypes.cs:6:7:6:13 | MyClass? |
59-
| NullableRefTypes.cs:22:16:22:17 | G1 | NullableRefTypes.cs:6:7:6:13 | MyClass[]! |
60-
| NullableRefTypes.cs:23:17:23:18 | G2 | NullableRefTypes.cs:6:7:6:13 | MyClass[]? |
61-
| NullableRefTypes.cs:24:16:24:17 | G3 | NullableRefTypes.cs:6:7:6:13 | MyClass[]! |
62-
| NullableRefTypes.cs:25:18:25:18 | H | NullableRefTypes.cs:6:7:6:13 | MyClass[][]! |
63-
| NullableRefTypes.cs:26:38:26:38 | x | NullableRefTypes.cs:6:7:6:13 | MyClass[][]! |
64-
| NullableRefTypes.cs:27:38:27:38 | x | NullableRefTypes.cs:6:7:6:13 | MyClass[][]! |
59+
| NullableRefTypes.cs:22:16:22:17 | G1 | NullableRefTypes.cs:6:7:6:13 | MyClass?[]! |
60+
| NullableRefTypes.cs:23:17:23:18 | G2 | NullableRefTypes.cs:6:7:6:13 | MyClass?[]? |
61+
| NullableRefTypes.cs:24:16:24:17 | G3 | NullableRefTypes.cs:6:7:6:13 | MyClass?[]! |
62+
| NullableRefTypes.cs:25:18:25:18 | H | NullableRefTypes.cs:6:7:6:13 | MyClass?[]![]! |
63+
| NullableRefTypes.cs:26:38:26:38 | x | NullableRefTypes.cs:6:7:6:13 | MyClass![]?[]! |
64+
| NullableRefTypes.cs:27:38:27:38 | x | NullableRefTypes.cs:6:7:6:13 | MyClass?[]![]! |
6565
| NullableRefTypes.cs:32:20:32:20 | a | NullableRefTypes.cs:6:7:6:13 | MyClass! |
6666
| NullableRefTypes.cs:32:31:32:31 | b | NullableRefTypes.cs:6:7:6:13 | MyClass? |
6767
| NullableRefTypes.cs:37:17:37:17 | a | NullableRefTypes.cs:6:7:6:13 | MyClass! |
@@ -76,7 +76,7 @@ assignableTypes
7676
| NullableRefTypes.cs:48:16:48:16 | value | NullableRefTypes.cs:47:23:47:25 | Del? |
7777
| NullableRefTypes.cs:51:19:51:19 | t | NullableRefTypes.cs:6:7:6:13 | MyClass |
7878
| NullableRefTypes.cs:51:19:51:19 | t | NullableRefTypes.cs:51:14:51:14 | T! |
79-
| NullableRefTypes.cs:59:54:59:59 | items2 | NullableRefTypes.cs:54:11:54:33 | Generic<MyClass,MyClass,IDisposable,MyClass>! |
79+
| NullableRefTypes.cs:59:54:59:59 | items2 | NullableRefTypes.cs:54:11:54:33 | Generic<MyClass?, MyClass!, IDisposable!, MyClass!>! |
8080
| NullableRefTypes.cs:61:25:61:25 | x | NullableRefTypes.cs:6:7:6:13 | MyClass |
8181
| NullableRefTypes.cs:61:25:61:25 | x | NullableRefTypes.cs:61:20:61:20 | T! |
8282
| NullableRefTypes.cs:67:18:67:18 | x | NullableRefTypes.cs:6:7:6:13 | MyClass? |
@@ -98,9 +98,9 @@ arrayElements
9898
| NullableRefTypes.cs:22:16:22:17 | G1 | NullableRefTypes.cs:6:7:6:13 | MyClass[] | NullableRefTypes.cs:6:7:6:13 | MyClass? |
9999
| NullableRefTypes.cs:23:17:23:18 | G2 | NullableRefTypes.cs:6:7:6:13 | MyClass[] | NullableRefTypes.cs:6:7:6:13 | MyClass? |
100100
| NullableRefTypes.cs:24:16:24:17 | G3 | NullableRefTypes.cs:6:7:6:13 | MyClass[] | NullableRefTypes.cs:6:7:6:13 | MyClass? |
101-
| NullableRefTypes.cs:25:18:25:18 | H | NullableRefTypes.cs:6:7:6:13 | MyClass[][] | NullableRefTypes.cs:6:7:6:13 | MyClass[]! |
102-
| NullableRefTypes.cs:26:38:26:38 | x | NullableRefTypes.cs:6:7:6:13 | MyClass[][] | NullableRefTypes.cs:6:7:6:13 | MyClass[]? |
103-
| NullableRefTypes.cs:27:38:27:38 | x | NullableRefTypes.cs:6:7:6:13 | MyClass[][] | NullableRefTypes.cs:6:7:6:13 | MyClass[]! |
101+
| NullableRefTypes.cs:25:18:25:18 | H | NullableRefTypes.cs:6:7:6:13 | MyClass[][] | NullableRefTypes.cs:6:7:6:13 | MyClass?[]! |
102+
| NullableRefTypes.cs:26:38:26:38 | x | NullableRefTypes.cs:6:7:6:13 | MyClass[][] | NullableRefTypes.cs:6:7:6:13 | MyClass![]? |
103+
| NullableRefTypes.cs:27:38:27:38 | x | NullableRefTypes.cs:6:7:6:13 | MyClass[][] | NullableRefTypes.cs:6:7:6:13 | MyClass?[]! |
104104
returnTypes
105105
| NullableRefTypes.cs:6:7:6:13 | MyClass | Void |
106106
| NullableRefTypes.cs:13:19:13:22 | get_C | MyClass? |

csharp/ql/test/library-tests/csharp8/NullableRefTypes.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ query predicate arrayElements(Variable v, ArrayType array, AnnotatedType element
3636

3737
query predicate returnTypes(Callable c, string t) {
3838
c.getFile().getBaseName() = "NullableRefTypes.cs" and
39-
t = c.getAnnotatedReturnType().toStringWithTypes()
39+
t = c.getAnnotatedReturnType().toString()
4040
}
4141

4242
query predicate typeArguments(ConstructedGeneric generic, int arg, string argument) {
@@ -45,7 +45,7 @@ query predicate typeArguments(ConstructedGeneric generic, int arg, string argume
4545
or
4646
generic = any(MethodCall mc).getTarget()
4747
) and
48-
argument = generic.getAnnotatedTypeArgument(arg).toStringWithTypes()
48+
argument = generic.getAnnotatedTypeArgument(arg).toString()
4949
}
5050

5151
query predicate nullableTypeParameters(TypeParameter p) {

0 commit comments

Comments
 (0)