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)
126public VisitResult(TypeSymbol? type, NullableAnnotation annotation, NullableFlowState state) 216private static readonly TypeWithState _invalidType = TypeWithState.Create(new UnsupportedMetadataTypeSymbol(), NullableFlowState.NotNull); 802var memberState = GetState(ref state, slot); 803var badState = symbolType.Type.IsPossiblyNullableReferenceTypeTypeParameter() && (annotations & FlowAnalysisAnnotations.NotNull) == 0 804? NullableFlowState.MaybeDefault 805: NullableFlowState.MaybeNull; 861SetState(ref this.State, memberSlot, type.Type.IsPossiblyNullableReferenceTypeTypeParameter() ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull); 1156var parameterState = GetState(ref state, memberSlot); 1188SetState(ref this.State, memberSlot, NullableFlowState.MaybeNull); 1301var parameterState = GetState(ref state, slot); 1357var parameterState = GetState(ref stateWhen, slot); 1384private void EnforceNotNullIfNotNull(SyntaxNode? syntaxOpt, LocalState state, ImmutableArray<ParameterSymbol> parameters, ImmutableHashSet<string> inputParamNames, NullableFlowState outputState, ParameterSymbol? outputParam) 1971private NullableFlowState GetState(ref LocalState state, int slot) 1974return NullableFlowState.NotNull; 1980private void SetState(ref LocalState state, int slot, NullableFlowState value, bool useNotNullsAsDefault = false) 2002private NullableFlowState GetDefaultState(ref LocalState state, int slot) 2007return NullableFlowState.NotNull; 2037return NullableFlowState.NotNull; 2366private static bool ShouldReportNullableAssignment(TypeWithAnnotations type, NullableFlowState state) 2383case NullableFlowState.NotNull: 2385case NullableFlowState.MaybeNull: 2461valueType.State == NullableFlowState.MaybeDefault; 2643var newState = valueType.State; 2746NullableFlowState value = isDefaultValue ? NullableFlowState.MaybeNull : fieldOrPropertyType.ToTypeWithState().State; 2758NullableFlowState.NotNull; 2806/// and the new state is not <see cref="NullableFlowState.NotNull"/>, this method should be called to perform the 2810private void SetStateAndTrackForFinally(ref LocalState state, int slot, NullableFlowState newState) 2814if (newState != NullableFlowState.NotNull && NonMonotonicState.HasValue) 2899private NullableFlowState GetDefaultState(Symbol symbol) 2992var state = GetParameterState(parameterType, parameter.FlowAnalysisAnnotations).State; 3030return TypeWithState.Create(parameterType.Type, NullableFlowState.MaybeDefault); 3035return TypeWithState.Create(parameterType.Type, NullableFlowState.NotNull); 3339SetState(ref state, slot, NullableFlowState.NotNull); 3838SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 3846SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 4000NullableFlowState resultState = getResultState(node, collectionKind); 4006static NullableFlowState getResultState(BoundCollectionExpression node, CollectionExpressionTypeKind collectionKind) 4018return NullableFlowState.NotNull; 4087(int slot, NullableFlowState resultState, Func<TypeSymbol, MethodSymbol?, int>? initialStateInferenceCompletion) = 4107NullableFlowState resultState, 4136NullableFlowState resultState) 4138Debug.Assert(resultState == NullableFlowState.NotNull); 4170(int slot, NullableFlowState resultState, Func<TypeSymbol, MethodSymbol?, int>? completion) inferInitialObjectState( 4178return (-1, NullableFlowState.NotNull, inferInitialObjectStateAsContinuation(node, arguments, argumentResults, hasObjectInitializer)); 4184var resultState = NullableFlowState.NotNull; 4216resultState = NullableFlowState.MaybeNull; 4251Debug.Assert(resultState == NullableFlowState.NotNull); 4385var result = new VisitResult(objectInitializer.Type, NullableAnnotation.NotAnnotated, NullableFlowState.NotNull); 4575SetAnalyzedNullability(node.ImplicitReceiverOpt, new VisitResult(node.ImplicitReceiverOpt.Type, NullableAnnotation.NotAnnotated, NullableFlowState.NotNull)); 4633SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 4638var resultType = TypeWithState.Create(node.Type, NullableFlowState.NotNull); 4746SetResultType(node, TypeWithState.Create(anonymousType, NullableFlowState.NotNull)); 4778SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 4783SetResultType(node, TypeWithState.Create(arrayType, NullableFlowState.NotNull)); 4859var elementState = BestTypeInferrer.GetNullableState(expressionTypes); 5061NullableFlowState resultState = NullableFlowState.NotNull; 5089resultState = NullableFlowState.MaybeNull; // Delegate removal can produce null. 5092resultState = NullableFlowState.NotNull; 5681SetState(ref stateToUpdate, slot, NullableFlowState.NotNull); 5710SetState(ref state, slot, NullableFlowState.NotNull); 5735if (GetState(ref state, slot) == NullableFlowState.NotNull) 5738SetState(ref state, slot, NullableFlowState.MaybeNull); 5767SetState(ref state, childSlot, NullableFlowState.NotNull); 5846SetState(ref this.State, leftSlot, NullableFlowState.NotNull); 5901BoundNullCoalescingOperatorResultKind.NoCommonType => (node.Type, NullableFlowState.NotNull), 5906BoundNullCoalescingOperatorResultKind.RightDynamicType => (rightResultType!, NullableFlowState.NotNull), 5913(TypeSymbol ResultType, NullableFlowState LeftState) getLeftResultType(TypeSymbol leftType, TypeSymbol rightType) 5923return (rightType, NullableFlowState.NotNull); 5928return (leftType, NullableFlowState.NotNull); 5931(TypeSymbol ResultType, NullableFlowState LeftState) getResultStateWithRightType(TypeSymbol leftType, TypeSymbol rightType) 5943TypeWithState.Create(leftType, NullableFlowState.NotNull), 5954return (rightType, NullableFlowState.NotNull); 6127SetResultType(node, TypeWithState.Create(resultType, NullableFlowState.MaybeDefault)); 6182var rValueState = consequenceRValue.State.Join(alternativeRValue.State); 6290NullableFlowState resultState; 6450SetResultType(node, TypeWithState.Create(rvalueType, NullableFlowState.NotNull)); 6919private NullableFlowState LearnFromCompareExchangeMethod(in CompareExchangeInfo compareExchangeInfo) 6939return NullableFlowState.NotNull; 6949var valueFlowState = compareExchangeInfo.Results[valueIndex].RValueType.State; 6956var locationFlowState = compareExchangeInfo.Results[locationIndex].RValueType.State; 7073return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 7078return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 7663SetState(ref state, memberSlot, NullableFlowState.NotNull); 7968var adjustedState = LearnFromCompareExchangeMethod(in compareExchangeInfoOpt); 8092return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8098return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8113return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8117return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8132return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8136return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8648NullableFlowState state = GetState(ref this.State, slot); 8923SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 8930SetState(ref this.State, slot, NullableFlowState.NotNull); 8948SetResultType(node, TypeWithState.Create(tupleOpt, NullableFlowState.NotNull)); 9070SetState(ref this.State, targetFieldSlot, NullableFlowState.NotNull); 9087SetState(ref this.State, targetFieldSlot, NullableFlowState.NotNull); 9323NullableFlowState resultState = NullableFlowState.NotNull; 9355resultState = NullableFlowState.NotNull; 9379return TypeWithState.Create(targetType, NullableFlowState.NotNull); 9385resultState = NullableFlowState.NotNull; 9389resultState = NullableFlowState.NotNull; 9394resultState = NullableFlowState.NotNull; 9434resultState = NullableFlowState.NotNull; 9558resultState = NullableFlowState.NotNull; 9628static TypeWithState calculateResultType(TypeWithAnnotations targetTypeWithNullability, bool fromExplicitCast, NullableFlowState resultState, bool isSuppressed, TypeSymbol targetType) 9632resultState = NullableFlowState.NotNull; 9637resultState = targetType?.IsTypeParameterDisallowingAnnotationInCSharp8() == true ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 9644static NullableFlowState getReferenceConversionResultState(TypeWithAnnotations targetType, TypeWithState operandType) 9646var state = operandType.State; 9649case NullableFlowState.MaybeNull: 9655return NullableFlowState.MaybeDefault; 9661return (annotation == NullableAnnotation.Annotated) ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 9665case NullableFlowState.MaybeDefault: 9668return NullableFlowState.MaybeNull; 9678static NullableFlowState getBoxingConversionResultState(TypeWithAnnotations targetType, TypeWithState operandType) 9680var state = operandType.State; 9681if (state == NullableFlowState.MaybeNull) 9686return NullableFlowState.MaybeDefault; 9696return (annotation == NullableAnnotation.Annotated) ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 9706static NullableFlowState getUnboxingConversionResultState(TypeWithState operandType) 9708var state = operandType.State; 9709if (state == NullableFlowState.MaybeNull) 9711return NullableFlowState.MaybeDefault; 9716static NullableFlowState getConversionResultState(TypeWithState operandType) 9718var state = operandType.State; 9719if (state == NullableFlowState.MaybeNull) 9721return NullableFlowState.MaybeDefault; 9857var resultType = TypeWithState.Create(targetType, NullableFlowState.NotNull); 9900NullableFlowState operandState = operandType.State; 10020private TypeWithState GetLiftedReturnType(TypeWithAnnotations returnType, NullableFlowState operandState) 10024NullableFlowState state = returnType.ToTypeWithState().State.Join(operandState); 10041private TypeWithState GetLiftedReturnTypeIfNecessary(bool isLifted, TypeWithAnnotations returnType, NullableFlowState operandState) 10137var result = TypeWithState.Create(delegateType, NullableFlowState.NotNull); 10434var rvalueResult = TypeWithState.Create(node.Type, NullableFlowState.NotNull); 10854var valueBeforeNullableWrapping = TypeWithState.Create(underlyingType.Type, NullableFlowState.NotNull); 10984SetResultType(node, TypeWithState.Create(receiverType.Type, NullableFlowState.NotNull)); 11008SetResultType(node, TypeWithState.Create(opType.Type, NullableFlowState.NotNull)); 11162SetResultType(node, TypeWithState.Create(receiverType.Type, NullableFlowState.NotNull)); 11190SetResultType(node, TypeWithState.Create(opType.Type, NullableFlowState.NotNull)); 11492var state = GetState(ref this.State, slot); 11504SetState(ref this.StateWhenTrue, containingSlot, NullableFlowState.NotNull); 11728if (enumeratorReturnType.State != NullableFlowState.NotNull) 11809var state = NullableFlowState.NotNull; 12006TypeWithState.Create(node.Type, node.OperatorKind.IsLifted() ? argumentResult.State : NullableFlowState.NotNull); 12274SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12296SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.MaybeDefault)); 12311SetState(ref this.State, slot, NullableFlowState.NotNull); 12357NullableFlowState resultState = NullableFlowState.NotNull; 12372resultState = NullableFlowState.MaybeDefault; 12410SetResultType(node, TypeWithState.Create(node.Type, node.Type?.CanContainNull() != false && node.ConstantValueOpt?.IsNull == true ? NullableFlowState.MaybeDefault : NullableFlowState.NotNull)); 12518node.IsAddition ? GetState(ref this.State, memberSlot).Meet(ResultType.State) : NullableFlowState.MaybeNull); 12635private bool ReportPossibleNullReceiverIfNeeded(TypeSymbol type, NullableFlowState state, bool checkNullableValueType, SyntaxNode syntax, out bool reportedDiagnostic) 12694SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12709SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12758SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12764SetResultType(node, TypeWithState.Create(type, NullableFlowState.NotNull)); 12832SetState(ref this.State, slot, NullableFlowState.NotNull); 12921Debug.Assert(ResultType is TypeWithState { Type: FunctionPointerTypeSymbol { }, State: NullableFlowState.NotNull }); 13226SetValue(Id, index, useNotNullsAsDefault ? NullableFlowState.NotNull : walker.GetDefaultState(ref this, slot)); 13230public NullableFlowState this[int slot] 13244private NullableFlowState GetValue(int id, int index) 13254private NullableFlowState GetValue(int index) 13258return NullableFlowState.NotNull; 13265var result = (_state[index], _state[index + 1]) switch 13267(false, false) => NullableFlowState.NotNull, // Should not be reachable 13268(true, false) => NullableFlowState.MaybeNull, 13269(false, true) => NullableFlowState.MaybeDefault, 13270(true, true) => NullableFlowState.NotNull 13276private void SetValue(int id, int index, NullableFlowState value) 13289private void SetValue(int index, NullableFlowState value) 13297NullableFlowState.MaybeNull => (true, false), 13298NullableFlowState.MaybeDefault => (false, true), 13299NullableFlowState.NotNull => (true, true), 13353var oldValue = oldReachable ? GetValue(i) : NullableFlowState.NotNull; 13354var newValue = oldValue.Join(other.GetValue(i)); 13379var oldValue = GetValue(i); 13380var newValue = oldValue.Meet(other.GetValue(i)); 13396var mayBeNull = GetValue(i) is NullableFlowState.MaybeNull or NullableFlowState.MaybeDefault; 13427NullableFlowState.MaybeNull => "?", 13428NullableFlowState.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)
126877private static readonly NullableFlowState[] s_AllNullableFlowStates = (NullableFlowState[])Enum.GetValues(typeof(NullableFlowState)); 126898var inputs = new[] { NullableFlowState.NotNull, NullableFlowState.MaybeNull }; 126900Func<int, int, NullableFlowState> getResult = (i, j) => inputs[i].Join(inputs[j]); 126902var expected = new NullableFlowState[2, 2] 126904{ NullableFlowState.NotNull, NullableFlowState.MaybeNull }, 126905{ NullableFlowState.MaybeNull, NullableFlowState.MaybeNull }, 126930var inputs = new[] { NullableFlowState.NotNull, NullableFlowState.MaybeNull }; 126931Func<int, int, NullableFlowState> getResult = (i, j) => inputs[i].Meet(inputs[j]); 126933var expected = new NullableFlowState[2, 2] 126935{ NullableFlowState.NotNull, NullableFlowState.NotNull }, 126936{ NullableFlowState.NotNull, NullableFlowState.MaybeNull }, 126963private static void AssertEqual(NullableFlowState[,] expected, Func<int, int, NullableFlowState> getResult, int size) 126965AssertEx.Equal<NullableFlowState>(expected, getResult, (na1, na2) => na1 == na2, na => $"NullableFlowState.{na}", "{0,-32:G}", size); 126984foreach (var a in s_AllNullableFlowStates) 126986foreach (var b in s_AllNullableFlowStates) 127014foreach (var a in s_AllNullableFlowStates) 127016foreach (var b in s_AllNullableFlowStates) 127018foreach (var c in s_AllNullableFlowStates) 127020var leftFirst = a.Join(b).Join(c); 127021var rightFirst = a.Join(b.Join(c)); 127048foreach (var a in s_AllNullableFlowStates) 127050foreach (var b in s_AllNullableFlowStates) 127052foreach (var c in s_AllNullableFlowStates) 127054var leftFirst = a.Meet(b).Meet(c); 127055var rightFirst = a.Meet(b.Meet(c)); 127101foreach (var a in s_AllNullableFlowStates) 127103foreach (var b in s_AllNullableFlowStates) 127105var leftFirst = a.Join(b); 127106var rightFirst = b.Join(a); 127129foreach (var a in s_AllNullableFlowStates) 127131foreach (var b in s_AllNullableFlowStates) 127133var leftFirst = a.Meet(b); 127134var rightFirst = b.Meet(a);