Skip to content

Commit 573646f

Browse files
committed
C#: Various C#8 features:
- Async streams (test only) - Unmanaged generic structs (extractor support) - Alternate interpolated strings (test only) - static local function (test only)
1 parent 5bbbd26 commit 573646f

File tree

12 files changed

+106
-0
lines changed

12 files changed

+106
-0
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/TypeParameter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public override void Populate()
3434
if (symbol.HasConstructorConstraint)
3535
Context.Emit(Tuples.general_type_parameter_constraints(constraints, 3));
3636

37+
if(symbol.HasUnmanagedTypeConstraint)
38+
Context.Emit(Tuples.general_type_parameter_constraints(constraints, 4));
39+
3740
ITypeSymbol baseType = symbol.HasValueTypeConstraint ?
3841
Context.Compilation.GetTypeByMetadataName(valueTypeName) :
3942
Context.Compilation.ObjectType;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ class TypeParameterConstraints extends Element, @type_parameter_constraints {
223223
/** Holds if these constraints include a general value type constraint. */
224224
predicate hasValueTypeConstraint() { general_type_parameter_constraints(this, 2) }
225225

226+
/** Holds if these constraints include an unmanaged type constraint. */
227+
predicate hasUnmanagedTypeConstraint() { general_type_parameter_constraints(this, 4) }
228+
226229
/** Gets a textual representation of these constraints. */
227230
override string toString() { result = "where " + this.getTypeParameter().toString() + ": ..." }
228231
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System;
2+
3+
class AlternateInterpolatedStrings
4+
{
5+
string s1 = $@"C:{12}";
6+
string s2 = @$"C:{12}";
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
inserts
2+
| AlternateInterpolatedStrings.cs:5:17:5:26 | $"..." | 1 | AlternateInterpolatedStrings.cs:5:23:5:24 | 12 |
3+
| AlternateInterpolatedStrings.cs:6:17:6:26 | $"..." | 1 | AlternateInterpolatedStrings.cs:6:23:6:24 | 12 |
4+
text
5+
| AlternateInterpolatedStrings.cs:5:17:5:26 | $"..." | 0 | AlternateInterpolatedStrings.cs:5:20:5:21 | "C:" |
6+
| AlternateInterpolatedStrings.cs:6:17:6:26 | $"..." | 0 | AlternateInterpolatedStrings.cs:6:20:6:21 | "C:" |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import csharp
2+
3+
query predicate inserts(InterpolatedStringExpr expr, int i, Expr insert) {
4+
insert = expr.getInsert(i)
5+
}
6+
7+
query predicate text(InterpolatedStringExpr expr, int i, StringLiteral literal) {
8+
literal = expr.getText(i)
9+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// semmle-extractor-options: /r:System.Threading.Tasks.dll /r:System.Threading.Tasks.Extensions.dll /r:netstandard.dll
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
class AsyncStreams
9+
{
10+
async IAsyncEnumerable<int> Items() {
11+
yield return 1;
12+
yield return 2;
13+
await Task.Delay(1000);
14+
yield return 3;
15+
}
16+
17+
async void F()
18+
{
19+
await foreach(var item in Items())
20+
Console.WriteLine(item);
21+
}
22+
}
23+
24+
namespace System
25+
{
26+
interface IAsyncDisposable
27+
{
28+
System.Threading.Tasks.ValueTask DisposeAsync();
29+
}
30+
}
31+
32+
namespace System.Collections.Generic
33+
{
34+
interface IAsyncEnumerable<out T>
35+
{
36+
public System.Collections.Generic.IAsyncEnumerator<T> GetAsyncEnumerator (System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
37+
}
38+
39+
interface IAsyncEnumerator<out T> : IAsyncDisposable
40+
{
41+
T Current { get; }
42+
System.Threading.Tasks.ValueTask<bool> MoveNextAsync();
43+
}
44+
}

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

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import csharp
2+
3+
class DefaultInterfaceMethod extends Method {
4+
DefaultInterfaceMethod() {
5+
this.hasBody() and
6+
this.getDeclaringType() instanceof Interface
7+
}
8+
}
9+
10+
query predicate defaultInterfaceMethods(DefaultInterfaceMethod m) { any() }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
class StaticLocalFunction
4+
{
5+
int F()
6+
{
7+
static int G(int x) => x;
8+
return G(12);
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System;
2+
3+
struct S<T, U> where T: unmanaged
4+
{
5+
int id;
6+
T value1;
7+
U value2;
8+
}

0 commit comments

Comments
 (0)