Skip to content

Commit

Permalink
(#41) ImportLogic: correct allowlist for the interface implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Dec 30, 2024
1 parent 93e9c79 commit 89be17b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup Label="Packaging">
<Version>2.0.1-pre06</Version>
<Version>2.0.1-pre07</Version>
<Copyright>Copyright © JetBrains 2024</Copyright>

<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
Expand Down
22 changes: 8 additions & 14 deletions src/Refasmer/Importer/ImportLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ private bool AllowImportType( EntityHandle typeHandle )
switch (typeHandle.Kind)
{
case HandleKind.TypeDefinition:
return true;
var type = _reader.GetTypeDefinition((TypeDefinitionHandle)typeHandle);
return Filter.AllowImport(type, _reader);
case HandleKind.TypeReference:
return true;
case HandleKind.TypeSpecification:
Expand Down Expand Up @@ -87,7 +88,7 @@ private TypeDefinitionHandle ImportTypeDefinitionSkeleton(TypeDefinitionHandle s
if (!forcePreservePrivateFields)
PostProcessSkippedValueTypeFields(skippedInstanceFields!, importedInstanceFields!);

var implementations = GetCurrentAssemblyInterfaceMethodImplementations(src);
var implementations = GetAllowlistedInterfaceMethodImplementations(src);

foreach (var srcMethodHandle in src.GetMethods())
{
Expand Down Expand Up @@ -402,7 +403,7 @@ public bool IsReferenceAssembly() =>
.Select(_reader.GetFullname)
.Any(name => name == FullNames.ReferenceAssembly);

private ImmutableHashSet<MethodDefinitionHandle> GetCurrentAssemblyInterfaceMethodImplementations(
private ImmutableHashSet<MethodDefinitionHandle> GetAllowlistedInterfaceMethodImplementations(
TypeDefinition type)
{
return GetImplementations().ToImmutableHashSet();
Expand All @@ -413,15 +414,8 @@ IEnumerable<MethodDefinitionHandle> GetImplementations()
.Select(_reader.GetMethodImplementation);
foreach (var mi in implementations)
{
if (!AllowImportType(_reader.GetMethodClass(mi.MethodDeclaration)))
continue;

var bodyHandle = (MethodDefinitionHandle)mi.MethodBody;
var method = _reader.GetMethodDefinition(bodyHandle);
var isStatic = (method.Attributes & MethodAttributes.Static) != 0;
if (isStatic) continue;

yield return bodyHandle;
if (AllowImportType(_reader.GetMethodClass(mi.MethodDeclaration)))
yield return (MethodDefinitionHandle)mi.MethodBody;
}
}
}
Expand All @@ -430,7 +424,7 @@ private bool AllowImportMethod(
IImmutableSet<MethodDefinitionHandle> implementations,
MethodDefinitionHandle methodHandle,
MethodDefinition method) =>
!implementations.Contains(methodHandle) && (Filter == null || Filter.AllowImport(method, _reader));
implementations.Contains(methodHandle) || Filter == null || Filter.AllowImport(method, _reader);

public ReservedBlob<GuidHandle> Import()
{
Expand Down Expand Up @@ -672,7 +666,7 @@ private IEnumerable<TypeDefinitionHandle> CalculateInternalTypesToPreserve(
AcceptFieldSignature(field, collector);
}

var methodImplementations = GetCurrentAssemblyInterfaceMethodImplementations(type);
var methodImplementations = GetAllowlistedInterfaceMethodImplementations(type);
foreach (var methodHandle in type.GetMethods())
{
var method = _reader.GetMethodDefinition(methodHandle);
Expand Down
1 change: 1 addition & 0 deletions tests/Refasmer.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public async Task CheckRefasmedType(string typeName)
[TestCase("RefasmerTestAssembly.NonBlittableGraph")]
[TestCase("RefasmerTestAssembly.EmptyStructWithStaticMember")]
[TestCase("RefasmerTestAssembly.NonEmptyStructWithStaticMember")]
[TestCase("RefasmerTestAssembly.CustomEnumerable")]
public async Task CheckRefasmedTypeOmitNonApi(string typeName)
{
var assemblyPath = await BuildTestAssembly();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class: RefasmerTestAssembly.CustomEnumerable
- interface impl: System.Collections.IEnumerable
methods:
- System.Collections.IEnumerable.GetEnumerator(): System.Collections.IEnumerator:
- .ctor(): System.Void:
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ methods:
- GetHashCode(): System.Int32:
- op_Equality(RefasmerTestAssembly.InterfaceImplementations a, RefasmerTestAssembly.InterfaceImplementations b): System.Boolean:
- op_Inequality(RefasmerTestAssembly.InterfaceImplementations a, RefasmerTestAssembly.InterfaceImplementations b): System.Boolean:
- InstanceMethod(): System.Void:
- .ctor(): System.Void:
public interface: RefasmerTestAssembly.IWithStaticMethods`1
methods:
- op_Equality(TSelf a, TSelf b): System.Boolean:
- <abstract>
- op_Inequality(TSelf a, TSelf b): System.Boolean:
- <abstract>
- InstanceMethod(): System.Void:
- <abstract>
9 changes: 9 additions & 0 deletions tests/RefasmerTestAssembly/InterfaceImplementations.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Collections;

namespace RefasmerTestAssembly;

public interface IWithStaticMethods<TSelf> where TSelf : IWithStaticMethods<TSelf>
{
static abstract bool operator ==(TSelf a, TSelf b);
static abstract bool operator !=(TSelf a, TSelf b);

void InstanceMethod();
}
public class InterfaceImplementations : IWithStaticMethods<InterfaceImplementations>
{
Expand All @@ -16,4 +19,10 @@ public class InterfaceImplementations : IWithStaticMethods<InterfaceImplementati
public static bool operator ==(InterfaceImplementations a, InterfaceImplementations b) => throw new NotSupportedException();

public static bool operator !=(InterfaceImplementations a, InterfaceImplementations b) => throw new NotSupportedException();

public void InstanceMethod() { }
}
public class CustomEnumerable : IEnumerable
{
IEnumerator IEnumerable.GetEnumerator() => null!;
}

0 comments on commit 89be17b

Please sign in to comment.