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); 3357SetState(ref state, slot, NullableFlowState.NotNull); 3854SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 3862SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 4016NullableFlowState resultState = getResultState(node, collectionKind); 4022static NullableFlowState getResultState(BoundCollectionExpression node, CollectionExpressionTypeKind collectionKind) 4034return NullableFlowState.NotNull; 4102(int slot, NullableFlowState resultState, Func<TypeSymbol, MethodSymbol?, int>? initialStateInferenceCompletion) = 4122NullableFlowState resultState, 4151NullableFlowState resultState) 4153Debug.Assert(resultState == NullableFlowState.NotNull); 4185(int slot, NullableFlowState resultState, Func<TypeSymbol, MethodSymbol?, int>? completion) inferInitialObjectState( 4193return (-1, NullableFlowState.NotNull, inferInitialObjectStateAsContinuation(node, arguments, argumentResults, hasObjectInitializer)); 4199var resultState = NullableFlowState.NotNull; 4231resultState = NullableFlowState.MaybeNull; 4266Debug.Assert(resultState == NullableFlowState.NotNull); 4400var result = new VisitResult(objectInitializer.Type, NullableAnnotation.NotAnnotated, NullableFlowState.NotNull); 4627SetAnalyzedNullability(node.ImplicitReceiverOpt, new VisitResult(node.ImplicitReceiverOpt.Type, NullableAnnotation.NotAnnotated, NullableFlowState.NotNull)); 4685SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 4690var resultType = TypeWithState.Create(node.Type, NullableFlowState.NotNull); 4798SetResultType(node, TypeWithState.Create(anonymousType, NullableFlowState.NotNull)); 4830SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 4835SetResultType(node, TypeWithState.Create(arrayType, NullableFlowState.NotNull)); 4911var elementState = BestTypeInferrer.GetNullableState(expressionTypes); 5113NullableFlowState resultState = NullableFlowState.NotNull; 5141resultState = NullableFlowState.MaybeNull; // Delegate removal can produce null. 5144resultState = NullableFlowState.NotNull; 5733SetState(ref stateToUpdate, slot, NullableFlowState.NotNull); 5762SetState(ref state, slot, NullableFlowState.NotNull); 5787if (GetState(ref state, slot) == NullableFlowState.NotNull) 5790SetState(ref state, slot, NullableFlowState.MaybeNull); 5819SetState(ref state, childSlot, NullableFlowState.NotNull); 5898SetState(ref this.State, leftSlot, NullableFlowState.NotNull); 5953BoundNullCoalescingOperatorResultKind.NoCommonType => (node.Type, NullableFlowState.NotNull), 5958BoundNullCoalescingOperatorResultKind.RightDynamicType => (rightResultType!, NullableFlowState.NotNull), 5965(TypeSymbol ResultType, NullableFlowState LeftState) getLeftResultType(TypeSymbol leftType, TypeSymbol rightType) 5975return (rightType, NullableFlowState.NotNull); 5980return (leftType, NullableFlowState.NotNull); 5983(TypeSymbol ResultType, NullableFlowState LeftState) getResultStateWithRightType(TypeSymbol leftType, TypeSymbol rightType) 5995TypeWithState.Create(leftType, NullableFlowState.NotNull), 6006return (rightType, NullableFlowState.NotNull); 6179SetResultType(node, TypeWithState.Create(resultType, NullableFlowState.MaybeDefault)); 6234var rValueState = consequenceRValue.State.Join(alternativeRValue.State); 6342NullableFlowState resultState; 6502SetResultType(node, TypeWithState.Create(rvalueType, NullableFlowState.NotNull)); 7013private NullableFlowState LearnFromCompareExchangeMethod(in CompareExchangeInfo compareExchangeInfo) 7033return NullableFlowState.NotNull; 7043var valueFlowState = compareExchangeInfo.Results[valueIndex].RValueType.State; 7050var locationFlowState = compareExchangeInfo.Results[locationIndex].RValueType.State; 7167return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 7172return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 7757SetState(ref state, memberSlot, NullableFlowState.NotNull); 8062var adjustedState = LearnFromCompareExchangeMethod(in compareExchangeInfoOpt); 8186return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8192return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8207return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8211return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8226return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8230return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8788NullableFlowState state = GetState(ref this.State, slot); 9063SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 9070SetState(ref this.State, slot, NullableFlowState.NotNull); 9088SetResultType(node, TypeWithState.Create(tupleOpt, NullableFlowState.NotNull)); 9210SetState(ref this.State, targetFieldSlot, NullableFlowState.NotNull); 9227SetState(ref this.State, targetFieldSlot, NullableFlowState.NotNull); 9463NullableFlowState resultState = NullableFlowState.NotNull; 9495resultState = NullableFlowState.NotNull; 9519return TypeWithState.Create(targetType, NullableFlowState.NotNull); 9525resultState = NullableFlowState.NotNull; 9529resultState = NullableFlowState.NotNull; 9534resultState = NullableFlowState.NotNull; 9574resultState = NullableFlowState.NotNull; 9698resultState = NullableFlowState.NotNull; 9768static TypeWithState calculateResultType(TypeWithAnnotations targetTypeWithNullability, bool fromExplicitCast, NullableFlowState resultState, bool isSuppressed, TypeSymbol targetType) 9772resultState = NullableFlowState.NotNull; 9777resultState = targetType?.IsTypeParameterDisallowingAnnotationInCSharp8() == true ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 9784static NullableFlowState getReferenceConversionResultState(TypeWithAnnotations targetType, TypeWithState operandType) 9786var state = operandType.State; 9789case NullableFlowState.MaybeNull: 9795return NullableFlowState.MaybeDefault; 9801return (annotation == NullableAnnotation.Annotated) ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 9805case NullableFlowState.MaybeDefault: 9808return NullableFlowState.MaybeNull; 9818static NullableFlowState getBoxingConversionResultState(TypeWithAnnotations targetType, TypeWithState operandType) 9820var state = operandType.State; 9821if (state == NullableFlowState.MaybeNull) 9826return NullableFlowState.MaybeDefault; 9836return (annotation == NullableAnnotation.Annotated) ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 9846static NullableFlowState getUnboxingConversionResultState(TypeWithState operandType) 9848var state = operandType.State; 9849if (state == NullableFlowState.MaybeNull) 9851return NullableFlowState.MaybeDefault; 9856static NullableFlowState getConversionResultState(TypeWithState operandType) 9858var state = operandType.State; 9859if (state == NullableFlowState.MaybeNull) 9861return NullableFlowState.MaybeDefault; 9997var resultType = TypeWithState.Create(targetType, NullableFlowState.NotNull); 10040NullableFlowState operandState = operandType.State; 10160private TypeWithState GetLiftedReturnType(TypeWithAnnotations returnType, NullableFlowState operandState) 10164NullableFlowState state = returnType.ToTypeWithState().State.Join(operandState); 10181private TypeWithState GetLiftedReturnTypeIfNecessary(bool isLifted, TypeWithAnnotations returnType, NullableFlowState operandState) 10277var result = TypeWithState.Create(delegateType, NullableFlowState.NotNull); 10574var rvalueResult = TypeWithState.Create(node.Type, NullableFlowState.NotNull); 10992var valueBeforeNullableWrapping = TypeWithState.Create(underlyingType.Type, NullableFlowState.NotNull); 11121SetResultType(node, TypeWithState.Create(receiverType.Type, NullableFlowState.NotNull)); 11145SetResultType(node, TypeWithState.Create(opType.Type, NullableFlowState.NotNull)); 11298SetResultType(node, TypeWithState.Create(receiverType.Type, NullableFlowState.NotNull)); 11326SetResultType(node, TypeWithState.Create(opType.Type, NullableFlowState.NotNull)); 11628var state = GetState(ref this.State, slot); 11640SetState(ref this.StateWhenTrue, containingSlot, NullableFlowState.NotNull); 11861if (enumeratorReturnType.State != NullableFlowState.NotNull) 11942var state = NullableFlowState.NotNull; 12146TypeWithState.Create(node.Type, node.OperatorKind.IsLifted() ? argumentResult.State : NullableFlowState.NotNull); 12414SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12436SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.MaybeDefault)); 12451SetState(ref this.State, slot, NullableFlowState.NotNull); 12497NullableFlowState resultState = NullableFlowState.NotNull; 12512resultState = NullableFlowState.MaybeDefault; 12550SetResultType(node, TypeWithState.Create(node.Type, node.Type?.CanContainNull() != false && node.ConstantValueOpt?.IsNull == true ? NullableFlowState.MaybeDefault : NullableFlowState.NotNull)); 12658node.IsAddition ? GetState(ref this.State, memberSlot).Meet(ResultType.State) : NullableFlowState.MaybeNull); 12775private bool ReportPossibleNullReceiverIfNeeded(TypeSymbol type, NullableFlowState state, bool checkNullableValueType, SyntaxNode syntax, out bool reportedDiagnostic) 12834SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12849SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12898SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12904SetResultType(node, TypeWithState.Create(type, NullableFlowState.NotNull)); 12972SetState(ref this.State, slot, NullableFlowState.NotNull); 13061Debug.Assert(ResultType is TypeWithState { Type: FunctionPointerTypeSymbol { }, State: NullableFlowState.NotNull }); 13366SetValue(Id, index, useNotNullsAsDefault ? NullableFlowState.NotNull : walker.GetDefaultState(ref this, slot)); 13370public NullableFlowState this[int slot] 13384private NullableFlowState GetValue(int id, int index) 13394private NullableFlowState GetValue(int index) 13398return NullableFlowState.NotNull; 13405var result = (_state[index], _state[index + 1]) switch 13407(false, false) => NullableFlowState.NotNull, // Should not be reachable 13408(true, false) => NullableFlowState.MaybeNull, 13409(false, true) => NullableFlowState.MaybeDefault, 13410(true, true) => NullableFlowState.NotNull 13416private void SetValue(int id, int index, NullableFlowState value) 13429private void SetValue(int index, NullableFlowState value) 13437NullableFlowState.MaybeNull => (true, false), 13438NullableFlowState.MaybeDefault => (false, true), 13439NullableFlowState.NotNull => (true, true), 13493var oldValue = oldReachable ? GetValue(i) : NullableFlowState.NotNull; 13494var newValue = oldValue.Join(other.GetValue(i)); 13519var oldValue = GetValue(i); 13520var newValue = oldValue.Meet(other.GetValue(i)); 13536var mayBeNull = GetValue(i) is NullableFlowState.MaybeNull or NullableFlowState.MaybeDefault; 13567NullableFlowState.MaybeNull => "?", 13568NullableFlowState.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)
1228static NullableFlowState getTypeArgumentState(in TypeWithAnnotations typeWithAnnotations) 1233return NullableFlowState.NotNull; 1237return type.IsNullableTypeOrTypeParameter() ? NullableFlowState.MaybeNull : NullableFlowState.NotNull; 1242return type.IsTypeParameterDisallowingAnnotationInCSharp8() ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 1244return NullableFlowState.NotNull; 1249return NullableFlowState.NotNull; 1251NullableFlowState? result = null; 1254var constraintState = getTypeArgumentState(constraintType); 1264return result ?? NullableFlowState.MaybeNull; 1267static bool constraintTypeAllows(in TypeWithAnnotations typeWithAnnotations, NullableFlowState state) 1269if (state == NullableFlowState.NotNull) 1296return 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)
126899private static readonly NullableFlowState[] s_AllNullableFlowStates = (NullableFlowState[])Enum.GetValues(typeof(NullableFlowState)); 126920var inputs = new[] { NullableFlowState.NotNull, NullableFlowState.MaybeNull }; 126922Func<int, int, NullableFlowState> getResult = (i, j) => inputs[i].Join(inputs[j]); 126924var expected = new NullableFlowState[2, 2] 126926{ NullableFlowState.NotNull, NullableFlowState.MaybeNull }, 126927{ NullableFlowState.MaybeNull, NullableFlowState.MaybeNull }, 126952var inputs = new[] { NullableFlowState.NotNull, NullableFlowState.MaybeNull }; 126953Func<int, int, NullableFlowState> getResult = (i, j) => inputs[i].Meet(inputs[j]); 126955var expected = new NullableFlowState[2, 2] 126957{ NullableFlowState.NotNull, NullableFlowState.NotNull }, 126958{ NullableFlowState.NotNull, NullableFlowState.MaybeNull }, 126985private static void AssertEqual(NullableFlowState[,] expected, Func<int, int, NullableFlowState> getResult, int size) 126987AssertEx.Equal<NullableFlowState>(expected, getResult, (na1, na2) => na1 == na2, na => $"NullableFlowState.{na}", "{0,-32:G}", size); 127006foreach (var a in s_AllNullableFlowStates) 127008foreach (var b in s_AllNullableFlowStates) 127036foreach (var a in s_AllNullableFlowStates) 127038foreach (var b in s_AllNullableFlowStates) 127040foreach (var c in s_AllNullableFlowStates) 127042var leftFirst = a.Join(b).Join(c); 127043var rightFirst = a.Join(b.Join(c)); 127070foreach (var a in s_AllNullableFlowStates) 127072foreach (var b in s_AllNullableFlowStates) 127074foreach (var c in s_AllNullableFlowStates) 127076var leftFirst = a.Meet(b).Meet(c); 127077var rightFirst = a.Meet(b.Meet(c)); 127123foreach (var a in s_AllNullableFlowStates) 127125foreach (var b in s_AllNullableFlowStates) 127127var leftFirst = a.Join(b); 127128var rightFirst = b.Join(a); 127151foreach (var a in s_AllNullableFlowStates) 127153foreach (var b in s_AllNullableFlowStates) 127155var leftFirst = a.Meet(b); 127156var rightFirst = b.Meet(a);