提交 5a0b78c9 编写于 作者: J Jared Parsons

Fixup nullable annotation

Had to clean up a few nullable annotations now that we are compiling
agaist `netcoreapp3.1` and hence get the full value of the framework
annotations.

This is also problematic though because there are now two places where
the compiler can see nullable attributes that are directly used by the
developer. For example `NotNullWhenAttribute`. This is both defined in
our assemblies for non-netcoreapp target frameworks and provided by the
SDK when targeting `netcoreapp3.1`.

This causes a problem for assemblies which have the following
characteristics:

1. Target `netcoreapp3.1`
1. Reference an assembly targeting `netstandard2.0` which uses our
nullable attributes definition
1. Has IVT into (2) above

These properties essentially define all of our unit test assemblies. In
that environment it's not possible to use nullable attributes in code
because the compiler can't disambiguate which definition of
`NotNullWhenAttribute` to use. This meant I had to temporarily remove a
few attributes until we can complete #40766.
上级 e1155098
...@@ -41,7 +41,7 @@ static CompilerServerLogger() ...@@ -41,7 +41,7 @@ static CompilerServerLogger()
try try
{ {
// Check if the environment // Check if the environment
string loggingFileName = Environment.GetEnvironmentVariable(environmentVariable); string? loggingFileName = Environment.GetEnvironmentVariable(environmentVariable);
if (loggingFileName != null) if (loggingFileName != null)
{ {
...@@ -75,15 +75,15 @@ public static void Initialize(string outputPrefix) ...@@ -75,15 +75,15 @@ public static void Initialize(string outputPrefix)
/// <summary> /// <summary>
/// Log an exception. Also logs information about inner exceptions. /// Log an exception. Also logs information about inner exceptions.
/// </summary> /// </summary>
public static void LogException(Exception e, string reason) public static void LogException(Exception exception, string reason)
{ {
if (s_loggingStream != null) if (s_loggingStream != null)
{ {
Log("Exception '{0}' occurred during '{1}'. Stack trace:\r\n{2}", e.Message, reason, e.StackTrace); Log("Exception '{0}' occurred during '{1}'. Stack trace:\r\n{2}", exception.Message, reason, exception.StackTrace);
int innerExceptionLevel = 0; int innerExceptionLevel = 0;
e = e.InnerException; Exception? e = exception.InnerException;
while (e != null) while (e != null)
{ {
Log("Inner exception[{0}] '{1}'. Stack trace: \r\n{1}", innerExceptionLevel, e.Message, e.StackTrace); Log("Inner exception[{0}] '{1}'. Stack trace: \r\n{1}", innerExceptionLevel, e.Message, e.StackTrace);
......
...@@ -343,7 +343,12 @@ public bool SkipCompilerExecution ...@@ -343,7 +343,12 @@ public bool SkipCompilerExecution
public string? TargetType public string? TargetType
{ {
set { _store[nameof(TargetType)] = CultureInfo.InvariantCulture.TextInfo.ToLower(value); } set
{
_store[nameof(TargetType)] = value != null
? CultureInfo.InvariantCulture.TextInfo.ToLower(value)
: null;
}
get { return (string?)_store[nameof(TargetType)]; } get { return (string?)_store[nameof(TargetType)]; }
} }
...@@ -467,11 +472,11 @@ protected override int ExecuteTool(string pathToTool, string responseFileCommand ...@@ -467,11 +472,11 @@ protected override int ExecuteTool(string pathToTool, string responseFileCommand
try try
{ {
var workingDir = CurrentDirectoryToUse(); var workingDir = CurrentDirectoryToUse();
string tempDir = BuildServerConnection.GetTempPath(workingDir); string? tempDir = BuildServerConnection.GetTempPath(workingDir);
if (!UseSharedCompilation || if (!UseSharedCompilation ||
HasToolBeenOverridden || HasToolBeenOverridden ||
!BuildServerConnection.IsCompilerServerSupported(tempDir)) !BuildServerConnection.IsCompilerServerSupported)
{ {
return base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands); return base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
} }
...@@ -483,6 +488,10 @@ protected override int ExecuteTool(string pathToTool, string responseFileCommand ...@@ -483,6 +488,10 @@ protected override int ExecuteTool(string pathToTool, string responseFileCommand
CompilerServerLogger.Log($"BuildResponseFile = '{responseFileCommands}'"); CompilerServerLogger.Log($"BuildResponseFile = '{responseFileCommands}'");
var clientDir = Path.GetDirectoryName(PathToManagedTool); var clientDir = Path.GetDirectoryName(PathToManagedTool);
if (clientDir is null || tempDir is null)
{
return base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
}
// Note: we can't change the "tool path" printed to the console when we run // Note: we can't change the "tool path" printed to the console when we run
// the Csc/Vbc task since MSBuild logs it for us before we get here. Instead, // the Csc/Vbc task since MSBuild logs it for us before we get here. Instead,
...@@ -567,10 +576,10 @@ private string CurrentDirectoryToUse() ...@@ -567,10 +576,10 @@ private string CurrentDirectoryToUse()
/// <summary> /// <summary>
/// Get the "LIB" environment variable, or NULL if none. /// Get the "LIB" environment variable, or NULL if none.
/// </summary> /// </summary>
private string LibDirectoryToUse() private string? LibDirectoryToUse()
{ {
// First check the real environment. // First check the real environment.
string libDirectory = Environment.GetEnvironmentVariable("LIB"); string? libDirectory = Environment.GetEnvironmentVariable("LIB");
// Now go through additional environment variables. // Now go through additional environment variables.
string[] additionalVariables = EnvironmentVariables; string[] additionalVariables = EnvironmentVariables;
......
...@@ -89,7 +89,7 @@ private static bool EndsWithDirectorySeparator(string path) ...@@ -89,7 +89,7 @@ private static bool EndsWithDirectorySeparator(string path)
private void MergeSourceRootMetadata(ITaskItem left, ITaskItem right) private void MergeSourceRootMetadata(ITaskItem left, ITaskItem right)
{ {
foreach (string metadataName in right.MetadataNames) foreach (string? metadataName in right.MetadataNames)
{ {
var leftValue = left.GetMetadata(metadataName); var leftValue = left.GetMetadata(metadataName);
var rightValue = right.GetMetadata(metadataName); var rightValue = right.GetMetadata(metadataName);
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<AssemblyVersion/> <AssemblyVersion/>
<!-- CA1819 (Properties should not return arrays) disabled as it is very common across this project. --> <!-- CA1819 (Properties should not return arrays) disabled as it is very common across this project. -->
<NoWarn>$(NoWarn);CA1819</NoWarn> <NoWarn>$(NoWarn);CA1819</NoWarn>
<!-- NuGet --> <!-- NuGet -->
<IsPackable>true</IsPackable> <IsPackable>true</IsPackable>
<PackageId>Microsoft.CodeAnalysis.Build.Tasks</PackageId> <PackageId>Microsoft.CodeAnalysis.Build.Tasks</PackageId>
...@@ -23,6 +23,12 @@ ...@@ -23,6 +23,12 @@
</PackageDescription> </PackageDescription>
<GenerateMicrosoftCodeAnalysisCommitHashAttribute>true</GenerateMicrosoftCodeAnalysisCommitHashAttribute> <GenerateMicrosoftCodeAnalysisCommitHashAttribute>true</GenerateMicrosoftCodeAnalysisCommitHashAttribute>
</PropertyGroup> </PropertyGroup>
<!-- Disable the nullable warnings when not compiling for netcoreapp3.1 -->
<PropertyGroup Condition="'$(TargetFramework)' != 'netcoreapp3.1'">
<NoWarn>$(NoWarn);8600;8601;8602;8603;8604</NoWarn>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<Content Include="*.targets"> <Content Include="*.targets">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
......
...@@ -99,7 +99,7 @@ private static Guid FindMvidInSections(ushort count, BinaryReader reader) ...@@ -99,7 +99,7 @@ private static Guid FindMvidInSections(ushort count, BinaryReader reader)
return s_empty; return s_empty;
} }
if (name.Length == 8 && name[0] == '.' && if (name!.Length == 8 && name[0] == '.' &&
name[1] == 'm' && name[2] == 'v' && name[3] == 'i' && name[4] == 'd' && name[5] == '\0') name[1] == 'm' && name[2] == 'v' && name[3] == 'i' && name[4] == 'd' && name[5] == '\0')
{ {
// Section: VirtualSize (4) // Section: VirtualSize (4)
...@@ -150,7 +150,7 @@ private static Guid ReadMvidSection(BinaryReader reader, uint pointerToMvidSecti ...@@ -150,7 +150,7 @@ private static Guid ReadMvidSection(BinaryReader reader, uint pointerToMvidSecti
return s_empty; return s_empty;
} }
return new Guid(guidBytes); return new Guid(guidBytes!);
} }
private static bool ReadUInt16(BinaryReader reader, out ushort output) private static bool ReadUInt16(BinaryReader reader, out ushort output)
...@@ -177,7 +177,7 @@ private static bool ReadUInt32(BinaryReader reader, out uint output) ...@@ -177,7 +177,7 @@ private static bool ReadUInt32(BinaryReader reader, out uint output)
return true; return true;
} }
private static bool ReadBytes(BinaryReader reader, int count, [NotNullWhen(true)] out byte[]? output) private static bool ReadBytes(BinaryReader reader, int count, out byte[]? output)
{ {
if (reader.BaseStream.Position + count >= reader.BaseStream.Length) if (reader.BaseStream.Position + count >= reader.BaseStream.Length)
{ {
......
...@@ -178,8 +178,8 @@ internal static string GenerateFullPathToTool(string toolName) ...@@ -178,8 +178,8 @@ internal static string GenerateFullPathToTool(string toolName)
var assemblyDirectory = Path.GetDirectoryName(assemblyPath); var assemblyDirectory = Path.GetDirectoryName(assemblyPath);
return RuntimeHostInfo.IsDesktopRuntime return RuntimeHostInfo.IsDesktopRuntime
? Path.Combine(assemblyDirectory, toolName) ? Path.Combine(assemblyDirectory!, toolName)
: Path.Combine(assemblyDirectory, "bincore", toolName); : Path.Combine(assemblyDirectory!, "bincore", toolName);
} }
} }
} }
...@@ -36,7 +36,7 @@ public sealed partial class ValidateBootstrap : Task ...@@ -36,7 +36,7 @@ public sealed partial class ValidateBootstrap : Task
public string? TasksAssemblyFullPath public string? TasksAssemblyFullPath
{ {
get { return _tasksAssemblyFullPath; } get { return _tasksAssemblyFullPath; }
set { _tasksAssemblyFullPath = NormalizePath(Path.GetFullPath(value)); } set { _tasksAssemblyFullPath = NormalizePath(Path.GetFullPath(value!)); }
} }
public ValidateBootstrap() public ValidateBootstrap()
...@@ -103,7 +103,7 @@ public override bool Execute() ...@@ -103,7 +103,7 @@ public override bool Execute()
return path; return path;
} }
private string GetDirectory(Assembly assembly) => Path.GetDirectoryName(Utilities.TryGetAssemblyPath(assembly)); private string? GetDirectory(Assembly assembly) => Path.GetDirectoryName(Utilities.TryGetAssemblyPath(assembly));
internal static void AddFailedLoad(AssemblyName name) internal static void AddFailedLoad(AssemblyName name)
{ {
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
// This was copied from https://github.com/dotnet/coreclr/blob/60f1e6265bd1039f023a82e0643b524d6aaf7845/src/System.Private.CoreLib/shared/System/Diagnostics/CodeAnalysis/NullableAttributes.cs // This was copied from https://github.com/dotnet/coreclr/blob/60f1e6265bd1039f023a82e0643b524d6aaf7845/src/System.Private.CoreLib/shared/System/Diagnostics/CodeAnalysis/NullableAttributes.cs
// and updated to have the scope of the attributes be internal. // and updated to have the scope of the attributes be internal.
#if NETSTANDARD2_0 || NET472 || NET20 || NETSTANDARD1_3 || NET45 #if !NETCOREAPP
#nullable enable #nullable enable
namespace System.Diagnostics.CodeAnalysis namespace System.Diagnostics.CodeAnalysis
...@@ -88,7 +88,4 @@ internal sealed class DoesNotReturnIfAttribute : Attribute ...@@ -88,7 +88,4 @@ internal sealed class DoesNotReturnIfAttribute : Attribute
} }
} }
#elif NETCOREAPP3_1
#else
#error "Unsupported configuration"
#endif #endif
\ No newline at end of file
...@@ -31,7 +31,7 @@ namespace Microsoft.CodeAnalysis.CommandLine ...@@ -31,7 +31,7 @@ namespace Microsoft.CodeAnalysis.CommandLine
/// communication layer which is shared between MSBuild and non-MSBuild components. This is the problem that /// communication layer which is shared between MSBuild and non-MSBuild components. This is the problem that
/// BuildPathsAlt fixes as the type lives with the build server communication code. /// BuildPathsAlt fixes as the type lives with the build server communication code.
/// </summary> /// </summary>
internal readonly struct BuildPathsAlt internal sealed class BuildPathsAlt
{ {
/// <summary> /// <summary>
/// The path which contains the compiler binaries and response files. /// The path which contains the compiler binaries and response files.
...@@ -77,7 +77,7 @@ internal sealed class BuildServerConnection ...@@ -77,7 +77,7 @@ internal sealed class BuildServerConnection
/// <summary> /// <summary>
/// Determines if the compiler server is supported in this environment. /// Determines if the compiler server is supported in this environment.
/// </summary> /// </summary>
internal static bool IsCompilerServerSupported(string tempPath) => GetPipeNameForPathOpt("") is object; internal static bool IsCompilerServerSupported => GetPipeNameForPathOpt("") is object;
public static Task<BuildResponse> RunServerCompilation( public static Task<BuildResponse> RunServerCompilation(
RequestLanguage language, RequestLanguage language,
...@@ -85,7 +85,7 @@ internal sealed class BuildServerConnection ...@@ -85,7 +85,7 @@ internal sealed class BuildServerConnection
List<string> arguments, List<string> arguments,
BuildPathsAlt buildPaths, BuildPathsAlt buildPaths,
string? keepAlive, string? keepAlive,
string libEnvVariable, string? libEnvVariable,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
var pipeNameOpt = sharedCompilationId ?? GetPipeNameForPathOpt(buildPaths.ClientDirectory); var pipeNameOpt = sharedCompilationId ?? GetPipeNameForPathOpt(buildPaths.ClientDirectory);
...@@ -108,7 +108,7 @@ internal sealed class BuildServerConnection ...@@ -108,7 +108,7 @@ internal sealed class BuildServerConnection
BuildPathsAlt buildPaths, BuildPathsAlt buildPaths,
string? pipeName, string? pipeName,
string? keepAlive, string? keepAlive,
string libEnvVariable, string? libEnvVariable,
int? timeoutOverride, int? timeoutOverride,
CreateServerFunc createServerFunc, CreateServerFunc createServerFunc,
CancellationToken cancellationToken) CancellationToken cancellationToken)
...@@ -588,7 +588,7 @@ internal static string GetClientMutexName(string pipeName) ...@@ -588,7 +588,7 @@ internal static string GetClientMutexName(string pipeName)
/// is <paramref name="workingDir"/>. This function must emulate <see cref="Path.GetTempPath"/> as /// is <paramref name="workingDir"/>. This function must emulate <see cref="Path.GetTempPath"/> as
/// closely as possible. /// closely as possible.
/// </summary> /// </summary>
public static string GetTempPath(string? workingDir) public static string? GetTempPath(string? workingDir)
{ {
if (PlatformInformation.IsUnix) if (PlatformInformation.IsUnix)
{ {
...@@ -656,7 +656,7 @@ internal sealed class FileMutex : IDisposable ...@@ -656,7 +656,7 @@ internal sealed class FileMutex : IDisposable
internal static string GetMutexDirectory() internal static string GetMutexDirectory()
{ {
var tempPath = BuildServerConnection.GetTempPath(null); var tempPath = BuildServerConnection.GetTempPath(null);
var result = Path.Combine(tempPath, ".roslyn"); var result = Path.Combine(tempPath!, ".roslyn");
Directory.CreateDirectory(result); Directory.CreateDirectory(result);
return result; return result;
} }
......
...@@ -33,7 +33,7 @@ internal static (string processFilePath, string commandLineArguments, string too ...@@ -33,7 +33,7 @@ internal static (string processFilePath, string commandLineArguments, string too
if (IsDotNetHost(out string? pathToDotNet)) if (IsDotNetHost(out string? pathToDotNet))
{ {
commandLineArguments = $@"exec ""{toolFilePath}"" {commandLineArguments}"; commandLineArguments = $@"exec ""{toolFilePath}"" {commandLineArguments}";
return (pathToDotNet, commandLineArguments, toolFilePath); return (pathToDotNet!, commandLineArguments, toolFilePath);
} }
else else
{ {
...@@ -58,7 +58,7 @@ internal static bool IsDotNetHost([NotNullWhen(true)] out string? pathToDotNet) ...@@ -58,7 +58,7 @@ internal static bool IsDotNetHost([NotNullWhen(true)] out string? pathToDotNet)
private static string DotNetHostPathEnvironmentName = "DOTNET_HOST_PATH"; private static string DotNetHostPathEnvironmentName = "DOTNET_HOST_PATH";
private static bool IsDotNetHost([NotNullWhen(true)] out string? pathToDotNet) private static bool IsDotNetHost(out string? pathToDotNet)
{ {
pathToDotNet = GetDotNetPathOrDefault(); pathToDotNet = GetDotNetPathOrDefault();
return true; return true;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册