Skip to content

Commit

Permalink
Fix some compiler errors that can occur with specific project setups
Browse files Browse the repository at this point in the history
  • Loading branch information
sbeca committed Dec 3, 2023
1 parent 8148f98 commit 977424e
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 3 deletions.
2 changes: 1 addition & 1 deletion contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Example:
#pragma warning disable

#if TASKSEXTENSIONSREFERENCED && (NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0)
#if TASKSEXTENSIONSREFERENCED && MEMORYREFERENCED && (NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0)

using System;
using System.Buffers;
Expand Down
4 changes: 4 additions & 0 deletions src/Consume/Consume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ class Consume
var split = "a b".Split(' ', StringSplitOptions.RemoveEmptyEntries);
split = "a b".Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
var contains = "a b".Contains(' ');

// Test to make sure there are no clashes in the Polyfill code with classes that
// might be defined in user code. See comments in Debug.cs for more details.
Debug.Log("Test log to make sure this is working");
}

#if HTTPREFERENCED
Expand Down
29 changes: 29 additions & 0 deletions src/Consume/Debug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

// Test to make sure there are no clashes in the Polyfill code with classes that might be defined in user code.
//
// Some codebases, for better or for worse, define classes with names that match common classes in the base
// .NET libraries, like "Debug". This works find in those codebases because they are usually contained in the
// same namespace, or are guarded in some other way. But if a Polyfill attribute is imported and uses code like:
// Debug.Assert(genericParameter.IsGenericParameter);
// instead of a fully qualified name like:
// System.Debug.Assert(genericParameter.IsGenericParameter);
// then users of Polyfill will get errors like the following, which they can't easily fix:
// 'Debug' does not contain a definition for 'Assert'
//
// So, this file defines a custom Debug class to make sure that Polyfill code doesn't clash with user code.

class Debug
{
public static void Log(string content) => Console.WriteLine(content);

public static void Log(ConsoleColor color, string content)
{
Console.ForegroundColor = color;
Console.WriteLine(content);
Console.ResetColor();
}

public static void LogWarning(string content) => Log(ConsoleColor.Yellow, content);
public static void LogError(string content) => Log(ConsoleColor.Red, content);
}
31 changes: 31 additions & 0 deletions src/ConsumeTasksWithNoMemory/ConsumeTasksWithNoMemory.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<!-- This project is specifically designed to guard against a miscompile where System.Threading.Tasks.Extensions is referenced, but System.Memory is not. -->
<!-- <PackageReference Include="System.Memory" Version="4.5.5" Condition="$(TargetFramework) != '.NETStandard' or $(TargetFrameworkIdentifier) == '.NETFramework' or $(TargetFramework.StartsWith('netcoreapp'))" /> -->
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" Condition="$(TargetFramework) == 'netstandard2.0' or $(TargetFramework) == 'netcoreapp2.0' or $(TargetFrameworkIdentifier) == '.NETFramework'" />
<Compile Include="..\Polyfill\*.cs">
<Link>Pollyfill\%(RecursiveDir)%(Filename).cs</Link>
</Compile>
<Compile Include="..\Polyfill\Nullable\*.cs">
<Link>Pollyfill\Nullable\%(RecursiveDir)%(Filename).cs</Link>
</Compile>
<Compile Include="..\Polyfill\Nullability\*.cs">
<Link>Pollyfill\Nullability\%(RecursiveDir)%(Filename).cs</Link>
</Compile>
<Compile Include="..\Polyfill\IndexRange\*.cs">
<Link>Pollyfill\IndexRange\%(RecursiveDir)%(Filename).cs</Link>
</Compile>
<Compile Include="..\Polyfill\Trimming\*.cs">
<Link>Pollyfill\Trimming\%(RecursiveDir)%(Filename).cs</Link>
</Compile>
<Compile Include="..\Polyfill\PlatformCompatibility\*.cs">
<Link>Pollyfill\PlatformCompatibility\%(RecursiveDir)%(Filename).cs</Link>
</Compile>
</ItemGroup>
<Import Project="$(ProjectDir)..\Polyfill\Polyfill.props" />
<Import Project="$(ProjectDir)..\Polyfill\Polyfill.targets" />
</Project>
6 changes: 6 additions & 0 deletions src/Polyfill.sln
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NoRefsTests", "NoRefsTests\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsumeNoRefs", "ConsumeNoRefs\ConsumeNoRefs.csproj", "{B4DC96CA-C700-499F-A9A2-0C767DCF8C30}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsumeTasksWithNoMemory", "ConsumeTasksWithNoMemory\ConsumeTasksWithNoMemory.csproj", "{96EF1E04-5862-4D9E-B800-A6402F1ADF7A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -75,6 +77,10 @@ Global
{B4DC96CA-C700-499F-A9A2-0C767DCF8C30}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B4DC96CA-C700-499F-A9A2-0C767DCF8C30}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B4DC96CA-C700-499F-A9A2-0C767DCF8C30}.Release|Any CPU.Build.0 = Release|Any CPU
{96EF1E04-5862-4D9E-B800-A6402F1ADF7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{96EF1E04-5862-4D9E-B800-A6402F1ADF7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96EF1E04-5862-4D9E-B800-A6402F1ADF7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96EF1E04-5862-4D9E-B800-A6402F1ADF7A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions src/Polyfill/Nullability/NullabilityInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System.Linq;
using System.Diagnostics.CodeAnalysis;

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

Expand Down
5 changes: 5 additions & 0 deletions src/Polyfill/Nullability/NullabilityInfoContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System.Linq;
using System.Diagnostics.CodeAnalysis;

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

Expand All @@ -15,6 +16,10 @@

namespace System.Reflection
{
// Some codebases define their own Debug class, which can cause clashes and compile errors if we aren't explicit here.
// See comments in Debug.cs in Consume project for more details.
using Debug = System.Diagnostics.Debug;

/// <summary>
/// Provides APIs for populating nullability information/context from reflection members:
/// <see cref="ParameterInfo"/>, <see cref="FieldInfo"/>, <see cref="PropertyInfo"/> and <see cref="EventInfo"/>.
Expand Down
4 changes: 4 additions & 0 deletions src/Polyfill/PolyfillExtensions_Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

static partial class PolyfillExtensions
{
#if MEMORYREFERENCED

/// <summary>
/// Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by
/// the number of bytes read, and monitors cancellation requests.
Expand Down Expand Up @@ -64,6 +66,8 @@ public static ValueTask WriteAsync(
return new(target.WriteAsync(segment.Array!, segment.Offset, segment.Count, cancellationToken));
}

#endif

/// <summary>
/// Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified
/// cancellation token. Both streams positions are advanced by the number of bytes copied.
Expand Down
2 changes: 1 addition & 1 deletion src/Polyfill/PolyfillExtensions_TextReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

static partial class PolyfillExtensions
{
#if TASKSEXTENSIONSREFERENCED && (NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0)
#if TASKSEXTENSIONSREFERENCED && MEMORYREFERENCED && (NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0)
/// <summary>
/// Asynchronously reads the characters from the current stream into a memory block.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Polyfill/PolyfillExtensions_TextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#pragma warning disable

#if TASKSEXTENSIONSREFERENCED && (NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0)
#if TASKSEXTENSIONSREFERENCED && MEMORYREFERENCED && (NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2_0)

using System;
using System.Buffers;
Expand Down
15 changes: 15 additions & 0 deletions src/Tests/NullabilitySync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public async Task Run()
using System.Linq;
using System.Diagnostics.CodeAnalysis;
""";

var suffix = """
Expand All @@ -60,6 +61,7 @@ public async Task Run()

infoContext = prefix + infoContext + suffix;
infoContext = MakeInternal(infoContext);
infoContext = AddDebugClassFix(infoContext);
OverWrite(infoContext, "NullabilityInfoContext.cs");

info = prefix + info + suffix;
Expand Down Expand Up @@ -87,6 +89,19 @@ static string MakeInternal(string source) =>
sealed class
""");

static string AddDebugClassFix(string source) =>
source
.Replace(
"namespace System.Reflection\r\n{",
"""
namespace System.Reflection
{
// Some codebases define their own Debug class, which can cause clashes and compile errors if we aren't explicit here.
// See comments in Debug.cs in Consume project for more details.
using Debug = System.Diagnostics.Debug;
""");

static void OverWrite(string content, string file)
{
var path = Path.Combine(dir, file);
Expand Down

0 comments on commit 977424e

Please sign in to comment.