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); 821var memberState = GetState(ref state, slot); 822var badState = symbolType.Type.IsPossiblyNullableReferenceTypeTypeParameter() && (annotations & FlowAnalysisAnnotations.NotNull) == 0 823? NullableFlowState.MaybeDefault 824: NullableFlowState.MaybeNull; 880SetState(ref this.State, memberSlot, type.Type.IsPossiblyNullableReferenceTypeTypeParameter() ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull); 1175var parameterState = GetState(ref state, memberSlot); 1207SetState(ref this.State, memberSlot, NullableFlowState.MaybeNull); 1320var parameterState = GetState(ref state, slot); 1376var parameterState = GetState(ref stateWhen, slot); 1403private void EnforceNotNullIfNotNull(SyntaxNode? syntaxOpt, LocalState state, ImmutableArray<ParameterSymbol> parameters, ImmutableHashSet<string> inputParamNames, NullableFlowState outputState, ParameterSymbol? outputParam) 1991private NullableFlowState GetState(ref LocalState state, int slot) 1994return NullableFlowState.NotNull; 2000private void SetState(ref LocalState state, int slot, NullableFlowState value, bool useNotNullsAsDefault = false) 2022private NullableFlowState GetDefaultState(ref LocalState state, int slot) 2027return NullableFlowState.NotNull; 2057return NullableFlowState.NotNull; 2386private static bool ShouldReportNullableAssignment(TypeWithAnnotations type, NullableFlowState state) 2403case NullableFlowState.NotNull: 2405case NullableFlowState.MaybeNull: 2481valueType.State == NullableFlowState.MaybeDefault; 2663var newState = valueType.State; 2766NullableFlowState value = isDefaultValue ? NullableFlowState.MaybeNull : fieldOrPropertyType.ToTypeWithState().State; 2778NullableFlowState.NotNull; 2826/// and the new state is not <see cref="NullableFlowState.NotNull"/>, this method should be called to perform the 2830private void SetStateAndTrackForFinally(ref LocalState state, int slot, NullableFlowState newState) 2834if (newState != NullableFlowState.NotNull && NonMonotonicState.HasValue) 2919private NullableFlowState GetDefaultState(Symbol symbol) 3012var state = GetParameterState(parameterType, parameter.FlowAnalysisAnnotations).State; 3050return TypeWithState.Create(parameterType.Type, NullableFlowState.MaybeDefault); 3055return TypeWithState.Create(parameterType.Type, NullableFlowState.NotNull); 3359SetState(ref state, slot, NullableFlowState.NotNull); 3856SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 3864SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 4018NullableFlowState resultState = getResultState(node, collectionKind); 4024static NullableFlowState getResultState(BoundCollectionExpression node, CollectionExpressionTypeKind collectionKind) 4036return NullableFlowState.NotNull; 4105(int slot, NullableFlowState resultState, Func<TypeSymbol, MethodSymbol?, int>? initialStateInferenceCompletion) = 4125NullableFlowState resultState, 4154NullableFlowState resultState) 4156Debug.Assert(resultState == NullableFlowState.NotNull); 4188(int slot, NullableFlowState resultState, Func<TypeSymbol, MethodSymbol?, int>? completion) inferInitialObjectState( 4196return (-1, NullableFlowState.NotNull, inferInitialObjectStateAsContinuation(node, arguments, argumentResults, hasObjectInitializer)); 4202var resultState = NullableFlowState.NotNull; 4234resultState = NullableFlowState.MaybeNull; 4269Debug.Assert(resultState == NullableFlowState.NotNull); 4403var result = new VisitResult(objectInitializer.Type, NullableAnnotation.NotAnnotated, NullableFlowState.NotNull); 4630SetAnalyzedNullability(node.ImplicitReceiverOpt, new VisitResult(node.ImplicitReceiverOpt.Type, NullableAnnotation.NotAnnotated, NullableFlowState.NotNull)); 4688SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 4693var resultType = TypeWithState.Create(node.Type, NullableFlowState.NotNull); 4801SetResultType(node, TypeWithState.Create(anonymousType, NullableFlowState.NotNull)); 4833SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 4838SetResultType(node, TypeWithState.Create(arrayType, NullableFlowState.NotNull)); 4914var elementState = BestTypeInferrer.GetNullableState(expressionTypes); 5116NullableFlowState resultState = NullableFlowState.NotNull; 5144resultState = NullableFlowState.MaybeNull; // Delegate removal can produce null. 5147resultState = NullableFlowState.NotNull; 5736SetState(ref stateToUpdate, slot, NullableFlowState.NotNull); 5765SetState(ref state, slot, NullableFlowState.NotNull); 5790if (GetState(ref state, slot) == NullableFlowState.NotNull) 5793SetState(ref state, slot, NullableFlowState.MaybeNull); 5822SetState(ref state, childSlot, NullableFlowState.NotNull); 5901SetState(ref this.State, leftSlot, NullableFlowState.NotNull); 5956BoundNullCoalescingOperatorResultKind.NoCommonType => (node.Type, NullableFlowState.NotNull), 5961BoundNullCoalescingOperatorResultKind.RightDynamicType => (rightResultType!, NullableFlowState.NotNull), 5968(TypeSymbol ResultType, NullableFlowState LeftState) getLeftResultType(TypeSymbol leftType, TypeSymbol rightType) 5978return (rightType, NullableFlowState.NotNull); 5983return (leftType, NullableFlowState.NotNull); 5986(TypeSymbol ResultType, NullableFlowState LeftState) getResultStateWithRightType(TypeSymbol leftType, TypeSymbol rightType) 5998TypeWithState.Create(leftType, NullableFlowState.NotNull), 6009return (rightType, NullableFlowState.NotNull); 6182SetResultType(node, TypeWithState.Create(resultType, NullableFlowState.MaybeDefault)); 6237var rValueState = consequenceRValue.State.Join(alternativeRValue.State); 6345NullableFlowState resultState; 6505SetResultType(node, TypeWithState.Create(rvalueType, NullableFlowState.NotNull)); 6975private NullableFlowState LearnFromCompareExchangeMethod(in CompareExchangeInfo compareExchangeInfo) 6995return NullableFlowState.NotNull; 7005var valueFlowState = compareExchangeInfo.Results[valueIndex].RValueType.State; 7012var locationFlowState = compareExchangeInfo.Results[locationIndex].RValueType.State; 7129return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 7134return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 7719SetState(ref state, memberSlot, NullableFlowState.NotNull); 8024var adjustedState = LearnFromCompareExchangeMethod(in compareExchangeInfoOpt); 8148return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8154return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8169return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8173return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8188return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8192return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8750NullableFlowState state = GetState(ref this.State, slot); 9025SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 9032SetState(ref this.State, slot, NullableFlowState.NotNull); 9050SetResultType(node, TypeWithState.Create(tupleOpt, NullableFlowState.NotNull)); 9172SetState(ref this.State, targetFieldSlot, NullableFlowState.NotNull); 9189SetState(ref this.State, targetFieldSlot, NullableFlowState.NotNull); 9425NullableFlowState resultState = NullableFlowState.NotNull; 9457resultState = NullableFlowState.NotNull; 9481return TypeWithState.Create(targetType, NullableFlowState.NotNull); 9487resultState = NullableFlowState.NotNull; 9491resultState = NullableFlowState.NotNull; 9496resultState = NullableFlowState.NotNull; 9536resultState = NullableFlowState.NotNull; 9660resultState = NullableFlowState.NotNull; 9730static TypeWithState calculateResultType(TypeWithAnnotations targetTypeWithNullability, bool fromExplicitCast, NullableFlowState resultState, bool isSuppressed, TypeSymbol targetType) 9734resultState = NullableFlowState.NotNull; 9739resultState = targetType?.IsTypeParameterDisallowingAnnotationInCSharp8() == true ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 9746static NullableFlowState getReferenceConversionResultState(TypeWithAnnotations targetType, TypeWithState operandType) 9748var state = operandType.State; 9751case NullableFlowState.MaybeNull: 9757return NullableFlowState.MaybeDefault; 9763return (annotation == NullableAnnotation.Annotated) ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 9767case NullableFlowState.MaybeDefault: 9770return NullableFlowState.MaybeNull; 9780static NullableFlowState getBoxingConversionResultState(TypeWithAnnotations targetType, TypeWithState operandType) 9782var state = operandType.State; 9783if (state == NullableFlowState.MaybeNull) 9788return NullableFlowState.MaybeDefault; 9798return (annotation == NullableAnnotation.Annotated) ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 9808static NullableFlowState getUnboxingConversionResultState(TypeWithState operandType) 9810var state = operandType.State; 9811if (state == NullableFlowState.MaybeNull) 9813return NullableFlowState.MaybeDefault; 9818static NullableFlowState getConversionResultState(TypeWithState operandType) 9820var state = operandType.State; 9821if (state == NullableFlowState.MaybeNull) 9823return NullableFlowState.MaybeDefault; 9959var resultType = TypeWithState.Create(targetType, NullableFlowState.NotNull); 10002NullableFlowState operandState = operandType.State; 10122private TypeWithState GetLiftedReturnType(TypeWithAnnotations returnType, NullableFlowState operandState) 10126NullableFlowState state = returnType.ToTypeWithState().State.Join(operandState); 10143private TypeWithState GetLiftedReturnTypeIfNecessary(bool isLifted, TypeWithAnnotations returnType, NullableFlowState operandState) 10239var result = TypeWithState.Create(delegateType, NullableFlowState.NotNull); 10536var rvalueResult = TypeWithState.Create(node.Type, NullableFlowState.NotNull); 10956var valueBeforeNullableWrapping = TypeWithState.Create(underlyingType.Type, NullableFlowState.NotNull); 11086SetResultType(node, TypeWithState.Create(receiverType.Type, NullableFlowState.NotNull)); 11110SetResultType(node, TypeWithState.Create(opType.Type, NullableFlowState.NotNull)); 11264SetResultType(node, TypeWithState.Create(receiverType.Type, NullableFlowState.NotNull)); 11292SetResultType(node, TypeWithState.Create(opType.Type, NullableFlowState.NotNull)); 11594var state = GetState(ref this.State, slot); 11606SetState(ref this.StateWhenTrue, containingSlot, NullableFlowState.NotNull); 11830if (enumeratorReturnType.State != NullableFlowState.NotNull) 11911var state = NullableFlowState.NotNull; 12108TypeWithState.Create(node.Type, node.OperatorKind.IsLifted() ? argumentResult.State : NullableFlowState.NotNull); 12376SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12398SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.MaybeDefault)); 12413SetState(ref this.State, slot, NullableFlowState.NotNull); 12459NullableFlowState resultState = NullableFlowState.NotNull; 12474resultState = NullableFlowState.MaybeDefault; 12512SetResultType(node, TypeWithState.Create(node.Type, node.Type?.CanContainNull() != false && node.ConstantValueOpt?.IsNull == true ? NullableFlowState.MaybeDefault : NullableFlowState.NotNull)); 12620node.IsAddition ? GetState(ref this.State, memberSlot).Meet(ResultType.State) : NullableFlowState.MaybeNull); 12737private bool ReportPossibleNullReceiverIfNeeded(TypeSymbol type, NullableFlowState state, bool checkNullableValueType, SyntaxNode syntax, out bool reportedDiagnostic) 12796SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12811SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12860SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12866SetResultType(node, TypeWithState.Create(type, NullableFlowState.NotNull)); 12934SetState(ref this.State, slot, NullableFlowState.NotNull); 13023Debug.Assert(ResultType is TypeWithState { Type: FunctionPointerTypeSymbol { }, State: NullableFlowState.NotNull }); 13328SetValue(Id, index, useNotNullsAsDefault ? NullableFlowState.NotNull : walker.GetDefaultState(ref this, slot)); 13332public NullableFlowState this[int slot] 13346private NullableFlowState GetValue(int id, int index) 13356private NullableFlowState GetValue(int index) 13360return NullableFlowState.NotNull; 13367var result = (_state[index], _state[index + 1]) switch 13369(false, false) => NullableFlowState.NotNull, // Should not be reachable 13370(true, false) => NullableFlowState.MaybeNull, 13371(false, true) => NullableFlowState.MaybeDefault, 13372(true, true) => NullableFlowState.NotNull 13378private void SetValue(int id, int index, NullableFlowState value) 13391private void SetValue(int index, NullableFlowState value) 13399NullableFlowState.MaybeNull => (true, false), 13400NullableFlowState.MaybeDefault => (false, true), 13401NullableFlowState.NotNull => (true, true), 13455var oldValue = oldReachable ? GetValue(i) : NullableFlowState.NotNull; 13456var newValue = oldValue.Join(other.GetValue(i)); 13481var oldValue = GetValue(i); 13482var newValue = oldValue.Meet(other.GetValue(i)); 13498var mayBeNull = GetValue(i) is NullableFlowState.MaybeNull or NullableFlowState.MaybeDefault; 13529NullableFlowState.MaybeNull => "?", 13530NullableFlowState.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)
126900private static readonly NullableFlowState[] s_AllNullableFlowStates = (NullableFlowState[])Enum.GetValues(typeof(NullableFlowState)); 126921var inputs = new[] { NullableFlowState.NotNull, NullableFlowState.MaybeNull }; 126923Func<int, int, NullableFlowState> getResult = (i, j) => inputs[i].Join(inputs[j]); 126925var expected = new NullableFlowState[2, 2] 126927{ NullableFlowState.NotNull, NullableFlowState.MaybeNull }, 126928{ NullableFlowState.MaybeNull, NullableFlowState.MaybeNull }, 126953var inputs = new[] { NullableFlowState.NotNull, NullableFlowState.MaybeNull }; 126954Func<int, int, NullableFlowState> getResult = (i, j) => inputs[i].Meet(inputs[j]); 126956var expected = new NullableFlowState[2, 2] 126958{ NullableFlowState.NotNull, NullableFlowState.NotNull }, 126959{ NullableFlowState.NotNull, NullableFlowState.MaybeNull }, 126986private static void AssertEqual(NullableFlowState[,] expected, Func<int, int, NullableFlowState> getResult, int size) 126988AssertEx.Equal<NullableFlowState>(expected, getResult, (na1, na2) => na1 == na2, na => $"NullableFlowState.{na}", "{0,-32:G}", size); 127007foreach (var a in s_AllNullableFlowStates) 127009foreach (var b in s_AllNullableFlowStates) 127037foreach (var a in s_AllNullableFlowStates) 127039foreach (var b in s_AllNullableFlowStates) 127041foreach (var c in s_AllNullableFlowStates) 127043var leftFirst = a.Join(b).Join(c); 127044var rightFirst = a.Join(b.Join(c)); 127071foreach (var a in s_AllNullableFlowStates) 127073foreach (var b in s_AllNullableFlowStates) 127075foreach (var c in s_AllNullableFlowStates) 127077var leftFirst = a.Meet(b).Meet(c); 127078var rightFirst = a.Meet(b.Meet(c)); 127124foreach (var a in s_AllNullableFlowStates) 127126foreach (var b in s_AllNullableFlowStates) 127128var leftFirst = a.Join(b); 127129var rightFirst = b.Join(a); 127152foreach (var a in s_AllNullableFlowStates) 127154foreach (var b in s_AllNullableFlowStates) 127156var leftFirst = a.Meet(b); 127157var rightFirst = b.Meet(a);