312 references to NullableFlowState
Microsoft.CodeAnalysis.CSharp (270)
Binder\Semantics\BestTypeInferrer.cs (3)
36public static NullableFlowState GetNullableState(ArrayBuilder<TypeWithState> types) 38NullableFlowState result = NullableFlowState.NotNull;
FlowAnalysis\NullableWalker.cs (194)
145public VisitResult(TypeSymbol? type, NullableAnnotation annotation, NullableFlowState state) 235private static readonly TypeWithState _invalidType = TypeWithState.Create(new UnsupportedMetadataTypeSymbol(), NullableFlowState.NotNull); 820var memberState = GetState(ref state, slot); 821var badState = symbolType.Type.IsPossiblyNullableReferenceTypeTypeParameter() && (annotations & FlowAnalysisAnnotations.NotNull) == 0 822? NullableFlowState.MaybeDefault 823: NullableFlowState.MaybeNull; 879SetState(ref this.State, memberSlot, type.Type.IsPossiblyNullableReferenceTypeTypeParameter() ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull); 1174var parameterState = GetState(ref state, memberSlot); 1206SetState(ref this.State, memberSlot, NullableFlowState.MaybeNull); 1319var parameterState = GetState(ref state, slot); 1375var parameterState = GetState(ref stateWhen, slot); 1402private void EnforceNotNullIfNotNull(SyntaxNode? syntaxOpt, LocalState state, ImmutableArray<ParameterSymbol> parameters, ImmutableHashSet<string> inputParamNames, NullableFlowState outputState, ParameterSymbol? outputParam) 1990private NullableFlowState GetState(ref LocalState state, int slot) 1993return NullableFlowState.NotNull; 1999private void SetState(ref LocalState state, int slot, NullableFlowState value, bool useNotNullsAsDefault = false) 2021private NullableFlowState GetDefaultState(ref LocalState state, int slot) 2026return NullableFlowState.NotNull; 2056return NullableFlowState.NotNull; 2385private static bool ShouldReportNullableAssignment(TypeWithAnnotations type, NullableFlowState state) 2402case NullableFlowState.NotNull: 2404case NullableFlowState.MaybeNull: 2480valueType.State == NullableFlowState.MaybeDefault; 2662var newState = valueType.State; 2765NullableFlowState value = isDefaultValue ? NullableFlowState.MaybeNull : fieldOrPropertyType.ToTypeWithState().State; 2777NullableFlowState.NotNull; 2825/// and the new state is not <see cref="NullableFlowState.NotNull"/>, this method should be called to perform the 2829private void SetStateAndTrackForFinally(ref LocalState state, int slot, NullableFlowState newState) 2833if (newState != NullableFlowState.NotNull && NonMonotonicState.HasValue) 2918private NullableFlowState GetDefaultState(Symbol symbol) 3011var state = GetParameterState(parameterType, parameter.FlowAnalysisAnnotations).State; 3049return TypeWithState.Create(parameterType.Type, NullableFlowState.MaybeDefault); 3054return 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)); 3957NullableFlowState.NotNull)); 4138NullableFlowState resultState = getResultState(node, collectionKind); 4154static NullableFlowState getResultState(BoundCollectionExpression node, CollectionExpressionTypeKind collectionKind) 4166return NullableFlowState.NotNull; 4236(int slot, NullableFlowState resultState, Func<TypeSymbol, MethodSymbol?, int>? initialStateInferenceCompletion) = 4256NullableFlowState resultState, 4285NullableFlowState resultState) 4287Debug.Assert(resultState == NullableFlowState.NotNull); 4319(int slot, NullableFlowState resultState, Func<TypeSymbol, MethodSymbol?, int>? completion) inferInitialObjectState( 4327return (-1, NullableFlowState.NotNull, inferInitialObjectStateAsContinuation(node, arguments, argumentResults, hasObjectInitializer)); 4333var resultState = NullableFlowState.NotNull; 4365resultState = NullableFlowState.MaybeNull; 4400Debug.Assert(resultState == NullableFlowState.NotNull); 4557var result = new VisitResult(objectInitializer.Type, NullableAnnotation.NotAnnotated, NullableFlowState.NotNull); 4784SetAnalyzedNullability(node.ImplicitReceiverOpt, new VisitResult(node.ImplicitReceiverOpt.Type, NullableAnnotation.NotAnnotated, NullableFlowState.NotNull)); 4842SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 4847var resultType = TypeWithState.Create(node.Type, NullableFlowState.NotNull); 4955SetResultType(node, TypeWithState.Create(anonymousType, NullableFlowState.NotNull)); 4987SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 4992SetResultType(node, TypeWithState.Create(arrayType, NullableFlowState.NotNull)); 5068var elementState = BestTypeInferrer.GetNullableState(expressionTypes); 5270NullableFlowState resultState = NullableFlowState.NotNull; 5298resultState = NullableFlowState.MaybeNull; // Delegate removal can produce null. 5301resultState = NullableFlowState.NotNull; 5890SetState(ref stateToUpdate, slot, NullableFlowState.NotNull); 5919SetState(ref state, slot, NullableFlowState.NotNull); 5944if (GetState(ref state, slot) == NullableFlowState.NotNull) 5947SetState(ref state, slot, NullableFlowState.MaybeNull); 5976SetState(ref state, childSlot, NullableFlowState.NotNull); 6055SetState(ref this.State, leftSlot, NullableFlowState.NotNull); 6110BoundNullCoalescingOperatorResultKind.NoCommonType => (node.Type, NullableFlowState.NotNull), 6115BoundNullCoalescingOperatorResultKind.RightDynamicType => (rightResultType!, NullableFlowState.NotNull), 6122(TypeSymbol ResultType, NullableFlowState LeftState) getLeftResultType(TypeSymbol leftType, TypeSymbol rightType) 6132return (rightType, NullableFlowState.NotNull); 6137return (leftType, NullableFlowState.NotNull); 6140(TypeSymbol ResultType, NullableFlowState LeftState) getResultStateWithRightType(TypeSymbol leftType, TypeSymbol rightType) 6152TypeWithState.Create(leftType, NullableFlowState.NotNull), 6163return (rightType, NullableFlowState.NotNull); 6336SetResultType(node, TypeWithState.Create(resultType, NullableFlowState.MaybeDefault)); 6391var rValueState = consequenceRValue.State.Join(alternativeRValue.State); 6499NullableFlowState resultState; 6659SetResultType(node, TypeWithState.Create(rvalueType, NullableFlowState.NotNull)); 7176private NullableFlowState LearnFromCompareExchangeMethod(in CompareExchangeInfo compareExchangeInfo) 7196return NullableFlowState.NotNull; 7206var valueFlowState = compareExchangeInfo.Results[valueIndex].RValueType.State; 7213var locationFlowState = compareExchangeInfo.Results[locationIndex].RValueType.State; 7330return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 7335return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 7913SetState(ref state, memberSlot, NullableFlowState.NotNull); 8218var adjustedState = LearnFromCompareExchangeMethod(in compareExchangeInfoOpt); 8342return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8348return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8363return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8367return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8382return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8386return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8955NullableFlowState state = GetState(ref this.State, slot); 9230SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 9237SetState(ref this.State, slot, NullableFlowState.NotNull); 9255SetResultType(node, TypeWithState.Create(tupleOpt, NullableFlowState.NotNull)); 9377SetState(ref this.State, targetFieldSlot, NullableFlowState.NotNull); 9394SetState(ref this.State, targetFieldSlot, NullableFlowState.NotNull); 9630NullableFlowState resultState = NullableFlowState.NotNull; 9662resultState = NullableFlowState.NotNull; 9686return TypeWithState.Create(targetType, NullableFlowState.NotNull); 9692resultState = NullableFlowState.NotNull; 9696resultState = NullableFlowState.NotNull; 9701resultState = NullableFlowState.NotNull; 9741resultState = NullableFlowState.NotNull; 9865resultState = NullableFlowState.NotNull; 9935static TypeWithState calculateResultType(TypeWithAnnotations targetTypeWithNullability, bool fromExplicitCast, NullableFlowState resultState, bool isSuppressed, TypeSymbol targetType) 9939resultState = NullableFlowState.NotNull; 9944resultState = targetType?.IsTypeParameterDisallowingAnnotationInCSharp8() == true ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 9951static NullableFlowState getReferenceConversionResultState(TypeWithAnnotations targetType, TypeWithState operandType) 9953var state = operandType.State; 9956case NullableFlowState.MaybeNull: 9962return NullableFlowState.MaybeDefault; 9968return (annotation == NullableAnnotation.Annotated) ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 9972case NullableFlowState.MaybeDefault: 9975return NullableFlowState.MaybeNull; 9985static NullableFlowState getBoxingConversionResultState(TypeWithAnnotations targetType, TypeWithState operandType) 9987var state = operandType.State; 9988if (state == NullableFlowState.MaybeNull) 9993return NullableFlowState.MaybeDefault; 10003return (annotation == NullableAnnotation.Annotated) ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 10013static NullableFlowState getUnboxingConversionResultState(TypeWithState operandType) 10015var state = operandType.State; 10016if (state == NullableFlowState.MaybeNull) 10018return NullableFlowState.MaybeDefault; 10023static NullableFlowState getConversionResultState(TypeWithState operandType) 10025var state = operandType.State; 10026if (state == NullableFlowState.MaybeNull) 10028return NullableFlowState.MaybeDefault; 10163var resultType = TypeWithState.Create(targetType, NullableFlowState.NotNull); 10206NullableFlowState operandState = operandType.State; 10326private TypeWithState GetLiftedReturnType(TypeWithAnnotations returnType, NullableFlowState operandState) 10330NullableFlowState state = returnType.ToTypeWithState().State.Join(operandState); 10347private TypeWithState GetLiftedReturnTypeIfNecessary(bool isLifted, TypeWithAnnotations returnType, NullableFlowState operandState) 10443var result = TypeWithState.Create(delegateType, NullableFlowState.NotNull); 10740var rvalueResult = TypeWithState.Create(node.Type, NullableFlowState.NotNull); 11158var valueBeforeNullableWrapping = TypeWithState.Create(underlyingType.Type, NullableFlowState.NotNull); 11291SetResultType(node, TypeWithState.Create(receiverType.Type, NullableFlowState.NotNull)); 11315SetResultType(node, TypeWithState.Create(opType.Type, NullableFlowState.NotNull)); 11472SetResultType(node, TypeWithState.Create(receiverType.Type, NullableFlowState.NotNull)); 11500SetResultType(node, TypeWithState.Create(opType.Type, NullableFlowState.NotNull)); 11810var state = GetState(ref this.State, slot); 11822SetState(ref this.StateWhenTrue, containingSlot, NullableFlowState.NotNull); 12044if (enumeratorReturnType.State != NullableFlowState.NotNull) 12125var state = NullableFlowState.NotNull; 12329TypeWithState.Create(node.Type, node.OperatorKind.IsLifted() ? argumentResult.State : NullableFlowState.NotNull); 12597SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 12619SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.MaybeDefault)); 12634SetState(ref this.State, slot, NullableFlowState.NotNull); 12680NullableFlowState resultState = NullableFlowState.NotNull; 12695resultState = NullableFlowState.MaybeDefault; 12733SetResultType(node, TypeWithState.Create(node.Type, node.Type?.CanContainNull() != false && node.ConstantValueOpt?.IsNull == true ? NullableFlowState.MaybeDefault : NullableFlowState.NotNull)); 12841node.IsAddition ? GetState(ref this.State, memberSlot).Meet(ResultType.State) : NullableFlowState.MaybeNull); 12958private bool ReportPossibleNullReceiverIfNeeded(TypeSymbol type, NullableFlowState state, bool checkNullableValueType, SyntaxNode syntax, out bool reportedDiagnostic) 13017SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 13032SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 13081SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 13087SetResultType(node, TypeWithState.Create(type, NullableFlowState.NotNull)); 13155SetState(ref this.State, slot, NullableFlowState.NotNull); 13246Debug.Assert(ResultType is TypeWithState { Type: FunctionPointerTypeSymbol { }, State: NullableFlowState.NotNull }); 13551SetValue(Id, index, useNotNullsAsDefault ? NullableFlowState.NotNull : walker.GetDefaultState(ref this, slot)); 13555public NullableFlowState this[int slot] 13569private NullableFlowState GetValue(int id, int index) 13579private NullableFlowState GetValue(int index) 13583return NullableFlowState.NotNull; 13590var result = (_state[index], _state[index + 1]) switch 13592(false, false) => NullableFlowState.NotNull, // Should not be reachable 13593(true, false) => NullableFlowState.MaybeNull, 13594(false, true) => NullableFlowState.MaybeDefault, 13595(true, true) => NullableFlowState.NotNull 13601private void SetValue(int id, int index, NullableFlowState value) 13614private void SetValue(int index, NullableFlowState value) 13622NullableFlowState.MaybeNull => (true, false), 13623NullableFlowState.MaybeDefault => (false, true), 13624NullableFlowState.NotNull => (true, true), 13678var oldValue = oldReachable ? GetValue(i) : NullableFlowState.NotNull; 13679var newValue = oldValue.Join(other.GetValue(i)); 13704var oldValue = GetValue(i); 13705var newValue = oldValue.Meet(other.GetValue(i)); 13721var mayBeNull = GetValue(i) is NullableFlowState.MaybeNull or NullableFlowState.MaybeDefault; 13752NullableFlowState.MaybeNull => "?", 13753NullableFlowState.MaybeDefault => "??",
FlowAnalysis\NullableWalker_Patterns.cs (6)
462SetState(ref State, outputSlot, NullableFlowState.NotNull); 514SetState(ref this.State, outputSlot, NullableFlowState.NotNull); // Slice value is assumed to be never null 522var inputState = GetState(ref this.State, inputSlot); 627var tempState = GetState(ref this.State, tempSlot); 1056NullableFlowState inferredState; 1106NullableFlowState 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)
126958private static readonly NullableFlowState[] s_AllNullableFlowStates = (NullableFlowState[])Enum.GetValues(typeof(NullableFlowState)); 126979var inputs = new[] { NullableFlowState.NotNull, NullableFlowState.MaybeNull }; 126981Func<int, int, NullableFlowState> getResult = (i, j) => inputs[i].Join(inputs[j]); 126983var expected = new NullableFlowState[2, 2] 126985{ NullableFlowState.NotNull, NullableFlowState.MaybeNull }, 126986{ NullableFlowState.MaybeNull, NullableFlowState.MaybeNull }, 127011var inputs = new[] { NullableFlowState.NotNull, NullableFlowState.MaybeNull }; 127012Func<int, int, NullableFlowState> getResult = (i, j) => inputs[i].Meet(inputs[j]); 127014var expected = new NullableFlowState[2, 2] 127016{ NullableFlowState.NotNull, NullableFlowState.NotNull }, 127017{ NullableFlowState.NotNull, NullableFlowState.MaybeNull }, 127044private static void AssertEqual(NullableFlowState[,] expected, Func<int, int, NullableFlowState> getResult, int size) 127046AssertEx.Equal<NullableFlowState>(expected, getResult, (na1, na2) => na1 == na2, na => $"NullableFlowState.{na}", "{0,-32:G}", size); 127065foreach (var a in s_AllNullableFlowStates) 127067foreach (var b in s_AllNullableFlowStates) 127095foreach (var a in s_AllNullableFlowStates) 127097foreach (var b in s_AllNullableFlowStates) 127099foreach (var c in s_AllNullableFlowStates) 127101var leftFirst = a.Join(b).Join(c); 127102var rightFirst = a.Join(b.Join(c)); 127129foreach (var a in s_AllNullableFlowStates) 127131foreach (var b in s_AllNullableFlowStates) 127133foreach (var c in s_AllNullableFlowStates) 127135var leftFirst = a.Meet(b).Meet(c); 127136var rightFirst = a.Meet(b.Meet(c)); 127182foreach (var a in s_AllNullableFlowStates) 127184foreach (var b in s_AllNullableFlowStates) 127186var leftFirst = a.Join(b); 127187var rightFirst = b.Join(a); 127210foreach (var a in s_AllNullableFlowStates) 127212foreach (var b in s_AllNullableFlowStates) 127214var leftFirst = a.Meet(b); 127215var rightFirst = b.Meet(a);