Skip to content

Commit 69576f9

Browse files
Merge pull request #4285 from Sergio0694/bugfix/csharp8.0-mvvm-toolkit
Fix MVVM Toolkit build errors when using C# < 9.0
2 parents fc5287e + 10cf2dd commit 69576f9

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservableValidatorValidateAllPropertiesGenerator.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ public void Execute(GeneratorExecutionContext context)
4242
return;
4343
}
4444

45-
// Validate the language version
46-
if (context.ParseOptions is not CSharpParseOptions { LanguageVersion: >= LanguageVersion.CSharp9 })
45+
// Validate the language version (this needs at least C# 8.0 due to static local functions being used).
46+
// If a lower C# version is set, just skip the execution silently. The fallback path will be used just fine.
47+
if (context.ParseOptions is not CSharpParseOptions { LanguageVersion: >= LanguageVersion.CSharp8 })
4748
{
48-
context.ReportDiagnostic(Diagnostic.Create(UnsupportedCSharpLanguageVersionError, null));
49+
return;
4950
}
5051

5152
// Get the symbol for the required attributes

Microsoft.Toolkit.Mvvm.SourceGenerators/Messaging/IMessengerRegisterAllGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public void Execute(GeneratorExecutionContext context)
3939
return;
4040
}
4141

42-
// Validate the language version
43-
if (context.ParseOptions is not CSharpParseOptions { LanguageVersion: >= LanguageVersion.CSharp9 })
42+
// Like in the ObservableValidator.ValidateALlProperties generator, execution is skipped if C# >= 8.0 isn't available
43+
if (context.ParseOptions is not CSharpParseOptions { LanguageVersion: >= LanguageVersion.CSharp8 })
4444
{
45-
context.ReportDiagnostic(Diagnostic.Create(UnsupportedCSharpLanguageVersionError, null));
45+
return;
4646
}
4747

4848
// Get the symbol for the IRecipient<T> interface type

UnitTests/UnitTests.SourceGenerators/Test_SourceGeneratorsDiagnostics.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,9 @@ public partial class SampleViewModel : ObservableValidator
319319
}
320320
}";
321321

322+
// This is explicitly allowed in C# < 9.0, as it doesn't use any new features
322323
VerifyGeneratedDiagnostics<ObservableValidatorValidateAllPropertiesGenerator>(
323-
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)),
324-
"MVVMTK0013");
324+
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)));
325325
}
326326

327327
[TestCategory("Mvvm")]
@@ -368,9 +368,9 @@ public void Receive(MyMessage message)
368368
}
369369
}";
370370

371+
// This is explicitly allowed in C# < 9.0, as it doesn't use any new features
371372
VerifyGeneratedDiagnostics<IMessengerRegisterAllGenerator>(
372-
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)),
373-
"MVVMTK0013");
373+
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)));
374374
}
375375

376376
/// <summary>

0 commit comments

Comments
 (0)