Skip to content

Fix #333. Compilation fails when there are 2 generic union types with the same name: MyUnion<T> and MyUnion<T1, T2>.#334

Merged
domn1995 merged 2 commits intodomn1995:mainfrom
Dixin:fix-generic
Jan 1, 2026
Merged

Fix #333. Compilation fails when there are 2 generic union types with the same name: MyUnion<T> and MyUnion<T1, T2>.#334
domn1995 merged 2 commits intodomn1995:mainfrom
Dixin:fix-generic

Conversation

@Dixin
Copy link
Copy Markdown
Contributor

@Dixin Dixin commented Nov 20, 2025

The following code cannot be compiled because of Result<T, TError> and Result<TError>:

[Union]
public partial record Result<T, TError>
{
    public partial record Ok(T Value);
    public partial record Error(TError Value);
}

[Union]
public partial record Result<TError>
{
    public partial record Ok();
    public partial record Error(TError Value);
}

public static class Result
{
    public static Result<T, TError> Ok<T, TError>(T value) => new Result<T, TError>.Ok(value);

    public static Result<T, TError> Error<T, TError>(TError value) => new Result<T, TError>.Error(value);

    public static Result<TError> Ok<TError>() => new Result<TError>.Ok();

    public static Result<TError> Error<TError>(TError value) => new Result<TError>.Error(value);
}

Error message:

CSC : warning CS8785: Generator 'UnionGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'The hintName '.Result.g.cs' of the added source file must be unique within a generator. (Parameter 'hintName')'.

The source generator uses the same file name for both Result<T, TError> and Result<TError>:

        context.AddSource(
            $"{unionRecord.Namespace}.{unionRecord.Name}.g.cs",
            SourceText.From(union, Encoding.UTF8)
        );

I would like to update it to:

        context.AddSource(
            unionRecord.TypeParameters.Count == 0
             ? $"{unionRecord.Namespace}.{unionRecord.Name}.g.cs"
             : $"{unionRecord.Namespace}.{unionRecord.Name}.{unionRecord.TypeParameters.Count}.g.cs",
            SourceText.From(union, Encoding.UTF8)
        );

@domn1995 domn1995 merged commit 324b8d3 into domn1995:main Jan 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants