UnitGenerator icon indicating copy to clipboard operation
UnitGenerator copied to clipboard

UnitOf(typeof(OtherLib.MyParsable),ParseMethod) does not work

Open takeisit opened this issue 1 year ago • 0 comments

UnitOf with ParseMethod option does not accept the type declared in other assemblies.

A solution has 2 projects:

  • OtherLib (.NET8 )
     <PackageReference Include="UnitGenerator" Version="1.6.2">
       <PrivateAssets>all</PrivateAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
     </PackageReference>
    
  • ConsoleApp (.NET8 )
     <PackageReference Include="UnitGenerator" Version="1.6.2">
       <PrivateAssets>all</PrivateAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
     </PackageReference>
     <ProjectReference Include="..\OtherLib\OtherLib.csproj" />
    

OtherLib:

using System.Diagnostics.CodeAnalysis;

namespace OtherLib
{
    public readonly struct MyParsable : IParsable<MyParsable>
    {
        public static MyParsable Parse(string s)
            => throw new NotImplementedException();

        public static bool TryParse([NotNullWhen(true)] string? s, [MaybeNullWhen(false)] out MyParsable result)
            => throw new NotImplementedException();

        public static MyParsable Parse(string s, IFormatProvider? provider)
            => throw new NotImplementedException();

        public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out MyParsable result)
            => throw new NotImplementedException();

        // workaround of #52
        public static MyParsable Parse(ReadOnlySpan<byte> s, IFormatProvider? provider)
            => throw new NotImplementedException();
        public static bool TryParse([NotNullWhen(true)] ReadOnlySpan<byte> s, IFormatProvider? provider, [MaybeNullWhen(false)] out MyParsable result)
            => throw new NotImplementedException();
    }

    [UnitGenerator.UnitOf(typeof(MyParsable), UnitGenerator.UnitGenerateOptions.ParseMethod)]
    public readonly partial struct StructInOtherLib
    {
        public static void Test()
            => StructInOtherLib.Parse("", null); // no error
    }
}

ConsoleApp:

OtherLib.StructInOtherLib.Test();
StructOutOfOtherLib.Test();

[UnitGenerator.UnitOf(typeof(OtherLib.MyParsable), UnitGenerator.UnitGenerateOptions.ParseMethod)]
public readonly partial struct StructOutOfOtherLib
{
    public static void Test()
        => StructOutOfOtherLib.Parse("", null);  // error CS1501: No overload for method 'Parse' takes 2
}

The generated code displayed on IDE(VS17.10.5):

    [System.ComponentModel.TypeConverter(typeof(StructOutOfOtherLibTypeConverter))]
    readonly partial struct StructOutOfOtherLib 
        : IEquatable<StructOutOfOtherLib>
#if NET7_0_OR_GREATER
        , IParsable<StructOutOfOtherLib>
#endif
#if NET8_0_OR_GREATER
        , IEqualityOperators<StructOutOfOtherLib, StructOutOfOtherLib, bool>
        , IUtf8SpanParsable<StructOutOfOtherLib>
#endif
    {
	// ...

The generated code emitted by using <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>:

    [System.ComponentModel.TypeConverter(typeof(StructOutOfOtherLibTypeConverter))]
    readonly partial struct StructOutOfOtherLib 
        : IEquatable<StructOutOfOtherLib>
#if NET8_0_OR_GREATER
        , IEqualityOperators<StructOutOfOtherLib, StructOutOfOtherLib, bool>
#endif
    {
	// ...

takeisit avatar Aug 01 '24 13:08 takeisit