Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,20 @@ private unsafe struct TypeNameUtf8
{
public required void* Utf8TypeName { get; init; }
public required int Utf8TypeNameLen { get; init; }

public bool Equals(TypeNameUtf8 other)
{
if (Utf8TypeNameLen != other.Utf8TypeNameLen)
return false;
byte* thisChars = (byte*)Utf8TypeName;
byte* otherChars = (byte*)other.Utf8TypeName;
for (int i = 0; i < Utf8TypeNameLen; i++)
{
if (thisChars[i] != otherChars[i])
return false;
}
return true;
}
}

[RequiresUnreferencedCode("Lazy TypeMap isn't supported for Trimmer scenarios")]
Expand All @@ -282,6 +296,8 @@ public DelayedType(TypeNameUtf8 typeNameUtf8, RuntimeAssembly fallbackAssembly)
_type = null;
}

public TypeNameUtf8 TypeName => _typeNameUtf8;

public unsafe Type GetOrLoadType()
{
if (_type is null)
Expand Down Expand Up @@ -328,7 +344,9 @@ protected override bool TryGetOrLoadType(string key, [NotNullWhen(true)] out Typ
public void Add(string key, TypeNameUtf8 targetType, RuntimeAssembly fallbackAssembly)
{
int hash = ComputeHashCode(key);
if (_lazyData.ContainsKey(hash))
// Allow duplicates that have the same string -> mapping. They may have different trimTargets.
// Warn if the mapping conflicts with an existing mapping.
if (_lazyData.TryGetValue(hash, out DelayedType? existing) && existing.TypeName.Equals(targetType))
{
ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

[assembly: TypeMap<UsedTypeMap>("TrimTargetIsTarget", typeof(TargetAndTrimTarget), typeof(TargetAndTrimTarget))]
[assembly: TypeMap<UsedTypeMap>("TrimTargetIsUnrelated", typeof(TargetType), typeof(TrimTarget))]
[assembly: TypeMap<UsedTypeMap>("DuplicateMappingWithDifferentTrimTargets", typeof(TargetType2), typeof(TrimTarget2))]
[assembly: TypeMap<UsedTypeMap>("DuplicateMappingWithDifferentTrimTargets", typeof(TargetType2), typeof(TrimTarget3))]
[assembly: TypeMap<UsedTypeMap>("TrimTargetIsUnreferenced", typeof(UnreferencedTargetType), typeof(UnreferencedTrimTarget))]
[assembly: TypeMapAssociation<UsedTypeMap>(typeof(SourceClass), typeof(ProxyType))]

Expand All @@ -34,7 +36,7 @@

IReadOnlyDictionary<string, Type> usedTypeMap = TypeMapping.GetOrCreateExternalTypeMapping<UsedTypeMap>();

if (!usedTypeMap.TryGetValue("TrimTargetIsTarget", out Type targetAndTrimTargetType))
if (!usedTypeMap.TryGetValue("TrimTargetIsTarget", out Type? targetAndTrimTargetType))
{
Console.WriteLine("TrimTargetIsTarget not found in used type map.");
return 1;
Expand All @@ -46,12 +48,13 @@
return 2;
}

if (!usedTypeMap.TryGetValue("TrimTargetIsUnrelated", out Type targetType))
if (!usedTypeMap.TryGetValue("TrimTargetIsUnrelated", out Type? targetType))
{
Console.WriteLine("TrimTargetIsUnrelated not found in used type map.");
return 3;
}


if (targetType != GetTypeWithoutTrimAnalysis(nameof(TargetType)))
{
Console.WriteLine("TrimTargetIsUnrelated type does not match expected type.");
Expand All @@ -71,7 +74,7 @@
}

IReadOnlyDictionary<Type, Type> usedProxyTypeMap = TypeMapping.GetOrCreateProxyTypeMapping<UsedTypeMap>();
if (!usedProxyTypeMap.TryGetValue(typeof(SourceClass), out Type proxyType))
if (!usedProxyTypeMap.TryGetValue(typeof(SourceClass), out Type? proxyType))
{
Console.WriteLine("SourceClass not found in used proxy type map.");
return 7;
Expand All @@ -95,6 +98,12 @@
return 10;
}

if (!usedTypeMap.TryGetValue("DuplicateMappingWithDifferentTrimTargets", out Type? duplicatedTarget))
{
Console.WriteLine("Could not find duplicated target type");
return 11;
}

return 100;

[MethodImpl(MethodImplOptions.NoInlining)]
Expand All @@ -106,7 +115,10 @@ static Type GetTypeWithoutTrimAnalysis(string typeName)
class UsedTypeMap;
class TargetAndTrimTarget;
class TargetType;
class TargetType1;
class TrimTarget;
class TrimTarget1;
class TrimTarget2;
class UnreferencedTargetType;
class UnreferencedTrimTarget;
class SourceClass;
Expand Down
Loading