-
Notifications
You must be signed in to change notification settings - Fork 4
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
Combining #if SYNC_ONLY with if(...) or using(...) ends up with missing code #45
Comments
@virzak I found that ISG does not like [Zomp.SyncMethodGenerator.CreateSyncVersion]
public async Task<T> FooAsync<T>()
{
#if MY_SPECIAL_SYMBOL
if (true)
#else
if (true)
#endif
{ }
return default;
} gives me public T Foo<T>()
{
return default;
} It would be more correct if ISG converts these non-related symbols as is withot touching it: public T Foo<T>()
{
#if MY_SPECIAL_SYMBOL
if (true)
#else
if (true)
#endif
{ }
return default;
} I suspect this may become a very difficult task, for example this will fail: #define MY_SPECIAL_SYMBOL
...
[Zomp.SyncMethodGenerator.CreateSyncVersion]
public async Task<T> FooAsync<T>()
{
#if MY_SPECIAL_SYMBOL
await Task.Delay(1);
#else
await Task.Delay(1);
#endif
return default;
} ISG should take a look inside of all branches to convert the code, now it looks only to the active branch, leave the other as is (with async style inside the sync method). |
Yes, this is more complex than it seems. I can definitely look into this, however I'm wondering a workaround is feasible? A statement like this: #if SYNC_ONLY
if (true)
#else
if (true)
#endif
await Task.Delay(100).ConfigureAwait(false); would require the generator to construct if statement from disabled trivia. Would this work for now? #if SYNC_ONLY
if (true)
global::System.Threading.Thread.Sleep(100);
#else
if (true)
await Task.Delay(100).ConfigureAwait(false);
#endif |
That probably looks reasonable for a naive one-liner, but in production code there's usually many lines within curly brackets which the code generator handles equally poorly. I should mention that the same problem also happens for usings, so then I would need to copy-paste the same code twice. Right now it looks like to me that for the methods where this is a problem it's better to not use the code generator at all and just keep and maintain a sync and a async manually. |
As workaround: it's also possible to define a constant value in the method like this: #if SYNC_ONLY
const bool isAsync = false;
#else
const bool isAsync = true;
#endif When C# compiles the if-statements: if (isAsync) await Task.Delay(100).ConfigureAwait(false); it sees that |
I don't know how well supported #if SYNC_ONLY #else "endif is supposed to be, but if I try doing the following it works perfectly like expected
However if I do something that has
()
like if or using inside the directives it'll lose all code after#endif
Because the generated code ends up like this
The text was updated successfully, but these errors were encountered: