C#: Test example with ref local, unsafe context and ref struct in async and iterator methods.

This commit is contained in:
Michael Nebel
2025-01-15 15:16:56 +01:00
parent f62a3ace9a
commit ca23e1b1fc
6 changed files with 56 additions and 0 deletions

View File

@@ -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 |

View File

@@ -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 |

View File

@@ -52,5 +52,26 @@ namespace Semmle
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;
}
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1 @@
| iterators.cs:14:29:14:38 | GetObjects | iterators.cs:23:22:23:25 | access to local variable zero |

View File

@@ -0,0 +1,5 @@
import csharp
from Callable c, Expr return
where c.canYieldReturn(return)
select c, return