Skip to content

Commit

Permalink
Sandbox error reference locator now works with generic method calls
Browse files Browse the repository at this point in the history
This means resolving the MethodSpec table entry for it.
  • Loading branch information
PJB3005 committed Nov 22, 2024
1 parent 5f3a543 commit 3086fc4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ END TEMPLATE-->

### Other

*None yet*
* Sandbox error reference locator now works with generic method calls.

### Internal

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private void ReportBadReferences(PEReader peReader, MetadataReader reader, IEnum
{
if (instruction.TryGetEntityHandle(out var handle))
{
if (refs.Contains(handle))
if (refs.Overlaps(ExpandHandle(reader, handle)))
{
var type = GetTypeFromDefinition(reader, methodDef.GetDeclaringType());
_sawmill.Error(
Expand All @@ -56,6 +56,12 @@ private static string DisplayHandle(MetadataReader reader, EntityHandle handle)
{
switch (handle.Kind)
{
case HandleKind.MethodSpecification:
var methodSpec = reader.GetMethodSpecification((MethodSpecificationHandle)handle);
var methodProvider = new TypeProvider();
var spec = methodSpec.DecodeSignature(methodProvider, 0);
return $"{DisplayHandle(reader, methodSpec.Method)}<{string.Join(", ", spec.Select(t => t.ToString()))}>";

case HandleKind.MemberReference:
var memberRef = reader.GetMemberReference((MemberReferenceHandle)handle);
var name = reader.GetString(memberRef.Name);
Expand Down Expand Up @@ -92,6 +98,17 @@ private static void ExpandReferences(MetadataReader reader, HashSet<EntityHandle
handles.UnionWith(toAdd);
}

private static IEnumerable<EntityHandle> ExpandHandle(MetadataReader reader, EntityHandle handle)
{
// Annoying, S.R.M gives no way to iterate over the MethodSpec table.
// This means the only way to correlate MethodSpec references is to do it for each handle.

yield return handle;

if (handle.Kind == HandleKind.MethodSpecification)
yield return reader.GetMethodSpecification((MethodSpecificationHandle)handle).Method;
}

private readonly struct ILInstruction
{
public readonly ILOpCode OpCode;
Expand Down

0 comments on commit 3086fc4

Please sign in to comment.