diff --git a/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs b/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs index ed6e1a9cc2af0..d2a202b8ddbc7 100644 --- a/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs +++ b/src/Compilers/CSharp/Portable/CSharpResources.Designer.cs @@ -12634,24 +12634,6 @@ internal static string WRN_AnalyzerCannotBeCreated_Title { } } - /// - /// Looks up a localized string similar to The 'as' operator may produce a null value when '{0}' is a non-nullable reference type.. - /// - internal static string WRN_AsOperatorMayReturnNull { - get { - return ResourceManager.GetString("WRN_AsOperatorMayReturnNull", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The 'as' operator may produce a null value for a type parameter.. - /// - internal static string WRN_AsOperatorMayReturnNull_Title { - get { - return ResourceManager.GetString("WRN_AsOperatorMayReturnNull_Title", resourceCulture); - } - } - /// /// Looks up a localized string similar to Attribute '{0}' from module '{1}' will be ignored in favor of the instance appearing in source. /// @@ -13795,24 +13777,6 @@ internal static string WRN_ComparisonToSelf_Title { } } - /// - /// Looks up a localized string similar to Conditional access may produce a null value when '{0}' is a non-nullable reference type.. - /// - internal static string WRN_ConditionalAccessMayReturnNull { - get { - return ResourceManager.GetString("WRN_ConditionalAccessMayReturnNull", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Conditional access may produce a null value for a type parameter.. - /// - internal static string WRN_ConditionalAccessMayReturnNull_Title { - get { - return ResourceManager.GetString("WRN_ConditionalAccessMayReturnNull_Title", resourceCulture); - } - } - /// /// Looks up a localized string similar to Different checksum values given for '{0}'. /// @@ -13885,24 +13849,6 @@ internal static string WRN_DebugFullNameTooLong_Title { } } - /// - /// Looks up a localized string similar to A default expression introduces a null value when '{0}' is a non-nullable reference type.. - /// - internal static string WRN_DefaultExpressionMayIntroduceNullT { - get { - return ResourceManager.GetString("WRN_DefaultExpressionMayIntroduceNullT", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to A default expression introduces a null value for a type parameter.. - /// - internal static string WRN_DefaultExpressionMayIntroduceNullT_Title { - get { - return ResourceManager.GetString("WRN_DefaultExpressionMayIntroduceNullT_Title", resourceCulture); - } - } - /// /// Looks up a localized string similar to The default value specified for parameter '{0}' will have no effect because it applies to a member that is used in contexts that do not allow optional arguments. /// @@ -15754,24 +15700,6 @@ internal static string WRN_NullAsNonNullable_Title { } } - /// - /// Looks up a localized string similar to A null literal introduces a null value when '{0}' is a non-nullable reference type.. - /// - internal static string WRN_NullLiteralMayIntroduceNullT { - get { - return ResourceManager.GetString("WRN_NullLiteralMayIntroduceNullT", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to A null literal introduces a null value for a type parameter.. - /// - internal static string WRN_NullLiteralMayIntroduceNullT_Title { - get { - return ResourceManager.GetString("WRN_NullLiteralMayIntroduceNullT_Title", resourceCulture); - } - } - /// /// Looks up a localized string similar to Possible null reference argument for parameter '{0}' in '{1}'.. /// diff --git a/src/Compilers/CSharp/Portable/CSharpResources.resx b/src/Compilers/CSharp/Portable/CSharpResources.resx index b06d12164a68f..d4e2755b927cb 100644 --- a/src/Compilers/CSharp/Portable/CSharpResources.resx +++ b/src/Compilers/CSharp/Portable/CSharpResources.resx @@ -5396,30 +5396,6 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ Nullability of reference types in value doesn't match target type. - - Conditional access may produce a null value when '{0}' is a non-nullable reference type. - - - Conditional access may produce a null value for a type parameter. - - - The 'as' operator may produce a null value when '{0}' is a non-nullable reference type. - - - The 'as' operator may produce a null value for a type parameter. - - - A default expression introduces a null value when '{0}' is a non-nullable reference type. - - - A default expression introduces a null value for a type parameter. - - - A null literal introduces a null value when '{0}' is a non-nullable reference type. - - - A null literal introduces a null value for a type parameter. - Call to non-readonly member '{0}' from a 'readonly' member results in an implicit copy of '{1}'. diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs index e5d2405fbf9ac..d9f2065d786d4 100644 --- a/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs +++ b/src/Compilers/CSharp/Portable/Errors/ErrorCode.cs @@ -1660,7 +1660,7 @@ internal enum ErrorCode ERR_ExplicitNullableAttribute = 8623, WRN_NullabilityMismatchInArgumentForOutput = 8624, WRN_NullAsNonNullable = 8625, - WRN_AsOperatorMayReturnNull = 8626, + //WRN_AsOperatorMayReturnNull = 8626, ERR_NullableUnconstrainedTypeParameter = 8627, ERR_AnnotationDisallowedInObjectCreation = 8628, WRN_NullableValueTypeMayBeNull = 8629, @@ -1672,7 +1672,7 @@ internal enum ErrorCode ERR_TripleDotNotAllowed = 8635, ERR_BadNullableContextOption = 8636, ERR_NullableDirectiveQualifierExpected = 8637, - WRN_ConditionalAccessMayReturnNull = 8638, + //WRN_ConditionalAccessMayReturnNull = 8638, ERR_BadNullableTypeof = 8639, ERR_ExpressionTreeCantContainRefStruct = 8640, ERR_ElseCannotStartStatement = 8641, @@ -1687,8 +1687,8 @@ internal enum ErrorCode ERR_IsNullableType = 8650, ERR_AsNullableType = 8651, ERR_FeatureInPreview = 8652, - WRN_DefaultExpressionMayIntroduceNullT = 8653, - WRN_NullLiteralMayIntroduceNullT = 8654, + //WRN_DefaultExpressionMayIntroduceNullT = 8653, + //WRN_NullLiteralMayIntroduceNullT = 8654, WRN_SwitchExpressionNotExhaustiveForNull = 8655, WRN_ImplicitCopyInReadOnlyMember = 8656, diff --git a/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs b/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs index f4cf1dcdb2c62..26097f362ff44 100644 --- a/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs +++ b/src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs @@ -39,10 +39,6 @@ static ErrorFacts() builder.Add(getId(ErrorCode.WRN_NullabilityMismatchInTypeParameterNotNullConstraint)); builder.Add(getId(ErrorCode.WRN_ThrowPossibleNull)); builder.Add(getId(ErrorCode.WRN_UnboxPossibleNull)); - builder.Add(getId(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT)); - builder.Add(getId(ErrorCode.WRN_NullLiteralMayIntroduceNullT)); - builder.Add(getId(ErrorCode.WRN_ConditionalAccessMayReturnNull)); - builder.Add(getId(ErrorCode.WRN_AsOperatorMayReturnNull)); builder.Add(getId(ErrorCode.WRN_SwitchExpressionNotExhaustiveForNull)); builder.Add(getId(ErrorCode.WRN_ConvertingNullableToNonNullable)); @@ -421,10 +417,6 @@ internal static int GetWarningLevel(ErrorCode code) case ErrorCode.WRN_CaseConstantNamedUnderscore: case ErrorCode.WRN_ThrowPossibleNull: case ErrorCode.WRN_UnboxPossibleNull: - case ErrorCode.WRN_DefaultExpressionMayIntroduceNullT: - case ErrorCode.WRN_NullLiteralMayIntroduceNullT: - case ErrorCode.WRN_ConditionalAccessMayReturnNull: - case ErrorCode.WRN_AsOperatorMayReturnNull: case ErrorCode.WRN_SwitchExpressionNotExhaustiveForNull: case ErrorCode.WRN_ImplicitCopyInReadOnlyMember: case ErrorCode.WRN_UnconsumedEnumeratorCancellationAttributeUsage: diff --git a/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs b/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs index 53f2d13f0f274..f478332e9a592 100644 --- a/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs +++ b/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs @@ -767,7 +767,6 @@ private NullableFlowState GetDefaultState(ref LocalState state, int slot) { localType = local.TypeWithAnnotations; } - return localType.ToTypeWithState().State; } case SymbolKind.Parameter: @@ -777,8 +776,7 @@ private NullableFlowState GetDefaultState(ref LocalState state, int slot) { parameterType = parameter.TypeWithAnnotations; } - - return parameterType.ToTypeWithState().State; + return GetParameterState(parameterType, parameter.FlowAnalysisAnnotations).State; } case SymbolKind.Field: case SymbolKind.Property: @@ -1032,57 +1030,82 @@ private enum AssignmentKind /// /// Reports top-level nullability problem in assignment. + /// Any conversion of the value should have been applied. /// - private bool ReportNullableAssignmentIfNecessary( + private void ReportNullableAssignmentIfNecessary( BoundExpression value, TypeWithAnnotations targetType, TypeWithState valueType, bool useLegacyWarnings, AssignmentKind assignmentKind = AssignmentKind.Assignment, ParameterSymbol parameterOpt = null, - Conversion conversion = default, Location location = null) { + // Callers should apply any conversions before calling this method + // (see https://github.com/dotnet/roslyn/issues/39867). + if (targetType.HasType && + !targetType.Type.Equals(valueType.Type, TypeCompareKind.AllIgnoreOptions)) + { + return; + } + if (value == null || value.WasCompilerGenerated || !targetType.HasType || - targetType.Type.IsValueType || - targetType.CanBeAssignedNull || - valueType.IsNotNull) + targetType.Type.IsValueType) { - return false; + return; + } + + switch (targetType.NullableAnnotation) + { + case NullableAnnotation.Oblivious: + case NullableAnnotation.Annotated: + return; + } + + switch (valueType.State) + { + case NullableFlowState.NotNull: + return; + case NullableFlowState.MaybeNull: + if (targetType.Type.IsTypeParameterDisallowingAnnotation()) + { + return; + } + break; } location ??= value.Syntax.GetLocation(); var unwrappedValue = SkipReferenceConversions(value); if (unwrappedValue.IsSuppressed) { - return false; + return; } - if (RequiresSafetyWarningWhenNullIntroduced(targetType)) + if (useLegacyWarnings && + valueType.Type?.TypeKind == TypeKind.TypeParameter && + valueType.State == NullableFlowState.MaybeDefault) { - HashSet useSiteDiagnostics = null; - if (conversion.Kind == ConversionKind.UnsetConversionKind) - conversion = this._conversions.ClassifyImplicitConversionFromType(valueType.Type, targetType.Type, ref useSiteDiagnostics); - - if (conversion.IsImplicit && !conversion.IsDynamic) - { - // For type parameters that cannot be annotated, the analysis must report those - // places where null values first sneak in, like `default`, `null`, and `GetFirstOrDefault`, - // as a safety diagnostic. This is NOT one of those places. - return false; - } - - useLegacyWarnings = false; + // No W warning reported assigning or casting [MaybeNull]T value to T + // because there is no syntax for declaring the target type as [MaybeNull]T. + return; } - if (reportNullLiteralAssignmentIfNecessary(value, location, valueType.ToTypeWithAnnotations())) + if (value.ConstantValue?.IsNull == true) { - return true; + // Report warning converting null literal to non-nullable reference type. + // target (e.g.: `object x = null;` or calling `void F(object y)` with `F(null)`). + if (useLegacyWarnings) + { + ReportNonSafetyDiagnostic(location); + } + else + { + ReportDiagnostic(assignmentKind == AssignmentKind.Return ? ErrorCode.WRN_NullReferenceReturn : ErrorCode.WRN_NullAsNonNullable, location); + } } - - if (assignmentKind == AssignmentKind.Argument) + else if (assignmentKind == AssignmentKind.Argument) { ReportDiagnostic(ErrorCode.WRN_NullReferenceArgument, location, GetParameterAsDiagnosticArgument(parameterOpt), @@ -1096,31 +1119,6 @@ private bool ReportNullableAssignmentIfNecessary( { ReportDiagnostic(assignmentKind switch { AssignmentKind.Return => ErrorCode.WRN_NullReferenceReturn, AssignmentKind.ForEachIterationVariable => ErrorCode.WRN_NullReferenceIterationVariable, _ => ErrorCode.WRN_NullReferenceAssignment }, location); } - - return true; - - // Report warning converting null literal to non-nullable reference type. - // target (e.g.: `object x = null;` or calling `void F(object y)` with `F(null)`). - bool reportNullLiteralAssignmentIfNecessary(BoundExpression expr, Location location, TypeWithAnnotations exprType) - { - if (expr.ConstantValue?.IsNull != true) - { - return false; - } - - // For type parameters that cannot be annotated, the analysis must report those - // places where null values first sneak in, like `default`, `null`, and `GetFirstOrDefault`, - // as a safety diagnostic. This is one of those places. - if (useLegacyWarnings && !RequiresSafetyWarningWhenNullIntroduced(exprType)) - { - ReportNonSafetyDiagnostic(location); - } - else - { - ReportDiagnostic(assignmentKind == AssignmentKind.Return ? ErrorCode.WRN_NullReferenceReturn : ErrorCode.WRN_NullAsNonNullable, location); - } - return true; - } } private static bool IsDefaultValue(BoundExpression expr) @@ -1303,17 +1301,17 @@ private void InheritNullableStateOfMember(int targetContainerSlot, int valueCont /// /// Whenever assigning a variable, and that variable is not declared at the point the state is being set, - /// and the new state might be , this method should be called to perform the + /// and the new state is not , this method should be called to perform the /// state setting and to ensure the mutation is visible outside the finally block when the mutation occurs in a /// finally block. /// private void SetStateAndTrackForFinally(ref LocalState state, int slot, NullableFlowState newState) { state[slot] = newState; - if (newState == NullableFlowState.MaybeNull && NonMonotonicState.HasValue) + if (newState != NullableFlowState.NotNull && NonMonotonicState.HasValue) { var tryState = NonMonotonicState.Value; - tryState[slot] = NullableFlowState.MaybeNull; + tryState[slot] = newState.Join(tryState[slot]); NonMonotonicState = tryState; } } @@ -1404,7 +1402,8 @@ private void EnterParameter(ParameterSymbol parameter, TypeWithAnnotations param Debug.Assert(!IsConditionalState); if (slot > 0) { - this.State[slot] = parameterType.ToTypeWithState().State; + var state = GetParameterState(parameterType, parameter.FlowAnalysisAnnotations).State; + this.State[slot] = state; if (EmptyStructTypeCache.IsTrackableStructType(parameterType.Type)) { InheritNullableStateOfTrackableStruct( @@ -1416,6 +1415,13 @@ private void EnterParameter(ParameterSymbol parameter, TypeWithAnnotations param } } + private static TypeWithState GetParameterState(TypeWithAnnotations parameterType, FlowAnalysisAnnotations parameterAnnotations) + { + return ((parameterAnnotations & FlowAnalysisAnnotations.AllowNull) != 0) ? + TypeWithState.Create(parameterType.Type, NullableFlowState.MaybeDefault) : + parameterType.ToTypeWithState(); + } + protected override BoundNode VisitReturnStatementNoAdjust(BoundReturnStatement node) { Debug.Assert(!IsConditionalState); @@ -1483,7 +1489,8 @@ private bool TryGetReturnType(out TypeWithAnnotations type) return false; } - var returnType = (_delegateInvokeMethod ?? method).ReturnTypeWithAnnotations; + var delegateOrMethod = _delegateInvokeMethod ?? method; + var returnType = delegateOrMethod.ReturnTypeWithAnnotations; Debug.Assert((object)returnType != LambdaSymbol.ReturnTypeIsBeingInferred); if (returnType.IsVoidType()) @@ -1494,7 +1501,7 @@ private bool TryGetReturnType(out TypeWithAnnotations type) if (!method.IsAsync) { - type = returnType; + type = ApplyUnconditionalAnnotations(returnType, delegateOrMethod.ReturnTypeFlowAnalysisAnnotations); return true; } @@ -1508,21 +1515,6 @@ private bool TryGetReturnType(out TypeWithAnnotations type) return false; } - private static bool RequiresSafetyWarningWhenNullIntroduced(TypeWithAnnotations typeWithAnnotations) - { - return - typeWithAnnotations is { Type: TypeSymbol type, NullableAnnotation: NullableAnnotation.NotAnnotated } && - type.IsTypeParameterDisallowingAnnotation() && - !type.IsNullableTypeOrTypeParameter(); - } - - private static bool RequiresSafetyWarningWhenNullIntroduced(TypeSymbol type) - { - return - type.IsTypeParameterDisallowingAnnotation() && - !type.IsNullableTypeOrTypeParameter(); - } - public override BoundNode VisitLocal(BoundLocal node) { var local = node.LocalSymbol; @@ -1538,7 +1530,7 @@ public override BoundNode VisitLocal(BoundLocal node) type = TypeWithAnnotations.Create(node.Type, type.NullableAnnotation); } - SetResult(node, GetAdjustedResult(type, slot), type); + SetResult(node, GetAdjustedResult(type.ToTypeWithState(), slot), type); return null; } @@ -2891,16 +2883,9 @@ public override BoundNode VisitConditionalAccess(BoundConditionalAccess node) oldType.IsNullableType() && !accessType.IsNullableType() ? MakeNullableOf(accessTypeWithAnnotations) : accessType; - // If the result type does not allow annotations, then we produce a warning because - // the result may be null. - if (RequiresSafetyWarningWhenNullIntroduced(resultType)) - { - ReportDiagnostic(ErrorCode.WRN_ConditionalAccessMayReturnNull, node.Syntax, accessType); - } - // Per LDM 2019-02-13 decision, the result of a conditional access "may be null" even if // both the receiver and right-hand-side are believed not to be null. - SetResultType(node, TypeWithState.Create(resultType, NullableFlowState.MaybeNull)); + SetResultType(node, TypeWithState.Create(resultType, NullableFlowState.MaybeDefault)); _currentConditionalReceiverVisitResult = default; _lastConditionalAccessSlot = previousConditionalAccessSlot; return null; @@ -3104,7 +3089,7 @@ TypeWithState convertResult( } } - bool IsReachable() + private bool IsReachable() => this.IsConditionalState ? (this.StateWhenTrue.Reachable || this.StateWhenFalse.Reachable) : this.State.Reachable; /// @@ -3400,7 +3385,7 @@ private FlowAnalysisAnnotations GetParameterAnnotations(ParameterSymbol paramete } /// - /// Fix a TypeWithAnnotations based on Maybe/NotNull annotations prior to a conversion or assignment. + /// Fix a TypeWithAnnotations based on Allow/DisallowNull annotations prior to a conversion or assignment. /// Note this does not work for nullable value types, so an additional check with may be required. /// private static TypeWithAnnotations ApplyLValueAnnotations(TypeWithAnnotations declaredType, FlowAnalysisAnnotations flowAnalysisAnnotations) @@ -3424,7 +3409,7 @@ private static TypeWithState ApplyUnconditionalAnnotations(TypeWithState typeWit { if ((annotations & FlowAnalysisAnnotations.MaybeNull) == FlowAnalysisAnnotations.MaybeNull) { - return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeNull); + return TypeWithState.Create(typeWithState.Type, NullableFlowState.MaybeDefault); } if ((annotations & FlowAnalysisAnnotations.NotNull) == FlowAnalysisAnnotations.NotNull) @@ -3435,6 +3420,16 @@ private static TypeWithState ApplyUnconditionalAnnotations(TypeWithState typeWit return typeWithState; } + private static TypeWithAnnotations ApplyUnconditionalAnnotations(TypeWithAnnotations declaredType, FlowAnalysisAnnotations annotations) + { + if ((annotations & FlowAnalysisAnnotations.MaybeNull) == FlowAnalysisAnnotations.MaybeNull) + { + return declaredType.AsAnnotated(); + } + + return declaredType; + } + // https://github.com/dotnet/roslyn/issues/29863 Record in the node whether type // arguments were implicit, to allow for cases where the syntax is not an // invocation (such as a synthesized call from a query interpretation). @@ -4478,11 +4473,6 @@ private static bool UseExpressionForConversion(BoundExpression value) /// /// Adjust declared type based on inferred nullability at the point of reference. /// - private TypeWithState GetAdjustedResult(TypeWithAnnotations type, int slot) - { - return GetAdjustedResult(type.ToTypeWithState(), slot); - } - private TypeWithState GetAdjustedResult(TypeWithState type, int slot) { if (slot > 0 && slot < this.State.Capacity) @@ -4645,7 +4635,7 @@ public override BoundNode VisitConversion(BoundConversion node) operandType, checkConversion: true, fromExplicitCast: fromExplicitCast, - useLegacyWarnings: fromExplicitCast && !RequiresSafetyWarningWhenNullIntroduced(explicitType), + useLegacyWarnings: fromExplicitCast, AssignmentKind.Assignment, reportTopLevelWarnings: fromExplicitCast, reportRemainingWarnings: true, @@ -5154,14 +5144,17 @@ private TypeWithState VisitConversion( case ConversionKind.ExplicitDynamic: case ConversionKind.ImplicitDynamic: + resultState = getConversionResultState(operandType); + break; + case ConversionKind.Boxing: - resultState = operandType.State; + resultState = getBoxingConversionResultState(operandType); break; case ConversionKind.Unboxing: if (targetType.IsNonNullableValueType()) { - if (operandType.MayBeNull && reportRemainingWarnings) + if (!operandType.IsNotNull && reportRemainingWarnings) { ReportDiagnostic(ErrorCode.WRN_UnboxPossibleNull, diagnosticLocationOpt); } @@ -5170,7 +5163,7 @@ private TypeWithState VisitConversion( } else { - resultState = operandType.State; + resultState = getUnboxingConversionResultState(operandType); } break; @@ -5179,19 +5172,11 @@ private TypeWithState VisitConversion( break; case ConversionKind.NoConversion: - resultState = operandType.State; + resultState = getConversionResultState(operandType); break; case ConversionKind.NullLiteral: case ConversionKind.DefaultLiteral: - if (checkConversion && RequiresSafetyWarningWhenNullIntroduced(targetTypeWithNullability) && !isSuppressed) - { - // For type parameters that cannot be annotated, the analysis must report those - // places where null values first sneak in, like `default`, `null`, and `GetFirstOrDefault`. - // This is one of those places. - ReportDiagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, diagnosticLocationOpt, GetTypeAsDiagnosticArgument(targetTypeWithNullability.Type)); - } - checkConversion = false; goto case ConversionKind.Identity; @@ -5222,20 +5207,6 @@ private TypeWithState VisitConversion( goto case ConversionKind.ImplicitReference; case ConversionKind.ImplicitReference: - if (reportTopLevelWarnings && - conversionOperand?.Kind == BoundKind.Literal && - conversionOperand.ConstantValue?.IsNull == true && - !conversionOperand.WasCompilerGenerated && - !isSuppressed && - RequiresSafetyWarningWhenNullIntroduced(targetTypeWithNullability)) - { - // For type parameters that cannot be annotated, the analysis must report those - // places where null values first sneak in, like `default`, `null`, and `GetFirstOrDefault`. - // This is one of those places. - ReportDiagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, diagnosticLocationOpt, targetType); - } - goto case ConversionKind.ExplicitReference; - case ConversionKind.ExplicitReference: // Inherit state from the operand. if (checkConversion) @@ -5244,7 +5215,7 @@ private TypeWithState VisitConversion( canConvertNestedNullability = conversion.Exists; } - resultState = operandType.State; + resultState = conversion.IsReference ? getReferenceConversionResultState(targetTypeWithNullability, operandType) : operandType.State; break; case ConversionKind.ImplicitNullable: @@ -5345,16 +5316,7 @@ private TypeWithState VisitConversion( // Need to report all warnings that apply since the warnings can be suppressed individually. if (reportTopLevelWarnings) { - if (RequiresSafetyWarningWhenNullIntroduced(targetTypeWithNullability) && conversion.IsImplicit && !conversion.IsDynamic) - { - // For type parameters that cannot be annotated, the analysis must report those - // places where null values first sneak in, like `default`, `null`, and `GetFirstOrDefault`, - // as a safety diagnostic. But we do not warn when such values flow through implicit conversion. - } - else - { - ReportNullableAssignmentIfNecessary(conversionOperand, targetTypeWithNullability, operandType, useLegacyWarnings, assignmentKind, parameterOpt, conversion, diagnosticLocationOpt); - } + ReportNullableAssignmentIfNecessary(conversionOperand, targetTypeWithNullability, resultType, useLegacyWarnings, assignmentKind, parameterOpt, diagnosticLocationOpt); } if (reportRemainingWarnings && !canConvertNestedNullability) { @@ -5388,6 +5350,63 @@ static TypeWithState calculateResultType(TypeWithAnnotations targetTypeWithNulla var resultType = TypeWithState.Create(targetType, resultState); return resultType; } + + static NullableFlowState getReferenceConversionResultState(TypeWithAnnotations targetType, TypeWithState operandType) + { + var state = operandType.State; + switch (state) + { + case NullableFlowState.MaybeNull: + if (operandType.Type?.IsTypeParameterDisallowingAnnotation() != true && + targetType.Type?.IsTypeParameterDisallowingAnnotation() == true) + { + return NullableFlowState.MaybeDefault; + } + break; + case NullableFlowState.MaybeDefault: + if (targetType.Type?.IsTypeParameterDisallowingAnnotation() == false) + { + return NullableFlowState.MaybeNull; + } + break; + } + return state; + } + + // Converting to a less-derived type (object, interface, type parameter). + // If the operand is MaybeNull or MaybeDefault, the result should be + // MaybeNull (if the target type allows) or MaybeDefault otherwise. + static NullableFlowState getBoxingConversionResultState(TypeWithState operandType) + { + return getConversionResultState(operandType); + } + + // Converting to a more-derived type (struct, class, type parameter). + // If the operand is MaybeNull or MaybeDefault, the result should be + // MaybeDefault. + static NullableFlowState getUnboxingConversionResultState(TypeWithState operandType) + { + var state = operandType.State; + if (state == NullableFlowState.MaybeNull) + { + return NullableFlowState.MaybeDefault; + } + return state; + } + + static NullableFlowState getConversionResultState(TypeWithState operandType) + { + var state = operandType.State; + if (state == NullableFlowState.MaybeNull) + { + var type = operandType.Type; + if (type is null || !type.IsTypeParameterDisallowingAnnotation()) + { + return NullableFlowState.MaybeDefault; + } + } + return state; + } } private TypeWithState VisitUserDefinedConversion( @@ -5469,14 +5488,7 @@ private TypeWithState VisitUserDefinedConversion( // method parameter type -> method return type var methodReturnType = methodOpt.ReturnTypeWithAnnotations; operandType = GetLiftedReturnTypeIfNecessary(isLiftedConversion, methodReturnType, operandState); - if (isLiftedConversion) - { - if (RequiresSafetyWarningWhenNullIntroduced(methodReturnType) && operandState == NullableFlowState.MaybeNull) - { - ReportNullableAssignmentIfNecessary(conversionOpt, targetTypeWithNullability, operandType, useLegacyWarnings: useLegacyWarnings, assignmentKind, parameterOpt, conversion, diagnosticLocation); - } - } - else + if (!isLiftedConversion) { // Analyze operator call return value (honoring [Maybe|NotNull] attribute annotations) https://github.com/dotnet/roslyn/issues/32671 } @@ -5884,8 +5896,9 @@ public override BoundNode VisitParameter(BoundParameter node) { var parameter = node.ParameterSymbol; int slot = GetOrCreateSlot(parameter); - var type = GetDeclaredParameterResult(parameter); - SetResult(node, GetAdjustedResult(type, slot), type); + var parameterType = GetDeclaredParameterResult(parameter); + var typeWithState = GetParameterState(parameterType, parameter.FlowAnalysisAnnotations); + SetResult(node, GetAdjustedResult(typeWithState, slot), parameterType); return null; } @@ -6011,10 +6024,10 @@ private static bool UseLegacyWarnings(BoundExpression expr, TypeWithAnnotations switch (expr.Kind) { case BoundKind.Local: - return expr.GetRefKind() == RefKind.None && !RequiresSafetyWarningWhenNullIntroduced(exprType); + return expr.GetRefKind() == RefKind.None; case BoundKind.Parameter: RefKind kind = ((BoundParameter)expr).ParameterSymbol.RefKind; - return kind == RefKind.None && !RequiresSafetyWarningWhenNullIntroduced(exprType); + return kind == RefKind.None; default: return false; } @@ -6663,7 +6676,8 @@ private Symbol VisitMemberAccess(BoundExpression node, BoundExpression receiverO } var type = member.GetTypeOrReturnType(); - var resultType = type.ToTypeWithState(); + var memberAnnotations = GetRValueAnnotations(member); + var resultType = ApplyUnconditionalAnnotations(type.ToTypeWithState(), memberAnnotations); // We are supposed to track information for the node. Use whatever we managed to // accumulate so far. @@ -6690,7 +6704,7 @@ private Symbol VisitMemberAccess(BoundExpression node, BoundExpression receiverO if (_expressionIsRead) { - ReportMaybeNullFromTypeParameterValueIfNeeded(node, resultType, GetRValueAnnotations(member)); + ReportMaybeNullFromTypeParameterValueIfNeeded(node, resultType, memberAnnotations); } SetResult(node, resultType, type); @@ -6835,7 +6849,7 @@ protected override void VisitForEachExpression(BoundForEachStatement node) var getEnumeratorMethod = (MethodSymbol)AsMemberOfType(convertedResult.Type, node.EnumeratorInfoOpt.GetEnumeratorMethod); var enumeratorReturnType = getEnumeratorMethod.ReturnTypeWithAnnotations.ToTypeWithState(); - if (enumeratorReturnType.State == NullableFlowState.MaybeNull) + if (enumeratorReturnType.State != NullableFlowState.NotNull) { ReportDiagnostic(ErrorCode.WRN_NullReferenceReceiver, expr.Syntax.GetLocation()); } @@ -7277,16 +7291,6 @@ public override BoundNode VisitDefaultExpression(BoundDefaultExpression node) // https://github.com/dotnet/roslyn/issues/33344: this fails to produce an updated tuple type for a default expression // (should produce nullable element types for those elements that are of reference types) SetResultType(node, TypeWithState.ForType(type)); - if (node.TargetType is object && RequiresSafetyWarningWhenNullIntroduced(node.Type) && - !node.IsSuppressed) - { - // For type parameters that cannot be annotated, the analysis must report those - // places where null values first sneak in, like `default`, `null`, and `GetFirstOrDefault`. - // This is one of those places. - // `default(...)` is handled here to get a better behavior with an oblivious target type, while `default` is handled in VisitConversions. - ReportDiagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, node.Syntax, GetTypeAsDiagnosticArgument(ResultType.Type)); - } - return result; } @@ -7330,11 +7334,7 @@ public override BoundNode VisitAsOperator(BoundAsOperator node) break; default: - resultState = NullableFlowState.MaybeNull; - if (RequiresSafetyWarningWhenNullIntroduced(node.TargetType.TypeWithAnnotations)) - { - ReportDiagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, node.Syntax, type); - } + resultState = NullableFlowState.MaybeDefault; break; } } @@ -7373,7 +7373,7 @@ public override BoundNode VisitLiteral(BoundLiteral node) var result = base.VisitLiteral(node); Debug.Assert(!IsConditionalState); - SetResultType(node, TypeWithState.Create(node.Type, node.Type?.CanContainNull() != false && node.ConstantValue?.IsNull == true ? NullableFlowState.MaybeNull : NullableFlowState.NotNull)); + SetResultType(node, TypeWithState.Create(node.Type, node.Type?.CanContainNull() != false && node.ConstantValue?.IsNull == true ? NullableFlowState.MaybeDefault : NullableFlowState.NotNull)); if (node.ConstantValue?.IsBoolean == true) { @@ -7908,7 +7908,8 @@ internal class LocalState : ILocalState internal struct LocalState : ILocalState #endif { - // The representation of a state is a bit vector. We map false<->NotNull and true<->MayBeNull. + // The representation of a state is a bit vector with two bits per slot: + // (false, false) => NotNull, (false, true) => MaybeNull, (true, true) => MaybeDefault. // Slot 0 is used to represent whether the state is reachable (true) or not. private BitVector _state; @@ -7921,7 +7922,7 @@ public static LocalState ReachableState(int capacity) if (capacity < 1) capacity = 1; - BitVector state = BitVector.Create(capacity); + BitVector state = BitVector.Create(capacity * 2); state[0] = true; return new LocalState(state); } @@ -7930,22 +7931,41 @@ public static LocalState UnreachableState { get { - BitVector state = BitVector.Create(1); + BitVector state = BitVector.Create(2); state[0] = false; return new LocalState(state); } } - public int Capacity => _state.Capacity; + public int Capacity => _state.Capacity / 2; - public void EnsureCapacity(int capacity) => _state.EnsureCapacity(capacity); + public void EnsureCapacity(int capacity) => _state.EnsureCapacity(capacity * 2); public NullableFlowState this[int slot] { - get => (slot < Capacity && this.Reachable && _state[slot]) ? NullableFlowState.MaybeNull : NullableFlowState.NotNull; - - // No states should be modified in unreachable code, as there is only one unreachable state. - set => _ = !this.Reachable || (_state[slot] = (value == NullableFlowState.MaybeNull)); + get + { + if (slot < Capacity && this.Reachable) + { + slot *= 2; + return (_state[slot + 1], _state[slot]) switch + { + (false, false) => NullableFlowState.NotNull, + (false, true) => NullableFlowState.MaybeNull, + (true, false) => throw ExceptionUtilities.UnexpectedValue(slot), + (true, true) => NullableFlowState.MaybeDefault + }; + } + return NullableFlowState.NotNull; + } + set + { + // No states should be modified in unreachable code, as there is only one unreachable state. + if (!this.Reachable) return; + slot *= 2; + _state[slot] = (value != NullableFlowState.NotNull); + _state[slot + 1] = (value == NullableFlowState.MaybeDefault); + } } /// @@ -7963,8 +7983,9 @@ internal string GetDebuggerDisplay() var pooledBuilder = PooledStringBuilder.GetInstance(); var builder = pooledBuilder.Builder; builder.Append(" "); - for (int i = this.Capacity - 1; i >= 0; i--) - builder.Append(_state[i] ? '?' : '!'); + int n = Math.Min(Capacity, 8); + for (int i = n - 1; i >= 0; i--) + builder.Append(_state[i * 2] ? '?' : '!'); return pooledBuilder.ToStringAndFree(); } diff --git a/src/Compilers/CSharp/Portable/Generated/ErrorFacts.Generated.cs b/src/Compilers/CSharp/Portable/Generated/ErrorFacts.Generated.cs index 7eaa7dc4cde7e..cf0d3c93611a8 100644 --- a/src/Compilers/CSharp/Portable/Generated/ErrorFacts.Generated.cs +++ b/src/Compilers/CSharp/Portable/Generated/ErrorFacts.Generated.cs @@ -213,18 +213,14 @@ public static bool IsWarning(ErrorCode code) case ErrorCode.WRN_NullabilityMismatchInParameterTypeOfTargetDelegate: case ErrorCode.WRN_NullabilityMismatchInArgumentForOutput: case ErrorCode.WRN_NullAsNonNullable: - case ErrorCode.WRN_AsOperatorMayReturnNull: case ErrorCode.WRN_NullableValueTypeMayBeNull: case ErrorCode.WRN_NullabilityMismatchInTypeParameterConstraint: case ErrorCode.WRN_MissingNonNullTypesContextForAnnotation: case ErrorCode.WRN_NullabilityMismatchInConstraintsOnImplicitImplementation: case ErrorCode.WRN_NullabilityMismatchInTypeParameterReferenceTypeConstraint: - case ErrorCode.WRN_ConditionalAccessMayReturnNull: case ErrorCode.WRN_NullabilityMismatchInExplicitlyImplementedInterface: case ErrorCode.WRN_NullabilityMismatchInInterfaceImplementedByBase: case ErrorCode.WRN_DuplicateInterfaceWithNullabilityMismatchInBaseList: - case ErrorCode.WRN_DefaultExpressionMayIntroduceNullT: - case ErrorCode.WRN_NullLiteralMayIntroduceNullT: case ErrorCode.WRN_SwitchExpressionNotExhaustiveForNull: case ErrorCode.WRN_ImplicitCopyInReadOnlyMember: case ErrorCode.WRN_NullabilityMismatchInConstraintsOnPartialImplementation: diff --git a/src/Compilers/CSharp/Portable/Symbols/NullableFlowState.cs b/src/Compilers/CSharp/Portable/Symbols/NullableFlowState.cs index 3264942efa07d..30e87ee974448 100644 --- a/src/Compilers/CSharp/Portable/Symbols/NullableFlowState.cs +++ b/src/Compilers/CSharp/Portable/Symbols/NullableFlowState.cs @@ -9,7 +9,19 @@ namespace Microsoft.CodeAnalysis.CSharp /// internal enum NullableFlowState : byte { - NotNull, - MaybeNull + /// + /// Not null. + /// + NotNull = 0b00, + + /// + /// Maybe null (type is nullable). + /// + MaybeNull = 0b01, + + /// + /// Maybe null (type may be not nullable). + /// + MaybeDefault = 0b11, } } diff --git a/src/Compilers/CSharp/Portable/Symbols/NullableFlowStateExtensions.cs b/src/Compilers/CSharp/Portable/Symbols/NullableFlowStateExtensions.cs index 00978e7f52f97..8e09b3ab4834a 100644 --- a/src/Compilers/CSharp/Portable/Symbols/NullableFlowStateExtensions.cs +++ b/src/Compilers/CSharp/Portable/Symbols/NullableFlowStateExtensions.cs @@ -6,7 +6,7 @@ namespace Microsoft.CodeAnalysis.CSharp { internal static class NullableFlowStateExtensions { - public static bool MayBeNull(this NullableFlowState state) => state == NullableFlowState.MaybeNull; + public static bool MayBeNull(this NullableFlowState state) => state != NullableFlowState.NotNull; public static bool IsNotNull(this NullableFlowState state) => state == NullableFlowState.NotNull; @@ -28,6 +28,7 @@ internal static CodeAnalysis.NullableFlowState ToPublicFlowState(this CSharp.Nul { CSharp.NullableFlowState.NotNull => CodeAnalysis.NullableFlowState.NotNull, CSharp.NullableFlowState.MaybeNull => CodeAnalysis.NullableFlowState.MaybeNull, + CSharp.NullableFlowState.MaybeDefault => CodeAnalysis.NullableFlowState.MaybeNull, _ => throw ExceptionUtilities.UnexpectedValue(nullableFlowState) }; diff --git a/src/Compilers/CSharp/Portable/Symbols/TypeWithState.cs b/src/Compilers/CSharp/Portable/Symbols/TypeWithState.cs index b3c3916835a31..8cc3b5d9b8f45 100644 --- a/src/Compilers/CSharp/Portable/Symbols/TypeWithState.cs +++ b/src/Compilers/CSharp/Portable/Symbols/TypeWithState.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Diagnostics; -using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.CSharp.Symbols { @@ -19,13 +18,18 @@ internal readonly struct TypeWithState public static TypeWithState ForType(TypeSymbol type) { - var state = type?.CanContainNull() != false ? NullableFlowState.MaybeNull : NullableFlowState.NotNull; - return new TypeWithState(type, state); + return Create(type, NullableFlowState.MaybeDefault); } public static TypeWithState Create(TypeSymbol type, NullableFlowState defaultState) { - var state = defaultState == NullableFlowState.MaybeNull && type?.CanContainNull() != false ? NullableFlowState.MaybeNull : NullableFlowState.NotNull; + if (defaultState == NullableFlowState.MaybeDefault && + (type is null || type.IsTypeParameterDisallowingAnnotation())) + { + Debug.Assert(type?.IsNullableTypeOrTypeParameter() != true); + return new TypeWithState(type, defaultState); + } + var state = defaultState != NullableFlowState.NotNull && type?.CanContainNull() != false ? NullableFlowState.MaybeNull : NullableFlowState.NotNull; return new TypeWithState(type, state); } @@ -39,7 +43,7 @@ public static TypeWithState Create(TypeWithAnnotations typeWithAnnotations, Flow { if ((annotations & FlowAnalysisAnnotations.MaybeNull) == FlowAnalysisAnnotations.MaybeNull) { - state = NullableFlowState.MaybeNull; + state = NullableFlowState.MaybeDefault; } else if ((annotations & FlowAnalysisAnnotations.NotNull) == FlowAnalysisAnnotations.NotNull) { @@ -55,12 +59,13 @@ public static TypeWithState Create(TypeWithAnnotations typeWithAnnotations, Flow state = NullableFlowState.NotNull; } - return new TypeWithState(type, state); + return Create(type, state); } private TypeWithState(TypeSymbol type, NullableFlowState state) { Debug.Assert(state == NullableFlowState.NotNull || type?.CanContainNull() != false); + Debug.Assert(state != NullableFlowState.MaybeDefault || type is null || type.IsTypeParameterDisallowingAnnotation()); Type = type; State = state; } diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf index 301de31075750..2c6c13114d2b8 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf @@ -1334,16 +1334,6 @@ SyntaxTree není součástí kompilace, takže se nedá odebrat. - - The 'as' operator may produce a null value when '{0}' is a non-nullable reference type. - Operátor as může vytvořit hodnotu null, pokud {0} je odkazový typ, který nemůže mít hodnotu null. - - - - The 'as' operator may produce a null value for a type parameter. - Operátor as může pro parametr typu vytvořit hodnotu null. - - The name '_' refers to the constant, not the discard pattern. Use 'var _' to discard the value, or '@_' to refer to a constant by that name. Název „_“ odkazuje na konstantu, ne na vzor discard. Zadáním „var _“ hodnotu zahodíte a zadáním „@_“ nastavíte pod tímto názvem odkaz na konstantu. @@ -1354,16 +1344,6 @@ Nepoužívejte „_“ jako konstantu case. - - Conditional access may produce a null value when '{0}' is a non-nullable reference type. - Podmíněný přístup může vytvořit hodnotu null, pokud {0} je odkazový typ, který nemůže mít hodnotu null. - - - - Conditional access may produce a null value for a type parameter. - Podmíněný přístup může pro parametr typu vytvořit hodnotu null. - - Converting null literal or possible null value to non-nullable type. Literál s hodnotou null nebo s možnou hodnotou null se převádí na typ bez možnosti hodnoty null. @@ -1374,16 +1354,6 @@ Literál s hodnotou null nebo s možnou hodnotou null se převádí na typ bez možnosti hodnoty null. - - A default expression introduces a null value when '{0}' is a non-nullable reference type. - Výchozí výraz zavádí hodnotu null, pokud {0} je odkazový typ, který nemůže mít hodnotu null. - - - - A default expression introduces a null value for a type parameter. - Výchozí výraz zavádí pro parametr typu hodnotu null. - - A possible null value may not be assigned to a target marked with the [DisallowNull] attribute Do cíle označeného atributem [DisallowNull] se nedá předat možná hodnota null @@ -1474,16 +1444,6 @@ Literál null nejde převést na odkazový typ, který nemůže mít hodnotu null. - - A null literal introduces a null value when '{0}' is a non-nullable reference type. - Literál null zavádí hodnotu null, pokud {0} je odkazový typ, který nemůže mít hodnotu null. - - - - A null literal introduces a null value for a type parameter. - Literál null zavádí pro parametr typu hodnotu null. - - Possible null reference argument for parameter '{0}' in '{1}'. V parametru {0} v {1} může být argument s odkazem null. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf index efe063d472119..7f84d09a65d76 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf @@ -1334,16 +1334,6 @@ SyntaxTree ist kein Teil der Kompilierung und kann daher nicht entfernt werden. - - The 'as' operator may produce a null value when '{0}' is a non-nullable reference type. - Der as-Operator kann einen NULL-Wert generieren, wenn "{0}" ein Verweistyp ist, der keine NULL-Werte zulässt. - - - - The 'as' operator may produce a null value for a type parameter. - Der as-Operator kann einen NULL-Wert für einen Typparameter generieren. - - The name '_' refers to the constant, not the discard pattern. Use 'var _' to discard the value, or '@_' to refer to a constant by that name. Der Name "_" verweist auf die Konstante, nicht auf das discard-Muster. Verwenden Sie "var _" zum Verwerfen des Werts oder "@_" zum Verweis auf eine Konstante über diesen Namen. @@ -1354,16 +1344,6 @@ Verwenden Sie "_" nicht für eine case-Konstante. - - Conditional access may produce a null value when '{0}' is a non-nullable reference type. - Der bedingte Zugriff generiert möglicherweise einen NULL-Wert, wenn "{0}" ein Verweistyp ist, der keine NULL-Werte zulässt. - - - - Conditional access may produce a null value for a type parameter. - Der bedingte Zugriff generiert möglicherweise einen NULL-Wert für einen Typparameter. - - Converting null literal or possible null value to non-nullable type. Das NULL-Literal oder ein möglicher NULL-Wert wird in einen Nicht-Nullable-Typ konvertiert. @@ -1374,16 +1354,6 @@ Das NULL-Literal oder ein möglicher NULL-Wert wird in einen Nicht-Nullable-Typ konvertiert. - - A default expression introduces a null value when '{0}' is a non-nullable reference type. - Ein Standardausdruck führt einen NULL-Wert ein, wenn "{0}" ein Verweistyp ist, der keine NULL-Werte zulässt. - - - - A default expression introduces a null value for a type parameter. - Ein Standardausdruck führt einen NULL-Wert für einen Typparameter ein. - - A possible null value may not be assigned to a target marked with the [DisallowNull] attribute Ein möglicher NULL-Wert darf nicht an ein Ziel übergeben werden, das mit dem [DisallowNull]-Attribut markiert ist. @@ -1474,16 +1444,6 @@ Ein NULL-Literal kann nicht in einen Verweistyp konvertiert werden, der keine NULL-Werte zulässt. - - A null literal introduces a null value when '{0}' is a non-nullable reference type. - Ein NULL-Literal führt einen NULL-Wert ein, wenn "{0}" ein Verweistyp ist, der keine NULL-Werte zulässt. - - - - A null literal introduces a null value for a type parameter. - Ein NULL-Literal führt einen NULL-Wert für einen Typparameter ein. - - Possible null reference argument for parameter '{0}' in '{1}'. Mögliches Nullverweisargument für den Parameter "{0}" in "{1}". diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf index f7c6745971011..ff3c3330f783e 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf @@ -1335,16 +1335,6 @@ SyntaxTree no forma parte de la compilación, así que no se puede quitar - - The 'as' operator may produce a null value when '{0}' is a non-nullable reference type. - El operador "as" puede producir un valor NULL cuando "{0}" es un tipo de referencia que no acepta valores NULL. - - - - The 'as' operator may produce a null value for a type parameter. - El operador "as" puede generar un valor NULL para un parámetro de tipo. - - The name '_' refers to the constant, not the discard pattern. Use 'var _' to discard the value, or '@_' to refer to a constant by that name. El nombre "_" hace referencia a la constante, no al patrón de descarte. Use "var _" para descartar el valor o "@_" para hacer referencia a una constante con ese nombre. @@ -1355,16 +1345,6 @@ No use "_" para una constante de caso. - - Conditional access may produce a null value when '{0}' is a non-nullable reference type. - El acceso condicional puede producir un valor NULL cuando "{0}" es un tipo de referencia que no acepta valores NULL. - - - - Conditional access may produce a null value for a type parameter. - El acceso condicional puede generar un valor NULL para un parámetro de tipo. - - Converting null literal or possible null value to non-nullable type. Se va a convertir un literal nulo o un posible valor nulo en un tipo que no acepta valores NULL @@ -1375,16 +1355,6 @@ Se va a convertir un literal nulo o un posible valor nulo en un tipo que no acepta valores NULL - - A default expression introduces a null value when '{0}' is a non-nullable reference type. - Una expresión predeterminada introduce un valor NULL cuando "{0}" es un tipo de referencia que no acepta valores NULL. - - - - A default expression introduces a null value for a type parameter. - Una expresión predeterminada introduce un valor NULL para un parámetro de tipo. - - A possible null value may not be assigned to a target marked with the [DisallowNull] attribute No se puede pasar un posible valor NULL a un destino marcado con el atributo [DisallowNull] @@ -1475,16 +1445,6 @@ No se puede convertir un literal NULL en un tipo de referencia que no acepta valores NULL. - - A null literal introduces a null value when '{0}' is a non-nullable reference type. - Un literal NULL introduce un valor NULL cuando "{0}" es un tipo de referencia que no acepta valores NULL. - - - - A null literal introduces a null value for a type parameter. - Un literal NULL introduce un valor NULL para un parámetro de tipo. - - Possible null reference argument for parameter '{0}' in '{1}'. Posible argumento de referencia nulo para el parámetro "{0}" en "{1}". diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf index 3f0c495358965..9dd39adcbb9fc 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf @@ -1334,16 +1334,6 @@ SyntaxTree ne faisant pas partie de la compilation, il ne peut pas être supprimé - - The 'as' operator may produce a null value when '{0}' is a non-nullable reference type. - L'opérateur 'as' peut produire une valeur null quand '{0}' est un type référence non Nullable. - - - - The 'as' operator may produce a null value for a type parameter. - L'opérateur 'as' peut produire une valeur null pour un paramètre de type. - - The name '_' refers to the constant, not the discard pattern. Use 'var _' to discard the value, or '@_' to refer to a constant by that name. Le nom '_' fait référence à la constante, pas au modèle d'abandon. Utilisez 'var _' pour abandonner la valeur, ou '@_' pour faire référence à une constante par ce nom. @@ -1354,16 +1344,6 @@ N'utilisez pas '_' pour une constante case. - - Conditional access may produce a null value when '{0}' is a non-nullable reference type. - L'accès conditionnel peut produire une valeur null quand '{0}' est un type référence non Nullable. - - - - Conditional access may produce a null value for a type parameter. - L'accès conditionnel peut produire une valeur null pour un paramètre de type. - - Converting null literal or possible null value to non-nullable type. Conversion de littéral ayant une valeur null ou d'une éventuelle valeur null en type non Nullable. @@ -1374,16 +1354,6 @@ Conversion de littéral ayant une valeur null ou d'une éventuelle valeur null en type non Nullable. - - A default expression introduces a null value when '{0}' is a non-nullable reference type. - Une expression par défaut introduit une valeur null quand '{0}' est un type référence non Nullable. - - - - A default expression introduces a null value for a type parameter. - Une expression par défaut introduit une valeur null pour un paramètre de type. - - A possible null value may not be assigned to a target marked with the [DisallowNull] attribute Vous ne devez pas passer une possible valeur null à une cible marquée avec l'attribut [DisallowNull] @@ -1474,16 +1444,6 @@ Impossible de convertir un littéral ayant une valeur null en type référence non Nullable. - - A null literal introduces a null value when '{0}' is a non-nullable reference type. - Un littéral ayant une valeur null introduit une valeur null quand '{0}' est un type référence non Nullable. - - - - A null literal introduces a null value for a type parameter. - Un littéral ayant une valeur null introduit une valeur null pour un paramètre de type. - - Possible null reference argument for parameter '{0}' in '{1}'. Existence possible d'un argument de référence null pour le paramètre '{0}' dans '{1}'. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf index 9ac10140a2bf6..4b03ff97f3e38 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf @@ -1334,16 +1334,6 @@ L'elemento SyntaxTree non fa parte della compilazione, di conseguenza non può essere rimosso - - The 'as' operator may produce a null value when '{0}' is a non-nullable reference type. - L'operatore 'as' può produrre un valore Null quando '{0}' è un tipo riferimento che non ammette i valori Null. - - - - The 'as' operator may produce a null value for a type parameter. - L'operatore 'as' può produrre un valore Null per un parametro di tipo. - - The name '_' refers to the constant, not the discard pattern. Use 'var _' to discard the value, or '@_' to refer to a constant by that name. Il nome '_' fa riferimento alla costante e non al criterio di eliminazione. Usare 'var _' per eliminare il valore oppure '@_' per fare riferimento a una costante in base a tale nome. @@ -1354,16 +1344,6 @@ Non usare '_' per una costante di case. - - Conditional access may produce a null value when '{0}' is a non-nullable reference type. - L'accesso condizionale può produrre un valore Null quando '{0}' è un tipo riferimento che non ammette i valori Null. - - - - Conditional access may produce a null value for a type parameter. - L'accesso condizionale può produrre un valore Null per un parametro di tipo. - - Converting null literal or possible null value to non-nullable type. Conversione del valore letterale Null o di un possibile valore Null in tipo non nullable. @@ -1374,16 +1354,6 @@ Conversione del valore letterale Null o di un possibile valore Null in tipo non nullable. - - A default expression introduces a null value when '{0}' is a non-nullable reference type. - Un'espressione predefinita introduce un valore Null quando '{0}' è un tipo riferimento che non ammette i valori Null. - - - - A default expression introduces a null value for a type parameter. - Un'espressione predefinita introduce un valore Null per un parametro di tipo. - - A possible null value may not be assigned to a target marked with the [DisallowNull] attribute Un possibile valore Null non può essere passato a una destinazione contrassegnata con l'attributo [DisallowNull] @@ -1474,16 +1444,6 @@ Non è possibile convertire il valore letterale Null in tipo riferimento che non ammette i valori Null. - - A null literal introduces a null value when '{0}' is a non-nullable reference type. - Un valore letterale Null introduce un valore Null quando '{0}' è un tipo riferimento che non ammette i valori Null. - - - - A null literal introduces a null value for a type parameter. - Un valore letterale Null introduce un valore Null per un parametro di tipo. - - Possible null reference argument for parameter '{0}' in '{1}'. Possibile argomento di riferimento Null per il parametro '{0}' in '{1}'. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf index 46a89bde0bd71..fc253c3ea8c58 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf @@ -1334,16 +1334,6 @@ SyntaxTree はコンパイルの一部ではないため削除できません - - The 'as' operator may produce a null value when '{0}' is a non-nullable reference type. - '{0}' が NULL 非許容の参照型の場合、'as' 演算子は null 値を生成することがあります。 - - - - The 'as' operator may produce a null value for a type parameter. - 'as' 演算子は、型パラメーターに対して null 値を生成することがあります。 - - The name '_' refers to the constant, not the discard pattern. Use 'var _' to discard the value, or '@_' to refer to a constant by that name. 名前 '_' は、破棄パターンではなく定数を参照しています。値を破棄する場合には 'var _' を、そのような名前の定数を参照する場合には '@_' を使用します。 @@ -1354,16 +1344,6 @@ case 定数に '_' を使用しないでください。 - - Conditional access may produce a null value when '{0}' is a non-nullable reference type. - 条件付きアクセスでは、'{0}' が NULL 非許容の参照型の場合、null 値が生成されることがあります。 - - - - Conditional access may produce a null value for a type parameter. - 条件付きアクセスでは、型パラメーターに null 値が生成されることがあります。 - - Converting null literal or possible null value to non-nullable type. Null リテラルまたは Null の可能性がある値を Null 非許容型に変換しています。 @@ -1374,16 +1354,6 @@ Null リテラルまたは Null の可能性がある値を Null 非許容型に変換しています。 - - A default expression introduces a null value when '{0}' is a non-nullable reference type. - '{0}' が null 非許容参照型である場合、既定の式は null 値を導入します。 - - - - A default expression introduces a null value for a type parameter. - 既定の式は、型パラメーターに null 値を導入します。 - - A possible null value may not be assigned to a target marked with the [DisallowNull] attribute [DisallowNull] 属性でマークされたターゲットに Null の可能性がある値を渡すことはできません @@ -1474,16 +1444,6 @@ null リテラルを null 非許容参照型に変換できません。 - - A null literal introduces a null value when '{0}' is a non-nullable reference type. - null リテラルは、'{0}' が null 非許容参照型である場合に null 値を導入します。 - - - - A null literal introduces a null value for a type parameter. - null リテラルは、型パラメーターに null 値を導入します。 - - Possible null reference argument for parameter '{0}' in '{1}'. '{1}' 内のパラメーター '{0}' に Null 参照引数がある可能性があります。 diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf index fbad2a17b5450..c0a0bca13436b 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf @@ -1334,16 +1334,6 @@ SyntaxTree는 컴파일의 일부가 아니므로 제거할 수 없습니다. - - The 'as' operator may produce a null value when '{0}' is a non-nullable reference type. - '{0}'이(가) nullable이 아닌 참조 형식이면 'as' 연산자가 null 값을 생성할 수 있습니다. - - - - The 'as' operator may produce a null value for a type parameter. - 'as' 연산자가 형식 매개 변수의 null 값을 생성할 수 있습니다. - - The name '_' refers to the constant, not the discard pattern. Use 'var _' to discard the value, or '@_' to refer to a constant by that name. '_' 이름은 상수를 참조하며, 무시 패턴은 참조하지 않습니다. 'var _'을 사용하여 값을 무시하거나 '@_'을 사용하여 해당 이름별 상수를 참조하세요. @@ -1354,16 +1344,6 @@ case 상수에 '_'을 사용하지 마세요. - - Conditional access may produce a null value when '{0}' is a non-nullable reference type. - '{0}'이(가) nullable이 아닌 참조 형식이면 조건부 액세스가 null 값을 생성할 수 있습니다. - - - - Conditional access may produce a null value for a type parameter. - 조건부 액세스가 형식 매개 변수의 null 값을 생성할 수 있습니다. - - Converting null literal or possible null value to non-nullable type. null 리터럴 또는 가능한 null 값을 nullable이 아닌 형식으로 변환하는 중입니다. @@ -1374,16 +1354,6 @@ null 리터럴 또는 가능한 null 값을 nullable이 아닌 형식으로 변환하는 중입니다. - - A default expression introduces a null value when '{0}' is a non-nullable reference type. - '{0}'이(가) nullable이 아닌 참조 형식이면 기본 식이 null 값을 지정합니다. - - - - A default expression introduces a null value for a type parameter. - 기본 식이 형식 매개 변수의 null 값을 지정합니다. - - A possible null value may not be assigned to a target marked with the [DisallowNull] attribute 가능한 null 값은 [DisallowNull] 특성으로 표시된 대상에 전달할 수 없음 @@ -1474,16 +1444,6 @@ Null 리터럴을 nullable이 아닌 참조 형식으로 변환할 수 없습니다. - - A null literal introduces a null value when '{0}' is a non-nullable reference type. - '{0}'이(가) nullable이 아닌 참조 형식이면 null 리터럴이 null 값을 지정합니다. - - - - A null literal introduces a null value for a type parameter. - null 리터럴이 형식 매개 변수의 null 값을 지정합니다. - - Possible null reference argument for parameter '{0}' in '{1}'. '{1}'의 매개 변수 '{0}'에 대한 가능한 null 참조 인수입니다. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf index 39ffad0032bc7..f73ada97f79d9 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf @@ -1334,16 +1334,6 @@ Element SyntaxTree nie jest częścią kompilacji, więc nie można go usunąć - - The 'as' operator may produce a null value when '{0}' is a non-nullable reference type. - Operator „as” może wygenerować wartość null, jeśli element „{0}” jest typem referencyjnym niedopuszczającym wartości null. - - - - The 'as' operator may produce a null value for a type parameter. - Operator „as” może wygenerować wartość null dla parametru typu. - - The name '_' refers to the constant, not the discard pattern. Use 'var _' to discard the value, or '@_' to refer to a constant by that name. Nazwa „_” odwołuje się do stałej, a nie do wzorca odrzucania. Użyj elementu „var _”, aby odrzucić wartość, lub użyj elementu „@_”, aby odwołać się do stałej za pomocą tej nazwy. @@ -1354,16 +1344,6 @@ Nie używaj elementu „_” dla stałej case. - - Conditional access may produce a null value when '{0}' is a non-nullable reference type. - Dostęp warunkowy może wygenerować wartość null, jeśli element „{0}” jest typem referencyjnym niedopuszczającym wartości null. - - - - Conditional access may produce a null value for a type parameter. - Dostęp warunkowy może wygenerować wartość null dla parametru typu. - - Converting null literal or possible null value to non-nullable type. Konwertowanie literału o wartości null lub możliwej wartości null na typ niedopuszczający wartości null. @@ -1374,16 +1354,6 @@ Konwertowanie literału o wartości null lub możliwej wartości null na typ niedopuszczający wartości null. - - A default expression introduces a null value when '{0}' is a non-nullable reference type. - Wyrażenie domyślne wprowadza wartość null, jeśli element „{0}” jest typem referencyjnym niedopuszczającym wartości null. - - - - A default expression introduces a null value for a type parameter. - Wyrażenie domyślne wprowadza wartość null dla parametru typu. - - A possible null value may not be assigned to a target marked with the [DisallowNull] attribute Możliwa wartość null nie może być przekazywana do elementu docelowego oznaczonego atrybutem [DisallowNull] @@ -1474,16 +1444,6 @@ Nie można przekonwertować literału o wartości null na typ referencyjny niedopuszczający wartości null. - - A null literal introduces a null value when '{0}' is a non-nullable reference type. - Literał o wartości null wprowadza wartość null, jeśli element „{0}” jest typem referencyjnym niedopuszczającym wartości null. - - - - A null literal introduces a null value for a type parameter. - Literał o wartości null wprowadza wartość null dla parametru typu. - - Possible null reference argument for parameter '{0}' in '{1}'. Możliwy argument odwołania o wartości null dla parametru „{0}” w „{1}”. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf index 5c0f011ef540b..c82e2a020108b 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf @@ -1334,16 +1334,6 @@ A SyntaxTree não faz parte da compilação, portanto, não pode ser removida - - The 'as' operator may produce a null value when '{0}' is a non-nullable reference type. - O operador 'as' pode produzir um valor nulo quando '{0}' é um tipo de referência que não permite valor nulo. - - - - The 'as' operator may produce a null value for a type parameter. - O operador 'as' pode produzir um valor nulo para um parâmetro de tipo. - - The name '_' refers to the constant, not the discard pattern. Use 'var _' to discard the value, or '@_' to refer to a constant by that name. O nome '_' refere-se à constante, não ao padrão de descarte. Use 'var _' para descartar o valor ou '@_' para referir-se a uma constante por esse nome. @@ -1354,16 +1344,6 @@ Não use '_' para uma constante de caso. - - Conditional access may produce a null value when '{0}' is a non-nullable reference type. - O acesso condicional pode produzir um valor nulo quando '{0}' é um tipo de referência que não permite valor nulo. - - - - Conditional access may produce a null value for a type parameter. - O acesso condicional pode produzir um valor nulo para um parâmetro de tipo. - - Converting null literal or possible null value to non-nullable type. Conversão de literal nula ou possível valor nulo em tipo não nulo. @@ -1374,16 +1354,6 @@ Conversão de literal nula ou possível valor nulo em tipo não nulo. - - A default expression introduces a null value when '{0}' is a non-nullable reference type. - Uma expressão padrão introduz um valor nulo quando '{0}' é um tipo de referência que não permite valor nulo. - - - - A default expression introduces a null value for a type parameter. - Uma expressão padrão introduz um valor nulo para um parâmetro de tipo. - - A possible null value may not be assigned to a target marked with the [DisallowNull] attribute Um possível valor nulo não pode ser passado para um destino marcado com o atributo [DisallowNull] @@ -1474,16 +1444,6 @@ Não é possível converter um literal nulo em um tipo de referência que não permite valor nulo. - - A null literal introduces a null value when '{0}' is a non-nullable reference type. - Um literal nulo introduz um valor nulo quando '{0}' é um tipo de referência que não permite valor nulo. - - - - A null literal introduces a null value for a type parameter. - Um literal nulo introduz um valor nulo para um parâmetro de tipo. - - Possible null reference argument for parameter '{0}' in '{1}'. Possível argumento de referência nula para o parâmetro '{0}' em '{1}'. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf index 4c525e8eb3307..c3ce9bcd5abc8 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf @@ -1334,16 +1334,6 @@ SyntaxTree не входит в компиляцию, поэтому его невозможно удалить - - The 'as' operator may produce a null value when '{0}' is a non-nullable reference type. - Оператор as может создавать значение NULL, если "{0}" является ссылочным типом, не допускающим значение NULL. - - - - The 'as' operator may produce a null value for a type parameter. - Оператор as может создавать значение NULL для параметра типа. - - The name '_' refers to the constant, not the discard pattern. Use 'var _' to discard the value, or '@_' to refer to a constant by that name. Имя "_" ссылается на константу, а не на шаблон отмены. Используйте "var _", чтобы отменить значение, или "@_", чтобы сослаться на константу по этому имени. @@ -1354,16 +1344,6 @@ Не используйте "_" для константы case. - - Conditional access may produce a null value when '{0}' is a non-nullable reference type. - Условный доступ может создавать значение NULL, если "{0}" является ссылочным типом, не допускающим значение NULL. - - - - Conditional access may produce a null value for a type parameter. - Условный доступ может создавать значение NULL для параметра типа. - - Converting null literal or possible null value to non-nullable type. Преобразование литерала, допускающего значение NULL или возможного значения NULL в тип, не допускающий значение NULL. @@ -1374,16 +1354,6 @@ Преобразование литерала, допускающего значение NULL или возможного значения NULL в тип, не допускающий значение NULL. - - A default expression introduces a null value when '{0}' is a non-nullable reference type. - Выражение по умолчанию вводит значение NULL, если "{0}" является ссылочным типом, не допускающим значение NULL. - - - - A default expression introduces a null value for a type parameter. - Выражение по умолчанию вводит значение NULL для параметра типа. - - A possible null value may not be assigned to a target marked with the [DisallowNull] attribute Возможное значение NULL не может быть передано целевому объекту, помеченному атрибутом [DisallowNull] @@ -1474,16 +1444,6 @@ Литерал, равный NULL, не может быть преобразован в ссылочный тип, не допускающий значение NULL. - - A null literal introduces a null value when '{0}' is a non-nullable reference type. - Литерал NULL вводит значение NULL, если "{0}" является ссылочным типом, не допускающим значение NULL. - - - - A null literal introduces a null value for a type parameter. - Литерал NULL вводит значение NULL для параметра типа. - - Possible null reference argument for parameter '{0}' in '{1}'. Возможно, аргумент-ссылка, допускающий значение NULL, для параметра "{0}" в "{1}". diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf index 2b88c024f68fe..2200dcbca1223 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf @@ -1334,16 +1334,6 @@ SyntaxTree derlemenin bir parçası olmadığından kaldırılamaz - - The 'as' operator may produce a null value when '{0}' is a non-nullable reference type. - '{0}' boş değer atanamayan bir başvuru türü olduğunda 'as' işleci null bir değer üretebilir. - - - - The 'as' operator may produce a null value for a type parameter. - 'as' işleci bir tür parametresi için null bir değer üretebilir. - - The name '_' refers to the constant, not the discard pattern. Use 'var _' to discard the value, or '@_' to refer to a constant by that name. '_' adı atma desenine değil sabite başvuruyor. Değeri atmak için 'var _' adını ya da bu ada sahip bir sabite başvurmak için '@_' adını kullanın. @@ -1354,16 +1344,6 @@ Bir case sabiti için '_' adını kullanmayın. - - Conditional access may produce a null value when '{0}' is a non-nullable reference type. - '{0}' boş değer atanamayan bir başvuru türü olduğunda koşullu erişim null bir değer üretebilir. - - - - Conditional access may produce a null value for a type parameter. - Koşullu erişim, tür parametresi için null bir değer üretebilir. - - Converting null literal or possible null value to non-nullable type. Null sabit değeri veya olası null değeri, boş değer atanamaz türe dönüştürülüyor. @@ -1374,16 +1354,6 @@ Null sabit değeri veya olası null değeri, boş değer atanamaz türe dönüştürülüyor. - - A default expression introduces a null value when '{0}' is a non-nullable reference type. - '{0}' boş değer atanamayan bir başvuru türü olduğunda varsayılan bir ifade null değer sağlar. - - - - A default expression introduces a null value for a type parameter. - Varsayılan bir ifade, bir tür parametresi için null değer sağlar. - - A possible null value may not be assigned to a target marked with the [DisallowNull] attribute Olası bir null değer, [DisallowNull] özniteliğiyle işaretlenmiş bir hedefe geçirilemez @@ -1474,16 +1444,6 @@ Null sabit değer, boş değer atanamayan başvuru türüne dönüştürülemiyor. - - A null literal introduces a null value when '{0}' is a non-nullable reference type. - '{0}' boş değer atanamayan bir başvuru türü olduğunda null sabit değer, bir null değer sağlar. - - - - A null literal introduces a null value for a type parameter. - Null sabit değer, tür parametresi için bir null değer sağlar. - - Possible null reference argument for parameter '{0}' in '{1}'. '{1}' içindeki '{0}' parametresi için olası null başvuru bağımsız değişkeni. diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf index 7503df260c04e..04d14156810ea 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf @@ -1334,16 +1334,6 @@ 编译中不包含 SyntaxTree,因此无法将其删除 - - The 'as' operator may produce a null value when '{0}' is a non-nullable reference type. - 当 "{0}" 是不可以为 null 的引用类型时,"as" 运算符可能会生成 null 值。 - - - - The 'as' operator may produce a null value for a type parameter. - "as" 运算符可能会为类型参数生成 null 值。 - - The name '_' refers to the constant, not the discard pattern. Use 'var _' to discard the value, or '@_' to refer to a constant by that name. 名称 "_" 引用常量,而不引用放弃模式。请使用 "var _" 放弃该值,或使用 "@_" 来引用该名称的常量。 @@ -1354,16 +1344,6 @@ 不要对大小写常量使用 "_"。 - - Conditional access may produce a null value when '{0}' is a non-nullable reference type. - 当 "{0}" 是不可以为 null 的引用类型时,条件访问可能会生成 null 值。 - - - - Conditional access may produce a null value for a type parameter. - 条件访问可能会为类型参数生成 null 值。 - - Converting null literal or possible null value to non-nullable type. 将 null 文本或可能的 null 值转换为非 null 类型。 @@ -1374,16 +1354,6 @@ 将 null 文本或可能的 null 值转换为非 null 类型。 - - A default expression introduces a null value when '{0}' is a non-nullable reference type. - 当 "{0}" 是不可以为 null 的引用类型时,默认表达式就会引入 null 值。 - - - - A default expression introduces a null value for a type parameter. - 默认表达式为类型参数引入了 null 值。 - - A possible null value may not be assigned to a target marked with the [DisallowNull] attribute 可能的 null 值可能不会传递到用 [DisallowNull] 属性标记的目标 @@ -1474,16 +1444,6 @@ 无法将 null 文本转换为不可为 null 的引用类型。 - - A null literal introduces a null value when '{0}' is a non-nullable reference type. - 当 "{0}" 是不可以为 null 的引用类型时, null 文本就会引入 null 值。 - - - - A null literal introduces a null value for a type parameter. - null 文本为类型参数引入了 null 值。 - - Possible null reference argument for parameter '{0}' in '{1}'. “{1}”中“{0}”形参的可能的 null 引用实参。 diff --git a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf index 31be7fab29761..7d126ccb9c88b 100644 --- a/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf +++ b/src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf @@ -1334,16 +1334,6 @@ 因為 SyntaxTree 不屬於編譯的一部份,所以無法將其移除 - - The 'as' operator may produce a null value when '{0}' is a non-nullable reference type. - 當 '{0}' 是不可為 null 的參考型別時,'as' 運算子可能會產生 null 值。 - - - - The 'as' operator may produce a null value for a type parameter. - 對於型別參數,'as' 運算子可能會產生 null 值。 - - The name '_' refers to the constant, not the discard pattern. Use 'var _' to discard the value, or '@_' to refer to a constant by that name. 名稱 '_' 參考常數而非捨棄模式。請使用 'var _' 來捨棄值,或使用 '@_' 來依該名稱參考常數。 @@ -1354,16 +1344,6 @@ 不可對 case 常數使用 '_'。 - - Conditional access may produce a null value when '{0}' is a non-nullable reference type. - 當 '{0}' 為不可為 null 的參考型別時,條件式存取可能會產生 null 值。 - - - - Conditional access may produce a null value for a type parameter. - 條件式存取可能會針對型別參數產生 null 值。 - - Converting null literal or possible null value to non-nullable type. 正在將 Null 常值或可能的 Null 值轉換為不可為 Null 的型別。 @@ -1374,16 +1354,6 @@ 正在將 Null 常值或可能的 Null 值轉換為不可為 Null 的型別。 - - A default expression introduces a null value when '{0}' is a non-nullable reference type. - 當 '{0}' 為不可為 null 的參考型別時,預設運算式會引入 null 值。 - - - - A default expression introduces a null value for a type parameter. - 預設運算式會針對型別參數引入 null 值。 - - A possible null value may not be assigned to a target marked with the [DisallowNull] attribute 可能為 null 的值,不能傳遞到標記了 [DisallowNull] 屬性的目標 @@ -1474,16 +1444,6 @@ 無法將 null 常值轉換成不可為 Null 的參考型別。 - - A null literal introduces a null value when '{0}' is a non-nullable reference type. - 當 '{0}' 為不可為 null 的參考型別時,null 常值會引入 null 值。 - - - - A null literal introduces a null value for a type parameter. - null 常值會針對型別參數引入 null 值。 - - Possible null reference argument for parameter '{0}' in '{1}'. '{1}' 中的參數 '{0}' 可能有 Null 參考引數。 diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs index b06486354a380..428a65a682234 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs @@ -91,7 +91,7 @@ private CSharpCompilation CreateNullableCompilation(CSharpTestSource source, IEn return CreateCompilation(source, options: WithNonNullTypesTrue(), references: references); } - [Fact, WorkItem(37362, "https://github.com/dotnet/roslyn/issues/37362")] + [Fact] public void DefaultLiteralInConditional() { var comp = CreateNullableCompilation(@" @@ -99,18 +99,13 @@ class C { public void M(bool condition, T t) { - t = default; // 1 - _ = condition ? t : default; // 2 - _ = condition ? default : t; // 3 + t = default; + _ = condition ? t : default; + _ = condition ? default : t; } }"); - // Missing warnings 2 and 3. https://github.com/dotnet/roslyn/issues/37362 - comp.VerifyDiagnostics( - // (6,13): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // t = default; // 1 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(6, 13) - ); + comp.VerifyDiagnostics(); } [Fact, WorkItem(33982, "https://github.com/dotnet/roslyn/issues/33982")] @@ -2685,17 +2680,14 @@ static void M(ref T t, dynamic? d) } static void M2(T t, dynamic? d) { - t = d; // 2 + t = d; } }"; var comp = CreateCompilation(source, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( // (6,13): warning CS8601: Possible null reference assignment. // t = d; // 1 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "d").WithLocation(6, 13), - // (10,13): warning CS8601: Possible null reference assignment. - // t = d; // 2 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "d").WithLocation(10, 13) + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "d").WithLocation(6, 13) ); } @@ -4231,9 +4223,9 @@ void M(T t) where T : class? }"; var comp = CreateCompilationWithIndexAndRange(source, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( - // (7,29): warning CS8654: A null literal introduces a null value when 'T' is a non-nullable reference type. + // (7,29): warning CS8625: Cannot convert null literal to non-nullable reference type. // var t2 = new[] { t, null }; // 1 - Diagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, "null").WithArguments("T").WithLocation(7, 29), + Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(7, 29), // (8,9): warning CS8602: Dereference of a possibly null reference. // t2[0].ToString(); // warn // 2 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t2[0]").WithLocation(8, 9) @@ -4521,15 +4513,9 @@ void M4(string x4, object? y4) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,14): warning CS8601: Possible null reference assignment. - // x1 = (T)y1; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)y1").WithLocation(6, 14), // (11,14): error CS0266: Cannot implicitly convert type 'object' to 'T'. An explicit conversion exists (are you missing a cast?) // x2 = y2; Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "y2").WithArguments("object", "T").WithLocation(11, 14), - // (11,14): warning CS8601: Possible null reference assignment. - // x2 = y2; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "y2").WithLocation(11, 14), // (16,14): warning CS8600: Converting null literal or possible null value to non-nullable type. // x3 = (string)y3; Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(string)y3").WithLocation(16, 14), @@ -13096,7 +13082,6 @@ class B : IA var ia = compilation.GetTypeByMetadataName("IA"); var b = compilation.GetTypeByMetadataName("B"); - var member = ia.GetMember("M1"); var implementing = (MethodSymbol)b.FindImplementationForInterfaceMember(member); var implemented = member.ConstructIfGeneric(implementing.TypeParameters.SelectAsArray(t => TypeWithAnnotations.Create(t))); @@ -17489,7 +17474,6 @@ void I.M(T value) Assert.False(tp.IsNullableType()); } - [Fact] public void PartialMethods_01() { @@ -21430,19 +21414,14 @@ static void M(C c, Base b) [Fact] public void MaybeNull_Indexer_Implementation() { - // Should we enforce nullability annotation attributes inside method bodies? https://github.com/dotnet/roslyn/issues/36039 var source = @"using System.Diagnostics.CodeAnalysis; public class CClass where TClass : class { - [MaybeNull]public TClass this[int i] { get => null; } // 1, maybe we shouldn't warn here + [MaybeNull]public TClass this[int i] { get => null; } }"; var comp = CreateNullableCompilation(new[] { MaybeNullAttributeDefinition, source }); - comp.VerifyDiagnostics( - // (4,51): warning CS8603: Possible null reference return. - // [MaybeNull]public TClass this[int i] { get => null; } // 1, maybe we shouldn't warn here - Diagnostic(ErrorCode.WRN_NullReferenceReturn, "null").WithLocation(4, 51) - ); + comp.VerifyDiagnostics(); } [Fact] @@ -29480,10 +29459,9 @@ static void M7([System.Diagnostics.CodeAnalysis.NotNull]T? t7) where T : clas } static void M8([System.Diagnostics.CodeAnalysis.AllowNull]T t8) where T : class { - new CClass().f2 = t8; // no warning, we currently don't honor the attribute on t6 within the method body + new CClass().f2 = t8; // 11 } }"; - // we currently don't honor the attribute on t6 within the method body https://github.com/dotnet/roslyn/issues/36039 var expected = new[] { @@ -29516,7 +29494,10 @@ static void M8([System.Diagnostics.CodeAnalysis.AllowNull]T t8) where T : cla Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t5").WithLocation(36, 31), // (47,30): warning CS8601: Possible null reference assignment. // new CClass().f2 = t7; // 10 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "t7").WithLocation(47, 30) + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "t7").WithLocation(47, 30), + // (51,30): warning CS8601: Possible null reference assignment. + // new CClass().f2 = t8; // 11 + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "t8").WithLocation(51, 30) }; var comp = CreateNullableCompilation(new[] { source, AllowNullAttributeDefinition, MaybeNullAttributeDefinition, NotNullAttributeDefinition }, @@ -38384,12 +38365,12 @@ static void F3() where T : struct }"; var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( - // (6,24): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (6,24): warning CS8601: Possible null reference assignment. // a1 = new T[] { default }; // 1 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(6, 24), - // (7,24): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(6, 24), + // (7,24): warning CS8601: Possible null reference assignment. // a1 = new T[] { default(T) }; // 2 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(7, 24), + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default(T)").WithLocation(7, 24), // (12,24): warning CS8625: Cannot convert null literal to non-nullable reference type. // a2 = new T[] { null }; // 3 Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(12, 24), @@ -43788,10 +43769,9 @@ public long this[long x] " }, options: WithNonNullTypesTrue()); c.VerifyDiagnostics( - // (22,22): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (22,22): warning CS8603: Possible null reference return. // get { return default(T); } - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(22, 22) - ); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T)").WithLocation(22, 22)); } [Fact] @@ -44177,10 +44157,9 @@ public long M1(long x) " }, options: WithNonNullTypesTrue()); c.VerifyDiagnostics( - // (22,16): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (22,16): warning CS8603: Possible null reference return. // return default(T); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(22, 16) - ); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T)").WithLocation(22, 16)); } [Fact] @@ -45793,15 +45772,9 @@ static void F() new[] { source }, options: WithNonNullTypesTrue(), parseOptions: TestOptions.Regular8); comp.VerifyDiagnostics( - // (5,17): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // var s = default(T); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(5, 17), // (6,9): warning CS8602: Dereference of a possibly null reference. // s.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s").WithLocation(6, 9), - // (7,17): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // var t = default(T?); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T?)").WithArguments("T").WithLocation(7, 17), // (7,25): error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint. // var t = default(T?); Diagnostic(ErrorCode.ERR_NullableUnconstrainedTypeParameter, "T?").WithLocation(7, 25), @@ -45946,11 +45919,7 @@ static void F() var comp = CreateCompilation( new[] { source }, options: WithNonNullTypesTrue(), parseOptions: TestOptions.Regular8); - // https://github.com/dotnet/roslyn/issues/29895: Improve this diagnostic. default is the cause of the error, but is not mentioned in the diagnostic. comp.VerifyDiagnostics( - // (5,15): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. - // T s = default; - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(5, 15), // (6,9): warning CS8602: Dereference of a possibly null reference. // s.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s").WithLocation(6, 9) @@ -50014,15 +49983,15 @@ System.Collections.Generic.IEnumerable M2() where T : class? } }"; CreateCompilation(new[] { source }, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,22): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (6,22): warning CS8603: Possible null reference return. // yield return default; // 1 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(6, 22), + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default").WithLocation(6, 22), // (10,22): warning CS8603: Possible null reference return. // yield return default; // 2 Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default").WithLocation(10, 22), - // (14,22): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (14,22): warning CS8603: Possible null reference return. // yield return default; // 3 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(14, 22) + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default").WithLocation(14, 22) ); } @@ -55345,7 +55314,6 @@ static bool G(out object o) comp.VerifyDiagnostics(); } - [Fact, WorkItem(31370, "https://github.com/dotnet/roslyn/issues/31370")] public void SuppressNullableWarning_Deconstruction() { @@ -55585,7 +55553,6 @@ static void M((string, C) t, C c) CompileAndVerify(comp); } - [Fact] public void SuppressNullableWarning_ValueType_01() { @@ -55760,9 +55727,6 @@ static void F3(T3 t3) where T3 : struct }"; var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( - // (5,9): warning CS8653: A default expression introduces a null value when 'T1' is a non-nullable reference type. - // default(T1).ToString(); // 1 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T1)").WithArguments("T1").WithLocation(5, 9), // (5,9): warning CS8602: Dereference of a possibly null reference. // default(T1).ToString(); // 1 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "default(T1)").WithLocation(5, 9), @@ -56650,12 +56614,12 @@ static void G6() where T : U // (33,28): warning CS8625: Cannot convert null literal to non-nullable reference type. // F6(default); Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(33, 28), - // (39,15): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (39,15): warning CS8604: Possible null reference argument for parameter 't' in 'void P.F0(T t = default(T))'. // F0(default); // 0 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(39, 15), - // (41,18): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "default").WithArguments("t", "void P.F0(T t = default(T))").WithLocation(39, 15), + // (41,18): warning CS8604: Possible null reference argument for parameter 't' in 'void P.F6(T t = default(T))'. // F6(default); // 0 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(41, 18), + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "default").WithArguments("t", "void P.F6(T t = default(T))").WithLocation(41, 18), // (46,15): warning CS8625: Cannot convert null literal to non-nullable reference type. // F0(default); // 1 Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(46, 15), @@ -56665,15 +56629,15 @@ static void G6() where T : U // (50,18): warning CS8625: Cannot convert null literal to non-nullable reference type. // F6(default); // 1 Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(50, 18), - // (66,15): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (66,15): warning CS8604: Possible null reference argument for parameter 't' in 'void P.F0(T t = default(T))'. // F0(default); // 3 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(66, 15), - // (69,15): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "default").WithArguments("t", "void P.F0(T t = default(T))").WithLocation(66, 15), + // (69,15): warning CS8604: Possible null reference argument for parameter 't' in 'void P.F3(T t = default(T))'. // F3(default); // 3 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(69, 15), - // (72,18): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "default").WithArguments("t", "void P.F3(T t = default(T))").WithLocation(69, 15), + // (72,18): warning CS8604: Possible null reference argument for parameter 't' in 'void P.F6(T t = default(T))'. // F6(default); // 3 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(72, 18), + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "default").WithArguments("t", "void P.F6(T t = default(T))").WithLocation(72, 18), // (78,15): warning CS8625: Cannot convert null literal to non-nullable reference type. // F0(default); // 4 Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(78, 15), @@ -56689,21 +56653,21 @@ static void G6() where T : U // (86,18): warning CS8625: Cannot convert null literal to non-nullable reference type. // F6(default); // 4 Diagnostic(ErrorCode.WRN_NullAsNonNullable, "default").WithLocation(86, 18), - // (91,15): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (91,15): warning CS8604: Possible null reference argument for parameter 't' in 'void P.F0(T t = default(T))'. // F0(default); // 5 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(91, 15), - // (93,15): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "default").WithArguments("t", "void P.F0(T t = default(T))").WithLocation(91, 15), + // (93,15): warning CS8604: Possible null reference argument for parameter 't' in 'void P.F5(T t = default(T))'. // F5(default); // 5 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(93, 15), - // (95,18): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "default").WithArguments("t", "void P.F5(T t = default(T))").WithLocation(93, 15), + // (95,18): warning CS8604: Possible null reference argument for parameter 't' in 'void P.F6(T t = default(T))'. // F6(default); // 5 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(95, 18), - // (100,15): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "default").WithArguments("t", "void P.F6(T t = default(T))").WithLocation(95, 18), + // (100,15): warning CS8604: Possible null reference argument for parameter 't' in 'void P.F0(T t = default(T))'. // F0(default); // 6 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(100, 15), - // (102,18): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "default").WithArguments("t", "void P.F0(T t = default(T))").WithLocation(100, 15), + // (102,18): warning CS8604: Possible null reference argument for parameter 't' in 'void P.F6(T t = default(T))'. // F6(default); // 6 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(102, 18) + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "default").WithArguments("t", "void P.F6(T t = default(T))").WithLocation(102, 18) ); // No warnings with C#7.3. @@ -56898,9 +56862,6 @@ class Program // (4,37): error CS0266: Cannot implicitly convert type 'T' to 'System.Collections.Generic.IEnumerator'. An explicit conversion exists (are you missing a cast?) // static IEnumerator M() => default(T); Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "default(T)").WithArguments("T", "System.Collections.Generic.IEnumerator").WithLocation(4, 37), - // (4,37): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // static IEnumerator M() => default(T); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(4, 37), // (4,37): warning CS8603: Possible null reference return. // static IEnumerator M() => default(T); Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T)").WithLocation(4, 37) @@ -57067,17 +57028,14 @@ public class C { static void M1(I? i) { - T t = (T)i; // 1 - _ = i.ToString(); // 2 + T t = (T)i; + _ = i.ToString(); // 1 } }"; var comp = CreateCompilation(source, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( - // (7,15): warning CS8601: Possible null reference assignment. - // T t = (T)i; // 1 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)i").WithLocation(7, 15), // (8,13): warning CS8602: Dereference of a possibly null reference. - // _ = i.ToString(); // 2 + // _ = i.ToString(); // 1 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "i").WithLocation(8, 13)); } @@ -57714,12 +57672,12 @@ class C // (6,39): warning CS8603: Possible null reference return. // static async Task F1() => null; Diagnostic(ErrorCode.WRN_NullReferenceReturn, "null").WithLocation(6, 39), - // (8,43): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (8,43): warning CS8603: Possible null reference return. // static async Task F3() { return default; } - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(8, 43), - // (9,43): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default").WithLocation(8, 43), + // (9,43): warning CS8603: Possible null reference return. // static async Task F4() { return default(T); } - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(9, 43), + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T)").WithLocation(9, 43), // (10,59): warning CS8603: Possible null reference return. // static async Task F5() where T : class { return null; } Diagnostic(ErrorCode.WRN_NullReferenceReturn, "null").WithLocation(10, 59), @@ -57729,10 +57687,9 @@ class C // (13,40): warning CS8603: Possible null reference return. // static async Task? G1() => null; Diagnostic(ErrorCode.WRN_NullReferenceReturn, "null").WithLocation(13, 40), - // (14,44): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (14,44): warning CS8603: Possible null reference return. // static async Task? G3() { return default; } - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(14, 44) - ); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default").WithLocation(14, 44)); } [Fact] @@ -57819,18 +57776,18 @@ static T M8() where T: C? }"; var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( - // (6,16): warning CS8654: A null literal introduces a null value when 'T' is a non-nullable reference type. + // (6,16): warning CS8603: Possible null reference return. // return (T)null; // 1 - Diagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, "(T)null").WithArguments("T").WithLocation(6, 16), - // (10,16): warning CS8654: A null literal introduces a null value when 'T' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)null").WithLocation(6, 16), + // (10,16): warning CS8603: Possible null reference return. // return (T)null; // 2 - Diagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, "(T)null").WithArguments("T").WithLocation(10, 16), - // (14,16): warning CS8654: A null literal introduces a null value when 'T' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)null").WithLocation(10, 16), + // (14,16): warning CS8603: Possible null reference return. // return null; // 3 - Diagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, "null").WithArguments("T").WithLocation(14, 16), - // (18,16): warning CS8654: A null literal introduces a null value when 'T' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "null").WithLocation(14, 16), + // (18,16): warning CS8603: Possible null reference return. // return null; // 4 - Diagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, "null").WithArguments("T").WithLocation(18, 16)); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "null").WithLocation(18, 16)); } [Fact] @@ -58426,18 +58383,9 @@ static void F5(U u) where U : T // (57,17): warning CS8619: Nullability of reference types in value of type 'C' doesn't match target type 'IOut'. // F4A((x, y)); // 20 Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "y").WithArguments("C", "IOut").WithLocation(57, 17), - // (64,26): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // (T, T) t5 = (u, default(T)); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(64, 26), // (65,31): warning CS8619: Nullability of reference types in value of type '(object?, object? u)' doesn't match target type '(object, object)'. // (object, object) v5 = (default(T), u); // 21 - Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "(default(T), u)").WithArguments("(object?, object? u)", "(object, object)").WithLocation(65, 31), - // (65,32): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // (object, object) v5 = (default(T), u); // 21 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(65, 32), - // (66,34): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // (object?, object?) w5 = (default(T), u); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(66, 34)); + Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "(default(T), u)").WithArguments("(object?, object? u)", "(object, object)").WithLocation(65, 31)); } [Fact] @@ -58510,18 +58458,9 @@ static void F5(U u) where U : T // (31,48): warning CS8619: Nullability of reference types in value of type 'C' doesn't match target type 'IOut'. // (IOut, IOut)? v4 = (x, y); // 8 Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "y").WithArguments("C", "IOut").WithLocation(31, 48), - // (36,27): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // (T, T)? t5 = (u, default(T)); // 9 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(36, 27), // (37,32): warning CS8619: Nullability of reference types in value of type '(object?, object? u)' doesn't match target type '(object, object)?'. // (object, object)? v5 = (default(T), u); // 10 - Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "(default(T), u)").WithArguments("(object?, object? u)", "(object, object)?").WithLocation(37, 32), - // (37,33): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // (object, object)? v5 = (default(T), u); // 10 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(37, 33), - // (38,35): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // (object?, object?)? w5 = (default(T), u); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(38, 35)); + Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "(default(T), u)").WithArguments("(object?, object? u)", "(object, object)?").WithLocation(37, 32)); } [Fact] @@ -58672,30 +58611,15 @@ static void F5(T t) where U : T // (31,52): warning CS8619: Nullability of reference types in value of type 'C' doesn't match target type 'IOut'. // var v4 = ((IOut, IOut))(x, y); // 8 Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "y").WithArguments("C", "IOut").WithLocation(31, 52), - // (36,27): warning CS8601: Possible null reference assignment. - // var t5 = ((U, U))(t, default(T)); // 9 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "t").WithLocation(36, 27), - // (36,30): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // var t5 = ((U, U))(t, default(T)); // 9 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(36, 30), - // (36,30): warning CS8601: Possible null reference assignment. - // var t5 = ((U, U))(t, default(T)); // 9 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default(T)").WithLocation(36, 30), // (37,18): warning CS8619: Nullability of reference types in value of type '(object?, object? t)' doesn't match target type '(object, object)'. // var v5 = ((object, object))(default(T), t); // 10 Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "((object, object))(default(T), t)").WithArguments("(object?, object? t)", "(object, object)").WithLocation(37, 18), - // (37,37): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // var v5 = ((object, object))(default(T), t); // 10 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(37, 37), // (37,37): warning CS8600: Converting null literal or possible null value to non-nullable type. // var v5 = ((object, object))(default(T), t); // 10 Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "default(T)").WithLocation(37, 37), // (37,49): warning CS8600: Converting null literal or possible null value to non-nullable type. // var v5 = ((object, object))(default(T), t); // 10 - Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "t").WithLocation(37, 49), - // (38,39): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // var w5 = ((object?, object?))(default(T), t); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(38, 39)); + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "t").WithLocation(37, 49)); } [Fact] @@ -58774,30 +58698,15 @@ static void F5(T t) where U : T // (31,53): warning CS8619: Nullability of reference types in value of type 'C' doesn't match target type 'IOut'. // var v4 = ((IOut, IOut)?)(x, y); // 8 Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "y").WithArguments("C", "IOut").WithLocation(31, 53), - // (36,28): warning CS8601: Possible null reference assignment. - // var t5 = ((U, U)?)(t, default(T)); // 9 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "t").WithLocation(36, 28), - // (36,31): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // var t5 = ((U, U)?)(t, default(T)); // 9 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(36, 31), - // (36,31): warning CS8601: Possible null reference assignment. - // var t5 = ((U, U)?)(t, default(T)); // 9 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default(T)").WithLocation(36, 31), // (37,18): warning CS8619: Nullability of reference types in value of type '(object?, object? t)' doesn't match target type '(object, object)?'. // var v5 = ((object, object)?)(default(T), t); // 10 Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "((object, object)?)(default(T), t)").WithArguments("(object?, object? t)", "(object, object)?").WithLocation(37, 18), - // (37,38): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // var v5 = ((object, object)?)(default(T), t); // 10 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(37, 38), // (37,38): warning CS8600: Converting null literal or possible null value to non-nullable type. // var v5 = ((object, object)?)(default(T), t); // 10 Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "default(T)").WithLocation(37, 38), // (37,50): warning CS8600: Converting null literal or possible null value to non-nullable type. // var v5 = ((object, object)?)(default(T), t); // 10 - Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "t").WithLocation(37, 50), - // (38,40): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // var w5 = ((object?, object?)?)(default(T), t); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(38, 40)); + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "t").WithLocation(37, 50)); } [Fact] @@ -63303,8 +63212,8 @@ static void F2() { C c2; c2 = new C() { F = default }; // 4 - c2 = new C() { F = c2.F }; - c2.F.ToString(); // 5 + c2 = new C() { F = c2.F }; // 5 + c2.F.ToString(); // 6 } }"; var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue()); @@ -63318,11 +63227,14 @@ static void F2() // (12,9): warning CS8602: Dereference of a possibly null reference. // c1.F.ToString(); // 3 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c1.F").WithLocation(12, 9), - // (17,31): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (17,31): warning CS8601: Possible null reference assignment. // c2 = new C() { F = default }; // 4 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(17, 31), + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(17, 31), + // (18,31): warning CS8601: Possible null reference assignment. + // c2 = new C() { F = c2.F }; // 5 + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "c2.F").WithLocation(18, 31), // (19,9): warning CS8602: Dereference of a possibly null reference. - // c2.F.ToString(); // 5 + // c2.F.ToString(); // 6 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "c2.F").WithLocation(19, 9)); } @@ -64989,9 +64901,9 @@ class Enumerable where T : IEnumerator? // (22,27): warning CS8602: Dereference of a possibly null reference. // foreach (var i in enumerable1) // 3 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "enumerable1").WithLocation(22, 27), - // (40,13): warning CS8653: A default expression introduces a null value when 'TEnumerator' is a non-nullable reference type. - // e = default; // 4 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("TEnumerator").WithLocation(40, 13), + // (41,34): warning CS8604: Possible null reference argument for parameter 't' in 'Enumerable C.Create(TEnumerator t)'. + // var enumerable2 = Create(e); + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "e").WithArguments("t", "Enumerable C.Create(TEnumerator t)").WithLocation(41, 34), // (50,27): warning CS8602: Dereference of a possibly null reference. // foreach (var i in enumerable1) // 5 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "enumerable1").WithLocation(50, 27), @@ -67486,9 +67398,9 @@ static void F(C a, bool c) }"; var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( - // (4,24): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (4,24): warning CS8601: Possible null reference assignment. // internal T field = default; - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(4, 24), + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(4, 24), // (8,16): warning CS8602: Dereference of a possibly null reference. // if (c) a.field.ToString(); // 1 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a.field").WithLocation(8, 16), @@ -70133,9 +70045,9 @@ static void F3() where T : struct, I }"; var comp = CreateCompilation(source, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( - // (9,16): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // T x1 = default; - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(9, 16), + // (12,34): warning CS8601: Possible null reference assignment. + // T y1 = new T() { P = x1 }; + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x1").WithLocation(12, 34), // (18,16): warning CS8600: Converting null literal or possible null value to non-nullable type. // T x2 = default; Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "default").WithLocation(18, 16), @@ -70346,62 +70258,49 @@ static void F1(T t1) where T : A static void F2(T t2) where T : A? { t2.ToString(); // 2 - t2 = default; // 3 + t2 = default; } static void F3(T t3) where T : I { t3.P.ToString(); - t3 = default; // 6 + t3 = default; } static void F4(T t4) where T : I? { - t4.P.ToString(); // 7 and 8 - t4.P = default; // 9 - t4 = default; // 10 + t4.P.ToString(); // 3 and 4 + t4.P = default; // 5 + t4 = default; } static void F5(T t5) where T : I { - t5.P.ToString(); // 11 and 12 + t5.P.ToString(); // 6 and 7 t5.P = default; - t5 = default; // 15 + t5 = default; } }"; var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue()); - // https://github.com/dotnet/roslyn/issues/29978: Various differences from expected warnings. comp.VerifyDiagnostics( - // (29,41): error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint. - // static void F5(T t5) where T : I - Diagnostic(ErrorCode.ERR_NullableUnconstrainedTypeParameter, "T?").WithLocation(29, 41), // (11,14): warning CS8600: Converting null literal or possible null value to non-nullable type. // t1 = default; // 1 Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "default").WithLocation(11, 14), // (15,9): warning CS8602: Dereference of a possibly null reference. // t2.ToString(); // 2 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t2").WithLocation(15, 9), - // (16,14): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // t2 = default; // 3 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(16, 14), - // (21,14): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // t3 = default; // 6 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(21, 14), // (25,9): warning CS8602: Dereference of a possibly null reference. - // t4.P.ToString(); // 7 and 8 + // t4.P.ToString(); // 3 and 4 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t4").WithLocation(25, 9), // (25,9): warning CS8602: Dereference of a possibly null reference. - // t4.P.ToString(); // 7 and 8 + // t4.P.ToString(); // 3 and 4 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t4.P").WithLocation(25, 9), - // (26,16): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // t4.P = default; // 9 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(26, 16), - // (27,14): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // t4 = default; // 10 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(27, 14), + // (26,16): warning CS8601: Possible null reference assignment. + // t4.P = default; // 5 + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(26, 16), + // (29,41): error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint. + // static void F5(T t5) where T : I + Diagnostic(ErrorCode.ERR_NullableUnconstrainedTypeParameter, "T?").WithLocation(29, 41), // (31,9): warning CS8602: Dereference of a possibly null reference. - // t5.P.ToString(); // 11 and 12 - Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t5.P").WithLocation(31, 9), - // (33,14): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // t5 = default; // 15 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(33, 14) + // t5.P.ToString(); // 6 and 7 + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t5.P").WithLocation(31, 9) ); } @@ -78750,12 +78649,12 @@ public class Test22 : Test2 public override void M1(S x) { x.ToString(); - x = default; // 2 + x = default; } public void Test() { - M1(new object()); // 3 + M1(new object()); // 2 } } @@ -78763,7 +78662,7 @@ public class Test31 : Test3 { public void Test() { - M1(new object()); // 4 + M1(new object()); // 3 } } @@ -78772,12 +78671,12 @@ public class Test32 : Test3 public override void M1(S x) { x.ToString(); - x = default; // 5 + x = default; } public void Test() { - M1(new object()); // 6 + M1(new object()); // 4 } } @@ -78793,8 +78692,8 @@ public class Test42 : Test4 { public override void M1(S x) { - x.ToString(); // 7 - x = default; // 8 + x.ToString(); // 5 + x = default; } public void Test() @@ -78815,8 +78714,8 @@ public class Test52 : Test5 { public override void M1(S x) { - x.ToString(); // 9 - x = default; // 10 + x.ToString(); // 6 + x = default; } public void Test() @@ -78838,7 +78737,7 @@ public class Test62 : Test6 public override void M1(S x) { x.ToString(); - x = default; // 11 + x = default; } public void Test() @@ -78860,7 +78759,7 @@ public class Test72 : Test7 public override void M1(S x) { x.ToString(); - x = default; // 12 + x = default; } public void Test() @@ -78877,39 +78776,21 @@ public void Test() // (7,9): warning CS8631: The type 'object?' cannot be used as type parameter 'S' in the generic type or method 'Test1.M1(S)'. Nullability of type argument 'object?' doesn't match constraint type 'object'. // M1(new object()); // 1 Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterConstraint, "M1").WithArguments("Test1.M1(S)", "object", "S", "object?").WithLocation(7, 9), - // (16,13): warning CS8653: A default expression introduces a null value when 'S' is a non-nullable reference type. - // x = default; // 2 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("S").WithLocation(16, 13), // (21,9): warning CS8631: The type 'object?' cannot be used as type parameter 'S' in the generic type or method 'Test22.M1(S)'. Nullability of type argument 'object?' doesn't match constraint type 'object'. - // M1(new object()); // 3 + // M1(new object()); // 2 Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterConstraint, "M1").WithArguments("Test22.M1(S)", "object", "S", "object?").WithLocation(21, 9), // (29,9): warning CS8631: The type 'object?' cannot be used as type parameter 'S' in the generic type or method 'Test3.M1(S)'. Nullability of type argument 'object?' doesn't match constraint type 'object'. - // M1(new object()); // 4 + // M1(new object()); // 3 Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterConstraint, "M1").WithArguments("Test3.M1(S)", "object", "S", "object?").WithLocation(29, 9), - // (38,13): warning CS8653: A default expression introduces a null value when 'S' is a non-nullable reference type. - // x = default; // 5 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("S").WithLocation(38, 13), // (43,9): warning CS8631: The type 'object?' cannot be used as type parameter 'S' in the generic type or method 'Test32.M1(S)'. Nullability of type argument 'object?' doesn't match constraint type 'object'. - // M1(new object()); // 6 + // M1(new object()); // 4 Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterConstraint, "M1").WithArguments("Test32.M1(S)", "object", "S", "object?").WithLocation(43, 9), // (59,9): warning CS8602: Dereference of a possibly null reference. - // x.ToString(); // 7 + // x.ToString(); // 5 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(59, 9), - // (60,13): warning CS8653: A default expression introduces a null value when 'S' is a non-nullable reference type. - // x = default; // 8 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("S").WithLocation(60, 13), // (81,9): warning CS8602: Dereference of a possibly null reference. - // x.ToString(); // 9 - Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(81, 9), - // (82,13): warning CS8653: A default expression introduces a null value when 'S' is a non-nullable reference type. - // x = default; // 10 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("S").WithLocation(82, 13), - // (104,13): warning CS8653: A default expression introduces a null value when 'S' is a non-nullable reference type. - // x = default; // 11 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("S").WithLocation(104, 13), - // (126,13): warning CS8653: A default expression introduces a null value when 'S' is a non-nullable reference type. - // x = default; // 12 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("S").WithLocation(126, 13) + // x.ToString(); // 6 + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(81, 9) ); } } @@ -82245,10 +82126,6 @@ public static void Test(T x) where T : notnull comp1.VerifyDiagnostics(); } - // https://github.com/dotnet/roslyn/issues/29981: Should report CS8600 for `T1 t = (T1)NullableObject();` - // and `T3 t = (T3)NullableObject();`. (See VisitConversion which skips reporting because the - // `object?` has an Unboxing conversion. Should report warning on unconverted operand - // when Unboxing.) [Fact] [WorkItem(29981, "https://github.com/dotnet/roslyn/issues/29981")] public void UnconstrainedTypeParameter_MayBeNonNullable() @@ -82259,9 +82136,10 @@ public void UnconstrainedTypeParameter_MayBeNonNullable() static object? NullableObject() => null; static T1 F1() => default; // warn: return type T1 may be non-null static T1 F2() => default(T1); // warn: return type T1 may be non-null - static void F4() + static T1 F4() { - T1 t1 = (T1)NullableObject(); // warn: T1 may be non-null + T1 t1 = (T1)NullableObject(); + return t1; } } class C2 where T2 : class @@ -82269,9 +82147,10 @@ class C2 where T2 : class static object? NullableObject() => null; static T2 F1() => default; // warn: return type T2 may be non-null static T2 F2() => default(T2); // warn: return type T2 may be non-null - static void F4() + static T2 F4() { T2 t2 = (T2)NullableObject(); // warn: T2 may be non-null + return t2; } } class C3 where T3 : new() @@ -82280,9 +82159,10 @@ static void F4() static T3 F1() => default; // warn: return type T3 may be non-null static T3 F2() => default(T3); // warn: return type T3 may be non-null static T3 F3() => new T3(); - static void F4() + static T3 F4() { T3 t = (T3)NullableObject(); // warn: T3 may be non-null + return t3; } } class C4 where T4 : I @@ -82290,9 +82170,10 @@ class C4 where T4 : I static object? NullableObject() => null; static T4 F1() => default; // warn: return type T4 may be non-null static T4 F2() => default(T4); // warn: return type T4 may be non-null - static void F4() + static T4 F4() { T4 t4 = (T4)NullableObject(); // warn: T4 may be non-null + return t4; } } class C5 where T5 : A @@ -82300,9 +82181,10 @@ class C5 where T5 : A static object? NullableObject() => null; static T5 F1() => default; // warn: return type T5 may be non-null static T5 F2() => default(T5); // warn: return type T5 may be non-null - static void F4() + static T5 F4() { T5 t5 = (T5)NullableObject(); // warn: T5 may be non-null + return t5; } } interface I @@ -82312,54 +82194,58 @@ class A { }"; var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue()); - // https://github.com/dotnet/roslyn/issues/29981: missing warnings comp.VerifyDiagnostics( - // (4,23): warning CS8653: A default expression introduces a null value when 'T1' is a non-nullable reference type. - // static T1 F1() => default; // warn: return type T1 may be non-null - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T1").WithLocation(4, 23), - // (14,23): warning CS8603: Possible null reference return. - // static T2 F1() => default; // warn: return type T2 may be non-null - Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default").WithLocation(14, 23), - // (45,23): warning CS8603: Possible null reference return. - // static T5 F1() => default; // warn: return type T5 may be non-null - Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default").WithLocation(45, 23), - // (35,23): warning CS8653: A default expression introduces a null value when 'T4' is a non-nullable reference type. - // static T4 F1() => default; // warn: return type T4 may be non-null - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T4").WithLocation(35, 23), - // (24,23): warning CS8653: A default expression introduces a null value when 'T3' is a non-nullable reference type. - // static T3 F1() => default; // warn: return type T3 may be non-null - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T3").WithLocation(24, 23), - // (5,23): warning CS8653: A default expression introduces a null value when 'T1' is a non-nullable reference type. - // static T1 F2() => default(T1); // warn: return type T1 may be non-null - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T1)").WithArguments("T1").WithLocation(5, 23), + // (9,16): warning CS8603: Possible null reference return. + // return t1; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t1").WithLocation(9, 16), // (15,23): warning CS8603: Possible null reference return. + // static T2 F1() => default; // warn: return type T2 may be non-null + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default").WithLocation(15, 23), + // (16,23): warning CS8603: Possible null reference return. // static T2 F2() => default(T2); // warn: return type T2 may be non-null - Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T2)").WithLocation(15, 23), - // (46,23): warning CS8603: Possible null reference return. - // static T5 F2() => default(T5); // warn: return type T5 may be non-null - Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T5)").WithLocation(46, 23), - // (36,23): warning CS8653: A default expression introduces a null value when 'T4' is a non-nullable reference type. - // static T4 F2() => default(T4); // warn: return type T4 may be non-null - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T4)").WithArguments("T4").WithLocation(36, 23), - // (25,23): warning CS8653: A default expression introduces a null value when 'T3' is a non-nullable reference type. - // static T3 F2() => default(T3); // warn: return type T3 may be non-null - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T3)").WithArguments("T3").WithLocation(25, 23), - // (8,17): warning CS8601: Possible null reference assignment. - // T1 t1 = (T1)NullableObject(); // warn: T1 may be non-null - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T1)NullableObject()").WithLocation(8, 17), - // (18,17): warning CS8600: Converting null literal or possible null value to non-nullable type. + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T2)").WithLocation(16, 23), + // (19,17): warning CS8600: Converting null literal or possible null value to non-nullable type. // T2 t2 = (T2)NullableObject(); // warn: T2 may be non-null - Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(T2)NullableObject()").WithLocation(18, 17), - // (49,17): warning CS8600: Converting null literal or possible null value to non-nullable type. + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(T2)NullableObject()").WithLocation(19, 17), + // (20,16): warning CS8603: Possible null reference return. + // return t2; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t2").WithLocation(20, 16), + // (26,23): warning CS8603: Possible null reference return. + // static T3 F1() => default; // warn: return type T3 may be non-null + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default").WithLocation(26, 23), + // (27,23): warning CS8603: Possible null reference return. + // static T3 F2() => default(T3); // warn: return type T3 may be non-null + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T3)").WithLocation(27, 23), + // (32,16): error CS0103: The name 't3' does not exist in the current context + // return t3; + Diagnostic(ErrorCode.ERR_NameNotInContext, "t3").WithArguments("t3").WithLocation(32, 16), + // (38,23): warning CS8603: Possible null reference return. + // static T4 F1() => default; // warn: return type T4 may be non-null + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default").WithLocation(38, 23), + // (39,23): warning CS8603: Possible null reference return. + // static T4 F2() => default(T4); // warn: return type T4 may be non-null + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T4)").WithLocation(39, 23), + // (43,16): warning CS8603: Possible null reference return. + // return t4; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t4").WithLocation(43, 16), + // (49,23): warning CS8603: Possible null reference return. + // static T5 F1() => default; // warn: return type T5 may be non-null + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default").WithLocation(49, 23), + // (50,23): warning CS8603: Possible null reference return. + // static T5 F2() => default(T5); // warn: return type T5 may be non-null + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T5)").WithLocation(50, 23), + // (53,17): warning CS8600: Converting null literal or possible null value to non-nullable type. // T5 t5 = (T5)NullableObject(); // warn: T5 may be non-null - Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(T5)NullableObject()").WithLocation(49, 17), - // (39,17): warning CS8601: Possible null reference assignment. - // T4 t4 = (T4)NullableObject(); // warn: T4 may be non-null - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T4)NullableObject()").WithLocation(39, 17), - // (29,16): warning CS8601: Possible null reference assignment. - // T3 t = (T3)NullableObject(); // warn: T3 may be non-null - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T3)NullableObject()").WithLocation(29, 16) - ); + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(T5)NullableObject()").WithLocation(53, 17), + // (54,16): warning CS8603: Possible null reference return. + // return t5; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t5").WithLocation(54, 16), + // (4,23): warning CS8603: Possible null reference return. + // static T1 F1() => default; // warn: return type T1 may be non-null + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default").WithLocation(4, 23), + // (5,23): warning CS8603: Possible null reference return. + // static T1 F2() => default(T1); // warn: return type T1 may be non-null + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T1)").WithLocation(5, 23)); } [Fact] @@ -82592,18 +82478,17 @@ public void UnconstrainedTypeParameter_Return_03() static T F14(U u) where U : class, T => (T)u; static T F15(U u) where U : struct, T => (T)u; static T F16(U u) where U : T, new() => (T)u; - static U F17(T t) where U : T => (U)t; // W on cast + static U F17(T t) where U : T => (U)t; // W on return static U F18(T t) where U : class, T => (U)t; // W on cast, W on return static U F19(T t) where U : struct, T => (U)t; - static U F20(T t) where U : T, new() => (U)t; // W on cast - static U F21(T t) => (U)(object)t; // W on cast, W on cast + static U F20(T t) where U : T, new() => (U)t; // W on return + static U F21(T t) => (U)(object)t; // W on cast, W on return }"; var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue()); - comp.VerifyDiagnostics( - // (19,44): warning CS8601: Possible null reference assignment. - // static U F17(T t) where U : T => (U)t; // W on cast - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(U)t").WithLocation(19, 44), + // (19,44): warning CS8603: Possible null reference return. + // static U F17(T t) where U : T => (U)t; // W on return + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(U)t").WithLocation(19, 44), // (20,51): warning CS8600: Converting null literal or possible null value to non-nullable type. // static U F18(T t) where U : class, T => (U)t; // W on cast, W on return Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(U)t").WithLocation(20, 51), @@ -82613,16 +82498,15 @@ public void UnconstrainedTypeParameter_Return_03() // (21,52): warning CS8605: Unboxing a possibly null value. // static U F19(T t) where U : struct, T => (U)t; Diagnostic(ErrorCode.WRN_UnboxPossibleNull, "(U)t").WithLocation(21, 52), - // (22,51): warning CS8601: Possible null reference assignment. - // static U F20(T t) where U : T, new() => (U)t; // W on cast - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(U)t").WithLocation(22, 51), - // (23,32): warning CS8601: Possible null reference assignment. - // static U F21(T t) => (U)(object)t; // W on cast, W on cast - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(U)(object)t").WithLocation(23, 32), + // (22,51): warning CS8603: Possible null reference return. + // static U F20(T t) where U : T, new() => (U)t; // W on return + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(U)t").WithLocation(22, 51), + // (23,32): warning CS8603: Possible null reference return. + // static U F21(T t) => (U)(object)t; // W on cast, W on return + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(U)(object)t").WithLocation(23, 32), // (23,35): warning CS8600: Converting null literal or possible null value to non-nullable type. - // static U F21(T t) => (U)(object)t; // W on cast, W on cast - Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(object)t").WithLocation(23, 35) - ); + // static U F21(T t) => (U)(object)t; // W on cast, W on return + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(object)t").WithLocation(23, 35)); } [Fact] @@ -82669,12 +82553,12 @@ class C var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( - // (4,39): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (4,39): warning CS8601: Possible null reference assignment. // static void F1(out T t) => t = default; // 1 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(4, 39), - // (5,39): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(4, 39), + // (5,39): warning CS8601: Possible null reference assignment. // static void F2(out T t) => t = default(T); // 2 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(5, 39), + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default(T)").WithLocation(5, 39), // (8,59): warning CS8601: Possible null reference assignment. // static void F5(U u, out T t) where T : U => t = (T)u; // 3 Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)u").WithLocation(8, 59), @@ -82773,9 +82657,9 @@ class C // (4,34): warning CS8600: Converting null literal or possible null value to non-nullable type. // static U F1(T t) => (U)(object)t; // 1 and 2 Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(object)t").WithLocation(4, 34), - // (4,31): warning CS8601: Possible null reference assignment. + // (4,31): warning CS8603: Possible null reference return. // static U F1(T t) => (U)(object)t; // 1 and 2 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(U)(object)t").WithLocation(4, 31), + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(U)(object)t").WithLocation(4, 31), // (5,50): warning CS8600: Converting null literal or possible null value to non-nullable type. // static U F2(T t) where U : class => (U)(object)t; // 3, 4 and 5 Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(object)t").WithLocation(5, 50), @@ -82807,7 +82691,7 @@ static void F0() } static void F1() { - T x1 = default; // 2 + T x1 = default; x1.ToString(); // 3 x1!.ToString(); x1?.ToString(); @@ -82841,18 +82725,9 @@ static T F4(T x4) }"; var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( - // (5,9): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // default(T).ToString(); // 1 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(5, 9), // (5,9): warning CS8602: Dereference of a possibly null reference. // default(T).ToString(); // 1 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "default(T)").WithLocation(5, 9), - // (6,9): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // default(T)?.ToString(); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(6, 9), - // (10,16): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // T x1 = default; // 2 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(10, 16), // (11,9): warning CS8602: Dereference of a possibly null reference. // x1.ToString(); // 3 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x1").WithLocation(11, 9), @@ -83216,9 +83091,6 @@ static void M() // (7,24): error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint. // var u = typeof(U?); Diagnostic(ErrorCode.ERR_NullableUnconstrainedTypeParameter, "U?").WithLocation(7, 24), - // (8,21): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // object? o = default(T?); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T?)").WithArguments("T").WithLocation(8, 21), // (8,29): error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint. // object? o = default(T?); Diagnostic(ErrorCode.ERR_NullableUnconstrainedTypeParameter, "T?").WithLocation(8, 29), @@ -85644,21 +85516,21 @@ void M5() // (9,8): warning CS0414: The field 'A.F' is assigned but its value is never used // T1 F; Diagnostic(ErrorCode.WRN_UnreferencedFieldAssg, "F").WithArguments("A.F").WithLocation(9, 8), - // (16,17): warning CS8654: A null literal introduces a null value when 'T1' is a non-nullable reference type. + // (16,17): warning CS8625: Cannot convert null literal to non-nullable reference type. // F = null; // 1 - Diagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, "null").WithArguments("T1").WithLocation(16, 17), - // (23,13): warning CS8654: A null literal introduces a null value when 'T1' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(16, 17), + // (23,13): warning CS8625: Cannot convert null literal to non-nullable reference type. // F = null; // 2 - Diagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, "null").WithArguments("T1").WithLocation(23, 13), + Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(23, 13), // (31,17): warning CS8625: Cannot convert null literal to non-nullable reference type. // F = null; // 3 Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(31, 17), - // (40,17): warning CS8654: A null literal introduces a null value when 'T1' is a non-nullable reference type. + // (40,17): warning CS8625: Cannot convert null literal to non-nullable reference type. // F = null; // 4 - Diagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, "null").WithArguments("T1").WithLocation(40, 17), - // (49,17): warning CS8654: A null literal introduces a null value when 'T2' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(40, 17), + // (49,17): warning CS8625: Cannot convert null literal to non-nullable reference type. // F = null; // 5 - Diagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, "null").WithArguments("T2").WithLocation(49, 17)); + Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(49, 17)); var b = comp.GetTypeByMetadataName("A`2+B"); Assert.NotNull(b); @@ -85843,18 +85715,18 @@ void M3() // (7,8): warning CS0414: The field 'A.F' is assigned but its value is never used // T1 F; Diagnostic(ErrorCode.WRN_UnreferencedFieldAssg, "F").WithArguments("A.F").WithLocation(7, 8), - // (14,17): warning CS8654: A null literal introduces a null value when 'T1' is a non-nullable reference type. + // (14,17): warning CS8625: Cannot convert null literal to non-nullable reference type. // F = null; // 1 - Diagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, "null").WithArguments("T1").WithLocation(14, 17), + Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(14, 17), // (29,17): warning CS8625: Cannot convert null literal to non-nullable reference type. // F = null; // 2 Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(29, 17), - // (38,17): warning CS8654: A null literal introduces a null value when 'T1' is a non-nullable reference type. + // (38,17): warning CS8625: Cannot convert null literal to non-nullable reference type. // F = null; // 3 - Diagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, "null").WithArguments("T1").WithLocation(38, 17), - // (47,17): warning CS8654: A null literal introduces a null value when 'T2' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(38, 17), + // (47,17): warning CS8625: Cannot convert null literal to non-nullable reference type. // F = null; // 4 - Diagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, "null").WithArguments("T2").WithLocation(47, 17)); + Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(47, 17)); var b = comp.GetTypeByMetadataName("A`2+B"); Assert.NotNull(b); @@ -87629,17 +87501,7 @@ public static void Test(T x) where T : notnull } "; var comp = CreateCompilation(new[] { source }); - comp.VerifyDiagnostics( - // (29,17): warning CS8653: A default expression introduces a null value when 'T1' is a non-nullable reference type. - // T1 x2 = default; - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T1").WithLocation(29, 17), - // (30,17): warning CS8653: A default expression introduces a null value when 'T2' is a non-nullable reference type. - // T2 y2 = default; - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T2").WithLocation(30, 17), - // (31,17): warning CS8653: A default expression introduces a null value when 'T3' is a non-nullable reference type. - // T3 z2 = default; - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T3").WithLocation(31, 17) - ); + comp.VerifyDiagnostics(); } [Fact] @@ -87826,7 +87688,7 @@ static void F(T x) x.ToString(); // 1 T y; y.ToString(); // 2 - y = default; // 3 + y = default; y.ToString(); // 4 x.ToString(); y.ToString(); @@ -87850,9 +87712,6 @@ static void F(T x) // (7,9): warning CS8602: Dereference of a possibly null reference. // y.ToString(); // 2 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(7, 9), - // (8,13): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // y = default; // 3 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(8, 13), // (9,9): warning CS8602: Dereference of a possibly null reference. // y.ToString(); // 4 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(9, 9), @@ -89862,15 +89721,9 @@ void M(object? x, object y) // (7,13): error CS0266: Cannot implicitly convert type 'object' to 'T'. An explicit conversion exists (are you missing a cast?) // z = x; Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "x").WithArguments("object", "T").WithLocation(7, 13), - // (7,13): warning CS8601: Possible null reference assignment. - // z = x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(7, 13), // (8,13): error CS0266: Cannot implicitly convert type 'object' to 'T'. An explicit conversion exists (are you missing a cast?) // z = y; Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "y").WithArguments("object", "T").WithLocation(8, 13), - // (9,13): warning CS8601: Possible null reference assignment. - // z = (T)x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(9, 13), // (10,9): warning CS8602: Dereference of a possibly null reference. // z.ToString(); // 1 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(10, 9)); @@ -89886,10 +89739,10 @@ class Outer void M(dynamic? x, dynamic y) { T z; - z = x; // 1 + z = x; z = y; - z = (T)x; // 2 - z.ToString(); // 3 + z = (T)x; + z.ToString(); // 1 z = (T)y; z.ToString(); } @@ -89897,14 +89750,8 @@ void M(dynamic? x, dynamic y) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (7,13): warning CS8601: Possible null reference assignment. - // z = x; // 1 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(7, 13), - // (9,13): warning CS8601: Possible null reference assignment. - // z = (T)x; // 2 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(9, 13), // (10,9): warning CS8602: Dereference of a possibly null reference. - // z.ToString(); // 3 + // z.ToString(); // 1 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(10, 9) ); } @@ -89934,15 +89781,9 @@ interface I1{} // (7,13): error CS0266: Cannot implicitly convert type 'object' to 'T'. An explicit conversion exists (are you missing a cast?) // z = x; Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "x").WithArguments("object", "T").WithLocation(7, 13), - // (7,13): warning CS8601: Possible null reference assignment. - // z = x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(7, 13), // (8,13): error CS0266: Cannot implicitly convert type 'object' to 'T'. An explicit conversion exists (are you missing a cast?) // z = y; Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "y").WithArguments("object", "T").WithLocation(8, 13), - // (9,13): warning CS8601: Possible null reference assignment. - // z = (T)x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(9, 13), // (10,9): warning CS8602: Dereference of a possibly null reference. // z.ToString(); // 1 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(10, 9)); @@ -89970,12 +89811,6 @@ interface I1{} "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (7,13): warning CS8601: Possible null reference assignment. - // z = x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(7, 13), - // (9,13): warning CS8601: Possible null reference assignment. - // z = (T)x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(9, 13), // (10,9): warning CS8602: Dereference of a possibly null reference. // z.ToString(); // 1 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(10, 9) @@ -90007,15 +89842,9 @@ interface I1{} // (7,13): error CS0266: Cannot implicitly convert type 'object' to 'T'. An explicit conversion exists (are you missing a cast?) // z = x; Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "x").WithArguments("object", "T").WithLocation(7, 13), - // (7,13): warning CS8601: Possible null reference assignment. - // z = x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(7, 13), // (8,13): error CS0266: Cannot implicitly convert type 'object' to 'T'. An explicit conversion exists (are you missing a cast?) // z = y; Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "y").WithArguments("object", "T").WithLocation(8, 13), - // (9,13): warning CS8601: Possible null reference assignment. - // z = (T)x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(9, 13), // (10,9): warning CS8602: Dereference of a possibly null reference. // z.ToString(); // 1 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(10, 9)); @@ -90043,12 +89872,6 @@ interface I1{} "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (7,13): warning CS8601: Possible null reference assignment. - // z = x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(7, 13), - // (9,13): warning CS8601: Possible null reference assignment. - // z = (T)x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(9, 13), // (10,9): warning CS8602: Dereference of a possibly null reference. // z.ToString(); // 1 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(10, 9) @@ -90075,12 +89898,6 @@ void M(T x) where U : T // (7,13): error CS0266: Cannot implicitly convert type 'T' to 'U'. An explicit conversion exists (are you missing a cast?) // y = x; Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "x").WithArguments("T", "U").WithLocation(7, 13), - // (7,13): warning CS8601: Possible null reference assignment. - // y = x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(7, 13), - // (8,13): warning CS8601: Possible null reference assignment. - // y = (U)x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(U)x").WithLocation(8, 13), // (9,9): warning CS8602: Dereference of a possibly null reference. // y.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(9, 9)); @@ -90108,12 +89925,6 @@ interface I1 {} // (7,13): error CS0266: Cannot implicitly convert type 'T' to 'U'. An explicit conversion exists (are you missing a cast?) // y = x; Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "x").WithArguments("T", "U").WithLocation(7, 13), - // (7,13): warning CS8601: Possible null reference assignment. - // y = x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(7, 13), - // (8,13): warning CS8601: Possible null reference assignment. - // y = (U)x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(U)x").WithLocation(8, 13), // (9,9): warning CS8602: Dereference of a possibly null reference. // y.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(9, 9)); @@ -90141,12 +89952,6 @@ interface I1 {} // (7,13): error CS0266: Cannot implicitly convert type 'T' to 'U'. An explicit conversion exists (are you missing a cast?) // y = x; Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "x").WithArguments("T", "U").WithLocation(7, 13), - // (7,13): warning CS8601: Possible null reference assignment. - // y = x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(7, 13), - // (8,13): warning CS8601: Possible null reference assignment. - // y = (U)x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(U)x").WithLocation(8, 13), // (9,9): warning CS8602: Dereference of a possibly null reference. // y.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(9, 9)); @@ -90220,9 +90025,9 @@ void M2(dynamic? x) { if (x != null) return; T y; - y = x; // 1 - y = (T)x; // 2 - y.ToString(); // 3 + y = x; + y = (T)x; + y.ToString(); // 1 } void M3(dynamic? x) { @@ -90236,9 +90041,9 @@ void M3(dynamic? x) else { T y; - y = x; // 4 - y = (T)x; // 5 - y.ToString(); // 6 + y = x; + y = (T)x; + y.ToString(); // 2 } } void M4(dynamic? x) @@ -90246,9 +90051,9 @@ void M4(dynamic? x) if (x == null) { T y; - y = x; // 7 - y = (T)x; // 8 - y.ToString(); // 9 + y = x; + y = (T)x; + y.ToString(); // 3 } else { @@ -90265,16 +90070,16 @@ void M5(dynamic? x) if (x == null || false) { T y; - y = x; // 10 - y = (T)x; // 11 - y.ToString(); // 12 + y = x; + y = (T)x; + y.ToString(); // 4 } else { T y; - y = x; // 13 - y = (T)x; // 14 - y.ToString(); // 15 + y = x; + y = (T)x; + y.ToString(); // 5 } } void M6(dynamic? x) @@ -90284,16 +90089,16 @@ void M6(dynamic? x) if (x == null && true) { T y; - y = x; // 16 - y = (T)x; // 17 - y.ToString(); // 18 + y = x; + y = (T)x; + y.ToString(); // 6 } else { T y; - y = x; // 19 - y = (T)x; // 20 - y.ToString(); // 21 + y = x; + y = (T)x; + y.ToString(); // 7 } } void M7(dynamic? x) @@ -90308,9 +90113,9 @@ void M7(dynamic? x) else { T y; - y = x; // 22 - y = (T)x; // 23 - y.ToString(); // 24 + y = x; + y = (T)x; + y.ToString(); // 8 } } void M8(dynamic? x) @@ -90318,9 +90123,9 @@ void M8(dynamic? x) if (!(x != null)) { T y; - y = x; // 25 - y = (T)x; // 26 - y.ToString(); // 27 + y = x; + y = (T)x; + y.ToString(); // 9 } else { @@ -90333,86 +90138,32 @@ void M8(dynamic? x) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (16,13): warning CS8601: Possible null reference assignment. - // y = x; // 1 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(16, 13), - // (17,13): warning CS8601: Possible null reference assignment. - // y = (T)x; // 2 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(17, 13), // (18,9): warning CS8602: Dereference of a possibly null reference. - // y.ToString(); // 3 + // y.ToString(); // 1 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(18, 9), - // (32,17): warning CS8601: Possible null reference assignment. - // y = x; // 4 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(32, 17), - // (33,17): warning CS8601: Possible null reference assignment. - // y = (T)x; // 5 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(33, 17), // (34,13): warning CS8602: Dereference of a possibly null reference. - // y.ToString(); // 6 + // y.ToString(); // 2 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(34, 13), - // (42,17): warning CS8601: Possible null reference assignment. - // y = x; // 7 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(42, 17), - // (43,17): warning CS8601: Possible null reference assignment. - // y = (T)x; // 8 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(43, 17), // (44,13): warning CS8602: Dereference of a possibly null reference. - // y.ToString(); // 9 + // y.ToString(); // 3 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(44, 13), - // (61,17): warning CS8601: Possible null reference assignment. - // y = x; // 10 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(61, 17), - // (62,17): warning CS8601: Possible null reference assignment. - // y = (T)x; // 11 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(62, 17), // (63,13): warning CS8602: Dereference of a possibly null reference. - // y.ToString(); // 12 + // y.ToString(); // 4 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(63, 13), - // (68,17): warning CS8601: Possible null reference assignment. - // y = x; // 13 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(68, 17), - // (69,17): warning CS8601: Possible null reference assignment. - // y = (T)x; // 14 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(69, 17), // (70,13): warning CS8602: Dereference of a possibly null reference. - // y.ToString(); // 15 + // y.ToString(); // 5 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(70, 13), - // (80,17): warning CS8601: Possible null reference assignment. - // y = x; // 16 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(80, 17), - // (81,17): warning CS8601: Possible null reference assignment. - // y = (T)x; // 17 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(81, 17), // (82,13): warning CS8602: Dereference of a possibly null reference. - // y.ToString(); // 18 + // y.ToString(); // 6 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(82, 13), - // (87,17): warning CS8601: Possible null reference assignment. - // y = x; // 19 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(87, 17), - // (88,17): warning CS8601: Possible null reference assignment. - // y = (T)x; // 20 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(88, 17), // (89,13): warning CS8602: Dereference of a possibly null reference. - // y.ToString(); // 21 + // y.ToString(); // 7 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(89, 13), - // (104,17): warning CS8601: Possible null reference assignment. - // y = x; // 22 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(104, 17), - // (105,17): warning CS8601: Possible null reference assignment. - // y = (T)x; // 23 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(105, 17), // (106,13): warning CS8602: Dereference of a possibly null reference. - // y.ToString(); // 24 + // y.ToString(); // 8 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(106, 13), - // (114,17): warning CS8601: Possible null reference assignment. - // y = x; // 25 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(114, 17), - // (115,17): warning CS8601: Possible null reference assignment. - // y = (T)x; // 26 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(115, 17), // (116,13): warning CS8602: Dereference of a possibly null reference. - // y.ToString(); // 27 + // y.ToString(); // 9 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(116, 13)); } @@ -90530,15 +90281,9 @@ void M(Outer? x, Outer y) where T : Outer? // (7,13): error CS0266: Cannot implicitly convert type 'Outer' to 'T'. An explicit conversion exists (are you missing a cast?) // z = x; Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "x").WithArguments("Outer", "T").WithLocation(7, 13), - // (7,13): warning CS8601: Possible null reference assignment. - // z = x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "x").WithLocation(7, 13), // (8,13): error CS0266: Cannot implicitly convert type 'Outer' to 'T'. An explicit conversion exists (are you missing a cast?) // z = y; Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "y").WithArguments("Outer", "T").WithLocation(8, 13), - // (9,13): warning CS8601: Possible null reference assignment. - // z = (T)x; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)x").WithLocation(9, 13), // (10,9): warning CS8602: Dereference of a possibly null reference. // z.ToString(); // 1 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(10, 9) @@ -90670,9 +90415,6 @@ void M0(T x) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,13): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. - // x = default; - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(6, 13), // (7,9): warning CS8602: Dereference of a possibly null reference. // x.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(7, 9) @@ -90694,9 +90436,6 @@ void M0(T x) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,13): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. - // x = default(T); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(6, 13), // (7,9): warning CS8602: Dereference of a possibly null reference. // x.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(7, 9) @@ -90720,9 +90459,6 @@ interface I1 {} "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,13): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. - // x = default; - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(6, 13), // (7,9): warning CS8602: Dereference of a possibly null reference. // x.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(7, 9) @@ -90746,9 +90482,6 @@ interface I1 {} "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,13): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. - // x = default(T); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(6, 13), // (7,9): warning CS8602: Dereference of a possibly null reference. // x.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(7, 9) @@ -90820,9 +90553,6 @@ interface I1 {} "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,13): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. - // x = default; - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(6, 13), // (7,9): warning CS8602: Dereference of a possibly null reference. // x.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(7, 9) @@ -90846,9 +90576,6 @@ interface I1 {} "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,13): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. - // x = default(T); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(6, 13), // (7,9): warning CS8602: Dereference of a possibly null reference. // x.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(7, 9) @@ -91081,9 +90808,6 @@ void M0(T x) where T : class? "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,13): warning CS8654: A null literal introduces a null value when 'T' is a non-nullable reference type. - // x = null; - Diagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, "null").WithArguments("T").WithLocation(6, 13), // (7,9): warning CS8602: Dereference of a possibly null reference. // x.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(7, 9)); @@ -91128,9 +90852,6 @@ void M0(T x) where T : Outer? "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,13): warning CS8654: A null literal introduces a null value when 'T' is a non-nullable reference type. - // x = null; - Diagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, "null").WithArguments("T").WithLocation(6, 13), // (7,9): warning CS8602: Dereference of a possibly null reference. // x.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(7, 9)); @@ -92705,13 +92426,7 @@ interface I1 {} CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( // (4,35): error CS8627: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint. // void M0(T x0) where T : I1 - Diagnostic(ErrorCode.ERR_NullableUnconstrainedTypeParameter, "T?").WithLocation(4, 35), - // (6,14): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. - // x0 = default; - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(6, 14), - // (11,14): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. - // x1 = default; - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(11, 14) + Diagnostic(ErrorCode.ERR_NullableUnconstrainedTypeParameter, "T?").WithLocation(4, 35) ); } @@ -93328,9 +93043,9 @@ void F(T y0) }"; var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( - // (8,14): warning CS8654: A null literal introduces a null value when 'T' is a non-nullable reference type. - // x0 = null; - Diagnostic(ErrorCode.WRN_NullLiteralMayIntroduceNullT, "null").WithArguments("T").WithLocation(8, 14)); + // (10,15): warning CS8604: Possible null reference argument for parameter 'x' in 'C C.M1(T x)'. + // M2(M1(x0), M1(y0)) = + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x0").WithArguments("x", "C C.M1(T x)").WithLocation(10, 15)); } [Fact] @@ -94262,9 +93977,6 @@ void M0(T x0) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8601: Possible null reference assignment. - // U y0 = (U)x0; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(U)x0").WithLocation(6, 16), // (9,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(9, 9), @@ -94293,9 +94005,6 @@ void M0(T x0) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8601: Possible null reference assignment. - // U y0 = (U)x0; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(U)x0").WithLocation(6, 16), // (9,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(9, 9), @@ -94325,9 +94034,6 @@ void M0(T x0) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8638: Conditional access may produce a null value when 'T' is a non-nullable reference type. - // T z0 = x0?.M1(); - Diagnostic(ErrorCode.WRN_ConditionalAccessMayReturnNull, "x0?.M1()").WithArguments("T").WithLocation(6, 16), // (9,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(9, 9), @@ -94357,12 +94063,6 @@ void M0(T x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8638: Conditional access may produce a null value when 'T' is a non-nullable reference type. - // T z0 = x0?.M1(); - Diagnostic(ErrorCode.WRN_ConditionalAccessMayReturnNull, "x0?.M1()").WithArguments("T").WithLocation(6, 16), - // (7,16): warning CS8601: Possible null reference assignment. - // U y0 = (U)z0; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(U)z0").WithLocation(7, 16), // (9,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(9, 9), @@ -94394,9 +94094,6 @@ void M0(T x0) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (7,16): warning CS8638: Conditional access may produce a null value when 'T' is a non-nullable reference type. - // T z0 = x0?.M1(); - Diagnostic(ErrorCode.WRN_ConditionalAccessMayReturnNull, "x0?.M1()").WithArguments("T").WithLocation(7, 16), // (10,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(10, 9), @@ -94427,9 +94124,6 @@ void M0(Outer? x0) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8638: Conditional access may produce a null value when 'T' is a non-nullable reference type. - // T z0 = x0?.M1(); - Diagnostic(ErrorCode.WRN_ConditionalAccessMayReturnNull, "x0?.M1()").WithArguments("T").WithLocation(6, 16), // (9,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(9, 9), @@ -94459,9 +94153,6 @@ void M0(Outer x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8638: Conditional access may produce a null value when 'T' is a non-nullable reference type. - // T z0 = x0?.M1(); - Diagnostic(ErrorCode.WRN_ConditionalAccessMayReturnNull, "x0?.M1()").WithArguments("T").WithLocation(6, 16), // (9,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(9, 9), @@ -94599,9 +94290,6 @@ void M0(T x0) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8638: Conditional access may produce a null value when 'U' is a non-nullable reference type. - // U z0 = x0?.M1(); - Diagnostic(ErrorCode.WRN_ConditionalAccessMayReturnNull, "x0?.M1()").WithArguments("U").WithLocation(6, 16), // (8,9): warning CS8602: Dereference of a possibly null reference. // z0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z0").WithLocation(8, 9) @@ -94627,9 +94315,6 @@ void M0(T x0) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (7,16): warning CS8638: Conditional access may produce a null value when 'U' is a non-nullable reference type. - // U z0 = x0?.M1(); - Diagnostic(ErrorCode.WRN_ConditionalAccessMayReturnNull, "x0?.M1()").WithArguments("U").WithLocation(7, 16), // (9,9): warning CS8602: Dereference of a possibly null reference. // z0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z0").WithLocation(9, 9) @@ -94654,9 +94339,6 @@ void M0(U x0) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8638: Conditional access may produce a null value when 'T' is a non-nullable reference type. - // U z0 = x0?.M1(); - Diagnostic(ErrorCode.WRN_ConditionalAccessMayReturnNull, "x0?.M1()").WithArguments("T").WithLocation(6, 16), // (8,9): warning CS8602: Dereference of a possibly null reference. // z0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z0").WithLocation(8, 9) @@ -94680,9 +94362,6 @@ void M0(U x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8638: Conditional access may produce a null value when 'T' is a non-nullable reference type. - // T z0 = x0?.M1(); - Diagnostic(ErrorCode.WRN_ConditionalAccessMayReturnNull, "x0?.M1()").WithArguments("T").WithLocation(6, 16), // (8,9): warning CS8602: Dereference of a possibly null reference. // z0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z0").WithLocation(8, 9) @@ -94708,9 +94387,6 @@ void M0(U x0) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (7,16): warning CS8638: Conditional access may produce a null value when 'T' is a non-nullable reference type. - // U z0 = x0?.M1(); - Diagnostic(ErrorCode.WRN_ConditionalAccessMayReturnNull, "x0?.M1()").WithArguments("T").WithLocation(7, 16), // (9,9): warning CS8602: Dereference of a possibly null reference. // z0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z0").WithLocation(9, 9) @@ -94736,9 +94412,6 @@ void M0(U x0) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (7,16): warning CS8638: Conditional access may produce a null value when 'T' is a non-nullable reference type. - // T z0 = x0?.M1(); - Diagnostic(ErrorCode.WRN_ConditionalAccessMayReturnNull, "x0?.M1()").WithArguments("T").WithLocation(7, 16), // (9,9): warning CS8602: Dereference of a possibly null reference. // z0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z0").WithLocation(9, 9) @@ -94763,9 +94436,6 @@ void M0(T x0) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8638: Conditional access may produce a null value when 'U' is a non-nullable reference type. - // U z0 = x0?.M1(); - Diagnostic(ErrorCode.WRN_ConditionalAccessMayReturnNull, "x0?.M1()").WithArguments("U").WithLocation(6, 16), // (8,9): warning CS8602: Dereference of a possibly null reference. // z0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z0").WithLocation(8, 9) @@ -94791,9 +94461,6 @@ void M0(T x0) "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (7,16): warning CS8638: Conditional access may produce a null value when 'U' is a non-nullable reference type. - // U z0 = x0?.M1(); - Diagnostic(ErrorCode.WRN_ConditionalAccessMayReturnNull, "x0?.M1()").WithArguments("U").WithLocation(7, 16), // (9,9): warning CS8602: Dereference of a possibly null reference. // z0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z0").WithLocation(9, 9) @@ -94966,9 +94633,6 @@ void M0(object x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8626: The 'as' operator may produce a null value when 'T' is a non-nullable reference type. - // T y0 = x0 as T; - Diagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, "x0 as T").WithArguments("T").WithLocation(6, 16), // (8,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(8, 9) @@ -94990,9 +94654,6 @@ void M0(object? x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8626: The 'as' operator may produce a null value when 'T' is a non-nullable reference type. - // T y0 = x0 as T; - Diagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, "x0 as T").WithArguments("T").WithLocation(6, 16), // (8,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(8, 9) @@ -95015,9 +94676,6 @@ void M0(object? x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (7,16): warning CS8626: The 'as' operator may produce a null value when 'T' is a non-nullable reference type. - // T y0 = x0 as T; - Diagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, "x0 as T").WithArguments("T").WithLocation(7, 16), // (9,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(9, 9) @@ -95039,9 +94697,6 @@ void M0(dynamic x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8626: The 'as' operator may produce a null value when 'T' is a non-nullable reference type. - // T y0 = x0 as T; - Diagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, "x0 as T").WithArguments("T").WithLocation(6, 16), // (8,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(8, 9) @@ -95063,9 +94718,6 @@ void M0(dynamic? x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8626: The 'as' operator may produce a null value when 'T' is a non-nullable reference type. - // T y0 = x0 as T; - Diagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, "x0 as T").WithArguments("T").WithLocation(6, 16), // (8,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(8, 9) @@ -95088,9 +94740,6 @@ void M0(dynamic? x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (7,16): warning CS8626: The 'as' operator may produce a null value when 'T' is a non-nullable reference type. - // T y0 = x0 as T; - Diagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, "x0 as T").WithArguments("T").WithLocation(7, 16), // (9,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(9, 9) @@ -95355,9 +95004,6 @@ void M0(Outer? x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8626: The 'as' operator may produce a null value when 'T' is a non-nullable reference type. - // T y0 = x0 as T; - Diagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, "x0 as T").WithArguments("T").WithLocation(6, 16), // (8,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(8, 9) @@ -95380,9 +95026,6 @@ void M0(Outer? x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (7,16): warning CS8626: The 'as' operator may produce a null value when 'T' is a non-nullable reference type. - // T y0 = x0 as T; - Diagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, "x0 as T").WithArguments("T").WithLocation(7, 16), // (9,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(9, 9) @@ -95404,9 +95047,6 @@ void M0(Outer x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8626: The 'as' operator may produce a null value when 'T' is a non-nullable reference type. - // T y0 = x0 as T; - Diagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, "x0 as T").WithArguments("T").WithLocation(6, 16), // (8,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(8, 9) @@ -95573,9 +95213,6 @@ void M0(Outer x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8626: The 'as' operator may produce a null value when 'T' is a non-nullable reference type. - // T y0 = x0 as T; - Diagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, "x0 as T").WithArguments("T").WithLocation(6, 16), // (8,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(8, 9) @@ -95741,9 +95378,6 @@ void M0(I1 x0) interface I1{} "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8626: The 'as' operator may produce a null value when 'T' is a non-nullable reference type. - // T y0 = x0 as T; - Diagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, "x0 as T").WithArguments("T").WithLocation(6, 16), // (8,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(8, 9) @@ -96138,9 +95772,6 @@ void M0(U x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8626: The 'as' operator may produce a null value when 'T' is a non-nullable reference type. - // T y0 = x0 as T; - Diagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, "x0 as T").WithArguments("T").WithLocation(6, 16), // (8,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(8, 9) @@ -96163,9 +95794,6 @@ void M0(U x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (7,16): warning CS8626: The 'as' operator may produce a null value when 'T' is a non-nullable reference type. - // T y0 = x0 as T; - Diagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, "x0 as T").WithArguments("T").WithLocation(7, 16), // (9,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(9, 9) @@ -96212,9 +95840,6 @@ void M0(U x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (6,16): warning CS8626: The 'as' operator may produce a null value when 'T' is a non-nullable reference type. - // T y0 = x0 as T; - Diagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, "x0 as T").WithArguments("T").WithLocation(6, 16), // (8,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(8, 9) @@ -96237,9 +95862,6 @@ void M0(U x0) } "; CreateCompilation(source, options: WithNonNullTypesTrue()).VerifyDiagnostics( - // (7,16): warning CS8626: The 'as' operator may produce a null value when 'T' is a non-nullable reference type. - // T y0 = x0 as T; - Diagnostic(ErrorCode.WRN_AsOperatorMayReturnNull, "x0 as T").WithArguments("T").WithLocation(7, 16), // (9,9): warning CS8602: Dereference of a possibly null reference. // y0.ToString(); Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y0").WithLocation(9, 9) @@ -102035,8 +101657,8 @@ static void F2(S? ns) } else { - var t2 = (T)ns; // 4 - _ = t2.ToString(); // 5 + var t2 = (T)ns; + _ = t2.ToString(); // 4 } } }"; @@ -102051,13 +101673,9 @@ static void F2(S? ns) // (21,17): warning CS8602: Dereference of a possibly null reference. // _ = t1.ToString(); // 3 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t1").WithLocation(21, 17), - // (25,22): warning CS8601: Possible null reference assignment. - // var t2 = (T)ns; // 4 - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "(T)ns").WithLocation(25, 22), // (26,17): warning CS8602: Dereference of a possibly null reference. - // _ = t2.ToString(); // 5 - Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t2").WithLocation(26, 17) - ); + // _ = t2.ToString(); // 4 + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t2").WithLocation(26, 17)); } [Fact] @@ -105025,7 +104643,6 @@ void Test2(bool a) Diagnostic(ErrorCode.ERR_NameNotInContext, "b").WithArguments("b").WithLocation(18, 31)); } - [WorkItem(30140, "https://github.com/dotnet/roslyn/issues/30140")] [Fact] public void NullCoalescingAssignment_UseInExpression() @@ -105355,24 +104972,21 @@ public void Deconstruction_02() { static void F() where U : class { - (T x, U y) = (default, default); // 1, 2 - x.ToString(); // 3 - y.ToString(); // 4 + (T x, U y) = (default, default); // 1 + x.ToString(); // 2 + y.ToString(); // 3 } }"; var comp = CreateCompilation(source, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( - // (5,23): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // (T x, U y) = (default, default); // 1, 2 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(5, 23), // (5,32): warning CS8600: Converting null literal or possible null value to non-nullable type. - // (T x, U y) = (default, default); // 1, 2 + // (T x, U y) = (default, default); // 1 Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "default").WithLocation(5, 32), // (6,9): warning CS8602: Dereference of a possibly null reference. - // x.ToString(); // 3 + // x.ToString(); // 2 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(6, 9), // (7,9): warning CS8602: Dereference of a possibly null reference. - // y.ToString(); // 4 + // y.ToString(); // 3 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(7, 9)); } @@ -108315,9 +107929,9 @@ static void F8() }"; var comp = CreateCompilation(source, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( - // (10,19): warning CS8653: A default expression introduces a null value when 'T1' is a non-nullable reference type. + // (10,19): warning CS8601: Possible null reference assignment. // C.F = default; // 1 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T1").WithLocation(10, 19), + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(10, 19), // (11,9): warning CS8602: Dereference of a possibly null reference. // C.F.ToString(); // 2 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "C.F").WithLocation(11, 9), @@ -108393,9 +108007,9 @@ static void F8() }"; var comp = CreateCompilation(source, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( - // (13,19): warning CS8653: A default expression introduces a null value when 'T1' is a non-nullable reference type. + // (13,19): warning CS8601: Possible null reference assignment. // C.P = default; // 1 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T1").WithLocation(13, 19), + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(13, 19), // (14,9): warning CS8602: Dereference of a possibly null reference. // C.P.ToString(); // 2 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "C.P").WithLocation(14, 9), @@ -108443,9 +108057,9 @@ static void F3() where T3 : class }"; var comp = CreateCompilation(source, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( - // (10,19): warning CS8653: A default expression introduces a null value when 'T1' is a non-nullable reference type. + // (10,19): warning CS8601: Possible null reference assignment. // S.F = default; // 1 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T1").WithLocation(10, 19), + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(10, 19), // (11,9): warning CS8602: Dereference of a possibly null reference. // S.F.ToString(); // 2 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "S.F").WithLocation(11, 9), @@ -108489,9 +108103,9 @@ static void F3() where T3 : class // (3,23): warning CS8618: Non-nullable property 'P' is uninitialized. Consider declaring the property as nullable. // internal static T P { get; set; } Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "P").WithArguments("property", "P").WithLocation(3, 23), - // (9,19): warning CS8653: A default expression introduces a null value when 'T1' is a non-nullable reference type. + // (9,19): warning CS8601: Possible null reference assignment. // S.P = default; // 1 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T1").WithLocation(9, 19), + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(9, 19), // (10,9): warning CS8602: Dereference of a possibly null reference. // S.P.ToString(); // 2 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "S.P").WithLocation(10, 9), @@ -108791,24 +108405,24 @@ static IB CreateB(T t) static void F1(T x1) { if (x1 == null) return; - T y1 = default; // 1 + T y1 = default; var ix1 = CreateB(x1); - var iy1 = CreateB(y1); - ix1.A(y1); - ix1.B(y1); + var iy1 = CreateB(y1); // 1 + ix1.A(y1); // 2 + ix1.B(y1); // 3 iy1.A(x1); iy1.B(x1); } static void F2() where T : class, new() { - T x2 = null; // 2 + T x2 = null; // 4 T? y2 = new T(); var ix2 = CreateB(x2); var iy2 = CreateB(y2); ix2.A(y2); ix2.B(y2); - iy2.A(x2); // 3 - iy2.B(x2); // 4 + iy2.A(x2); // 5 + iy2.B(x2); // 6 } static void F3() where T : struct { @@ -108824,17 +108438,23 @@ static void F3() where T : struct }"; var comp = CreateCompilation(source, options: WithNonNullTypesTrue()); comp.VerifyDiagnostics( - // (18,16): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // T y1 = default; // 1 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(18, 16), + // (20,27): warning CS8604: Possible null reference argument for parameter 't' in 'IB Program.CreateB(T t)'. + // var iy1 = CreateB(y1); // 1 + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y1").WithArguments("t", "IB Program.CreateB(T t)").WithLocation(20, 27), + // (21,15): warning CS8604: Possible null reference argument for parameter 't' in 'void IA.A(T t)'. + // ix1.A(y1); // 2 + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y1").WithArguments("t", "void IA.A(T t)").WithLocation(21, 15), + // (22,15): warning CS8604: Possible null reference argument for parameter 't' in 'void IB.B(T t)'. + // ix1.B(y1); // 3 + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y1").WithArguments("t", "void IB.B(T t)").WithLocation(22, 15), // (28,16): warning CS8600: Converting null literal or possible null value to non-nullable type. - // T x2 = null; // 2 + // T x2 = null; // 4 Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(28, 16), // (34,15): warning CS8604: Possible null reference argument for parameter 't' in 'void IA.A(T t)'. - // iy2.A(x2); // 3 + // iy2.A(x2); // 5 Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x2").WithArguments("t", "void IA.A(T t)").WithLocation(34, 15), // (35,15): warning CS8604: Possible null reference argument for parameter 't' in 'void IB.B(T t)'. - // iy2.B(x2); // 4 + // iy2.B(x2); // 6 Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x2").WithArguments("t", "void IB.B(T t)").WithLocation(35, 15)); } @@ -110925,7 +110545,6 @@ class C2 : I where U : class Diagnostic(ErrorCode.WRN_NullabilityMismatchInReturnTypeOnExplicitImplementation, "Goo").WithArguments("U I.Goo(T? value)").WithLocation(18, 13)); } - [Fact] [WorkItem(34508, "https://github.com/dotnet/roslyn/issues/34508")] public void AmbiguousExplicitInterfaceImplementation() @@ -112558,9 +112177,6 @@ static void M() // (13,13): error CS1113: Extension method 'E.F(T)' defined on value type 'T' cannot be used to create delegates // d = default(T).F; Diagnostic(ErrorCode.ERR_ValueTypeExtDelegate, "default(T).F").WithArguments("E.F(T)", "T").WithLocation(13, 13), - // (13,13): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // d = default(T).F; - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(13, 13), // (15,13): error CS1113: Extension method 'E.F(V)' defined on value type 'V' cannot be used to create delegates // d = default(V).F; Diagnostic(ErrorCode.ERR_ValueTypeExtDelegate, "default(V).F").WithArguments("E.F(V)", "V").WithLocation(15, 13), @@ -112596,9 +112212,6 @@ static void M() // (12,19): error CS1113: Extension method 'E.F(T)' defined on value type 'T' cannot be used to create delegates // _ = new D(default(T).F); Diagnostic(ErrorCode.ERR_ValueTypeExtDelegate, "default(T).F").WithArguments("E.F(T)", "T").WithLocation(12, 19), - // (12,19): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. - // _ = new D(default(T).F); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(12, 19), // (14,19): error CS1113: Extension method 'E.F(V)' defined on value type 'V' cannot be used to create delegates // _ = new D(default(V).F); Diagnostic(ErrorCode.ERR_ValueTypeExtDelegate, "default(V).F").WithArguments("E.F(V)", "V").WithLocation(14, 19), @@ -115051,7 +114664,6 @@ void M(object? o3) }} }}"; - var comp = CreateCompilation(new[] { source1, source2 }, options: WithNonNullTypesFalse()); comp.VerifyDiagnostics(expectedDiagnostics); @@ -117908,5 +117520,1614 @@ static void F2(C<(T, T)> c) where T : class? Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t.Item1").WithLocation(24, 9) ); } + + [Fact] + [WorkItem(38339, "https://github.com/dotnet/roslyn/issues/38339")] + public void AllowNull_Default() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class C +{ + internal T _f1 = default(T); + internal T _f2 = default; + [AllowNull] internal T _f3 = default(T); + [AllowNull] internal T _f4 = default; +}"; + var comp = CreateCompilation(new[] { source, AllowNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (5,34): warning CS8601: Possible null reference assignment. + // internal T _f1 = default(T); + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default(T)").WithLocation(5, 34), + // (6,34): warning CS8601: Possible null reference assignment. + // internal T _f2 = default; + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(6, 34)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Join() + { + var source = +@"#nullable enable +class C where T : new() +{ + static T F1(bool b) + { + T t1; + if (b) t1 = default(T); + else t1 = default(T); + return t1; // 1 + } + static T F2(bool b, T t) + { + T t2; + if (b) t2 = default(T); + else t2 = t; + return t2; // 2 + } + static T F3(bool b) + { + T t3; + if (b) t3 = default(T); + else t3 = new T(); + return t3; // 3 + } + static T F4(bool b, T t) + { + T t4; + if (b) t4 = t; + else t4 = default(T); + return t4; // 4 + } + static T F5(bool b, T t) + { + T t5; + if (b) t5 = t; + else t5 = t; + return t5; + } + static T F6(bool b, T t) + { + T t6; + if (b) t6 = t; + else t6 = new T(); + return t6; + } + static T F7(bool b) + { + T t7; + if (b) t7 = new T(); + else t7 = default(T); + return t7; // 5 + } + static T F8(bool b, T t) + { + T t8; + if (b) t8 = new T(); + else t8 = t; + return t8; + } + static T F9(bool b) + { + T t9; + if (b) t9 = new T(); + else t9 = new T(); + return t9; + } +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics( + // (9,16): warning CS8603: Possible null reference return. + // return t1; // 1 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t1").WithLocation(9, 16), + // (16,16): warning CS8603: Possible null reference return. + // return t2; // 2 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t2").WithLocation(16, 16), + // (23,16): warning CS8603: Possible null reference return. + // return t3; // 3 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t3").WithLocation(23, 16), + // (30,16): warning CS8603: Possible null reference return. + // return t4; // 4 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t4").WithLocation(30, 16), + // (51,16): warning CS8603: Possible null reference return. + // return t7; // 5 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t7").WithLocation(51, 16)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Meet_01() + { + var source = +@"#nullable enable +class C where T : new() +{ + static T F1() + { + T t1; + try { t1 = default(T); } + finally { t1 = default(T); } + return t1; // 1 + } + static T F2(T t) + { + T t2; + try { t2 = default(T); } + finally { t2 = t; } + return t2; + } + static T F3() + { + T t3; + try { t3 = default(T); } + finally { t3 = new T(); } + return t3; + } + static T F4(T t) + { + T t4; + try { t4 = t; } + finally { t4 = default(T); } + return t4; // 2 + } + static T F5(T t) + { + T t5; + try { t5 = t; } + finally { t5 = t; } + return t5; + } + static T F6(T t) + { + T t6; + try { t6 = t; } + finally { t6 = new T(); } + return t6; + } + static T F7() + { + T t7; + try { t7 = new T(); } + finally { t7 = default(T); } + return t7; // 3 + } + static T F8(T t) + { + T t8; + try { t8 = new T(); } + finally { t8 = t; } + return t8; + } + static T F9() + { + T t9; + try { t9 = new T(); } + finally { t9 = new T(); } + return t9; + } +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics( + // (9,16): warning CS8603: Possible null reference return. + // return t1; // 1 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t1").WithLocation(9, 16), + // (30,16): warning CS8603: Possible null reference return. + // return t4; // 2 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t4").WithLocation(30, 16), + // (51,16): warning CS8603: Possible null reference return. + // return t7; // 3 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t7").WithLocation(51, 16)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Meet_02() + { + var source = +@"#nullable enable +class C where T : new() +{ + static void F0(T t) + { + } + static T F2(T t) + { + T t2 = t; + T r2; + try + { + t2 = default(T); + t2 = t; + } + finally + { + F0(t2); // 1 + r2 = t2; + } + return r2; // 2 + } + static T F3(T t) + { + T t3 = t; + T r3; + try + { + t3 = default(T); + t3 = new T(); + } + finally + { + F0(t3); // 3 + r3 = t3; + } + return r3; // 4 + } + static T F4(T t) + { + T t4 = t; + T r4; + try + { + t4 = t; + t4 = default(T); + } + finally + { + F0(t4); // 5 + r4 = t4; + } + return r4; // 6 + } + static T F6(T t) + { + T t6 = t; + T r6; + try + { + t6 = t; + t6 = new T(); + } + finally + { + F0(t6); + r6 = t6; + } + return r6; + } + static T F7(T t) + { + T t7 = t; + T r7; + try + { + t7 = new T(); + t7 = default(T); + } + finally + { + F0(t7); // 7 + r7 = t7; + } + return r7; // 8 + } + static T F8(T t) + { + T t8 = t; + T r8; + try + { + t8 = new T(); + t8 = t; + } + finally + { + F0(t8); + r8 = t8; + } + return r8; + } +}"; + var comp = CreateCompilation(source); + // Ideally, there should not be a warning for 2 or 4 because the return + // statements are only executed when no exceptions are thrown. + comp.VerifyDiagnostics( + // (18,16): warning CS8604: Possible null reference argument for parameter 't' in 'void C.F0(T t)'. + // F0(t2); // 1 + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void C.F0(T t)").WithLocation(18, 16), + // (21,16): warning CS8603: Possible null reference return. + // return r2; // 2 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "r2").WithLocation(21, 16), + // (34,16): warning CS8604: Possible null reference argument for parameter 't' in 'void C.F0(T t)'. + // F0(t3); // 3 + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void C.F0(T t)").WithLocation(34, 16), + // (37,16): warning CS8603: Possible null reference return. + // return r3; // 4 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "r3").WithLocation(37, 16), + // (50,16): warning CS8604: Possible null reference argument for parameter 't' in 'void C.F0(T t)'. + // F0(t4); // 5 + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t4").WithArguments("t", "void C.F0(T t)").WithLocation(50, 16), + // (53,16): warning CS8603: Possible null reference return. + // return r4; // 6 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "r4").WithLocation(53, 16), + // (82,16): warning CS8604: Possible null reference argument for parameter 't' in 'void C.F0(T t)'. + // F0(t7); // 7 + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t7").WithArguments("t", "void C.F0(T t)").WithLocation(82, 16), + // (85,16): warning CS8603: Possible null reference return. + // return r7; // 8 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "r7").WithLocation(85, 16)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Conversions_01() + { + var source = +@"#nullable enable +class Program +{ + static T F01() => default(T); + static T F02() where T : class => default(T); + static T F03() where T : struct => default(T); + static T F04() where T : notnull => default(T); + static T F05() where U : T => default(U); + static T F06() where U : class, T => default(U); + static T F07() where U : struct, T => default(U); + static T F08() where U : notnull, T => default(U); + static T F09() => (T)default(T); + static T F10() where T : class => (T)default(T); + static T F11() where T : struct => (T)default(T); + static T F12() where T : notnull => (T)default(T); + static T F13() where U : T => (T)default(U); + static T F14() where U : class, T => (T)default(U); + static T F15() where U : struct, T => (T)default(U); + static T F16() where U : notnull, T => (T)default(U); + static U F17() where U : T => (U)default(T); + static U F18() where U : class, T => (U)default(T); + static U F19() where U : struct, T => (U)default(T); + static U F20() where U : notnull, T => (U)default(T); + static U F21() => (U)(object)default(T); +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics( + // (4,26): warning CS8603: Possible null reference return. + // static T F01() => default(T); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T)").WithLocation(4, 26), + // (5,42): warning CS8603: Possible null reference return. + // static T F02() where T : class => default(T); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T)").WithLocation(5, 42), + // (7,44): warning CS8603: Possible null reference return. + // static T F04() where T : notnull => default(T); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T)").WithLocation(7, 44), + // (8,41): warning CS8603: Possible null reference return. + // static T F05() where U : T => default(U); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(U)").WithLocation(8, 41), + // (9,48): warning CS8603: Possible null reference return. + // static T F06() where U : class, T => default(U); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(U)").WithLocation(9, 48), + // (11,50): warning CS8603: Possible null reference return. + // static T F08() where U : notnull, T => default(U); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(U)").WithLocation(11, 50), + // (12,26): warning CS8603: Possible null reference return. + // static T F09() => (T)default(T); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)default(T)").WithLocation(12, 26), + // (13,42): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static T F10() where T : class => (T)default(T); + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(T)default(T)").WithLocation(13, 42), + // (13,42): warning CS8603: Possible null reference return. + // static T F10() where T : class => (T)default(T); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)default(T)").WithLocation(13, 42), + // (15,44): warning CS8603: Possible null reference return. + // static T F12() where T : notnull => (T)default(T); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)default(T)").WithLocation(15, 44), + // (16,41): warning CS8603: Possible null reference return. + // static T F13() where U : T => (T)default(U); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)default(U)").WithLocation(16, 41), + // (17,48): warning CS8603: Possible null reference return. + // static T F14() where U : class, T => (T)default(U); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)default(U)").WithLocation(17, 48), + // (19,50): warning CS8603: Possible null reference return. + // static T F16() where U : notnull, T => (T)default(U); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)default(U)").WithLocation(19, 50), + // (20,41): warning CS8603: Possible null reference return. + // static U F17() where U : T => (U)default(T); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(U)default(T)").WithLocation(20, 41), + // (21,48): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static U F18() where U : class, T => (U)default(T); + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(U)default(T)").WithLocation(21, 48), + // (21,48): warning CS8603: Possible null reference return. + // static U F18() where U : class, T => (U)default(T); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(U)default(T)").WithLocation(21, 48), + // (22,49): warning CS8605: Unboxing a possibly null value. + // static U F19() where U : struct, T => (U)default(T); + Diagnostic(ErrorCode.WRN_UnboxPossibleNull, "(U)default(T)").WithLocation(22, 49), + // (23,50): warning CS8603: Possible null reference return. + // static U F20() where U : notnull, T => (U)default(T); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(U)default(T)").WithLocation(23, 50), + // (24,29): warning CS8603: Possible null reference return. + // static U F21() => (U)(object)default(T); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(U)(object)default(T)").WithLocation(24, 29), + // (24,32): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static U F21() => (U)(object)default(T); + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(object)default(T)").WithLocation(24, 32)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Conversions_02() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + [return: MaybeNull]static T F01() => default(T); + [return: MaybeNull]static T F02() where T : class => default(T); + [return: MaybeNull]static T F03() where T : struct => default(T); + [return: MaybeNull]static T F04() where T : notnull => default(T); + [return: MaybeNull]static T F05() where U : T => default(U); + [return: MaybeNull]static T F06() where U : class, T => default(U); + [return: MaybeNull]static T F07() where U : struct, T => default(U); + [return: MaybeNull]static T F08() where U : notnull, T => default(U); + [return: MaybeNull]static T F09() => (T)default(T); + [return: MaybeNull]static T F10() where T : class => (T)default(T); + [return: MaybeNull]static T F11() where T : struct => (T)default(T); + [return: MaybeNull]static T F12() where T : notnull => (T)default(T); + [return: MaybeNull]static T F13() where U : T => (T)default(U); + [return: MaybeNull]static T F14() where U : class, T => (T)default(U); + [return: MaybeNull]static T F15() where U : struct, T => (T)default(U); + [return: MaybeNull]static T F16() where U : notnull, T => (T)default(U); + [return: MaybeNull]static U F17() where U : T => (U)default(T); + [return: MaybeNull]static U F18() where U : class, T => (U)default(T); + [return: MaybeNull]static U F19() where U : struct, T => (U)default(T); + [return: MaybeNull]static U F20() where U : notnull, T => (U)default(T); + [return: MaybeNull]static U F21() => (U)(object)default(T); +}"; + var comp = CreateCompilation(new[] { source, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (14,61): warning CS8600: Converting null literal or possible null value to non-nullable type. + // [return: MaybeNull]static T F10() where T : class => (T)default(T); + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(T)default(T)").WithLocation(14, 61), + // (22,67): warning CS8600: Converting null literal or possible null value to non-nullable type. + // [return: MaybeNull]static U F18() where U : class, T => (U)default(T); + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(U)default(T)").WithLocation(22, 67), + // (23,68): warning CS8605: Unboxing a possibly null value. + // [return: MaybeNull]static U F19() where U : struct, T => (U)default(T); + Diagnostic(ErrorCode.WRN_UnboxPossibleNull, "(U)default(T)").WithLocation(23, 68), + // (25,51): warning CS8600: Converting null literal or possible null value to non-nullable type. + // [return: MaybeNull]static U F21() => (U)(object)default(T); + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(object)default(T)").WithLocation(25, 51)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Conversions_02A() + { + var source = +@"#nullable enable +class Program +{ + static T? F02() where T : class => default(T); + static T? F10() where T : class => (T)default(T); + static U? F18() where U : class, T => (U)default(T); +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics( + // (5,43): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static T? F10() where T : class => (T)default(T); + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(T)default(T)").WithLocation(5, 43), + // (6,49): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static U? F18() where U : class, T => (U)default(T); + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(U)default(T)").WithLocation(6, 49)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Conversions_03() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + static T F01([AllowNull]T t) => t; + static T F02([AllowNull]T t) where T : class => t; + static T F03([AllowNull]T t) where T : struct => t; + static T F04([AllowNull]T t) where T : notnull => t; + static T F05([AllowNull]U u) where U : T => u; + static T F06([AllowNull]U u) where U : class, T => u; + static T F07([AllowNull]U u) where U : struct, T => u; + static T F08([AllowNull]U u) where U : notnull, T => u; + static T F09([AllowNull]T t) => (T)t; + static T F10([AllowNull]T t) where T : class => (T)t; + static T F11([AllowNull]T t) where T : struct => (T)t; + static T F12([AllowNull]T t) where T : notnull => (T)t; + static T F13([AllowNull]U u) where U : T => (T)u; + static T F14([AllowNull]U u) where U : class, T => (T)u; + static T F15([AllowNull]U u) where U : struct, T => (T)u; + static T F16([AllowNull]U u) where U : notnull, T => (T)u; + static U F17([AllowNull]T t) where U : T => (U)t; + static U F18([AllowNull]T t) where U : class, T => (U)t; + static U F19([AllowNull]T t) where U : struct, T => (U)t; + static U F20([AllowNull]T t) where U : notnull, T => (U)t; + static U F21([AllowNull]T t) => (U)(object)t; +}"; + var comp = CreateCompilation(new[] { source, AllowNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (5,40): warning CS8603: Possible null reference return. + // static T F01([AllowNull]T t) => t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t").WithLocation(5, 40), + // (6,56): warning CS8603: Possible null reference return. + // static T F02([AllowNull]T t) where T : class => t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t").WithLocation(6, 56), + // (8,58): warning CS8603: Possible null reference return. + // static T F04([AllowNull]T t) where T : notnull => t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t").WithLocation(8, 58), + // (9,55): warning CS8603: Possible null reference return. + // static T F05([AllowNull]U u) where U : T => u; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "u").WithLocation(9, 55), + // (10,62): warning CS8603: Possible null reference return. + // static T F06([AllowNull]U u) where U : class, T => u; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "u").WithLocation(10, 62), + // (12,64): warning CS8603: Possible null reference return. + // static T F08([AllowNull]U u) where U : notnull, T => u; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "u").WithLocation(12, 64), + // (13,40): warning CS8603: Possible null reference return. + // static T F09([AllowNull]T t) => (T)t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)t").WithLocation(13, 40), + // (14,56): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static T F10([AllowNull]T t) where T : class => (T)t; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(T)t").WithLocation(14, 56), + // (14,56): warning CS8603: Possible null reference return. + // static T F10([AllowNull]T t) where T : class => (T)t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)t").WithLocation(14, 56), + // (16,58): warning CS8603: Possible null reference return. + // static T F12([AllowNull]T t) where T : notnull => (T)t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)t").WithLocation(16, 58), + // (17,55): warning CS8603: Possible null reference return. + // static T F13([AllowNull]U u) where U : T => (T)u; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)u").WithLocation(17, 55), + // (18,62): warning CS8603: Possible null reference return. + // static T F14([AllowNull]U u) where U : class, T => (T)u; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)u").WithLocation(18, 62), + // (20,64): warning CS8603: Possible null reference return. + // static T F16([AllowNull]U u) where U : notnull, T => (T)u; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)u").WithLocation(20, 64), + // (21,55): warning CS8603: Possible null reference return. + // static U F17([AllowNull]T t) where U : T => (U)t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(U)t").WithLocation(21, 55), + // (22,62): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static U F18([AllowNull]T t) where U : class, T => (U)t; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(U)t").WithLocation(22, 62), + // (22,62): warning CS8603: Possible null reference return. + // static U F18([AllowNull]T t) where U : class, T => (U)t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(U)t").WithLocation(22, 62), + // (23,63): warning CS8605: Unboxing a possibly null value. + // static U F19([AllowNull]T t) where U : struct, T => (U)t; + Diagnostic(ErrorCode.WRN_UnboxPossibleNull, "(U)t").WithLocation(23, 63), + // (24,64): warning CS8603: Possible null reference return. + // static U F20([AllowNull]T t) where U : notnull, T => (U)t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(U)t").WithLocation(24, 64), + // (25,43): warning CS8603: Possible null reference return. + // static U F21([AllowNull]T t) => (U)(object)t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(U)(object)t").WithLocation(25, 43), + // (25,46): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static U F21([AllowNull]T t) => (U)(object)t; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(object)t").WithLocation(25, 46)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Conversions_03A() + { + var source = +@"#nullable enable +class Program +{ + static T F02(T? t) where T : class => t; + static T F06(U? u) where U : class, T => u; + static T F10(T? t) where T : class => (T)t; + static T F14(U? u) where U : class, T => (T)u; +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics( + // (4,46): warning CS8603: Possible null reference return. + // static T F02(T? t) where T : class => t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t").WithLocation(4, 46), + // (5,52): warning CS8603: Possible null reference return. + // static T F06(U? u) where U : class, T => u; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "u").WithLocation(5, 52), + // (6,46): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static T F10(T? t) where T : class => (T)t; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(T)t").WithLocation(6, 46), + // (6,46): warning CS8603: Possible null reference return. + // static T F10(T? t) where T : class => (T)t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)t").WithLocation(6, 46), + // (7,52): warning CS8603: Possible null reference return. + // static T F14(U? u) where U : class, T => (T)u; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)u").WithLocation(7, 52)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Conversions_04() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + [return: MaybeNull]static T F01(T t) => t; + [return: MaybeNull]static T F02(T t) where T : class => t; + [return: MaybeNull]static T F03(T t) where T : struct => t; + [return: MaybeNull]static T F04(T t) where T : notnull => t; + [return: MaybeNull]static T F05(U u) where U : T => u; + [return: MaybeNull]static T F06(U u) where U : class, T => u; + [return: MaybeNull]static T F07(U u) where U : struct, T => u; + [return: MaybeNull]static T F08(U u) where U : notnull, T => u; + [return: MaybeNull]static T F09(T t) => (T)t; + [return: MaybeNull]static T F10(T t) where T : class => (T)t; + [return: MaybeNull]static T F11(T t) where T : struct => (T)t; + [return: MaybeNull]static T F12(T t) where T : notnull => (T)t; + [return: MaybeNull]static T F13(U u) where U : T => (T)u; + [return: MaybeNull]static T F14(U u) where U : class, T => (T)u; + [return: MaybeNull]static T F15(U u) where U : struct, T => (T)u; + [return: MaybeNull]static T F16(U u) where U : notnull, T => (T)u; + [return: MaybeNull]static U F17(T t) where U : T => (U)t; + [return: MaybeNull]static U F18(T t) where U : class, T => (U)t; + [return: MaybeNull]static U F19(T t) where U : struct, T => (U)t; + [return: MaybeNull]static U F20(T t) where U : notnull, T => (U)t; + [return: MaybeNull]static U F21(T t) => (U)(object)t; +}"; + var comp = CreateCompilation(new[] { source, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (22,70): warning CS8600: Converting null literal or possible null value to non-nullable type. + // [return: MaybeNull]static U F18(T t) where U : class, T => (U)t; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(U)t").WithLocation(22, 70), + // (23,71): warning CS8605: Unboxing a possibly null value. + // [return: MaybeNull]static U F19(T t) where U : struct, T => (U)t; + Diagnostic(ErrorCode.WRN_UnboxPossibleNull, "(U)t").WithLocation(23, 71), + // (25,54): warning CS8600: Converting null literal or possible null value to non-nullable type. + // [return: MaybeNull]static U F21(T t) => (U)(object)t; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(object)t").WithLocation(25, 54)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Conversions_04A() + { + var source = +@"#nullable enable +class Program +{ + static T? F02(T t) where T : class => t; + static T? F10(T t) where T : class => (T)t; + static U? F18(T t) where U : class, T => (U)t; +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics( + // (6,52): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static U? F18(T t) where U : class, T => (U)t; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(U)t").WithLocation(6, 52)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Conversions_05() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + [return: MaybeNull]static T F01([AllowNull]T t) => t; + [return: MaybeNull]static T F02([AllowNull]T t) where T : class => t; + [return: MaybeNull]static T F03([AllowNull]T t) where T : struct => t; + [return: MaybeNull]static T F04([AllowNull]T t) where T : notnull => t; + [return: MaybeNull]static T F05([AllowNull]U u) where U : T => u; + [return: MaybeNull]static T F06([AllowNull]U u) where U : class, T => u; + [return: MaybeNull]static T F07([AllowNull]U u) where U : struct, T => u; + [return: MaybeNull]static T F08([AllowNull]U u) where U : notnull, T => u; + [return: MaybeNull]static T F09([AllowNull]T t) => (T)t; + [return: MaybeNull]static T F10([AllowNull]T t) where T : class => (T)t; + [return: MaybeNull]static T F11([AllowNull]T t) where T : struct => (T)t; + [return: MaybeNull]static T F12([AllowNull]T t) where T : notnull => (T)t; + [return: MaybeNull]static T F13([AllowNull]U u) where U : T => (T)u; + [return: MaybeNull]static T F14([AllowNull]U u) where U : class, T => (T)u; + [return: MaybeNull]static T F15([AllowNull]U u) where U : struct, T => (T)u; + [return: MaybeNull]static T F16([AllowNull]U u) where U : notnull, T => (T)u; + [return: MaybeNull]static U F17([AllowNull]T t) where U : T => (U)t; + [return: MaybeNull]static U F18([AllowNull]T t) where U : class, T => (U)t; + [return: MaybeNull]static U F19([AllowNull]T t) where U : struct, T => (U)t; + [return: MaybeNull]static U F20([AllowNull]T t) where U : notnull, T => (U)t; + [return: MaybeNull]static U F21([AllowNull]T t) => (U)(object)t; +}"; + var comp = CreateCompilation(new[] { source, AllowNullAttributeDefinition, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (14,75): warning CS8600: Converting null literal or possible null value to non-nullable type. + // [return: MaybeNull]static T F10([AllowNull]T t) where T : class => (T)t; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(T)t").WithLocation(14, 75), + // (22,81): warning CS8600: Converting null literal or possible null value to non-nullable type. + // [return: MaybeNull]static U F18([AllowNull]T t) where U : class, T => (U)t; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(U)t").WithLocation(22, 81), + // (23,82): warning CS8605: Unboxing a possibly null value. + // [return: MaybeNull]static U F19([AllowNull]T t) where U : struct, T => (U)t; + Diagnostic(ErrorCode.WRN_UnboxPossibleNull, "(U)t").WithLocation(23, 82), + // (25,65): warning CS8600: Converting null literal or possible null value to non-nullable type. + // [return: MaybeNull]static U F21([AllowNull]T t) => (U)(object)t; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(object)t").WithLocation(25, 65)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Conversions_05A() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + static T? F02(T? t) where T : class => t; + static T? F10(T? t) where T : class => (T)t; + static U? F18([AllowNull]T t) where U : class, T => (U)t; +}"; + var comp = CreateCompilation(new[] { source, AllowNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (6,47): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static T? F10(T? t) where T : class => (T)t; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(T)t").WithLocation(6, 47), + // (7,63): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static U? F18([AllowNull]T t) where U : class, T => (U)t; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(U)t").WithLocation(7, 63)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Conversions_06() + { + var source = +@"#nullable enable +class Program +{ + static T F01(dynamic? d) => d; + static T F02(dynamic? d) where T : class => d; + static T F03(dynamic? d) where T : struct => d; + static T F04(dynamic? d) where T : notnull => d; + static T F09(dynamic? d) => (T)d; + static T F10(dynamic? d) where T : class => (T)d; + static T F11(dynamic? d) where T : struct => (T)d; + static T F12(dynamic? d) where T : notnull => (T)d; +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics( + // (4,36): warning CS8603: Possible null reference return. + // static T F01(dynamic? d) => d; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "d").WithLocation(4, 36), + // (5,52): warning CS8603: Possible null reference return. + // static T F02(dynamic? d) where T : class => d; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "d").WithLocation(5, 52), + // (7,54): warning CS8603: Possible null reference return. + // static T F04(dynamic? d) where T : notnull => d; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "d").WithLocation(7, 54), + // (8,36): warning CS8603: Possible null reference return. + // static T F09(dynamic? d) => (T)d; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)d").WithLocation(8, 36), + // (9,52): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static T F10(dynamic? d) where T : class => (T)d; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(T)d").WithLocation(9, 52), + // (9,52): warning CS8603: Possible null reference return. + // static T F10(dynamic? d) where T : class => (T)d; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)d").WithLocation(9, 52), + // (11,54): warning CS8603: Possible null reference return. + // static T F12(dynamic? d) where T : notnull => (T)d; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(T)d").WithLocation(11, 54)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Conversions_07() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + [return: MaybeNull]static T F01(dynamic? d) => d; + [return: MaybeNull]static T F02(dynamic? d) where T : class => d; + [return: MaybeNull]static T F03(dynamic? d) where T : struct => d; + [return: MaybeNull]static T F04(dynamic? d) where T : notnull => d; + [return: MaybeNull]static T F09(dynamic? d) => (T)d; + [return: MaybeNull]static T F10(dynamic? d) where T : class => (T)d; + [return: MaybeNull]static T F11(dynamic? d) where T : struct => (T)d; + [return: MaybeNull]static T F12(dynamic? d) where T : notnull => (T)d; +}"; + var comp = CreateCompilation(new[] { source, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (10,71): warning CS8600: Converting null literal or possible null value to non-nullable type. + // [return: MaybeNull]static T F10(dynamic? d) where T : class => (T)d; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(T)d").WithLocation(10, 71)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Conversions_08() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + static dynamic F01([AllowNull]T t) => t; + static dynamic F02([AllowNull]T t) where T : class => t; + static dynamic F03([AllowNull]T t) where T : struct => t; + static dynamic F04([AllowNull]T t) where T : notnull => t; + static dynamic F09([AllowNull]T t) => (dynamic)t; + static dynamic F10([AllowNull]T t) where T : class => (dynamic)t; + static dynamic F11([AllowNull]T t) where T : struct => (dynamic)t; + static dynamic F12([AllowNull]T t) where T : notnull => (dynamic)t; +}"; + var comp = CreateCompilation(new[] { source, AllowNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (5,46): warning CS8603: Possible null reference return. + // static dynamic F01([AllowNull]T t) => t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t").WithLocation(5, 46), + // (6,62): warning CS8603: Possible null reference return. + // static dynamic F02([AllowNull]T t) where T : class => t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t").WithLocation(6, 62), + // (8,64): warning CS8603: Possible null reference return. + // static dynamic F04([AllowNull]T t) where T : notnull => t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t").WithLocation(8, 64), + // (9,46): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static dynamic F09([AllowNull]T t) => (dynamic)t; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(dynamic)t").WithLocation(9, 46), + // (9,46): warning CS8603: Possible null reference return. + // static dynamic F09([AllowNull]T t) => (dynamic)t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(dynamic)t").WithLocation(9, 46), + // (10,62): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static dynamic F10([AllowNull]T t) where T : class => (dynamic)t; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(dynamic)t").WithLocation(10, 62), + // (10,62): warning CS8603: Possible null reference return. + // static dynamic F10([AllowNull]T t) where T : class => (dynamic)t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(dynamic)t").WithLocation(10, 62), + // (12,64): warning CS8600: Converting null literal or possible null value to non-nullable type. + // static dynamic F12([AllowNull]T t) where T : notnull => (dynamic)t; + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(dynamic)t").WithLocation(12, 64), + // (12,64): warning CS8603: Possible null reference return. + // static dynamic F12([AllowNull]T t) where T : notnull => (dynamic)t; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "(dynamic)t").WithLocation(12, 64)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_Conversions_09() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + static dynamic? F01([AllowNull]T t) => t; + static dynamic? F02([AllowNull]T t) where T : class => t; + static dynamic? F03([AllowNull]T t) where T : struct => t; + static dynamic? F04([AllowNull]T t) where T : notnull => t; + static dynamic? F09([AllowNull]T t) => (dynamic?)t; + static dynamic? F10([AllowNull]T t) where T : class => (dynamic?)t; + static dynamic? F11([AllowNull]T t) where T : struct => (dynamic?)t; + static dynamic? F12([AllowNull]T t) where T : notnull => (dynamic?)t; +}"; + var comp = CreateCompilation(new[] { source, AllowNullAttributeDefinition }); + comp.VerifyDiagnostics(); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_01() + { + var source = +@"#nullable enable +using System; +using System.Diagnostics.CodeAnalysis; +class C where T : class? +{ + public C(T x) => f = x; + T f; + T P1 { get => f; set => f = value; } + [AllowNull] T P2 { get => f; set => f = value ?? throw new ArgumentNullException(); } + [MaybeNull] T P3 { get => default!; set => f = value; } + void M1() + { + P1 = null; // 1 + P2 = null; + P3 = null; // 2 + } + void M2() + { + f = P1; + f = P2; + f = P3; // 3 + } +}"; + var comp = CreateCompilation(new[] { source, AllowNullAttributeDefinition, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (13,14): warning CS8625: Cannot convert null literal to non-nullable reference type. + // P1 = null; // 1 + Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(13, 14), + // (15,14): warning CS8625: Cannot convert null literal to non-nullable reference type. + // P3 = null; // 2 + Diagnostic(ErrorCode.WRN_NullAsNonNullable, "null").WithLocation(15, 14), + // (21,13): warning CS8601: Possible null reference assignment. + // f = P3; // 3 + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "P3").WithLocation(21, 13)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_02() + { + var source = +@"#nullable enable +class C +{ + internal T F = default!; + static C F0() => throw null!; + static T F1() + { + T t = default(T); + return t; // 1 + } + static void F2() + { + var t = default(T); + F0().F = t; // 2 + } +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics( + // (9,16): warning CS8603: Possible null reference return. + // return t; // 1 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t").WithLocation(9, 16), + // (14,18): warning CS8601: Possible null reference assignment. + // F0().F = t; // 2 + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "t").WithLocation(14, 18)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_03() + { + var source = +@"#nullable enable +class Program +{ + static void M() where T : notnull + { + if (default(T) == null) { } + } +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics(); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_04() + { + var source = +@"#nullable enable +#pragma warning disable 649 +using System.Diagnostics.CodeAnalysis; +class C +{ + [MaybeNull]T F = default!; + static void M(C x, C y) + { + if (x.F == null) return; + x.F.ToString(); + y.F.ToString(); // 1 + } +}"; + var comp = CreateCompilation(new[] { source, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (11,9): warning CS8602: Dereference of a possibly null reference. + // y.F.ToString(); // 1 + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y.F").WithLocation(11, 9)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_05() + { + var source = +@"#nullable enable +class Program +{ + static void M() where T : notnull + { + var t = default(T); + t.ToString(); // 1 + } +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics( + // (7,9): warning CS8602: Dereference of a possibly null reference. + // t.ToString(); // 1 + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t").WithLocation(7, 9)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_06() + { + var source = +@"#nullable enable +class Program +{ + static void M() where T : notnull + { + T t = default; + t.ToString(); // 1 + } +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics( + // (7,9): warning CS8602: Dereference of a possibly null reference. + // t.ToString(); // 1 + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t").WithLocation(7, 9)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_07() + { + var source = +@"#nullable enable +class Program +{ + static void M(T t) where T : notnull + { + if (t == null) { } + t.ToString(); // 1 + } +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics( + // (7,9): warning CS8602: Dereference of a possibly null reference. + // t.ToString(); // 1 + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t").WithLocation(7, 9)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_08() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + [return: MaybeNull]static T F() => throw null!; + static T1 F1() + { + return F(); // 1 + } + static T2 F2() where T2 : notnull + { + return F(); // 2 + } + static T3 F3() where T3 : class + { + return F(); // 3 + } + static T4 F4() where T4 : class? + { + return F(); // 4 + } + static T5 F5() where T5 : struct + { + return F(); + } +}"; + var comp = CreateCompilation(new[] { source, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (8,16): warning CS8603: Possible null reference return. + // return F(); // 1 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "F()").WithLocation(8, 16), + // (12,16): warning CS8603: Possible null reference return. + // return F(); // 2 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "F()").WithLocation(12, 16), + // (16,16): warning CS8603: Possible null reference return. + // return F(); // 3 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "F()").WithLocation(16, 16), + // (20,16): warning CS8603: Possible null reference return. + // return F(); // 4 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "F()").WithLocation(20, 16)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_09() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + [return: MaybeNull]static T F() => throw null!; + [return: MaybeNull]static T1 F1() + { + return F(); + } + [return: MaybeNull]static T2 F2() where T2 : notnull + { + return F(); + } + [return: MaybeNull]static T3 F3() where T3 : class + { + return F(); + } + [return: MaybeNull]static T4 F4() where T4 : class? + { + return F(); + } + [return: MaybeNull]static T5 F5() where T5 : struct + { + return F(); + } +}"; + var comp = CreateCompilation(new[] { source, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics(); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_10() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + [return: MaybeNull]static T F() => throw null!; + static T1 F1() + { + T1 t1 = F(); + return t1; + } + static T2 F2() where T2 : notnull + { + T2 t2 = F(); + return t2; + } + static T3 F3() where T3 : class + { + T3 t3 = F(); + return t3; + } + static T4 F4() where T4 : class? + { + T4 t4 = F(); + return t4; + } + static T5 F5() where T5 : struct + { + T5 t5 = F(); + return t5; + } +}"; + var comp = CreateCompilation(new[] { source, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (9,16): warning CS8603: Possible null reference return. + // return t1; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t1").WithLocation(9, 16), + // (14,16): warning CS8603: Possible null reference return. + // return t2; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t2").WithLocation(14, 16), + // (18,17): warning CS8600: Converting null literal or possible null value to non-nullable type. + // T3 t3 = F(); + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "F()").WithLocation(18, 17), + // (19,16): warning CS8603: Possible null reference return. + // return t3; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t3").WithLocation(19, 16), + // (24,16): warning CS8603: Possible null reference return. + // return t4; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t4").WithLocation(24, 16)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_11() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + [return: MaybeNull]static T F() => throw null!; + static T M() where T : notnull + { + var t = F(); + return t; // 1 + } +}"; + var comp = CreateCompilation(new[] { source, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (9,16): warning CS8603: Possible null reference return. + // return t; // 1 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t").WithLocation(9, 16)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_12() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + static T Identity(T t) => t; + [return: MaybeNull]static T F() => throw null!; + static T F1() => Identity(F()); // 1 + static T F2() where T : notnull => Identity(F()); // 2 + static T F3() where T : class => Identity(F()); // 3 + static T F4() where T : class? => Identity(F()); // 4 + static T F5() where T : struct => Identity(F()); +}"; + var comp = CreateCompilation(new[] { source, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (7,34): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.Identity(T t)'. + // static T F1() => Identity(F()); // 1 + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "F()").WithArguments("t", "T Program.Identity(T t)").WithLocation(7, 34), + // (8,52): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.Identity(T t)'. + // static T F2() where T : notnull => Identity(F()); // 2 + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "F()").WithArguments("t", "T Program.Identity(T t)").WithLocation(8, 52), + // (9,41): warning CS8603: Possible null reference return. + // static T F3() where T : class => Identity(F()); // 3 + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "Identity(F())").WithLocation(9, 41), + // (10,51): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.Identity(T t)'. + // static T F4() where T : class? => Identity(F()); // 4 + Diagnostic(ErrorCode.WRN_NullReferenceArgument, "F()").WithArguments("t", "T Program.Identity(T t)").WithLocation(10, 51)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_13() + { + var source = +@"#nullable enable +class Program +{ + static void F1(object? x1) + { + var y1 = (T)x1; + _ = y1.ToString(); // 1 + } + static void F2(object? x2) + { + var y2 = (T)x2!; + _ = y2.ToString(); + } +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics( + // (7,13): warning CS8602: Dereference of a possibly null reference. + // _ = y1.ToString(); // 1 + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y1").WithLocation(7, 13)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_14() + { + var source = +@"#nullable enable +#pragma warning disable 414 +using System.Diagnostics.CodeAnalysis; +class C +{ + T F1 = default; + [AllowNull]T F2 = default; + [MaybeNull]T F3 = default; +}"; + var comp = CreateCompilation(new[] { source, AllowNullAttributeDefinition, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (6,12): warning CS8601: Possible null reference assignment. + // T F1 = default; + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(6, 12), + // (8,23): warning CS8601: Possible null reference assignment. + // [MaybeNull]T F3 = default; + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(8, 23)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_15() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class C +{ + T F1 = default!; + [AllowNull]T F2 = default!; + [MaybeNull]T F3 = default!; + void M1(T x, [AllowNull]T y) + { + F1 = x; + F2 = x; + F3 = x; + F1 = y; // 1 + F2 = y; + F3 = y; // 2 + } +}"; + var comp = CreateCompilation(new[] { source, AllowNullAttributeDefinition, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (13,14): warning CS8601: Possible null reference assignment. + // F1 = y; // 1 + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "y").WithLocation(13, 14), + // (15,14): warning CS8601: Possible null reference assignment. + // F3 = y; // 2 + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "y").WithLocation(15, 14)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_16() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + static T F1(bool b, T t, U u) where U : T => b ? t : u; + static T F2(bool b, T t, [AllowNull]U u) where U : T => b ? t : u; + static T F3(bool b, [AllowNull]T t, U u) where U : T => b ? t : u; + static T F4(bool b, [AllowNull]T t, [AllowNull]U u) where U : T => b ? t : u; +}"; + var comp = CreateCompilation(new[] { source, AllowNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (6,67): warning CS8603: Possible null reference return. + // static T F2(bool b, T t, [AllowNull]U u) where U : T => b ? t : u; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "b ? t : u").WithLocation(6, 67), + // (7,67): warning CS8603: Possible null reference return. + // static T F3(bool b, [AllowNull]T t, U u) where U : T => b ? t : u; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "b ? t : u").WithLocation(7, 67), + // (8,78): warning CS8603: Possible null reference return. + // static T F4(bool b, [AllowNull]T t, [AllowNull]U u) where U : T => b ? t : u; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "b ? t : u").WithLocation(8, 78)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_17() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + static T F0(T t) => t ?? default; + static T F1(T t) => t ?? default(T); + static T F2(T t) where U : T => t ?? default(U); + static T F3(T t, U u) where U : T => t ?? u; + static T F4(T t, [AllowNull]U u) where U : T => t ?? u; + static T F5([AllowNull]T t, U u) where U : T => t ?? u; + static T F6([AllowNull]T t, [AllowNull]U u) where U : T => t ?? u; +}"; + var comp = CreateCompilation(new[] { source, AllowNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (5,28): warning CS8603: Possible null reference return. + // static T F0(T t) => t ?? default; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t ?? default").WithLocation(5, 28), + // (6,28): warning CS8603: Possible null reference return. + // static T F1(T t) => t ?? default(T); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t ?? default(T)").WithLocation(6, 28), + // (7,43): warning CS8603: Possible null reference return. + // static T F2(T t) where U : T => t ?? default(U); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t ?? default(U)").WithLocation(7, 43), + // (9,59): warning CS8603: Possible null reference return. + // static T F4(T t, [AllowNull]U u) where U : T => t ?? u; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t ?? u").WithLocation(9, 59), + // (11,70): warning CS8603: Possible null reference return. + // static T F6([AllowNull]T t, [AllowNull]U u) where U : T => t ?? u; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t ?? u").WithLocation(11, 70)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_18() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + static T F0(T t) => t ??= default; + static T F1(T t) => t ??= default(T); + static T F2(T t) where U : T => t ??= default(U); + static T F3(T t, U u) where U : T => t ??= u; + static T F4(T t, [AllowNull]U u) where U : T => t ??= u; + static T F5([AllowNull]T t, U u) where U : T => t ??= u; + static T F6([AllowNull]T t, [AllowNull]U u) where U : T => t ??= u; +}"; + var comp = CreateCompilation(new[] { source, AllowNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (5,28): warning CS8603: Possible null reference return. + // static T F0(T t) => t ??= default; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t ??= default").WithLocation(5, 28), + // (6,28): warning CS8603: Possible null reference return. + // static T F1(T t) => t ??= default(T); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t ??= default(T)").WithLocation(6, 28), + // (7,43): warning CS8603: Possible null reference return. + // static T F2(T t) where U : T => t ??= default(U); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t ??= default(U)").WithLocation(7, 43), + // (9,59): warning CS8603: Possible null reference return. + // static T F4(T t, [AllowNull]U u) where U : T => t ??= u; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t ??= u").WithLocation(9, 59), + // (11,70): warning CS8603: Possible null reference return. + // static T F6([AllowNull]T t, [AllowNull]U u) where U : T => t ??= u; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t ??= u").WithLocation(11, 70)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_19() + { + var source = +@"#nullable enable +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +class Program +{ + static IEnumerable F(T t1, [AllowNull]T t2) + { + yield return default(T); + yield return t1; + yield return t2; + } + static IEnumerator F(U u1, [AllowNull]U u2) where U : T + { + yield return default(U); + yield return u1; + yield return u2; + } +}"; + var comp = CreateCompilation(new[] { source, AllowNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (8,22): warning CS8603: Possible null reference return. + // yield return default(T); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T)").WithLocation(8, 22), + // (10,22): warning CS8603: Possible null reference return. + // yield return t2; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "t2").WithLocation(10, 22), + // (14,22): warning CS8603: Possible null reference return. + // yield return default(U); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(U)").WithLocation(14, 22), + // (16,22): warning CS8603: Possible null reference return. + // yield return u2; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "u2").WithLocation(16, 22)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_20() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + [return: MaybeNull] static T F() => default; + static void F1() { _ = F(); } + static void F2() where T : class { _ = F(); } + static void F3() where T : struct { _ = F(); } + static void F4() where T : notnull { _ = F(); } +}"; + var comp = CreateCompilation(new[] { source, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics(); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + [WorkItem(39888, "https://github.com/dotnet/roslyn/issues/39888")] + public void MaybeNullT_21() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +class Program +{ + [return: MaybeNull]static T F1(bool b, T t) => b switch { false => t, _ => default }; + [return: MaybeNull]static T F2(bool b, T t) => b switch { false => default, _ => t }; + [return: MaybeNull]static T F3(bool b, T t) where T : class => b switch { false => t, _ => default }; + [return: MaybeNull]static T F4(bool b, T t) where T : class => b switch { false => default, _ => t }; + [return: MaybeNull]static T F5(bool b, T t) where T : struct => b switch { false => t, _ => default }; + [return: MaybeNull]static T F6(bool b, T t) where T : struct => b switch { false => default, _ => t }; + [return: MaybeNull]static T F7(bool b, T t) where T : notnull => b switch { false => t, _ => default }; + [return: MaybeNull]static T F8(bool b, T t) where T : notnull => b switch { false => default, _ => t }; +}"; + var comp = CreateCompilation(new[] { source, MaybeNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (5,83): warning CS8601: Possible null reference assignment. + // [return: MaybeNull]static T F1(bool b, T t) => b switch { false => t, _ => default }; + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(5, 83), + // (6,75): warning CS8601: Possible null reference assignment. + // [return: MaybeNull]static T F2(bool b, T t) => b switch { false => default, _ => t }; + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(6, 75), + // (11,101): warning CS8601: Possible null reference assignment. + // [return: MaybeNull]static T F7(bool b, T t) where T : notnull => b switch { false => t, _ => default }; + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(11, 101), + // (12,93): warning CS8601: Possible null reference assignment. + // [return: MaybeNull]static T F8(bool b, T t) where T : notnull => b switch { false => default, _ => t }; + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(12, 93)); + } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void MaybeNullT_22() + { + var source = +@"#nullable enable +using System.Diagnostics.CodeAnalysis; +using System.Threading.Tasks; +class Program +{ + static async Task F(int i, T x, [AllowNull]T y) + { + await Task.Delay(0); + switch (i) + { + case 0: return default(T); + case 1: return x; + default: return y; + } + } +}"; + var comp = CreateCompilation(new[] { source, AllowNullAttributeDefinition }); + comp.VerifyDiagnostics( + // (11,24): warning CS8603: Possible null reference return. + // case 0: return default(T); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "default(T)").WithLocation(11, 24), + // (13,25): warning CS8603: Possible null reference return. + // default: return y; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "y").WithLocation(13, 25)); + } + + [Fact] + [WorkItem(37362, "https://github.com/dotnet/roslyn/issues/37362")] + public void MaybeNullT_23() + { + var source = +@"#nullable enable +class Program +{ + static T Get() + { + throw new System.NotImplementedException(); + } + static T F1(bool b) => b ? Get() : default; + static T F2(bool b) => b ? default : Get(); + static T F3() => false ? Get() : default; + static T F4() => true ? Get() : default; + static T F5() => false ? default : Get(); + static T F6() => true ? default : Get(); +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics( + // (8,31): warning CS8603: Possible null reference return. + // static T F1(bool b) => b ? Get() : default; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "b ? Get() : default").WithLocation(8, 31), + // (9,31): warning CS8603: Possible null reference return. + // static T F2(bool b) => b ? default : Get(); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "b ? default : Get()").WithLocation(9, 31), + // (10,25): warning CS8603: Possible null reference return. + // static T F3() => false ? Get() : default; + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "false ? Get() : default").WithLocation(10, 25), + // (13,25): warning CS8603: Possible null reference return. + // static T F6() => true ? default : Get(); + Diagnostic(ErrorCode.WRN_NullReferenceReturn, "true ? default : Get()").WithLocation(13, 25)); + } } } diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs index 1651ad4ff999a..9f26199125abf 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs @@ -967,10 +967,10 @@ static void F1(object o, T tin) } else { - t1 = default; // 1 + t1 = default; } - t1.ToString(); // 2 + t1.ToString(); // 1 if (!(o is T t2)) { @@ -980,7 +980,7 @@ static void F1(object o, T tin) { t2.ToString(); } - t2.ToString(); // 3 + t2.ToString(); // 2 if (!(o is T t3)) return; t3.ToString(); @@ -989,14 +989,11 @@ static void F1(object o, T tin) "; var comp = CreateNullableCompilation(source); comp.VerifyDiagnostics( - // (12,18): warning CS8652: A default expression introduces a null value when 'T' is a non-nullable reference type. - // t1 = default; // 1 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(12, 18), // (15,9): warning CS8602: Dereference of a possibly null reference. - // t1.ToString(); // 2 + // t1.ToString(); // 1 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t1").WithLocation(15, 9), // (25,9): warning CS8602: Dereference of a possibly null reference. - // t2.ToString(); // 3 + // t2.ToString(); // 2 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t2").WithLocation(25, 9) ); } @@ -1852,6 +1849,7 @@ static void M(string? s) [Fact] [WorkItem(34233, "https://github.com/dotnet/roslyn/issues/34233")] + [WorkItem(39888, "https://github.com/dotnet/roslyn/issues/39888")] public void SwitchExpressionResultType_01() { CSharpCompilation c = CreateNullableCompilation(@" @@ -1914,6 +1912,7 @@ public interface IIn { } public interface IOut { } "); c.VerifyTypes(); + // Should not report warnings 5 or 6 (see https://github.com/dotnet/roslyn/issues/39888). c.VerifyDiagnostics( // (33,15): error CS8506: No best type was found for the switch expression. // _ = i switch { 1 => x, _ => y }/*T:!*/; // 1 @@ -1927,12 +1926,12 @@ public interface IOut { } // (40,29): warning CS8619: Nullability of reference types in value of type 'I' doesn't match target type 'I'. // _ = i switch { 1 => y, _ => x }/*T:I!*/; // 4 Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "y").WithArguments("I", "I").WithLocation(40, 29), - // (45,37): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (45,37): warning CS8601: Possible null reference assignment. // _ = i switch { 1 => x, _ => default }/*T:T*/; // 5 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(45, 37), - // (46,29): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(45, 37), + // (46,29): warning CS8601: Possible null reference assignment. // _ = i switch { 1 => default, _ => x }/*T:T*/; // 6 - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(46, 29)); + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(46, 29)); } } } diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/UninitializedNonNullableFieldTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/UninitializedNonNullableFieldTests.cs index 4d34a24985344..9d2dd9a548f9d 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/UninitializedNonNullableFieldTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/UninitializedNonNullableFieldTests.cs @@ -214,12 +214,12 @@ public void UnconstrainedGenericType() // (3,16): warning CS0649: Field 'C.F1' is never assigned to, and will always have its default value // internal T F1; Diagnostic(ErrorCode.WRN_UnassignedInternalField, "F1").WithArguments("C.F1", "").WithLocation(3, 16), - // (5,21): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (5,21): warning CS8601: Possible null reference assignment. // internal T F3 = default; - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(5, 21), - // (6,21): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(5, 21), + // (6,21): warning CS8601: Possible null reference assignment. // internal T F4 = default(T); - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default(T)").WithArguments("T").WithLocation(6, 21)); + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default(T)").WithLocation(6, 21)); } [Fact] @@ -524,9 +524,9 @@ class C // (9,34): warning CS8618: Non-nullable property 'P3' is uninitialized. Consider declaring the property as nullable. // [MaybeNull] private static T P3 { get; } Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "P3").WithArguments("property", "P3").WithLocation(9, 34), - // (10,48): warning CS8653: A default expression introduces a null value when 'T' is a non-nullable reference type. + // (10,48): warning CS8601: Possible null reference assignment. // [MaybeNull] private static T P4 { get; } = default; - Diagnostic(ErrorCode.WRN_DefaultExpressionMayIntroduceNullT, "default").WithArguments("T").WithLocation(10, 48), + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "default").WithLocation(10, 48), // (11,22): warning CS8618: Non-nullable property 'P5' is uninitialized. Consider declaring the property as nullable. // private static U P5 { get; set; } Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "P5").WithArguments("property", "P5").WithLocation(11, 22)); diff --git a/src/Compilers/CSharp/Test/Symbol/Symbols/Source/NullablePublicAPITests.cs b/src/Compilers/CSharp/Test/Symbol/Symbols/Source/NullablePublicAPITests.cs index b42ee84e7cb6b..f6191b4ab2370 100644 --- a/src/Compilers/CSharp/Test/Symbol/Symbols/Source/NullablePublicAPITests.cs +++ b/src/Compilers/CSharp/Test/Symbol/Symbols/Source/NullablePublicAPITests.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Immutable; using System.Linq; -using Microsoft.CodeAnalysis.CSharp.Symbols; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Test.Utilities; using Microsoft.CodeAnalysis.Diagnostics; @@ -3788,5 +3787,39 @@ void M() {} var symbolInfo = specModel.GetSymbolInfo(newAttributeUsage.ArgumentList.Arguments[0].Expression); Assert.Equal(SpecialType.System_String, ((IFieldSymbol)symbolInfo.Symbol).Type.SpecialType); } + + [Fact] + [WorkItem(38638, "https://github.com/dotnet/roslyn/issues/38638")] + public void TypeParameter_Default() + { + var source = +@"#nullable enable +abstract class A +{ + internal abstract void F() where U : T; +} +class B1 : A +{ + internal override void F() { _ = default(U); } +} +class B2 : A +{ + internal override void F() { _ = default(U); } +}"; + var comp = CreateCompilation(source); + comp.VerifyDiagnostics(); + var tree = comp.SyntaxTrees[0]; + var model = comp.GetSemanticModel(tree); + var exprs = tree.GetRoot().DescendantNodes().OfType().ToArray(); + verify(exprs[0], PublicNullableAnnotation.NotAnnotated, PublicNullableFlowState.MaybeNull); + verify(exprs[1], PublicNullableAnnotation.Annotated, PublicNullableFlowState.MaybeNull); + + void verify(DefaultExpressionSyntax expr, PublicNullableAnnotation expectedAnnotation, PublicNullableFlowState expectedState) + { + var info = model.GetTypeInfo(expr).Nullability; + Assert.Equal(expectedAnnotation, info.Annotation); + Assert.Equal(expectedState, info.FlowState); + } + } } } diff --git a/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs b/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs index edcf4ae1f223d..5e5df94d988e7 100644 --- a/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs +++ b/src/Compilers/CSharp/Test/Syntax/Diagnostics/DiagnosticTest.cs @@ -302,10 +302,6 @@ public void WarningLevel_2() case ErrorCode.ERR_FeatureInPreview: case ErrorCode.WRN_ThrowPossibleNull: case ErrorCode.WRN_UnboxPossibleNull: - case ErrorCode.WRN_ConditionalAccessMayReturnNull: - case ErrorCode.WRN_AsOperatorMayReturnNull: - case ErrorCode.WRN_DefaultExpressionMayIntroduceNullT: - case ErrorCode.WRN_NullLiteralMayIntroduceNullT: case ErrorCode.WRN_SwitchExpressionNotExhaustiveForNull: case ErrorCode.WRN_ImplicitCopyInReadOnlyMember: case ErrorCode.WRN_NullabilityMismatchInTypeParameterNotNullConstraint: