Skip to content
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

Fix MVVM Toolkit build errors when using C# < 9.0 #4285

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> interface type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ObservableValidatorValidateAllPropertiesGenerator>(
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)),
"MVVMTK0013");
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)));
}

[TestCategory("Mvvm")]
Expand Down Expand Up @@ -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<IMessengerRegisterAllGenerator>(
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)),
"MVVMTK0013");
CSharpSyntaxTree.ParseText(source, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp7_3)));
}

/// <summary>
Expand Down