Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
86 changes: 84 additions & 2 deletions Janus.Analyzers/Models/UnionTypeAttribute.Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private void Initialize(ITypeSymbol variant, CancellationToken ct)
Type = new(
isArray
? VariantTypeKind.Reference
: actualVariant.IsUnmanagedType
: actualVariant.IsUnmanagedOrUnmanagedUnionType
? VariantTypeKind.Unmanaged
: VariantTypeKind.Value,
IsNullable: true,
Expand All @@ -106,7 +106,7 @@ private void Initialize(ITypeSymbol variant, CancellationToken ct)
var name = variant.ToDisplayString(TypeDisplayFormat);
Type = extractedVariant switch
{
{ IsUnmanagedType: true } =>
{ IsUnmanagedOrUnmanagedUnionType: true } =>
new(isArray
? VariantTypeKind.Reference
: VariantTypeKind.Unmanaged,
Expand Down Expand Up @@ -172,3 +172,85 @@ public override Boolean IsNullable
set => base.IsNullable = value;
}
}

file static class Extensions
{
extension(ITypeSymbol symbol)
{
public Boolean IsUnmanagedOrUnmanagedUnionType
{
get
{
var result = isUnmanaged(symbol, null);

return result;

Boolean isUnmanaged(ITypeSymbol candidate, Dictionary<ITypeSymbol, Boolean?>? unmanagedTypes)
{
if (!candidate.IsUnmanagedType)
{
unmanagedTypes?[candidate] = false;
return false;
}

unmanagedTypes = new Dictionary<ITypeSymbol, Boolean?>(SymbolEqualityComparer.Default);

if (unmanagedTypes.TryGetValue(candidate, out var memoizedValue))
{
if (memoizedValue is { } memoizedResult)
{
return memoizedResult;
}

return true;
}

var typeAttributes = candidate.GetAttributes();
foreach (var attribute in typeAttributes)
{
if (!attribute.IsUnionTypeAttribute())
{
continue;
}

foreach (var variant in attribute.AttributeClass?.TypeArguments ?? [])
{
if (isUnmanaged(variant, unmanagedTypes))
{
continue;
}

unmanagedTypes[candidate] = false;
return false;
}
}

if (candidate is INamedTypeSymbol namedCandidate)
{
foreach (var typeParameter in namedCandidate.TypeParameters)
{
if (typeParameter.HasUnmanagedTypeConstraint)
{
continue;
}

var typeParameterAttributes = typeParameter.GetAttributes();

foreach (var attribute in typeParameterAttributes)
{
if (attribute.IsUnionTypeAttribute())
{
unmanagedTypes[candidate] = false;
return false;
}
}
}
}

unmanagedTypes[candidate] = true;
return true;
}
}
}
}
}
20 changes: 20 additions & 0 deletions Janus.Tests.EndToEnd/UnionTypeVariantTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MPL-2.0

namespace RhoMicro.CodeAnalysis.Janus.EndToEnd.Tests;

public partial class UnionTypeVariantTests
{
[UnionType<String>]
partial struct Text;

[UnionType<Text, Int32>]
partial struct Union;

[Fact]
public void ManagedUnionTypeStructVariantDoesNotThrowTle()
{
Text text = "foo";
Union union = text;
Assert.Equal(text, union.CastToText);
}
}
Loading