zompinc / sync-method-generator

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

Generator crashes when a method contains multiple preprocessor directives with curly brackets #83

Closed persn closed 2 months ago

persn commented 2 months ago

Consider the following code

        [Zomp.SyncMethodGenerator.CreateSyncVersion(OmitNullableDirective = true)]
        public async Task Foo1Async()
        {
            await Task.Delay(100);
#if DEBUG
            {
#endif

#if DEBUG
            }
#endif
        }

        [Zomp.SyncMethodGenerator.CreateSyncVersion(OmitNullableDirective = true)]
        public async Task Foo2Async()
        {
            await Task.Delay(100);
#if DEBUG
            {
            }
#endif
        }

        [Zomp.SyncMethodGenerator.CreateSyncVersion(OmitNullableDirective = true)]
        public async Task Foo3Async()
        {
            await Task.Delay(100);
#if DEBUG
            {
            }
#endif

#if DEBUG
            {
            }
#endif
        }

Foo2Async and Foo3Async generates and compiles fine, however Foo1Async crashes with the message 1>CSC : warning CS8785: Generator 'SyncMethodSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'InvalidOperationException' with message 'Stack empty.'.

persn commented 2 months ago

This also fails with the same error message, might be related, or might be a different issue

        [Zomp.SyncMethodGenerator.CreateSyncVersion(OmitNullableDirective = true)]
        public async Task FooAsync()
        {
            await Task.Delay(100);

            var bar =
#if DEBUG
                true;
#else
                false;
#endif
        }
virzak commented 2 months ago

Looking at this on the weekend. Thanks so much for all the issues! Makes the project better.

virzak commented 2 months ago

Fixed in version 1.4.13

virzak commented 2 months ago

Actually, the second test hasn't been fixed

virzak commented 2 months ago

Second test is a different issue now tracked in #85