Fix for using C# 8.0 projects (e.g. UWP projects stuck on .NET Core 3)#398
Fix for using C# 8.0 projects (e.g. UWP projects stuck on .NET Core 3)#398Sergio0694 merged 5 commits intoCommunityToolkit:mainfrom
Conversation
|
I discovered that the unit tests for the generators actually never check for compilation errors at all (it just checks for diagnostics coming directly from the generators, it doesn't check for errors in the generated code), so there are actually other cases where newer C# features are being generated and breaking tests. I am looking at how best to solve these. Edit: see the description, I think the best route is to make these generators explicitly give the unsupported language version diagnostic and we should add tests that validate successful compilation. I've implemented these in my change. |
…egisterAllGenerator to provide a nicer "unsupported C# language version" diagnostic instead of simply generating unsupported code and failing later in compilation
…t, update tests to use the minimum supported C# language version (8.0), and also validate that unsupported version cases actually succeed with the supported version
| VariableDeclarator(Identifier(propertyName)) | ||
| .WithInitializer(EqualsValueClause( | ||
| ImplicitObjectCreationExpression() | ||
| ObjectCreationExpression(IdentifierName(typeName)) |
There was a problem hiding this comment.
We could guard it using language version but at this point, if we're going to drop .NET Native target as commented in the issue, then we could atleast warn about the minimum language version requirement.
I already did something like this in my private fork to generate language version compatible code. So, there's no need to check/warn about the version requirement. It always produces compilable code for the given target runtime.
| .Select(static (item, _) => Execute.GetInfo(item)) | ||
| .WithComparer(ValidationInfo.Comparer.Default); | ||
|
|
||
| context.FilterWithLanguageVersion(ref validationInfo, LanguageVersion.CSharp8, UnsupportedCSharpLanguageVersionError); |
There was a problem hiding this comment.
The ovservable validator doesn't use C# 8 features, no? This seems unnecessary (it's not free).
There was a problem hiding this comment.
Even so, it's better to check against a min version of the language instead of leaving it up in the air!
There was a problem hiding this comment.
No, it isn't. As I said, this adds too much overhead. Besides, I already plan to move this out of the generator.
I'd really like for this PR to only fix that one thing so it's nice and simple and we can merge it quickly.
EDIT: on second thought, the generator should just skip, but never cause a build error here.
CommunityToolkit.Mvvm.SourceGenerators/Messaging/IMessengerRegisterAllGenerator.cs
Outdated
Show resolved
Hide resolved
| }"; | ||
|
|
||
| // This is explicitly allowed in C# < 8.0, as it doesn't use any new features | ||
| VerifyGeneratedDiagnostics<ObservableValidatorValidateAllPropertiesGenerator>( |
There was a problem hiding this comment.
Undo this change (see previous comment), this generator doesn't require C# 8
| new ObservableValidatorValidateAllPropertiesGenerator(), | ||
| new RelayCommandGenerator() | ||
| }; | ||
| VerifyGeneratedDiagnostics(CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8)), generators, new string[] { }); |
There was a problem hiding this comment.
This needs to be updated so that only the lang version required by each generator is used, not just C# 8 for all.
These two generators should never cause a build error
Sergio0694
left a comment
There was a problem hiding this comment.
Pushed a couple tweaks, LGTM! ![]()
Thank you!
|
Hello, One question. Have you tested with UWP projects? Because we are stuck on .Net Core 2.2 and C# 7.3, not .Net Core 3.0. We can use C# 8.0 in .net 2.2 but without all the features: https://www.reflectionit.nl/blog/2019/using-c-8-0-in-core-2-x-net-framework-and-uwp-projects |
Closes #376
This change fixes support for C# language version 8.0 by swapping a
ImplicitObjectCreationExpression(which uses the C# 9 language featureType myField = new();) to instead use a normalObjectCreationExpressionwith the type explicitly defined (Type myField = new Type();). This enables the source analyzer to work on .NET Core 3.0 projects once again, such as UWP projects.As part of testing this fix, I found that the source generator tests as written today don't actually validate successful compilation of any supported scenario, and that a number of scenarios that the tests claim to work on C# 7.3 don't actually work anymore. Therefore I've also changed the following:
UnsupportedCSharpLanguageVersion_*tests, I've also added validation that the source code does compile with C# 8.0.PR Checklist
Other information