Skip to content

Commit

Permalink
Make MaxSupportedLangVersion calculation dynamic
Browse files Browse the repository at this point in the history
Uses historical trend to calculate language version based on the
.NET version, removing hardcoded mappings. Ensures
future .NET versions automatically align with expected C# version.
  • Loading branch information
kasperk81 committed Nov 7, 2024
1 parent 103fe0c commit 3d9bb3e
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions src/Compilers/Core/MSBuildTask/Microsoft.CSharp.Core.targets
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,20 @@
<!-- .NETCoreApp < 3.0, .NETStandard < 2.1, or any other target framework -->
<_MaxSupportedLangVersion Condition="('$(TargetFrameworkIdentifier)' != '.NETCoreApp' OR '$(_TargetFrameworkVersionWithoutV)' &lt; '3.0') AND
('$(TargetFrameworkIdentifier)' != '.NETStandard' OR '$(_TargetFrameworkVersionWithoutV)' &lt; '2.1')">7.3</_MaxSupportedLangVersion>

<!-- .NETCoreApp < 5.0, .NETStandard == 2.1 -->
<_MaxSupportedLangVersion Condition="(('$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND '$(_TargetFrameworkVersionWithoutV)' &lt; '5.0') OR
('$(TargetFrameworkIdentifier)' == '.NETStandard' AND '$(_TargetFrameworkVersionWithoutV)' == '2.1')) AND
'$(_MaxSupportedLangVersion)' == ''">8.0</_MaxSupportedLangVersion>

<!-- .NETCoreApp == 5.0 -->
<_MaxSupportedLangVersion Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND '$(_TargetFrameworkVersionWithoutV)' == '5.0' AND
'$(_MaxSupportedLangVersion)' == ''">9.0</_MaxSupportedLangVersion>

<!-- .NETCoreApp == 6.0 -->
<_MaxSupportedLangVersion Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND '$(_TargetFrameworkVersionWithoutV)' == '6.0' AND
'$(_MaxSupportedLangVersion)' == ''">10.0</_MaxSupportedLangVersion>

<!-- .NETCoreApp == 7.0 -->
<_MaxSupportedLangVersion Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND '$(_TargetFrameworkVersionWithoutV)' == '7.0' AND
'$(_MaxSupportedLangVersion)' == ''">11.0</_MaxSupportedLangVersion>

<!-- .NETCoreApp == 8.0 -->
<_MaxSupportedLangVersion Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND '$(_TargetFrameworkVersionWithoutV)' == '8.0' AND
'$(_MaxSupportedLangVersion)' == ''">12.0</_MaxSupportedLangVersion>

<!-- .NETCoreApp == 9.0 -->
<_MaxSupportedLangVersion Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND '$(_TargetFrameworkVersionWithoutV)' == '9.0' AND
'$(_MaxSupportedLangVersion)' == ''">13.0</_MaxSupportedLangVersion>
<!--
Automatically calculate the maximum supported C# language version based on the .NET Target Framework.
- Pattern: .NET 5.0 uses C# 9.0, .NET 6.0 uses C# 10.0, and so on.
- Starting from C# 9.0 for .NET 5.0, we add the difference between the major .NET version and 5
to determine the correct language version.
-->
<_MaxSupportedLangVersion Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND
'$(_MaxSupportedLangVersion)' == ''">$([MSBuild]::Add(9, $([MSBuild]::Subtract($(_TargetFrameworkVersionWithoutV), 5)))).0</_MaxSupportedLangVersion>

<MaxSupportedLangVersion>$(_MaxSupportedLangVersion)</MaxSupportedLangVersion>
<LangVersion Condition="'$(LangVersion)' == '' AND '$(_MaxSupportedLangVersion)' != ''">$(_MaxSupportedLangVersion)</LangVersion>
Expand Down

0 comments on commit 3d9bb3e

Please sign in to comment.