zompinc / sync-method-generator

Generates a synchronized version of an async method
MIT License
48 stars 4 forks source link

Combining ?. with extension method produces invalid code #79

Closed persn closed 2 months ago

persn commented 3 months ago

The following code

    internal partial class Class17
    {
        [Zomp.SyncMethodGenerator.CreateSyncVersion(OmitNullableDirective = true)]
        public async Task FooAsync()
        {
            var bar = new Bar2();
            var delay = bar?.GetInt() ?? 0;
            await Task.Delay(delay);
        }
    }

    internal class Bar2
    {
    }

    internal static class Bar2Extensions
    {
        public static int GetInt(this Bar2 bar2) => 0;
    }

Produces the following

        public void Foo()
        {
            var bar = new global::ClassLibrary1.Bar2();
            var delay = (!bar.HasValue?(int?)null: global::ClassLibrary1.Bar2Extensions.GetInt(bar))?? 0;
            global::System.Threading.Thread.Sleep(delay);
        }

The class Bar2 doesn't have a HasValue method so this produces compile fails

error CS1061: 'Bar2' does not contain a definition for 'HasValue' and no accessible extension method 'HasValue' accepting a first argument of type 'Bar2' could be found (are you missing a using directive or an assembly reference?)
warning CS8603: Possible null reference return.
virzak commented 3 months ago

Reproduced. Will fix this weekend.