diff --git a/Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservableValidatorValidateAllPropertiesGenerator.cs b/Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservableValidatorValidateAllPropertiesGenerator.cs index 330cc919531..dd68fa72bba 100644 --- a/Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservableValidatorValidateAllPropertiesGenerator.cs +++ b/Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservableValidatorValidateAllPropertiesGenerator.cs @@ -42,10 +42,11 @@ public void Execute(GeneratorExecutionContext context) return; } - // Validate the language version - if (context.ParseOptions is not CSharpParseOptions { LanguageVersion: >= LanguageVersion.CSharp9 }) + // Validate the language version (this needs at least C# 8.0 due to static local functions being used). + // If a lower C# version is set, just skip the execution silently. The fallback path will be used just fine. + if (context.ParseOptions is not CSharpParseOptions { LanguageVersion: >= LanguageVersion.CSharp8 }) { - context.ReportDiagnostic(Diagnostic.Create(UnsupportedCSharpLanguageVersionError, null)); + return; } // Get the symbol for the required attributes diff --git a/Microsoft.Toolkit.Mvvm.SourceGenerators/Messaging/IMessengerRegisterAllGenerator.cs b/Microsoft.Toolkit.Mvvm.SourceGenerators/Messaging/IMessengerRegisterAllGenerator.cs index 9faeecf3ac5..10eb7f1a865 100644 --- a/Microsoft.Toolkit.Mvvm.SourceGenerators/Messaging/IMessengerRegisterAllGenerator.cs +++ b/Microsoft.Toolkit.Mvvm.SourceGenerators/Messaging/IMessengerRegisterAllGenerator.cs @@ -39,10 +39,10 @@ public void Execute(GeneratorExecutionContext context) return; } - // Validate the language version - if (context.ParseOptions is not CSharpParseOptions { LanguageVersion: >= LanguageVersion.CSharp9 }) + // Like in the ObservableValidator.ValidateALlProperties generator, execution is skipped if C# >= 8.0 isn't available + if (context.ParseOptions is not CSharpParseOptions { LanguageVersion: >= LanguageVersion.CSharp8 }) { - context.ReportDiagnostic(Diagnostic.Create(UnsupportedCSharpLanguageVersionError, null)); + return; } // Get the symbol for the IRecipient interface type diff --git a/UnitTests/UnitTests.SourceGenerators/Test_SourceGeneratorsDiagnostics.cs b/UnitTests/UnitTests.SourceGenerators/Test_SourceGeneratorsDiagnostics.cs index 7cb6bad19e0..42359ba7774 100644 --- a/UnitTests/UnitTests.SourceGenerators/Test_SourceGeneratorsDiagnostics.cs +++ b/UnitTests/UnitTests.SourceGenerators/Test_SourceGeneratorsDiagnostics.cs @@ -319,9 +319,9 @@ public partial class SampleViewModel : ObservableValidator } }"; + // This is explicitly allowed in C# < 9.0, as it doesn't use any new features VerifyGeneratedDiagnostics( - CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)), - "MVVMTK0013"); + CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3))); } [TestCategory("Mvvm")] @@ -368,9 +368,9 @@ public void Receive(MyMessage message) } }"; + // This is explicitly allowed in C# < 9.0, as it doesn't use any new features VerifyGeneratedDiagnostics( - CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)), - "MVVMTK0013"); + CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3))); } ///