299 references to NullableFlowState
Microsoft.CodeAnalysis.CSharp (257)
Binder\Semantics\BestTypeInferrer.cs (3)
36public static NullableFlowState GetNullableState(ArrayBuilder<TypeWithState> types) 38NullableFlowState result = NullableFlowState.NotNull;
FlowAnalysis\NullableWalker.cs (182)
111public VisitResult(TypeSymbol? type, NullableAnnotation annotation, NullableFlowState state) 187private static readonly TypeWithState _invalidType = TypeWithState.Create(ErrorTypeSymbol.UnknownResultType, NullableFlowState.NotNull); 747var memberState = GetState(ref state, slot); 748var badState = symbolType.Type.IsPossiblyNullableReferenceTypeTypeParameter() && (annotations & FlowAnalysisAnnotations.NotNull) == 0 749? NullableFlowState.MaybeDefault 750: NullableFlowState.MaybeNull; 849var parameterState = GetState(ref state, memberSlot); 909SetState(ref this.State, memberSlot, type.Type.IsPossiblyNullableReferenceTypeTypeParameter() ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull); 1063SetState(ref this.State, memberSlot, NullableFlowState.MaybeNull); 1160var parameterState = GetState(ref state, slot); 1216var parameterState = GetState(ref stateWhen, slot); 1243private void EnforceNotNullIfNotNull(SyntaxNode? syntaxOpt, LocalState state, ImmutableArray<ParameterSymbol> parameters, ImmutableHashSet<string> inputParamNames, NullableFlowState outputState, ParameterSymbol? outputParam) 1819private NullableFlowState GetState(ref LocalState state, int slot) 1822return NullableFlowState.NotNull; 1828private void SetState(ref LocalState state, int slot, NullableFlowState value, bool useNotNullsAsDefault = false) 1850private NullableFlowState GetDefaultState(ref LocalState state, int slot) 1855return NullableFlowState.NotNull; 1885return NullableFlowState.NotNull; 2190private static bool ShouldReportNullableAssignment(TypeWithAnnotations type, NullableFlowState state) 2207case NullableFlowState.NotNull: 2209case NullableFlowState.MaybeNull: 2290valueType.State == NullableFlowState.MaybeDefault; 2472var newState = valueType.State; 2572NullableFlowState value = isDefaultValue ? NullableFlowState.MaybeNull : fieldOrPropertyType.ToTypeWithState().State; 2584NullableFlowState.NotNull; 2616/// and the new state is not <see cref="NullableFlowState.NotNull"/>, this method should be called to perform the 2620private void SetStateAndTrackForFinally(ref LocalState state, int slot, NullableFlowState newState) 2624if (newState != NullableFlowState.NotNull && NonMonotonicState.HasValue) 2657private NullableFlowState GetDefaultState(Symbol symbol) 2751var state = GetParameterState(parameterType, parameter.FlowAnalysisAnnotations).State; 2789return TypeWithState.Create(parameterType.Type, NullableFlowState.MaybeDefault); 2794return TypeWithState.Create(parameterType.Type, NullableFlowState.NotNull); 3448SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 3467(int slot, NullableFlowState resultState, Func<TypeSymbol, MethodSymbol?, int>? initialStateInferenceCompletion) = inferInitialObjectState(node, type, constructor, arguments, argumentResults, isTargetTyped); 3487NullableFlowState resultState, 3516NullableFlowState resultState) 3518Debug.Assert(resultState == NullableFlowState.NotNull); 3551(int slot, NullableFlowState resultState, Func<TypeSymbol, MethodSymbol?, int>? completion) inferInitialObjectState( 3558return (-1, NullableFlowState.NotNull, inferInitialObjectStateAsContinuation(node, arguments, argumentResults)); 3566var resultState = NullableFlowState.NotNull; 3596resultState = NullableFlowState.MaybeNull; 3630Debug.Assert(resultState == NullableFlowState.NotNull); 3764var result = new VisitResult(objectInitializer.Type, NullableAnnotation.NotAnnotated, NullableFlowState.NotNull); 3953SetAnalyzedNullability(node.ImplicitReceiverOpt, new VisitResult(node.ImplicitReceiverOpt.Type, NullableAnnotation.NotAnnotated, NullableFlowState.NotNull)); 4010SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 4116SetResultType(node, TypeWithState.Create(anonymousType, NullableFlowState.NotNull)); 4148SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 4153SetResultType(node, TypeWithState.Create(arrayType, NullableFlowState.NotNull)); 4230var elementState = BestTypeInferrer.GetNullableState(expressionTypes); 4410NullableFlowState resultState = NullableFlowState.NotNull; 4438resultState = NullableFlowState.MaybeNull; // Delegate removal can produce null. 4441resultState = NullableFlowState.NotNull; 4954SetState(ref stateToUpdate, slot, NullableFlowState.NotNull); 4983SetState(ref state, slot, NullableFlowState.NotNull); 5008if (GetState(ref state, slot) == NullableFlowState.NotNull) 5011SetState(ref state, slot, NullableFlowState.MaybeNull); 5040SetState(ref state, childSlot, NullableFlowState.NotNull); 5159BoundNullCoalescingOperatorResultKind.NoCommonType => (node.Type, NullableFlowState.NotNull), 5164BoundNullCoalescingOperatorResultKind.RightDynamicType => (rightResultType!, NullableFlowState.NotNull), 5171(TypeSymbol ResultType, NullableFlowState LeftState) getLeftResultType(TypeSymbol leftType, TypeSymbol rightType) 5181return (rightType, NullableFlowState.NotNull); 5186return (leftType, NullableFlowState.NotNull); 5189(TypeSymbol ResultType, NullableFlowState LeftState) getResultStateWithRightType(TypeSymbol leftType, TypeSymbol rightType) 5201TypeWithState.Create(leftType, NullableFlowState.NotNull), 5212return (rightType, NullableFlowState.NotNull); 5384SetResultType(node, TypeWithState.Create(resultType, NullableFlowState.MaybeDefault)); 5445var rValueState = consequenceRValue.State.Join(alternativeRValue.State); 5522NullableFlowState resultState; 5674SetResultType(node, TypeWithState.Create(rvalueType, NullableFlowState.NotNull)); 5907private NullableFlowState LearnFromCompareExchangeMethod(in CompareExchangeInfo compareExchangeInfo) 5927return NullableFlowState.NotNull; 5937var valueFlowState = compareExchangeInfo.Results[valueIndex].RValueType.State; 5944var locationFlowState = compareExchangeInfo.Results[locationIndex].RValueType.State; 6041return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 6046return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 6442SetState(ref state, memberSlot, NullableFlowState.NotNull); 6716var adjustedState = LearnFromCompareExchangeMethod(in compareExchangeInfoOpt); 6840return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 6846return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 6861return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 6865return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 6880return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 6884return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 7312NullableFlowState state = GetState(ref this.State, slot); 7577SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 7584SetState(ref this.State, slot, NullableFlowState.NotNull); 7596SetResultType(node, TypeWithState.Create(tupleOpt, NullableFlowState.NotNull)); 7718SetState(ref this.State, targetFieldSlot, NullableFlowState.NotNull); 7735SetState(ref this.State, targetFieldSlot, NullableFlowState.NotNull); 7957NullableFlowState resultState = NullableFlowState.NotNull; 7991resultState = NullableFlowState.NotNull; 8015return TypeWithState.Create(targetType, NullableFlowState.NotNull); 8021resultState = NullableFlowState.NotNull; 8025resultState = NullableFlowState.NotNull; 8030resultState = NullableFlowState.NotNull; 8069resultState = NullableFlowState.NotNull; 8193resultState = NullableFlowState.NotNull; 8237static TypeWithState calculateResultType(TypeWithAnnotations targetTypeWithNullability, bool fromExplicitCast, NullableFlowState resultState, bool isSuppressed, TypeSymbol targetType) 8241resultState = NullableFlowState.NotNull; 8246resultState = targetType?.IsTypeParameterDisallowingAnnotationInCSharp8() == true ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 8253static NullableFlowState getReferenceConversionResultState(TypeWithAnnotations targetType, TypeWithState operandType) 8255var state = operandType.State; 8258case NullableFlowState.MaybeNull: 8264return NullableFlowState.MaybeDefault; 8270return (annotation == NullableAnnotation.Annotated) ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 8274case NullableFlowState.MaybeDefault: 8277return NullableFlowState.MaybeNull; 8287static NullableFlowState getBoxingConversionResultState(TypeWithAnnotations targetType, TypeWithState operandType) 8289var state = operandType.State; 8290if (state == NullableFlowState.MaybeNull) 8295return NullableFlowState.MaybeDefault; 8305return (annotation == NullableAnnotation.Annotated) ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 8315static NullableFlowState getUnboxingConversionResultState(TypeWithState operandType) 8317var state = operandType.State; 8318if (state == NullableFlowState.MaybeNull) 8320return NullableFlowState.MaybeDefault; 8325static NullableFlowState getConversionResultState(TypeWithState operandType) 8327var state = operandType.State; 8328if (state == NullableFlowState.MaybeNull) 8330return NullableFlowState.MaybeDefault; 8457var resultType = TypeWithState.Create(targetType, NullableFlowState.NotNull); 8500NullableFlowState operandState = operandType.State; 8617private TypeWithState GetLiftedReturnType(TypeWithAnnotations returnType, NullableFlowState operandState) 8621NullableFlowState state = returnType.ToTypeWithState().State.Join(operandState); 8638private TypeWithState GetLiftedReturnTypeIfNecessary(bool isLifted, TypeWithAnnotations returnType, NullableFlowState operandState) 8734var result = TypeWithState.Create(delegateType, NullableFlowState.NotNull); 9019var rvalueResult = TypeWithState.Create(node.Type, NullableFlowState.NotNull); 9435var valueBeforeNullableWrapping = TypeWithState.Create(underlyingType.Type, NullableFlowState.NotNull); 9695resultType = TypeWithState.Create(node.Type, NullableFlowState.NotNull); 9902var state = GetState(ref this.State, slot); 9914SetState(ref this.StateWhenTrue, containingSlot, NullableFlowState.NotNull); 10097if (enumeratorReturnType.State != NullableFlowState.NotNull) 10178var state = NullableFlowState.NotNull; 10372TypeWithState.Create(node.Type, node.OperatorKind.IsLifted() ? argumentResult.State : NullableFlowState.NotNull); 10419return TypeWithState.Create(node.Type, NullableFlowState.NotNull); 10537SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 10559SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.MaybeDefault)); 10574SetState(ref this.State, slot, NullableFlowState.NotNull); 10622NullableFlowState resultState = NullableFlowState.NotNull; 10637resultState = NullableFlowState.MaybeDefault; 10675SetResultType(node, TypeWithState.Create(node.Type, node.Type?.CanContainNull() != false && node.ConstantValueOpt?.IsNull == true ? NullableFlowState.MaybeDefault : NullableFlowState.NotNull)); 10783node.IsAddition ? GetState(ref this.State, memberSlot).Meet(ResultType.State) : NullableFlowState.MaybeNull); 10900private bool ReportPossibleNullReceiverIfNeeded(TypeSymbol type, NullableFlowState state, bool checkNullableValueType, SyntaxNode syntax, out bool reportedDiagnostic) 10959SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 10974SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 11023SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 11029SetResultType(node, TypeWithState.Create(type, NullableFlowState.NotNull)); 11097SetState(ref this.State, slot, NullableFlowState.NotNull); 11186Debug.Assert(ResultType is TypeWithState { Type: FunctionPointerTypeSymbol { }, State: NullableFlowState.NotNull }); 11491SetValue(Id, index, useNotNullsAsDefault ? NullableFlowState.NotNull : walker.GetDefaultState(ref this, slot)); 11495public NullableFlowState this[int slot] 11509private NullableFlowState GetValue(int id, int index) 11519private NullableFlowState GetValue(int index) 11523return NullableFlowState.NotNull; 11527var result = (_state[index], _state[index + 1]) switch 11530(true, false) => NullableFlowState.MaybeNull, 11531(false, true) => NullableFlowState.MaybeDefault, 11532(true, true) => NullableFlowState.NotNull 11537private void SetValue(int id, int index, NullableFlowState value) 11550private void SetValue(int index, NullableFlowState value) 11558NullableFlowState.MaybeNull => (true, false), 11559NullableFlowState.MaybeDefault => (false, true), 11560NullableFlowState.NotNull => (true, true), 11611var oldValue = GetValue(i); 11612var newValue = oldValue.Join(other.GetValue(i)); 11637var oldValue = GetValue(i); 11638var newValue = oldValue.Meet(other.GetValue(i)); 11654var mayBeNull = GetValue(i) is NullableFlowState.MaybeNull or NullableFlowState.MaybeDefault; 11685NullableFlowState.MaybeNull => "?", 11686NullableFlowState.MaybeDefault => "??",
FlowAnalysis\NullableWalker_Patterns.cs (5)
443SetState(ref State, outputSlot, NullableFlowState.NotNull); 537SetState(ref this.State, outputSlot, NullableFlowState.NotNull); // Slice value is assumed to be never null 640var tempState = GetState(ref this.State, tempSlot); 913NullableFlowState inferredState; 961NullableFlowState inferredState = BestTypeInferrer.GetNullableState(resultTypes);
Symbols\ConstraintsHelper.cs (14)
1121static NullableFlowState getTypeArgumentState(in TypeWithAnnotations typeWithAnnotations) 1126return NullableFlowState.NotNull; 1130return type.IsNullableTypeOrTypeParameter() ? NullableFlowState.MaybeNull : NullableFlowState.NotNull; 1135return type.IsTypeParameterDisallowingAnnotationInCSharp8() ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 1137return NullableFlowState.NotNull; 1142return NullableFlowState.NotNull; 1144NullableFlowState? result = null; 1147var constraintState = getTypeArgumentState(constraintType); 1157return result ?? NullableFlowState.MaybeNull; 1160static bool constraintTypeAllows(in TypeWithAnnotations typeWithAnnotations, NullableFlowState state) 1162if (state == NullableFlowState.NotNull) 1189return 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)
784static NullableFlowState getFlowState(TypeSymbol type, NullableAnnotation annotation) 788return annotation.IsAnnotated() ? NullableFlowState.MaybeDefault : NullableFlowState.NotNull; 792return annotation switch { NullableAnnotation.Annotated => NullableFlowState.MaybeDefault, NullableAnnotation.NotAnnotated => NullableFlowState.MaybeNull, _ => NullableFlowState.NotNull }; 796return annotation switch { NullableAnnotation.Annotated => NullableFlowState.MaybeDefault, _ => NullableFlowState.NotNull }; 800return NullableFlowState.MaybeNull; 802return 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)
123107private static readonly NullableFlowState[] s_AllNullableFlowStates = (NullableFlowState[])Enum.GetValues(typeof(NullableFlowState)); 123128var inputs = new[] { NullableFlowState.NotNull, NullableFlowState.MaybeNull }; 123130Func<int, int, NullableFlowState> getResult = (i, j) => inputs[i].Join(inputs[j]); 123132var expected = new NullableFlowState[2, 2] 123134{ NullableFlowState.NotNull, NullableFlowState.MaybeNull }, 123135{ NullableFlowState.MaybeNull, NullableFlowState.MaybeNull }, 123160var inputs = new[] { NullableFlowState.NotNull, NullableFlowState.MaybeNull }; 123161Func<int, int, NullableFlowState> getResult = (i, j) => inputs[i].Meet(inputs[j]); 123163var expected = new NullableFlowState[2, 2] 123165{ NullableFlowState.NotNull, NullableFlowState.NotNull }, 123166{ NullableFlowState.NotNull, NullableFlowState.MaybeNull }, 123193private static void AssertEqual(NullableFlowState[,] expected, Func<int, int, NullableFlowState> getResult, int size) 123195AssertEx.Equal<NullableFlowState>(expected, getResult, (na1, na2) => na1 == na2, na => $"NullableFlowState.{na}", "{0,-32:G}", size); 123214foreach (var a in s_AllNullableFlowStates) 123216foreach (var b in s_AllNullableFlowStates) 123244foreach (var a in s_AllNullableFlowStates) 123246foreach (var b in s_AllNullableFlowStates) 123248foreach (var c in s_AllNullableFlowStates) 123250var leftFirst = a.Join(b).Join(c); 123251var rightFirst = a.Join(b.Join(c)); 123278foreach (var a in s_AllNullableFlowStates) 123280foreach (var b in s_AllNullableFlowStates) 123282foreach (var c in s_AllNullableFlowStates) 123284var leftFirst = a.Meet(b).Meet(c); 123285var rightFirst = a.Meet(b.Meet(c)); 123331foreach (var a in s_AllNullableFlowStates) 123333foreach (var b in s_AllNullableFlowStates) 123335var leftFirst = a.Join(b); 123336var rightFirst = b.Join(a); 123359foreach (var a in s_AllNullableFlowStates) 123361foreach (var b in s_AllNullableFlowStates) 123363var leftFirst = a.Meet(b); 123364var rightFirst = b.Meet(a);