Skip to content

Commit

Permalink
C#: Add extractor support for the allows ref struct general type para…
Browse files Browse the repository at this point in the history
…meter constraint.
  • Loading branch information
michaelnebel committed Jan 2, 2025
1 parent a1db011 commit b26c342
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public override void Populate(TextWriter trapFile)
if (Symbol.HasNotNullConstraint)
trapFile.general_type_parameter_constraints(this, 6);

if (Symbol.AllowsRefLikeType)
trapFile.general_type_parameter_constraints(this, 7);

foreach (var abase in Symbol.GetAnnotatedTypeConstraints())
{
var t = Type.Create(Context, abase.Symbol);
Expand Down
3 changes: 3 additions & 0 deletions csharp/ql/lib/semmle/code/csharp/Generics.qll
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ class TypeParameterConstraints extends Element, @type_parameter_constraints {
/** Holds if these constraints include a not-null type constraint. */
predicate hasNotNullTypeConstraint() { general_type_parameter_constraints(this, 6) }

/** Holds if these constraints include a `allows ref struct` constraint. */
predicate hasAllowRefLikeTypeConstraint() { general_type_parameter_constraints(this, 7) }

/** Gets a textual representation of these constraints. */
override string toString() { result = "where " + this.getTypeParameter().getName() + ": ..." }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ hasUnmanagedTypeConstraint
hasNullableRefTypeConstraint
hasNotNullConstraint
| TypeParameterConstraints.cs:14:20:14:21 | T5 | file://:0:0:0:0 | where T5: ... |
hasAllowRefLikeTypeConstraint
| TypeParameterConstraints.cs:18:20:18:21 | T7 | file://:0:0:0:0 | where T7: ... |
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ query predicate hasNullableRefTypeConstraint(TypeParameter tp, TypeParameterCons
query predicate hasNotNullConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
typeParameterContraints(tp, tpc) and tpc.hasNotNullTypeConstraint()
}

query predicate hasAllowRefLikeTypeConstraint(TypeParameter tp, TypeParameterConstraints tpc) {
typeParameterContraints(tp, tpc) and tpc.hasAllowRefLikeTypeConstraint()
}

0 comments on commit b26c342

Please sign in to comment.