Skip to content

Commit

Permalink
respect LangVersion for preview features (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Sep 12, 2024
1 parent c4a0cf6 commit f8faee0
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/Consume/Consume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ void UsePreviewFeature()
{
}

#if LangVersion13

[OverloadResolutionPriority(1)]
void Method(int x)
{
Expand All @@ -126,7 +128,10 @@ void Method(object x)
{
}

#endif

#if FeatureMemory

void CollectionBuilderAttribute()
{
MyCollection myCollection = [1, 2, 3, 4, 5];
Expand All @@ -140,9 +145,11 @@ class MyCollection(ReadOnlySpan<int> initValues)

public static MyCollection Create(ReadOnlySpan<int> values) => new(values);
}

#endif

#if FeatureValueTuple

void Ranges()
{
var range = "value"[1..];
Expand All @@ -154,6 +161,7 @@ void Ranges()
// "value2"
// }[..1];
}

#endif

#endregion
Expand Down Expand Up @@ -326,6 +334,7 @@ void Random_Methods()
}

#if FeatureMemory

void ReadOnlySpan_Methods()
{
var readOnlySpan = "value".AsSpan();
Expand All @@ -340,18 +349,22 @@ void ReadOnlySpan_Methods()
result = readOnlySpan.StartsWith("value", StringComparison.Ordinal);
var split = readOnlySpan.Split('a');
split = readOnlySpan.Split("a".AsSpan());
#if LangVersion13
split = readOnlySpan.SplitAny('a');
split = readOnlySpan.SplitAny("a".AsSpan());
#endif
}

#endif

#if FeatureMemory

void Regex_Methods()
{
var regex = new Regex("result");
regex.IsMatch("value".AsSpan());
}

#endif

void SByte_Methods()
Expand Down
18 changes: 18 additions & 0 deletions src/ConsumeCs12/ConsumeCs12.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<NoWarn>$(NoWarn);PolyfillTargetsForNuget</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>12.0</LangVersion>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT'">net461;net462;net47;net471;net472;net48;net481;net6.0-windows</TargetFrameworks>
<TargetFrameworks>$(TargetFrameworks);netstandard2.0;netstandard2.1;netcoreapp2.0;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Memory" Condition="$(TargetFramework) != '.NETStandard' or $(TargetFrameworkIdentifier) == '.NETFramework' or $(TargetFramework.StartsWith('netcoreapp'))" />
<PackageReference Include="System.ValueTuple" Condition="$(TargetFramework.StartsWith('net46'))" />
<PackageReference Include="System.Net.Http" Condition="$(TargetFramework.StartsWith('net4'))" />
<PackageReference Include="System.Threading.Tasks.Extensions" Condition="$(TargetFramework) == 'netstandard2.0' or $(TargetFramework) == 'netcoreapp2.0' or $(TargetFrameworkIdentifier) == '.NETFramework'" />
<Compile Include="..\Consume\*.cs" />
</ItemGroup>
<Import Project="$(SolutionDir)\Polyfill\Polyfill.targets" />
<Import Project="$(SolutionDir)\TestIncludes.targets" />
</Project>
18 changes: 18 additions & 0 deletions src/ConsumeCs13/ConsumeCs13.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<NoWarn>$(NoWarn);PolyfillTargetsForNuget</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>13.0</LangVersion>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT'">net461;net462;net47;net471;net472;net48;net481;net6.0-windows</TargetFrameworks>
<TargetFrameworks>$(TargetFrameworks);netstandard2.0;netstandard2.1;netcoreapp2.0;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Memory" Condition="$(TargetFramework) != '.NETStandard' or $(TargetFrameworkIdentifier) == '.NETFramework' or $(TargetFramework.StartsWith('netcoreapp'))" />
<PackageReference Include="System.ValueTuple" Condition="$(TargetFramework.StartsWith('net46'))" />
<PackageReference Include="System.Net.Http" Condition="$(TargetFramework.StartsWith('net4'))" />
<PackageReference Include="System.Threading.Tasks.Extensions" Condition="$(TargetFramework) == 'netstandard2.0' or $(TargetFramework) == 'netcoreapp2.0' or $(TargetFrameworkIdentifier) == '.NETFramework'" />
<Compile Include="..\Consume\*.cs" />
</ItemGroup>
<Import Project="$(SolutionDir)\Polyfill\Polyfill.targets" />
<Import Project="$(SolutionDir)\TestIncludes.targets" />
</Project>
18 changes: 18 additions & 0 deletions src/ConsumeCsDefault/ConsumeCsDefault.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<NoWarn>$(NoWarn);PolyfillTargetsForNuget</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>default</LangVersion>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT'">net461;net462;net47;net471;net472;net48;net481;net6.0-windows</TargetFrameworks>
<TargetFrameworks>$(TargetFrameworks);netstandard2.0;netstandard2.1;netcoreapp2.0;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Memory" Condition="$(TargetFramework) != '.NETStandard' or $(TargetFrameworkIdentifier) == '.NETFramework' or $(TargetFramework.StartsWith('netcoreapp'))" />
<PackageReference Include="System.ValueTuple" Condition="$(TargetFramework.StartsWith('net46'))" />
<PackageReference Include="System.Net.Http" Condition="$(TargetFramework.StartsWith('net4'))" />
<PackageReference Include="System.Threading.Tasks.Extensions" Condition="$(TargetFramework) == 'netstandard2.0' or $(TargetFramework) == 'netcoreapp2.0' or $(TargetFrameworkIdentifier) == '.NETFramework'" />
<Compile Include="..\Consume\*.cs" />
</ItemGroup>
<Import Project="$(SolutionDir)\Polyfill\Polyfill.targets" />
<Import Project="$(SolutionDir)\TestIncludes.targets" />
</Project>
18 changes: 18 additions & 0 deletions src/ConsumeCsLatest/ConsumeCsLatest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<NoWarn>$(NoWarn);PolyfillTargetsForNuget</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT'">net461;net462;net47;net471;net472;net48;net481;net6.0-windows</TargetFrameworks>
<TargetFrameworks>$(TargetFrameworks);netstandard2.0;netstandard2.1;netcoreapp2.0;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Memory" Condition="$(TargetFramework) != '.NETStandard' or $(TargetFrameworkIdentifier) == '.NETFramework' or $(TargetFramework.StartsWith('netcoreapp'))" />
<PackageReference Include="System.ValueTuple" Condition="$(TargetFramework.StartsWith('net46'))" />
<PackageReference Include="System.Net.Http" Condition="$(TargetFramework.StartsWith('net4'))" />
<PackageReference Include="System.Threading.Tasks.Extensions" Condition="$(TargetFramework) == 'netstandard2.0' or $(TargetFramework) == 'netcoreapp2.0' or $(TargetFrameworkIdentifier) == '.NETFramework'" />
<Compile Include="..\Consume\*.cs" />
</ItemGroup>
<Import Project="$(SolutionDir)\Polyfill\Polyfill.targets" />
<Import Project="$(SolutionDir)\TestIncludes.targets" />
</Project>
18 changes: 18 additions & 0 deletions src/ConsumeCsLatestMajor/ConsumeCsLatestMajor.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<NoWarn>$(NoWarn);PolyfillTargetsForNuget</NoWarn>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latestMajor</LangVersion>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT'">net461;net462;net47;net471;net472;net48;net481;net6.0-windows</TargetFrameworks>
<TargetFrameworks>$(TargetFrameworks);netstandard2.0;netstandard2.1;netcoreapp2.0;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Memory" Condition="$(TargetFramework) != '.NETStandard' or $(TargetFrameworkIdentifier) == '.NETFramework' or $(TargetFramework.StartsWith('netcoreapp'))" />
<PackageReference Include="System.ValueTuple" Condition="$(TargetFramework.StartsWith('net46'))" />
<PackageReference Include="System.Net.Http" Condition="$(TargetFramework.StartsWith('net4'))" />
<PackageReference Include="System.Threading.Tasks.Extensions" Condition="$(TargetFramework) == 'netstandard2.0' or $(TargetFramework) == 'netcoreapp2.0' or $(TargetFrameworkIdentifier) == '.NETFramework'" />
<Compile Include="..\Consume\*.cs" />
</ItemGroup>
<Import Project="$(SolutionDir)\Polyfill\Polyfill.targets" />
<Import Project="$(SolutionDir)\TestIncludes.targets" />
</Project>
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>6.6.1</Version>
<Version>6.6.2</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Polyfill</PackageTags>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
Expand Down
30 changes: 30 additions & 0 deletions src/Polyfill.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TargetFrameworkUsage", "Tar
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsumeMemoryWithNoTask", "ConsumeMemoryWithNoTask\ConsumeMemoryWithNoTask.csproj", "{1BDDA7F0-0D45-418C-B3DB-BBA39C29FD77}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsumeCs12", "ConsumeCs12\ConsumeCs12.csproj", "{4ACF4760-B3C5-4D07-9D92-2B8225BA8881}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsumeCs13", "ConsumeCs13\ConsumeCs13.csproj", "{EE99F818-0D49-434F-B787-348CA34051EF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsumeCsLatest", "ConsumeCsLatest\ConsumeCsLatest.csproj", "{684570FB-7049-44F0-9DCC-32F73F242694}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsumeCsLatestMajor", "ConsumeCsLatestMajor\ConsumeCsLatestMajor.csproj", "{9BB64345-DE4F-4396-9320-AAA712D7E3CE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsumeCsDefault", "ConsumeCsDefault\ConsumeCsDefault.csproj", "{86AA1F8D-4081-4D31-812E-866C0951415D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -96,6 +106,26 @@ Global
{1BDDA7F0-0D45-418C-B3DB-BBA39C29FD77}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1BDDA7F0-0D45-418C-B3DB-BBA39C29FD77}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1BDDA7F0-0D45-418C-B3DB-BBA39C29FD77}.Release|Any CPU.Build.0 = Release|Any CPU
{4ACF4760-B3C5-4D07-9D92-2B8225BA8881}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4ACF4760-B3C5-4D07-9D92-2B8225BA8881}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4ACF4760-B3C5-4D07-9D92-2B8225BA8881}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4ACF4760-B3C5-4D07-9D92-2B8225BA8881}.Release|Any CPU.Build.0 = Release|Any CPU
{EE99F818-0D49-434F-B787-348CA34051EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EE99F818-0D49-434F-B787-348CA34051EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EE99F818-0D49-434F-B787-348CA34051EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EE99F818-0D49-434F-B787-348CA34051EF}.Release|Any CPU.Build.0 = Release|Any CPU
{684570FB-7049-44F0-9DCC-32F73F242694}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{684570FB-7049-44F0-9DCC-32F73F242694}.Debug|Any CPU.Build.0 = Debug|Any CPU
{684570FB-7049-44F0-9DCC-32F73F242694}.Release|Any CPU.ActiveCfg = Release|Any CPU
{684570FB-7049-44F0-9DCC-32F73F242694}.Release|Any CPU.Build.0 = Release|Any CPU
{9BB64345-DE4F-4396-9320-AAA712D7E3CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9BB64345-DE4F-4396-9320-AAA712D7E3CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9BB64345-DE4F-4396-9320-AAA712D7E3CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9BB64345-DE4F-4396-9320-AAA712D7E3CE}.Release|Any CPU.Build.0 = Release|Any CPU
{86AA1F8D-4081-4D31-812E-866C0951415D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{86AA1F8D-4081-4D31-812E-866C0951415D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{86AA1F8D-4081-4D31-812E-866C0951415D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{86AA1F8D-4081-4D31-812E-866C0951415D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
7 changes: 7 additions & 0 deletions src/Polyfill/Polyfill.targets
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
<LowerFramework>$(TargetFramework.ToLower())</LowerFramework>
<LowerFrameworks>$(TargetFrameworks.ToLower())</LowerFrameworks>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::VersionGreaterThanOrEquals($(SdkAnalysisLevel), '9'))">
<DefineConstants Condition="$(LangVersion) == '13'">$(DefineConstants);LangVersion13</DefineConstants>
<DefineConstants Condition="$(LangVersion) == 'latest'">$(DefineConstants);LangVersion13</DefineConstants>
<DefineConstants Condition="$(LangVersion) == 'preview'">$(DefineConstants);LangVersion13</DefineConstants>
<DefineConstants Condition="$(LangVersion) == 'default'">$(DefineConstants);LangVersion13</DefineConstants>
<DefineConstants Condition="$(LangVersion) == 'latestMajor'">$(DefineConstants);LangVersion13</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(AllowUnsafeBlocks) == 'true' ">
<DefineConstants>$(DefineConstants);AllowUnsafeBlocks</DefineConstants>
</PropertyGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/Polyfill/Polyfill_Memory_SpanSplit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public static SpanSplitEnumerator<T> Split<T>(this ReadOnlySpan<T> source, ReadO
where T : IEquatable<T> =>
new SpanSplitEnumerator<T>(source, separator, treatAsSingleSeparator: true);

#if LangVersion13

/// <summary>
/// Returns a type that allows for enumeration of each element within a split span
/// using any of the provided elements.
Expand All @@ -63,6 +65,8 @@ public static SpanSplitEnumerator<T> SplitAny<T>(this ReadOnlySpan<T> source, [U
where T : IEquatable<T> =>
new SpanSplitEnumerator<T>(source, separators);

#endif

#if NET8_0

/// <summary>
Expand Down

0 comments on commit f8faee0

Please sign in to comment.