316 references to NullableFlowState
Microsoft.CodeAnalysis.CSharp (274)
Binder\Semantics\BestTypeInferrer.cs (3)
36public static NullableFlowState GetNullableState(ArrayBuilder<TypeWithState> types) 38NullableFlowState result = NullableFlowState.NotNull;
FlowAnalysis\NullableWalker.cs (198)
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; 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; 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); 3358SetState(ref state, slot, NullableFlowState.NotNull); 3631placeholderResult = new VisitResult(placeholder.Type, NullableAnnotation.NotAnnotated, NullableFlowState.NotNull); 3882SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 3890SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 3985NullableFlowState.NotNull)); 4166NullableFlowState resultState = getResultState(node, collectionKind); 4182static NullableFlowState getResultState(BoundCollectionExpression node, CollectionExpressionTypeKind collectionKind) 4194return NullableFlowState.NotNull; 4264(int slot, NullableFlowState resultState, Func<TypeSymbol, MethodSymbol?, int>? initialStateInferenceCompletion) = 4284NullableFlowState resultState, 4313NullableFlowState resultState) 4315Debug.Assert(resultState == NullableFlowState.NotNull); 4347(int slot, NullableFlowState resultState, Func<TypeSymbol, MethodSymbol?, int>? completion) inferInitialObjectState( 4355return (-1, NullableFlowState.NotNull, inferInitialObjectStateAsContinuation(node, arguments, argumentResults, hasObjectInitializer)); 4361var resultState = NullableFlowState.NotNull; 4393resultState = NullableFlowState.MaybeNull; 4449Debug.Assert(resultState == NullableFlowState.NotNull); 4458NullableFlowState operandState; 4629var result = new VisitResult(objectInitializer.Type, NullableAnnotation.NotAnnotated, NullableFlowState.NotNull); 4856SetAnalyzedNullability(node.ImplicitReceiverOpt, new VisitResult(node.ImplicitReceiverOpt.Type, NullableAnnotation.NotAnnotated, NullableFlowState.NotNull)); 4914SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 4919var resultType = TypeWithState.Create(node.Type, NullableFlowState.NotNull); 5027SetResultType(node, TypeWithState.Create(anonymousType, NullableFlowState.NotNull)); 5059SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 5064SetResultType(node, TypeWithState.Create(arrayType, NullableFlowState.NotNull)); 5140var elementState = BestTypeInferrer.GetNullableState(expressionTypes); 5342NullableFlowState resultState = NullableFlowState.NotNull; 5370resultState = NullableFlowState.MaybeNull; // Delegate removal can produce null. 5373resultState = NullableFlowState.NotNull; 5962SetState(ref stateToUpdate, slot, NullableFlowState.NotNull); 5991SetState(ref state, slot, NullableFlowState.NotNull); 6016if (GetState(ref state, slot) == NullableFlowState.NotNull) 6019SetState(ref state, slot, NullableFlowState.MaybeNull); 6048SetState(ref state, childSlot, NullableFlowState.NotNull); 6127SetState(ref this.State, leftSlot, NullableFlowState.NotNull); 6182BoundNullCoalescingOperatorResultKind.NoCommonType => (node.Type, NullableFlowState.NotNull), 6187BoundNullCoalescingOperatorResultKind.RightDynamicType => (rightResultType!, NullableFlowState.NotNull), 6194(TypeSymbol ResultType, NullableFlowState LeftState) getLeftResultType(TypeSymbol leftType, TypeSymbol rightType) 6205return (rightType, NullableFlowState.NotNull); 6211return (leftType, NullableFlowState.NotNull); 6214(TypeSymbol ResultType, NullableFlowState LeftState) getResultStateWithRightType(TypeSymbol leftType, TypeSymbol rightType) 6226TypeWithState.Create(leftType, NullableFlowState.NotNull), 6237return (rightType, NullableFlowState.NotNull); 6410SetResultType(node, TypeWithState.Create(resultType, NullableFlowState.MaybeDefault)); 6465var rValueState = consequenceRValue.State.Join(alternativeRValue.State); 6573NullableFlowState resultState; 6733SetResultType(node, TypeWithState.Create(rvalueType, NullableFlowState.NotNull)); 7250private NullableFlowState LearnFromCompareExchangeMethod(in CompareExchangeInfo compareExchangeInfo) 7270return NullableFlowState.NotNull; 7280var valueFlowState = compareExchangeInfo.Results[valueIndex].RValueType.State; 7287var locationFlowState = compareExchangeInfo.Results[locationIndex].RValueType.State; 7404return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 7409return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8016SetState(ref state, memberSlot, NullableFlowState.NotNull); 8317var adjustedState = LearnFromCompareExchangeMethod(in compareExchangeInfoOpt); 8466return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8472return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8487return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8491return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 8506return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); 8510return TypeWithState.Create(typeWithState.Type, NullableFlowState.NotNull); 9079NullableFlowState state = GetState(ref this.State, slot); 9354SetResultType(node, TypeWithState.Create(null, NullableFlowState.NotNull)); 9361SetState(ref this.State, slot, NullableFlowState.NotNull); 9379SetResultType(node, TypeWithState.Create(tupleOpt, NullableFlowState.NotNull)); 9502SetState(ref this.State, targetFieldSlot, NullableFlowState.NotNull); 9519SetState(ref this.State, targetFieldSlot, NullableFlowState.NotNull); 9815NullableFlowState resultState = NullableFlowState.NotNull; 9847resultState = NullableFlowState.NotNull; 9872return TypeWithState.Create(targetType, NullableFlowState.NotNull); 9878resultState = NullableFlowState.NotNull; 9882resultState = NullableFlowState.NotNull; 9887resultState = NullableFlowState.NotNull; 9948resultState = NullableFlowState.NotNull; 10110resultState = NullableFlowState.NotNull; 10179static TypeWithState calculateResultType(TypeWithAnnotations targetTypeWithNullability, bool fromExplicitCast, NullableFlowState resultState, bool isSuppressed, TypeSymbol targetType) 10183resultState = NullableFlowState.NotNull; 10188resultState = targetType?.IsTypeParameterDisallowingAnnotationInCSharp8() == true ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 10195static NullableFlowState getReferenceConversionResultState(TypeWithAnnotations targetType, TypeWithState operandType) 10197var state = operandType.State; 10200case NullableFlowState.MaybeNull: 10206return NullableFlowState.MaybeDefault; 10212return (annotation == NullableAnnotation.Annotated) ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 10216case NullableFlowState.MaybeDefault: 10219return NullableFlowState.MaybeNull; 10229static NullableFlowState getBoxingConversionResultState(TypeWithAnnotations targetType, TypeWithState operandType) 10231var state = operandType.State; 10232if (state == NullableFlowState.MaybeNull) 10237return NullableFlowState.MaybeDefault; 10247return (annotation == NullableAnnotation.Annotated) ? NullableFlowState.MaybeDefault : NullableFlowState.MaybeNull; 10257static NullableFlowState getUnboxingConversionResultState(TypeWithState operandType) 10259var state = operandType.State; 10260if (state == NullableFlowState.MaybeNull) 10262return NullableFlowState.MaybeDefault; 10267static NullableFlowState getConversionResultState(TypeWithState operandType) 10269var state = operandType.State; 10270if (state == NullableFlowState.MaybeNull) 10272return NullableFlowState.MaybeDefault; 10407var resultType = TypeWithState.Create(targetType, NullableFlowState.NotNull); 10450NullableFlowState operandState = operandType.State; 10637var unionTypeWithState = TypeWithState.Create(toType, NullableFlowState.NotNull); 10675return TypeWithState.Create(targetType, NullableFlowState.NotNull); 10725private TypeWithState GetLiftedReturnType(TypeWithAnnotations returnType, NullableFlowState operandState) 10729NullableFlowState state = returnType.ToTypeWithState().State.Join(operandState); 10746private TypeWithState GetLiftedReturnTypeIfNecessary(bool isLifted, TypeWithAnnotations returnType, NullableFlowState operandState) 10842var result = TypeWithState.Create(delegateType, NullableFlowState.NotNull); 11139var rvalueResult = TypeWithState.Create(node.Type, NullableFlowState.NotNull); 11557var valueBeforeNullableWrapping = TypeWithState.Create(underlyingType.Type, NullableFlowState.NotNull); 11690SetResultType(node, TypeWithState.Create(receiverType.Type, NullableFlowState.NotNull)); 11714SetResultType(node, TypeWithState.Create(opType.Type, NullableFlowState.NotNull)); 11879SetResultType(node, TypeWithState.Create(receiverType.Type, NullableFlowState.NotNull)); 11907SetResultType(node, TypeWithState.Create(opType.Type, NullableFlowState.NotNull)); 12226var state = GetState(ref this.State, slot); 12248SetState(ref this.StateWhenTrue, slotToSet, NullableFlowState.NotNull); 12470if (enumeratorReturnType.State != NullableFlowState.NotNull) 12541var state = NullableFlowState.NotNull; 12745TypeWithState.Create(node.Type, node.OperatorKind.IsLifted() ? argumentResult.State : NullableFlowState.NotNull); 13018SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 13040SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.MaybeDefault)); 13055SetState(ref this.State, slot, NullableFlowState.NotNull); 13101NullableFlowState resultState = NullableFlowState.NotNull; 13116resultState = NullableFlowState.MaybeDefault; 13154SetResultType(node, TypeWithState.Create(node.Type, node.Type?.CanContainNull() != false && node.ConstantValueOpt?.IsNull == true ? NullableFlowState.MaybeDefault : NullableFlowState.NotNull)); 13262node.IsAddition ? GetState(ref this.State, memberSlot).Meet(ResultType.State) : NullableFlowState.MaybeNull); 13379private bool ReportPossibleNullReceiverIfNeeded(TypeSymbol type, NullableFlowState state, bool checkNullableValueType, SyntaxNode syntax, out bool reportedDiagnostic) 13438SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 13453SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 13502SetResultType(node, TypeWithState.Create(node.Type, NullableFlowState.NotNull)); 13508SetResultType(node, TypeWithState.Create(type, NullableFlowState.NotNull)); 13576SetState(ref this.State, slot, NullableFlowState.NotNull); 13718Debug.Assert(ResultType is TypeWithState { Type: FunctionPointerTypeSymbol { }, State: NullableFlowState.NotNull }); 14023SetValue(Id, index, useNotNullsAsDefault ? NullableFlowState.NotNull : walker.GetDefaultState(ref this, slot)); 14027public NullableFlowState this[int slot] 14041private NullableFlowState GetValue(int id, int index) 14051private NullableFlowState GetValue(int index) 14055return NullableFlowState.NotNull; 14062var result = (_state[index], _state[index + 1]) switch 14064(false, false) => NullableFlowState.NotNull, // Should not be reachable 14065(true, false) => NullableFlowState.MaybeNull, 14066(false, true) => NullableFlowState.MaybeDefault, 14067(true, true) => NullableFlowState.NotNull 14073private void SetValue(int id, int index, NullableFlowState value) 14086private void SetValue(int index, NullableFlowState value) 14094NullableFlowState.MaybeNull => (true, false), 14095NullableFlowState.MaybeDefault => (false, true), 14096NullableFlowState.NotNull => (true, true), 14150var oldValue = oldReachable ? GetValue(i) : NullableFlowState.NotNull; 14151var newValue = oldValue.Join(other.GetValue(i)); 14176var oldValue = GetValue(i); 14177var newValue = oldValue.Meet(other.GetValue(i)); 14193var mayBeNull = GetValue(i) is NullableFlowState.MaybeNull or NullableFlowState.MaybeDefault; 14224NullableFlowState.MaybeNull => "?", 14225NullableFlowState.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)
126949private static readonly NullableFlowState[] s_AllNullableFlowStates = (NullableFlowState[])Enum.GetValues(typeof(NullableFlowState)); 126970var inputs = new[] { NullableFlowState.NotNull, NullableFlowState.MaybeNull }; 126972Func<int, int, NullableFlowState> getResult = (i, j) => inputs[i].Join(inputs[j]); 126974var expected = new NullableFlowState[2, 2] 126976{ NullableFlowState.NotNull, NullableFlowState.MaybeNull }, 126977{ NullableFlowState.MaybeNull, NullableFlowState.MaybeNull }, 127002var inputs = new[] { NullableFlowState.NotNull, NullableFlowState.MaybeNull }; 127003Func<int, int, NullableFlowState> getResult = (i, j) => inputs[i].Meet(inputs[j]); 127005var expected = new NullableFlowState[2, 2] 127007{ NullableFlowState.NotNull, NullableFlowState.NotNull }, 127008{ NullableFlowState.NotNull, NullableFlowState.MaybeNull }, 127035private static void AssertEqual(NullableFlowState[,] expected, Func<int, int, NullableFlowState> getResult, int size) 127037AssertEx.Equal<NullableFlowState>(expected, getResult, (na1, na2) => na1 == na2, na => $"NullableFlowState.{na}", "{0,-32:G}", size); 127056foreach (var a in s_AllNullableFlowStates) 127058foreach (var b in s_AllNullableFlowStates) 127086foreach (var a in s_AllNullableFlowStates) 127088foreach (var b in s_AllNullableFlowStates) 127090foreach (var c in s_AllNullableFlowStates) 127092var leftFirst = a.Join(b).Join(c); 127093var rightFirst = a.Join(b.Join(c)); 127120foreach (var a in s_AllNullableFlowStates) 127122foreach (var b in s_AllNullableFlowStates) 127124foreach (var c in s_AllNullableFlowStates) 127126var leftFirst = a.Meet(b).Meet(c); 127127var rightFirst = a.Meet(b.Meet(c)); 127173foreach (var a in s_AllNullableFlowStates) 127175foreach (var b in s_AllNullableFlowStates) 127177var leftFirst = a.Join(b); 127178var rightFirst = b.Join(a); 127201foreach (var a in s_AllNullableFlowStates) 127203foreach (var b in s_AllNullableFlowStates) 127205var leftFirst = a.Meet(b); 127206var rightFirst = b.Meet(a);