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

Calling method with generic return type and this keyword will omit return type #44

Closed
persn opened this issue Jan 16, 2024 · 6 comments
Closed

Comments

@persn
Copy link

persn commented Jan 16, 2024

Like the title says.. Using the following code

internal partial class Class4
{
    [Zomp.SyncMethodGenerator.CreateSyncVersion]
    public async Task<T> FooAsync<T>(CancellationToken cancellationToken = default)
    {
        return await this.InnerFooAsync<T>(cancellationToken).ConfigureAwait(false);
    }

    [Zomp.SyncMethodGenerator.CreateSyncVersion]
    private async Task<T> InnerFooAsync<T>(CancellationToken cancellationToken = default)
    {
        await Task.Delay(100, cancellationToken);
        return default;
    }
}

Generates the following

internal partial class Class4
{
    public T Foo<T>()
    {
        return this.InnerFoo();
    }
}

When I use the this keyword when calling InnerFooAsync the return type magically vanishes, resulting in error

ClassLibrary1.Class4.FooAsync.g.cs(9,21,9,29): error CS0411: The type arguments for method 'Class4.InnerFoo<T>()' cannot be inferred from the usage. Try specifying the type arguments explicitly.

This is not a big deal for me personally since I figured out that removing the this keyword solves the issue, it's excessive and redundant anyway, but it's going to be a real headscratcher for anyone else who runs into this problem.
Actually in some cases you have to explicitly use this to get the correct behavior so it's quite important that this works.

@persn
Copy link
Author

persn commented Jan 16, 2024

The same thing happens when invoking static methods with generic return type

internal static partial class Class6
{
    [Zomp.SyncMethodGenerator.CreateSyncVersion]
    public static async Task<T> FooAsync<T>(CancellationToken cancellationToken = default)
    {
        return await Class6.InnerFooAsync<T>(cancellationToken).ConfigureAwait(false);
    }

    [Zomp.SyncMethodGenerator.CreateSyncVersion]
    private static async Task<T> InnerFooAsync<T>(CancellationToken cancellationToken = default)
    {
        await Task.Delay(100, cancellationToken);
        return default;
    }
}

@persn
Copy link
Author

persn commented Jan 18, 2024

Another example using Task.Result

public Task<int> FooAsync()
{
    return Task.FromResult<int>(1);
}

@virzak
Copy link
Contributor

virzak commented Jan 21, 2024

@persn Slightly more complex problem than anticipated. Will try fixing in the next week.

@virzak virzak closed this as completed in f27382a Jan 21, 2024
@virzak
Copy link
Contributor

virzak commented Jan 21, 2024

@persn The fix is in version 1.3.5 on nuget.

Will get to the rest of your issues in the next week or next weekend. Please let me know if any of the other issues are a showstopper and I'll look at them first.

@persn
Copy link
Author

persn commented Jan 22, 2024

@virzak I think all the issues I've reported needs to be resolved before I can use this source generator in production code, maybe except the InternalsVisibleTo issue since I need that for test code mostly. However if I get to pick a favorite it would be #45, if that was resolved it would open up more paths in code base that I could use this generator on to test it out more.

@virzak
Copy link
Contributor

virzak commented Jan 22, 2024

@persn Will complete at earliest convenience. Just having some urgent client deliverables this week. Cheers and thanks for understanding.

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

No branches or pull requests

2 participants