diff --git a/src/Utilities/Compiler/Lightup/ITypeSymbolExtensions.cs b/src/PublicApiAnalyzers/Core/Analyzers/ITypeSymbolExtensions.cs similarity index 99% rename from src/Utilities/Compiler/Lightup/ITypeSymbolExtensions.cs rename to src/PublicApiAnalyzers/Core/Analyzers/ITypeSymbolExtensions.cs index d86f3795d1..c8b2f49847 100644 --- a/src/Utilities/Compiler/Lightup/ITypeSymbolExtensions.cs +++ b/src/PublicApiAnalyzers/Core/Analyzers/ITypeSymbolExtensions.cs @@ -12,6 +12,5 @@ private static readonly Func s_nullableAnnotati public static NullableAnnotation NullableAnnotation(this ITypeSymbol typeSymbol) => s_nullableAnnotation(typeSymbol); - } } diff --git a/src/Utilities/Compiler/Lightup/NullableAnnotation.cs b/src/PublicApiAnalyzers/Core/Analyzers/NullableAnnotation.cs similarity index 100% rename from src/Utilities/Compiler/Lightup/NullableAnnotation.cs rename to src/PublicApiAnalyzers/Core/Analyzers/NullableAnnotation.cs diff --git a/src/Utilities/Compiler/Lightup/NullableContext.cs b/src/PublicApiAnalyzers/Core/Analyzers/NullableContext.cs similarity index 100% rename from src/Utilities/Compiler/Lightup/NullableContext.cs rename to src/PublicApiAnalyzers/Core/Analyzers/NullableContext.cs diff --git a/src/Utilities/Compiler/Lightup/NullableContextExtensions.cs b/src/PublicApiAnalyzers/Core/Analyzers/NullableContextExtensions.cs similarity index 100% rename from src/Utilities/Compiler/Lightup/NullableContextExtensions.cs rename to src/PublicApiAnalyzers/Core/Analyzers/NullableContextExtensions.cs diff --git a/src/Utilities/Compiler/Analyzer.Utilities.projitems b/src/Utilities/Compiler/Analyzer.Utilities.projitems index 4ece31a81d..7408a82696 100644 --- a/src/Utilities/Compiler/Analyzer.Utilities.projitems +++ b/src/Utilities/Compiler/Analyzer.Utilities.projitems @@ -65,14 +65,9 @@ - - - - - diff --git a/src/Utilities/Compiler/Lightup/INegatedPatternOperationWrapper.cs b/src/Utilities/Compiler/Lightup/INegatedPatternOperationWrapper.cs deleted file mode 100644 index fe0caee4f2..0000000000 --- a/src/Utilities/Compiler/Lightup/INegatedPatternOperationWrapper.cs +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. - -#if HAS_IOPERATION - -namespace Analyzer.Utilities.Lightup -{ - using System; - using System.Diagnostics.CodeAnalysis; - using Microsoft.CodeAnalysis; - using Microsoft.CodeAnalysis.Operations; - - [SuppressMessage("Performance", "CA1815:Override equals and operator equals on value types", Justification = "Not a comparable instance.")] - internal readonly struct INegatedPatternOperationWrapper : IOperationWrapper - { - internal const string WrappedTypeName = "Microsoft.CodeAnalysis.Operations.INegatedPatternOperation"; - private static readonly Type? WrappedType = OperationWrapperHelper.GetWrappedType(typeof(INegatedPatternOperationWrapper)); - - private static readonly Func PatternAccessor = LightupHelpers.CreateOperationPropertyAccessor(WrappedType, nameof(Pattern), fallbackResult: null!); - - private INegatedPatternOperationWrapper(IOperation operation) - { - WrappedOperation = operation; - } - - public IOperation WrappedOperation { get; } - public ITypeSymbol? Type => this.WrappedOperation.Type; - public IPatternOperation Pattern => PatternAccessor(WrappedOperation); - - public static INegatedPatternOperationWrapper FromOperation(IOperation operation) - { - if (operation == null) - { - return default; - } - - if (!IsInstance(operation)) - { - throw new InvalidCastException($"Cannot cast '{operation.GetType().FullName}' to '{WrappedTypeName}'"); - } - - return new INegatedPatternOperationWrapper(operation); - } - - public static bool IsInstance(IOperation operation) - { - return operation != null && LightupHelpers.CanWrapOperation(operation, WrappedType); - } - } -} - -#endif diff --git a/src/Utilities/Compiler/Lightup/OperationWrapperHelper.cs b/src/Utilities/Compiler/Lightup/OperationWrapperHelper.cs index b8bd9269ae..71e47fc6da 100644 --- a/src/Utilities/Compiler/Lightup/OperationWrapperHelper.cs +++ b/src/Utilities/Compiler/Lightup/OperationWrapperHelper.cs @@ -14,8 +14,7 @@ internal static class OperationWrapperHelper private static readonly Assembly s_codeAnalysisAssembly = typeof(SyntaxNode).GetTypeInfo().Assembly; private static readonly ImmutableDictionary WrappedTypes = ImmutableDictionary.Create() - .Add(typeof(IFunctionPointerInvocationOperationWrapper), s_codeAnalysisAssembly.GetType(IFunctionPointerInvocationOperationWrapper.WrappedTypeName)) - .Add(typeof(INegatedPatternOperationWrapper), s_codeAnalysisAssembly.GetType(INegatedPatternOperationWrapper.WrappedTypeName)); + .Add(typeof(IFunctionPointerInvocationOperationWrapper), s_codeAnalysisAssembly.GetType(IFunctionPointerInvocationOperationWrapper.WrappedTypeName)); /// /// Gets the type that is wrapped by the given wrapper. diff --git a/src/Utilities/FlowAnalysis/FlowAnalysis/Analysis/PointsToAnalysis/PointsToAnalysis.PointsToDataFlowOperationVisitor.cs b/src/Utilities/FlowAnalysis/FlowAnalysis/Analysis/PointsToAnalysis/PointsToAnalysis.PointsToDataFlowOperationVisitor.cs index 6c68afe98b..faabde23da 100644 --- a/src/Utilities/FlowAnalysis/FlowAnalysis/Analysis/PointsToAnalysis/PointsToAnalysis.PointsToDataFlowOperationVisitor.cs +++ b/src/Utilities/FlowAnalysis/FlowAnalysis/Analysis/PointsToAnalysis/PointsToAnalysis.PointsToDataFlowOperationVisitor.cs @@ -6,7 +6,6 @@ using System.Diagnostics; using Analyzer.Utilities; using Analyzer.Utilities.Extensions; -using Analyzer.Utilities.Lightup; using Analyzer.Utilities.PooledObjects; using Microsoft.CodeAnalysis.FlowAnalysis.DataFlow.CopyAnalysis; using Microsoft.CodeAnalysis.FlowAnalysis.DataFlow.ValueContentAnalysis; @@ -14,8 +13,6 @@ namespace Microsoft.CodeAnalysis.FlowAnalysis.DataFlow.PointsToAnalysis { - using NullableAnnotation = Analyzer.Utilities.Lightup.NullableAnnotation; - public partial class PointsToAnalysis : ForwardDataFlowAnalysis { /// @@ -979,7 +976,7 @@ private static bool IsSpecialFactoryMethodReturningNonNullValue(IMethodSymbol me { if (!method.IsStatic || !method.Name.StartsWith("Create", StringComparison.Ordinal) || - method.ReturnType.NullableAnnotation() == NullableAnnotation.Annotated) + method.ReturnType.NullableAnnotation == NullableAnnotation.Annotated) { return false; } diff --git a/src/Utilities/FlowAnalysis/FlowAnalysis/Framework/DataFlow/DataFlowOperationVisitor.cs b/src/Utilities/FlowAnalysis/FlowAnalysis/Framework/DataFlow/DataFlowOperationVisitor.cs index a32497d09b..714f205f88 100644 --- a/src/Utilities/FlowAnalysis/FlowAnalysis/Framework/DataFlow/DataFlowOperationVisitor.cs +++ b/src/Utilities/FlowAnalysis/FlowAnalysis/Framework/DataFlow/DataFlowOperationVisitor.cs @@ -8,7 +8,6 @@ using System.Linq; using Analyzer.Utilities; using Analyzer.Utilities.Extensions; -using Analyzer.Utilities.Lightup; using Analyzer.Utilities.PooledObjects; using Microsoft.CodeAnalysis.FlowAnalysis.DataFlow.CopyAnalysis; using Microsoft.CodeAnalysis.FlowAnalysis.DataFlow.PointsToAnalysis; @@ -1267,9 +1266,8 @@ private bool TryInferConversion( IPatternOperation patternOperation = isPatternOperation.Pattern; bool direct = true; - if (INegatedPatternOperationWrapper.IsInstance(patternOperation)) + if (patternOperation is INegatedPatternOperation negatedPattern) { - INegatedPatternOperationWrapper negatedPattern = INegatedPatternOperationWrapper.FromOperation(patternOperation); patternOperation = negatedPattern.Pattern; direct = false; } @@ -1582,48 +1580,33 @@ private void PerformPredicateAnalysisCore(IOperation operation, TAnalysisData ta equals: FlowBranchConditionKind == ControlFlowConditionKind.WhenTrue, isReferenceEquality: false, targetAnalysisData: targetAnalysisData); break; - default: - // TODO: Remove the below string based checks when we move to Microsoft.CodeAnalysis 3.7 or later. - // TODO: File a tracking bug. - var kindStr = isPatternOperation.Pattern.Kind.ToString(); - switch (kindStr) + case OperationKind.NegatedPattern: + var negatedPattern = (INegatedPatternOperation)isPatternOperation.Pattern; + if (negatedPattern.Pattern is IConstantPatternOperation negatedConstantPattern) { - case "NegatedPattern": - if (isPatternOperation.Pattern.Children.FirstOrDefault() is IPatternOperation negatedPattern) - { - if (negatedPattern is IConstantPatternOperation negatedConstantPattern) - { - predicateValueKind = SetValueForEqualsOrNotEqualsComparisonOperator(isPatternOperation.Value, negatedConstantPattern.Value, - equals: FlowBranchConditionKind == ControlFlowConditionKind.WhenFalse, isReferenceEquality: false, targetAnalysisData: targetAnalysisData); - } - - break; - } - else - { - goto default; - } + predicateValueKind = SetValueForEqualsOrNotEqualsComparisonOperator(isPatternOperation.Value, negatedConstantPattern.Value, + equals: FlowBranchConditionKind == ControlFlowConditionKind.WhenFalse, isReferenceEquality: false, targetAnalysisData: targetAnalysisData); + } - case "RelationalPattern": - // For the true branch, set the pattern operation value to NotNull. - if (FlowBranchConditionKind == ControlFlowConditionKind.WhenTrue) - { - predicateValueKind = SetValueForIsNullComparisonOperator(isPatternOperation.Value, equals: false, targetAnalysisData: targetAnalysisData); - } + break; - break; + case OperationKind.RelationalPattern: + // For the true branch, set the pattern operation value to NotNull. + if (FlowBranchConditionKind == ControlFlowConditionKind.WhenTrue) + { + predicateValueKind = SetValueForIsNullComparisonOperator(isPatternOperation.Value, equals: false, targetAnalysisData: targetAnalysisData); + } - case "BinaryPattern": - // These high level patterns should not be present in the lowered CFG: https://github.com/dotnet/roslyn/issues/47068 - predicateValueKind = PredicateValueKind.Unknown; - break; + break; - default: - Debug.Fail($"Unknown pattern kind '{isPatternOperation.Pattern.Kind}'"); - predicateValueKind = PredicateValueKind.Unknown; - break; - } + case OperationKind.BinaryPattern: + // These high level patterns should not be present in the lowered CFG: https://github.com/dotnet/roslyn/issues/47068 + predicateValueKind = PredicateValueKind.Unknown; + break; + default: + Debug.Fail($"Unknown pattern kind '{isPatternOperation.Pattern.Kind}'"); + predicateValueKind = PredicateValueKind.Unknown; break; }