Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
takeisit opened this issue Aug 1, 2024 · 1 comment
Open

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

takeisit opened this issue Aug 1, 2024 · 1 comment
Labels

Comments

@takeisit
Copy link

takeisit commented Aug 1, 2024

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
    {
	// ...
Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the stale label Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant