Skip to content

Commit ecc6c98

Browse files
Extend associated type support to all type shape kinds. (#251)
1 parent 5f7388c commit ecc6c98

11 files changed

Lines changed: 42 additions & 22 deletions

File tree

src/PolyType.Roslyn/ModelGenerator/TypeDataModelGenerator.Object.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,13 @@ type.TypeKind is not (TypeKind.Struct or TypeKind.Class or TypeKind.Interface) |
121121
return true;
122122
}
123123

124-
private void IncludeAssociatedShapes(ITypeSymbol type, ImmutableArray<AssociatedTypeModel> associatedTypes, ref TypeDataModelGenerationContext ctx)
124+
/// <summary>
125+
/// Includes the specified associated types to the type graph traversal.
126+
/// </summary>
127+
/// <param name="type">The type from which the associated types originate.</param>
128+
/// <param name="associatedTypes">The associated types to include.</param>
129+
/// <param name="ctx">The type graph traversal context.</param>
130+
protected void IncludeAssociatedShapes(ITypeSymbol type, ImmutableArray<AssociatedTypeModel> associatedTypes, ref TypeDataModelGenerationContext ctx)
125131
{
126132
_associatedTypes = _associatedTypes.SetItem(
127133
type,

src/PolyType.SourceGenerator/Parser/Parser.Diagnostics.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,6 @@ public sealed partial class Parser
119119
defaultSeverity: DiagnosticSeverity.Error,
120120
isEnabledByDefault: true);
121121

122-
private static DiagnosticDescriptor AssociatedTypeInExternalAssembly { get; } = new DiagnosticDescriptor(
123-
id: "PT0017",
124-
title: "Associated type must be declared in same assembly.",
125-
messageFormat: "The associated type '{0}' is not declared in the same assembly as the attribute annotation specifying it.",
126-
category: "PolyType.SourceGenerator",
127-
defaultSeverity: DiagnosticSeverity.Error,
128-
isEnabledByDefault: true);
129-
130122
internal static DiagnosticDescriptor ConflictingMarshalers { get; } = new DiagnosticDescriptor(
131123
id: "PT0018",
132124
title: "Multiple marshalers specified.",

src/PolyType.SourceGenerator/Parser/Parser.ModelMapper.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,6 @@ private ImmutableEquatableSet<AssociatedTypeId> CollectAssociatedTypes(TypeDataM
278278
ITypeSymbol[] typeArgs = namedType?.GetRecursiveTypeArguments() ?? [];
279279
foreach ((INamedTypeSymbol openType, IAssemblySymbol associatedAssembly, Location? location, TypeShapeRequirements requirements) in associatedTypeSymbols)
280280
{
281-
if (!SymbolEqualityComparer.Default.Equals(openType.ContainingAssembly, associatedAssembly))
282-
{
283-
ReportDiagnostic(AssociatedTypeInExternalAssembly, location, openType.GetFullyQualifiedName());
284-
continue;
285-
}
286-
287281
if (!IsAccessibleSymbol(openType))
288282
{
289283
// Skip types that are not accessible in the current scope

src/PolyType.SourceGenerator/Parser/Parser.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,9 +685,12 @@ private TypeDataModelGenerationStatus MapSurrogateType(ITypeSymbol type, ITypeSy
685685
MarshalerType = namedMarshaler,
686686
Methods = MapMethods(type, ref ctx, methodFlags),
687687
Events = MapEvents(type, ref ctx, methodFlags),
688+
AssociatedTypes = associatedTypes,
688689
};
689690
}
690691

692+
// Include any associated shapes.
693+
IncludeAssociatedShapes(type, associatedTypes, ref ctx);
691694
return status;
692695

693696
TypeDataModelGenerationStatus ReportInvalidMarshalerAndExit()

src/PolyType.SourceGenerator/SourceFormatter/SourceFormatter.Enum.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ private void FormatEnumTypeShapeFactory(SourceWriter writer, string methodName,
1313
return new global::PolyType.SourceGenModel.SourceGenEnumTypeShape<{{enumShapeType.Type.FullyQualifiedName}}, {{enumShapeType.UnderlyingType.FullyQualifiedName}}>
1414
{
1515
UnderlyingType = {{GetShapeModel(enumShapeType.UnderlyingType).SourceIdentifier}},
16+
AssociatedTypeShapes = {{FormatAssociatedTypeShapes(enumShapeType)}},
1617
Provider = this,
1718
Members = new global::System.Collections.Generic.Dictionary<string, {{enumShapeType.UnderlyingType.FullyQualifiedName}}>({{enumShapeType.Members.Count}}, global::System.StringComparer.Ordinal)
1819
{
1920
{{string.Join("\n ", enumShapeType.Members.Select(member => $"[\"{member.Key}\"] = {member.Value},"))}}
2021
}
2122
};
2223
}
23-
""");
24+
""", trimNullAssignmentLines: true);
2425
}
2526
}

src/PolyType.SourceGenerator/SourceFormatter/SourceFormatter.FSharpUnion.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ private void FormatFSharpUnionTypeShapeFactory(SourceWriter writer, string metho
2121
GetUnionCaseIndexFunc = {{FormatFSharpUnionTagReader(unionShapeModel)}},
2222
CreateMethodsFunc = {{FormatNull(methodFactoryMethodName)}},
2323
CreateEventsFunc = {{FormatNull(eventFactoryMethodName)}},
24+
AssociatedTypeShapes = {{FormatAssociatedTypeShapes(unionShapeModel)}},
2425
Provider = this,
2526
};
2627
}

src/PolyType.SourceGenerator/SourceFormatter/SourceFormatter.Function.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ private void FormatFunctionTypeShapeFactory(SourceWriter writer, string methodNa
2626
FunctionInvoker = {{FormatFunctionInvoker(functionShapeModel, functionArgumentStateFQN)}},
2727
FromDelegateFunc = {{FormatNull(FormatFromDelegateFunc(functionShapeModel, functionArgumentStateFQN, requiredParametersMaskFieldName, requireAsync: false))}},
2828
FromAsyncDelegateFunc = {{FormatNull(FormatFromDelegateFunc(functionShapeModel, functionArgumentStateFQN, requiredParametersMaskFieldName, requireAsync: true))}},
29+
AssociatedTypeShapes = {{FormatAssociatedTypeShapes(functionShapeModel)}},
2930
Provider = this,
3031
};
3132
}

src/PolyType.SourceGenerator/SourceFormatter/SourceFormatter.Optional.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ private void FormatOptionalTypeShapeFactory(SourceWriter writer, string methodNa
2323
Deconstructor = {{FormatDeconstructor()}},
2424
CreateMethodsFunc = {{FormatNull(methodFactoryMethodName)}},
2525
CreateEventsFunc = {{FormatNull(eventFactoryMethodName)}},
26+
AssociatedTypeShapes = {{FormatAssociatedTypeShapes(optionalShapeModel)}},
2627
Provider = this,
2728
};
2829
}
29-
""");
30+
""", trimNullAssignmentLines: true);
3031
writer.WriteLine("#pragma warning restore CS8767", disableIndentation: true);
3132

3233
if (methodFactoryMethodName is not null)

src/PolyType.SourceGenerator/SourceFormatter/SourceFormatter.Surrogate.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ private void FormatSurrogateTypeShapeFactory(SourceWriter writer, string methodN
1515
{
1616
return new global::PolyType.SourceGenModel.SourceGenSurrogateTypeShape<{{surrogateShapeModel.Type.FullyQualifiedName}}, {{surrogateShapeModel.SurrogateType.FullyQualifiedName}}>
1717
{
18-
Marshaler = new {{surrogateShapeModel.MarshalerType.FullyQualifiedName}}(),
18+
Marshaler = new {{surrogateShapeModel.MarshalerType.FullyQualifiedName}}()!,
1919
SurrogateType = {{GetShapeModel(surrogateShapeModel.SurrogateType).SourceIdentifier}},
2020
CreateMethodsFunc = {{FormatNull(methodFactoryMethodName)}},
2121
CreateEventsFunc = {{FormatNull(eventFactoryMethodName)}},
22+
AssociatedTypeShapes = {{FormatAssociatedTypeShapes(surrogateShapeModel)}},
2223
Provider = this,
2324
};
2425
}
25-
""");
26+
""", trimNullAssignmentLines: true);
2627

2728
if (methodFactoryMethodName is not null)
2829
{

src/PolyType.SourceGenerator/SourceFormatter/SourceFormatter.Union.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ private void FormatUnionTypeShapeFactory(SourceWriter writer, string methodName,
2323
GetUnionCaseIndexFunc = {{getUnionCaseIndexMethod}},
2424
CreateMethodsFunc = {{FormatNull(methodFactoryMethodName)}},
2525
CreateEventsFunc = {{FormatNull(eventFactoryMethodName)}},
26+
AssociatedTypeShapes = {{FormatAssociatedTypeShapes(unionShapeModel)}},
2627
Provider = this,
2728
};
2829
}
29-
""");
30+
""", trimNullAssignmentLines: true);
3031

3132
writer.WriteLine();
3233
FormatUnionCasesFactory(writer, unionShapeModel, createUnionCasesMethodName);

0 commit comments

Comments
 (0)