310 references to NullableFlowState
Microsoft.CodeAnalysis.CSharp (268)
Binder\Semantics\BestTypeInferrer.cs (3)
36public static NullableFlowState GetNullableState(ArrayBuilder<TypeWithState> types) 38NullableFlowState result = NullableFlowState.NotNull;
FlowAnalysis\NullableWalker.cs (193)
145public VisitResult(TypeSymbol? type, NullableAnnotation annotation, NullableFlowState state) 235private static readonly TypeWithState _invalidType = TypeWithState.Create(new UnsupportedMetadataTypeSymbol(), NullableFlowState.NotNull); 819var memberState = GetState(ref state, slot); 820var badState = symbolType.Type.IsPossiblyNullableReferenceTypeTypeParameter() && (annotations & FlowAnalysisAnnotations.NotNull) == 0 821? NullableFlowState.MaybeDefault 822: NullableFlowState.MaybeNull; 878SetState(ref this.State, memberSlot, type.Type.IsPossiblyNullableReferenceTypeTypeParameter() ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull); 1173var parameterState = GetState(ref state, memberSlot); 1205SetState(ref this.State, memberSlot, NullableFlowState.MaybeNull); 1318var parameterState = GetState(ref state, slot); 1374var parameterState = GetState(ref stateWhen, slot); 1401private void EnforceNotNullIfNotNull(SyntaxNode? syntaxOpt, LocalState state, ImmutableArray<ParameterSymbol> parameters, ImmutableHashSet<string> inputParamNames, NullableFlowState outputState, ParameterSymbol? outputParam) 1989private NullableFlowState GetState(ref LocalState state, int slot) 1992return NullableFlowState.NotNull; 1998private void SetState(ref LocalState state, int slot, NullableFlowState value, bool useNotNullsAsDefault = false) 2020private NullableFlowState GetDefaultState(ref LocalState state, int slot) 2025return NullableFlowState.NotNull; 2055return NullableFlowState.NotNull; 2384private static bool ShouldReportNullableAssignment(TypeWithAnnotations type, NullableFlowState state) 2401case NullableFlowState.NotNull: 2403case NullableFlowState.MaybeNull: 2479valueType.State == NullableFlowState.MaybeDefault; 2661var newState = valueType.State; 2764NullableFlowState value = isDefaultValue ? NullableFlowState.MaybeNull : fieldOrPropertyType.ToTypeWithState().State; 2776NullableFlowState.NotNull; 2824/// and the new state is not <see cref="NullableFlowState.NotNull"/>, this method should be called to perform the 2828private void SetStateAndTrackForFinally(ref LocalState state, int slot, NullableFlowState newState) 2832if (newState != NullableFlowState.NotNull && NonMonotonicState.HasValue) 2917private NullableFlowState GetDefaultState(Symbol symbol) 3010var state = GetParameterState(parameterType, parameter.FlowAnalysisAnnotations).State; 3048return TypeWithState.Create(parameterType.Type, NullableFlowState.MaybeDefault); 3053return TypeWithState.Create(parameterType.Type, NullableFlowState.NotNull); 3356SetState(ref state, slot, NullableFlowState.NotNull); 3853SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 3861SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 4015NullableFlowState resultState = getResultState(node, collectionKind); 4021static NullableFlowState getResultState(BoundCollectionExpression node, CollectionExpressionTypeKind collectionKind) 4033return NullableFlowState.NotNull; 4101(int slot, NullableFlowState resultState, Func<TypeSymbol, MethodSymbol?, int>? initialStateInferenceCompletion) = 4121NullableFlowState resultState, 4150NullableFlowState resultState) 4152Debug.Assert(resultState == NullableFlowState.NotNull); 4184(int slot, NullableFlowState resultState, Func<TypeSymbol, MethodSymbol?, int>? completion) inferInitialObjectState( 4192return (-1, NullableFlowState.NotNull, inferInitialObjectStateAsContinuation(node, arguments, argumentResults, hasObjectInitializer)); 4198var resultState = NullableFlowState.NotNull; 4230resultState = NullableFlowState.MaybeNull; 4265Debug.Assert(resultState == NullableFlowState.NotNull); 4399var result = new VisitResult(objectInitializer.Type, NullableAnnotation.NotAnnotated, NullableFlowState.NotNull); 4626SetAnalyzedNullability(node.ImplicitReceiverOpt, new VisitResult(node.ImplicitReceiverOpt.Type, NullableAnnotation.NotAnnotated, NullableFlowState.NotNull)); 4684SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 4689var resultType = TypeWithState.Create(node.Type, NullableFlowState.NotNull); 4797SetResultType(node, TypeWithState.Create(anonymousType, NullableFlowState.NotNull)); 4829SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 4834SetResultType(node, TypeWithState.Create(arrayType, NullableFlowState.NotNull)); 4910var elementState = BestTypeInferrer.GetNullableState(expressionTypes); 5112NullableFlowState resultState = NullableFlowState.NotNull; 5140resultState = NullableFlowState.MaybeNull; // Delegate removal can produce null. 5143resultState = NullableFlowState.NotNull; 5732SetState(ref stateToUpdate, slot, NullableFlowState.NotNull); 5761SetState(ref state, slot, NullableFlowState.NotNull); 5786if (GetState(ref state, slot) == NullableFlowState.NotNull) 5789SetState(ref state, slot, NullableFlowState.MaybeNull); 5818SetState(ref state, childSlot, NullableFlowState.NotNull); 5897SetState(ref this.State, leftSlot, NullableFlowState.NotNull); 5952BoundNullCoalescingOperatorResultKind.NoCommonType => (node.Type, NullableFlowState.NotNull), 5957BoundNullCoalescingOperatorResultKind.RightDynamicType => (rightResultType!, NullableFlowState.NotNull), 5964(TypeSymbol ResultType, NullableFlowState LeftState) getLeftResultType(TypeSymbol leftType, TypeSymbol rightType) 5974return (rightType, NullableFlowState.NotNull); 5979return (leftType, NullableFlowState.NotNull); 5982(TypeSymbol ResultType, NullableFlowState LeftState) getResultStateWithRightType(TypeSymbol leftType, TypeSymbol rightType) 5994TypeWithState.Create(leftType, NullableFlowState.NotNull), 6005return (rightType, NullableFlowState.NotNull); 6178SetResultType(node, TypeWithState.Create(resultType, NullableFlowState.MaybeDefault)); 6233var rValueState = consequenceRValue.State.Join(alternativeRValue.State); 6341NullableFlowState resultState; 6501SetResultType(node, TypeWithState.Create(rvalueType, NullableFlowState.NotNull)); 7012private NullableFlowState LearnFromCompareExchangeMethod(in CompareExchangeInfo compareExchangeInfo) 7032return NullableFlowState.NotNull; 7042var valueFlowState = compareExchangeInfo.Results[valueIndex].RValueType.State; 7049var locationFlowState = compareExchangeInfo.Results[locationIndex].RValueType.State; 7166return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 7171return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 7756SetState(ref state, memberSlot, NullableFlowState.NotNull); 8061var adjustedState = LearnFromCompareExchangeMethod(in compareExchangeInfoOpt); 8185return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8191return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8206return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8210return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8225return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8229return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8787NullableFlowState state = GetState(ref this.State, slot); 9062SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 9069SetState(ref this.State, slot, NullableFlowState.NotNull); 9087SetResultType(node, TypeWithState.Create(tupleOpt, NullableFlowState.NotNull)); 9209SetState(ref this.State, targetFieldSlot, NullableFlowState.NotNull); 9226SetState(ref this.State, targetFieldSlot, NullableFlowState.NotNull); 9462NullableFlowState resultState = NullableFlowState.NotNull; 9494resultState = NullableFlowState.NotNull; 9518return TypeWithState.Create(targetType, NullableFlowState.NotNull); 9524resultState = NullableFlowState.NotNull; 9528resultState = NullableFlowState.NotNull; 9533resultState = NullableFlowState.NotNull; 9573resultState = NullableFlowState.NotNull; 9697resultState = NullableFlowState.NotNull; 9767static TypeWithState calculateResultType(TypeWithAnnotations targetTypeWithNullability, bool fromExplicitCast, NullableFlowState resultState, bool isSuppressed, TypeSymbol targetType) 9771resultState = NullableFlowState.NotNull; 9776resultState = targetType?.IsTypeParameterDisallowingAnnotationInCSharp8() == true ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 9783static NullableFlowState getReferenceConversionResultState(TypeWithAnnotations targetType, TypeWithState operandType) 9785var state = operandType.State; 9788case NullableFlowState.MaybeNull: 9794return NullableFlowState.MaybeDefault; 9800return (annotation == NullableAnnotation.Annotated) ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 9804case NullableFlowState.MaybeDefault: 9807return NullableFlowState.MaybeNull; 9817static NullableFlowState getBoxingConversionResultState(TypeWithAnnotations targetType, TypeWithState operandType) 9819var state = operandType.State; 9820if (state == NullableFlowState.MaybeNull) 9825return NullableFlowState.MaybeDefault; 9835return (annotation == NullableAnnotation.Annotated) ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 9845static NullableFlowState getUnboxingConversionResultState(TypeWithState operandType) 9847var state = operandType.State; 9848if (state == NullableFlowState.MaybeNull) 9850return NullableFlowState.MaybeDefault; 9855static NullableFlowState getConversionResultState(TypeWithState operandType) 9857var state = operandType.State; 9858if (state == NullableFlowState.MaybeNull) 9860return NullableFlowState.MaybeDefault; 9996var resultType = TypeWithState.Create(targetType, NullableFlowState.NotNull); 10039NullableFlowState operandState = operandType.State; 10159private TypeWithState GetLiftedReturnType(TypeWithAnnotations returnType, NullableFlowState operandState) 10163NullableFlowState state = returnType.ToTypeWithState().State.Join(operandState); 10180private TypeWithState GetLiftedReturnTypeIfNecessary(bool isLifted, TypeWithAnnotations returnType, NullableFlowState operandState) 10276var result = TypeWithState.Create(delegateType, NullableFlowState.NotNull); 10573var rvalueResult = TypeWithState.Create(node.Type, NullableFlowState.NotNull); 10991var valueBeforeNullableWrapping = TypeWithState.Create(underlyingType.Type, NullableFlowState.NotNull); 11120SetResultType(node, TypeWithState.Create(receiverType.Type, NullableFlowState.NotNull)); 11144SetResultType(node, TypeWithState.Create(opType.Type, NullableFlowState.NotNull)); 11297SetResultType(node, TypeWithState.Create(receiverType.Type, NullableFlowState.NotNull)); 11325SetResultType(node, TypeWithState.Create(opType.Type, NullableFlowState.NotNull)); 11627var state = GetState(ref this.State, slot); 11639SetState(ref this.StateWhenTrue, containingSlot, NullableFlowState.NotNull); 11860if (enumeratorReturnType.State != NullableFlowState.NotNull) 11941var state = NullableFlowState.NotNull; 12145TypeWithState.Create(node.Type, node.OperatorKind.IsLifted() ? argumentResult.State : NullableFlowState.NotNull); 12413SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12435SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.MaybeDefault)); 12450SetState(ref this.State, slot, NullableFlowState.NotNull); 12496NullableFlowState resultState = NullableFlowState.NotNull; 12511resultState = NullableFlowState.MaybeDefault; 12549SetResultType(node, TypeWithState.Create(node.Type, node.Type?.CanContainNull() != false && node.ConstantValueOpt?.IsNull == true ? NullableFlowState.MaybeDefault : NullableFlowState.NotNull)); 12657node.IsAddition ? GetState(ref this.State, memberSlot).Meet(ResultType.State) : NullableFlowState.MaybeNull); 12774private bool ReportPossibleNullReceiverIfNeeded(TypeSymbol type, NullableFlowState state, bool checkNullableValueType, SyntaxNode syntax, out bool reportedDiagnostic) 12833SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12848SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12897SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12903SetResultType(node, TypeWithState.Create(type, NullableFlowState.NotNull)); 12971SetState(ref this.State, slot, NullableFlowState.NotNull); 13062Debug.Assert(ResultType is TypeWithState { Type: FunctionPointerTypeSymbol { }, State: NullableFlowState.NotNull }); 13367SetValue(Id, index, useNotNullsAsDefault ? NullableFlowState.NotNull : walker.GetDefaultState(ref this, slot)); 13371public NullableFlowState this[int slot] 13385private NullableFlowState GetValue(int id, int index) 13395private NullableFlowState GetValue(int index) 13399return NullableFlowState.NotNull; 13406var result = (_state[index], _state[index + 1]) switch 13408(false, false) => NullableFlowState.NotNull, // Should not be reachable 13409(true, false) => NullableFlowState.MaybeNull, 13410(false, true) => NullableFlowState.MaybeDefault, 13411(true, true) => NullableFlowState.NotNull 13417private void SetValue(int id, int index, NullableFlowState value) 13430private void SetValue(int index, NullableFlowState value) 13438NullableFlowState.MaybeNull => (true, false), 13439NullableFlowState.MaybeDefault => (false, true), 13440NullableFlowState.NotNull => (true, true), 13494var oldValue = oldReachable ? GetValue(i) : NullableFlowState.NotNull; 13495var newValue = oldValue.Join(other.GetValue(i)); 13520var oldValue = GetValue(i); 13521var newValue = oldValue.Meet(other.GetValue(i)); 13537var mayBeNull = GetValue(i) is NullableFlowState.MaybeNull or NullableFlowState.MaybeDefault; 13568NullableFlowState.MaybeNull => "?", 13569NullableFlowState.MaybeDefault => "??",
FlowAnalysis\NullableWalker_Patterns.cs (5)
481SetState(ref State, outputSlot, NullableFlowState.NotNull); 579SetState(ref this.State, outputSlot, NullableFlowState.NotNull); // Slice value is assumed to be never null 682var tempState = GetState(ref this.State, tempSlot); 958NullableFlowState inferredState; 1008NullableFlowState inferredState = BestTypeInferrer.GetNullableState(resultTypes);
Symbols\ConstraintsHelper.cs (14)
1226static NullableFlowState getTypeArgumentState(in TypeWithAnnotations typeWithAnnotations) 1231return NullableFlowState.NotNull; 1235return type.IsNullableTypeOrTypeParameter() ? NullableFlowState.MaybeNull : NullableFlowState.NotNull; 1240return type.IsTypeParameterDisallowingAnnotationInCSharp8() ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 1242return NullableFlowState.NotNull; 1247return NullableFlowState.NotNull; 1249NullableFlowState? result = null; 1252var constraintState = getTypeArgumentState(constraintType); 1262return result ?? NullableFlowState.MaybeNull; 1265static bool constraintTypeAllows(in TypeWithAnnotations typeWithAnnotations, NullableFlowState state) 1267if (state == NullableFlowState.NotNull) 1294return state == NullableFlowState.MaybeNull;
Symbols\NullableAnnotationExtensions.cs (1)
104var flowState = TypeWithAnnotations.Create(type, annotation).ToTypeWithState().State;
Symbols\NullableFlowState.cs (1)
11/// When in doubt we conservatively use <see cref="NullableFlowState.NotNull"/>
Symbols\NullableFlowStateExtensions.cs (20)
13public static bool MayBeNull(this NullableFlowState state) => state != NullableFlowState.NotNull; 15public static bool IsNotNull(this NullableFlowState state) => state == NullableFlowState.NotNull; 19/// The result is <see cref="NullableFlowState.MaybeNull"/> if either operand is that. 21public static NullableFlowState Join(this NullableFlowState a, NullableFlowState b) => (a > b) ? a : b; 25/// The result is <see cref="NullableFlowState.NotNull"/> if either operand is that. 27public static NullableFlowState Meet(this NullableFlowState a, NullableFlowState b) => (a < b) ? a : b; 29internal static CodeAnalysis.NullableFlowState ToPublicFlowState(this CSharp.NullableFlowState nullableFlowState) => 32CSharp.NullableFlowState.NotNull => CodeAnalysis.NullableFlowState.NotNull, 33CSharp.NullableFlowState.MaybeNull => CodeAnalysis.NullableFlowState.MaybeNull, 34CSharp.NullableFlowState.MaybeDefault => CodeAnalysis.NullableFlowState.MaybeNull, 39public static CSharp.NullableFlowState ToInternalFlowState(this CodeAnalysis.NullableFlowState flowState) => 42CodeAnalysis.NullableFlowState.None => CSharp.NullableFlowState.NotNull, 43CodeAnalysis.NullableFlowState.NotNull => CSharp.NullableFlowState.NotNull, 44CodeAnalysis.NullableFlowState.MaybeNull => CSharp.NullableFlowState.MaybeNull,
Symbols\TypeWithAnnotations.cs (11)
813static NullableFlowState getFlowState(TypeSymbol type, NullableAnnotation annotation) 817return annotation.IsAnnotated() ? NullableFlowState.MaybeDefault : NullableFlowState.NotNull; 821return annotation switch { NullableAnnotation.Annotated => NullableFlowState.MaybeDefault, NullableAnnotation.NotAnnotated => NullableFlowState.MaybeNull, _ => NullableFlowState.NotNull }; 825return annotation switch { NullableAnnotation.Annotated => NullableFlowState.MaybeDefault, _ => NullableFlowState.NotNull }; 829return NullableFlowState.MaybeNull; 831return annotation switch { NullableAnnotation.Annotated => NullableFlowState.MaybeNull, _ => NullableFlowState.NotNull };
Symbols\TypeWithState.cs (20)
17public readonly NullableFlowState State; 20public bool MayBeNull => State == NullableFlowState.MaybeNull; 21public bool IsNotNull => State == NullableFlowState.NotNull; 25return Create(type, NullableFlowState.MaybeDefault); 28public static TypeWithState Create(TypeSymbol? type, NullableFlowState defaultState) 30if (defaultState == NullableFlowState.MaybeDefault && 36var state = defaultState != NullableFlowState.NotNull && type?.CanContainNull() != false ? NullableFlowState.MaybeNull : NullableFlowState.NotNull; 45NullableFlowState state; 50state = NullableFlowState.MaybeDefault; 54state = NullableFlowState.NotNull; 63state = NullableFlowState.NotNull; 69private TypeWithState(TypeSymbol? type, NullableFlowState state) 71Debug.Assert(state == NullableFlowState.NotNull || type?.CanContainNull() != false); 72Debug.Assert(state != NullableFlowState.MaybeDefault || type is null || type.IsTypeParameterDisallowingAnnotationInCSharp8()); 81public TypeWithState WithNotNullState() => new TypeWithState(Type, NullableFlowState.NotNull); 83public TypeWithState WithSuppression(bool suppress) => suppress ? new TypeWithState(Type, NullableFlowState.NotNull) : this; 90return (State == NullableFlowState.MaybeDefault || asAnnotatedType) ? type.SetIsAnnotated(compilation) : type;
Microsoft.CodeAnalysis.CSharp.Semantic.UnitTests (42)
Semantics\NullableReferenceTypesTests.cs (42)
126961private static readonly NullableFlowState[] s_AllNullableFlowStates = (NullableFlowState[])Enum.GetValues(typeof(NullableFlowState)); 126982var inputs = new[] { NullableFlowState.NotNull, NullableFlowState.MaybeNull }; 126984Func<int, int, NullableFlowState> getResult = (i, j) => inputs[i].Join(inputs[j]); 126986var expected = new NullableFlowState[2, 2] 126988{ NullableFlowState.NotNull, NullableFlowState.MaybeNull }, 126989{ NullableFlowState.MaybeNull, NullableFlowState.MaybeNull }, 127014var inputs = new[] { NullableFlowState.NotNull, NullableFlowState.MaybeNull }; 127015Func<int, int, NullableFlowState> getResult = (i, j) => inputs[i].Meet(inputs[j]); 127017var expected = new NullableFlowState[2, 2] 127019{ NullableFlowState.NotNull, NullableFlowState.NotNull }, 127020{ NullableFlowState.NotNull, NullableFlowState.MaybeNull }, 127047private static void AssertEqual(NullableFlowState[,] expected, Func<int, int, NullableFlowState> getResult, int size) 127049AssertEx.Equal<NullableFlowState>(expected, getResult, (na1, na2) => na1 == na2, na => $"NullableFlowState.{na}", "{0,-32:G}", size); 127068foreach (var a in s_AllNullableFlowStates) 127070foreach (var b in s_AllNullableFlowStates) 127098foreach (var a in s_AllNullableFlowStates) 127100foreach (var b in s_AllNullableFlowStates) 127102foreach (var c in s_AllNullableFlowStates) 127104var leftFirst = a.Join(b).Join(c); 127105var rightFirst = a.Join(b.Join(c)); 127132foreach (var a in s_AllNullableFlowStates) 127134foreach (var b in s_AllNullableFlowStates) 127136foreach (var c in s_AllNullableFlowStates) 127138var leftFirst = a.Meet(b).Meet(c); 127139var rightFirst = a.Meet(b.Meet(c)); 127185foreach (var a in s_AllNullableFlowStates) 127187foreach (var b in s_AllNullableFlowStates) 127189var leftFirst = a.Join(b); 127190var rightFirst = b.Join(a); 127213foreach (var a in s_AllNullableFlowStates) 127215foreach (var b in s_AllNullableFlowStates) 127217var leftFirst = a.Meet(b); 127218var rightFirst = b.Meet(a);