Skip to content

Commit

Permalink
Turn implicit conversion into compiled templates
Browse files Browse the repository at this point in the history
  • Loading branch information
kzu committed Dec 7, 2024
1 parent 5268693 commit c0605b1
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/StructId.Analyzer/ConstructorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class ConstructorGenerator() : BaseGenerator(
ThisAssembly.Resources.Templates.ConstructorT.Text,
ReferenceCheck.TypeExists)
{
// NOTE: since we only emit the ctor if the struct doesn't already have one,
// we cannot switch this to the simpler compiled templates, which don't have conditional logic
protected override IncrementalValuesProvider<TemplateArgs> OnInitialize(IncrementalGeneratorInitializationContext context, IncrementalValuesProvider<TemplateArgs> source)
=> base.OnInitialize(context, source.Where(x
=> x.StructId.DeclaringSyntaxReferences.Select(r => r.GetSyntax()).OfType<TypeDeclarationSyntax>().All(s => s.ParameterList == null)));
Expand Down
10 changes: 0 additions & 10 deletions src/StructId.Analyzer/ConversionGenerator.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/StructId.Analyzer/TemplatedGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var type = idType.DeclaringSyntaxReferences[0].GetSyntax(cancellation) as TypeDeclarationSyntax;
var iface = type?.BaseList?.Types.FirstOrDefault()?.Type;
if (type == null || iface == null)
return new Template(structId, idType, attribute, known);
return new Template(structId, idType, attribute, known) { OriginalTId = idType };

if (x.Right.Compilation.GetSemanticModel(type.SyntaxTree).GetSymbolInfo(iface).Symbol is not INamedTypeSymbol ifaceType)
return new Template(structId, idType, attribute, known);
Expand Down
7 changes: 0 additions & 7 deletions src/StructId/ResourceTemplates/Conversion.cs

This file was deleted.

10 changes: 10 additions & 0 deletions src/StructId/Templates/Conversion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// <auto-generated />

using StructId;

[TStructId]
file partial record struct TSelf(string Value)
{
public static implicit operator string(TSelf id) => id.Value;
public static explicit operator TSelf(string value) => new(value);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// <auto-generated />

readonly partial record struct TSelf
using StructId;

[TStructId]
file partial record struct TSelf(TId Value)
{
public static implicit operator TId(TSelf id) => id.Value;
public static explicit operator TSelf(TId value) => new(value);
}
}

file record struct TId;

0 comments on commit c0605b1

Please sign in to comment.