Skip to content

Commit

Permalink
Merge pull request #18498 from michaelnebel/csharp/refandunsafe
Browse files Browse the repository at this point in the history
C# 13: [TEST ONLY] Test example with ref local, unsafe context and ref struct in async- and iterator methods.
  • Loading branch information
michaelnebel authored Jan 16, 2025
2 parents ba2b7ab + ca23e1b commit 0452b4a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions csharp/ql/test/library-tests/async/Async.expected
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@
| async.cs:50:49:50:57 | OpenAsync | file://:0:0:0:0 | async |
| async.cs:50:49:50:57 | OpenAsync | file://:0:0:0:0 | private |
| async.cs:50:49:50:57 | OpenAsync | file://:0:0:0:0 | static |
| async.cs:64:40:64:53 | GetObjectAsync | file://:0:0:0:0 | async |
| async.cs:64:40:64:53 | GetObjectAsync | file://:0:0:0:0 | private |
| async.cs:64:40:64:53 | GetObjectAsync | file://:0:0:0:0 | static |
1 change: 1 addition & 0 deletions csharp/ql/test/library-tests/async/Await.expected
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
| 42 | async.cs:42:46:42:70 | await ... | async.cs:42:52:42:70 | call to method OpenAsync |
| 44 | async.cs:44:38:44:66 | await ... | async.cs:44:44:44:66 | call to method ReadToEndAsync |
| 52 | async.cs:52:13:52:51 | await ... | async.cs:52:19:52:51 | call to method PrintContentLengthAsync |
| 73 | async.cs:73:13:73:31 | await ... | async.cs:73:19:73:31 | call to method Delay |
21 changes: 21 additions & 0 deletions csharp/ql/test/library-tests/async/async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,26 @@ private static async Task<StreamReader> OpenAsync(string filename)
await PrintContentLengthAsync(filename);
return File.OpenText(filename);
}

private ref struct RS
{
public int GetZero() { return 0; }
}

private static int one = 1;

// Test that we can use ref locals, ref structs and unsafe blocks in async methods.
private static async Task<int> GetObjectAsync()
{
unsafe
{
// Do pointer stuff
}
RS rs;
ref int i = ref one;
var zero = rs.GetZero();
await Task.Delay(i);
return zero;
}
}
}
25 changes: 25 additions & 0 deletions csharp/ql/test/library-tests/iterators/iterators.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;

public ref struct RS
{
public int GetZero() { return 0; }
}

public class C
{
private int one = 1;

// Test that we can use unsafe context, ref locals and ref structs in iterators.
public IEnumerable<int> GetObjects()
{
unsafe
{
// Do pointer stuff
}
ref int i = ref one;
RS rs;
var zero = rs.GetZero();
yield return zero;
}
}
1 change: 1 addition & 0 deletions csharp/ql/test/library-tests/iterators/iterators.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| iterators.cs:14:29:14:38 | GetObjects | iterators.cs:23:22:23:25 | access to local variable zero |
5 changes: 5 additions & 0 deletions csharp/ql/test/library-tests/iterators/iterators.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import csharp

from Callable c, Expr return
where c.canYieldReturn(return)
select c, return

0 comments on commit 0452b4a

Please sign in to comment.