未验证 提交 53b2957f 编写于 作者: R Rikki Gibson 提交者: GitHub

Update argument state when parameter has not-null type (#46072)

上级 e128267a
......@@ -4955,7 +4955,7 @@ static bool hasNoNonNullableCounterpart(TypeSymbol type)
case RefKind.In:
{
// learn from post-conditions [Maybe/NotNull, Maybe/NotNullWhen] without using an assignment
learnFromPostConditions(argument, parameterAnnotations);
learnFromPostConditions(argument, parameterType, parameterAnnotations);
}
break;
case RefKind.Ref:
......@@ -5139,11 +5139,15 @@ static TypeWithState applyPostConditionsWhenFalse(TypeWithState typeWithState, F
return typeWithState;
}
void learnFromPostConditions(BoundExpression argument, FlowAnalysisAnnotations parameterAnnotations)
void learnFromPostConditions(BoundExpression argument, TypeWithAnnotations parameterType, FlowAnalysisAnnotations parameterAnnotations)
{
// Note: NotNull = NotNullWhen(true) + NotNullWhen(false)
bool notNullWhenTrue = (parameterAnnotations & FlowAnalysisAnnotations.NotNullWhenTrue) != 0;
bool notNullWhenFalse = (parameterAnnotations & FlowAnalysisAnnotations.NotNullWhenFalse) != 0;
bool disallowNull = (parameterAnnotations & FlowAnalysisAnnotations.DisallowNull) != 0;
bool setNotNullFromParameterType = !argument.IsSuppressed
&& !parameterType.Type.IsPossiblyNullableReferenceTypeTypeParameter()
&& parameterType.NullableAnnotation.IsNotAnnotated();
// Note: MaybeNull = MaybeNullWhen(true) + MaybeNullWhen(false)
bool maybeNullWhenTrue = (parameterAnnotations & FlowAnalysisAnnotations.MaybeNullWhenTrue) != 0;
......@@ -5153,7 +5157,9 @@ void learnFromPostConditions(BoundExpression argument, FlowAnalysisAnnotations p
{
LearnFromNullTest(argument, ref State);
}
else if (notNullWhenTrue && notNullWhenFalse && !IsConditionalState && !(maybeNullWhenTrue && maybeNullWhenFalse))
else if (((notNullWhenTrue && notNullWhenFalse) || disallowNull || setNotNullFromParameterType)
&& !IsConditionalState
&& !(maybeNullWhenTrue || maybeNullWhenFalse))
{
LearnFromNonNullTest(argument, ref State);
}
......
......@@ -9097,10 +9097,10 @@ class C2 : B
}
class P
{
static void F(object? x, object y, C1 c)
static void F(bool b, object? x, object y, C1 c)
{
c.F(x, y).ToString();
c.G(x, y).ToString();
if (b) c.F(x, y).ToString();
if (b) c.G(x, y).ToString();
((B)c).F(x, y).ToString();
((B)c).G(x, y).ToString();
((A)c).F(x, y).ToString();
......@@ -9131,28 +9131,28 @@ static void F(object? x, object y, C2 c)
var comp2 = CreateCompilation(source2, references: new[] { ref0, ref1 });
comp2.VerifyDiagnostics(
// (9,37): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' context.
// (9,37): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
// public override object? G(object? x, object? y) => x;
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(9, 37),
// (9,48): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' context.
// (9,48): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
// public override object? G(object? x, object? y) => x;
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(9, 48),
// (9,27): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' context.
// (9,27): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
// public override object? G(object? x, object? y) => x;
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(9, 27),
// (21,25): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' context.
// (21,25): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
// static void F(object? x, object y, C2 c)
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(21, 25),
// (13,25): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' context.
// static void F(object? x, object y, C1 c)
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(13, 25),
// (8,37): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' context.
// (13,33): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
// static void F(bool b, object? x, object y, C1 c)
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(13, 33),
// (8,37): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
// public override object? F(object? x, object? y) => x;
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(8, 37),
// (8,48): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' context.
// (8,48): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
// public override object? F(object? x, object? y) => x;
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(8, 48),
// (8,27): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' context.
// (8,27): warning CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
// public override object? F(object? x, object? y) => x;
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation, "?").WithLocation(8, 27)
);
......@@ -9167,12 +9167,12 @@ static void F(object? x, object y, C2 c)
comp2 = CreateCompilation(new[] { source2 }, options: WithNonNullTypesTrue(), references: new[] { ref0, ref1 });
comp2.VerifyDiagnostics(
// (15,13): warning CS8604: Possible null reference argument for parameter 'x' in 'object C1.F(object x, object y)'.
// c.F(x, y).ToString();
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x").WithArguments("x", "object C1.F(object x, object y)").WithLocation(15, 13),
// (16,13): warning CS8604: Possible null reference argument for parameter 'x' in 'object C1.G(object x, object y)'.
// c.G(x, y).ToString();
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x").WithArguments("x", "object C1.G(object x, object y)").WithLocation(16, 13),
// (15,20): warning CS8604: Possible null reference argument for parameter 'x' in 'object C1.F(object x, object y)'.
// if (b) c.F(x, y).ToString();
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x").WithArguments("x", "object C1.F(object x, object y)").WithLocation(15, 20),
// (16,20): warning CS8604: Possible null reference argument for parameter 'x' in 'object C1.G(object x, object y)'.
// if (b) c.G(x, y).ToString();
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x").WithArguments("x", "object C1.G(object x, object y)").WithLocation(16, 20),
// (19,18): warning CS8604: Possible null reference argument for parameter 'x' in 'object? A.F(object x, object? y)'.
// ((A)c).F(x, y).ToString();
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x").WithArguments("x", "object? A.F(object x, object? y)").WithLocation(19, 18),
......@@ -18601,18 +18601,20 @@ class C
static void F(I<object> x, I<object?> y, I<object>? z, I<object?>? w, I<object?>[]? a)
{
G(x);
G(y);
G(x, x, x);
G(y); // 1
G(x, x, x); // 2, 3
G(x, y, y);
G(x, x, y, z, w);
G(y: x, x: y);
G(x, x, y, z, w); // 4, 5, 6, 7
G(y: x, x: y); // 8, 9
G(y: y, x: x);
G(x, a);
G(x, a); // 10
G(x, new I<object?>[0]);
G(x, new[] { x, x });
G(x, new[] { x, x }); // 11
G(x, new[] { y, y });
G(x, new[] { x, y, z });
G(y: new[] { x, x }, x: y);
// note that the array type below is reinferred to 'I<object>[]'
// due to previous usage of the variables as call arguments.
G(x, new[] { x, y, z }); // 12, 13
G(y: new[] { x, x }, x: y); // 14, 15
G(y: new[] { y, y }, x: x);
}
static void G(I<object> x, params I<object?>[] y)
......@@ -18621,51 +18623,51 @@ static void G(I<object> x, params I<object?>[] y)
}";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (7,11): warning CS8620: Nullability of reference types in argument of type 'I<object?>' doesn't match target type 'I<object>' for parameter 'x' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(y);
// (7,11): warning CS8620: Argument of type 'I<object?>' cannot be used for parameter 'x' of type 'I<object>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// G(y); // 1
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "y").WithArguments("I<object?>", "I<object>", "x", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(7, 11),
// (8,14): warning CS8620: Nullability of reference types in argument of type 'I<object>' doesn't match target type 'I<object?>' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(x, x, x);
// (8,14): warning CS8620: Argument of type 'I<object>' cannot be used for parameter 'y' of type 'I<object?>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// G(x, x, x); // 2, 3
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "x").WithArguments("I<object>", "I<object?>", "y", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(8, 14),
// (8,17): warning CS8620: Nullability of reference types in argument of type 'I<object>' doesn't match target type 'I<object?>' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(x, x, x);
// (8,17): warning CS8620: Argument of type 'I<object>' cannot be used for parameter 'y' of type 'I<object?>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// G(x, x, x); // 2, 3
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "x").WithArguments("I<object>", "I<object?>", "y", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(8, 17),
// (10,14): warning CS8620: Nullability of reference types in argument of type 'I<object>' doesn't match target type 'I<object?>' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(x, x, y, z, w);
// (10,14): warning CS8620: Argument of type 'I<object>' cannot be used for parameter 'y' of type 'I<object?>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// G(x, x, y, z, w); // 4, 5, 6, 7
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "x").WithArguments("I<object>", "I<object?>", "y", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(10, 14),
// (10,20): warning CS8604: Possible null reference argument for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(x, x, y, z, w);
// G(x, x, y, z, w); // 4, 5, 6, 7
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "z").WithArguments("y", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(10, 20),
// (10,20): warning CS8620: Nullability of reference types in argument of type 'I<object>' doesn't match target type 'I<object?>' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(x, x, y, z, w);
// (10,20): warning CS8620: Argument of type 'I<object>' cannot be used for parameter 'y' of type 'I<object?>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// G(x, x, y, z, w); // 4, 5, 6, 7
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "z").WithArguments("I<object>", "I<object?>", "y", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(10, 20),
// (10,23): warning CS8604: Possible null reference argument for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(x, x, y, z, w);
// G(x, x, y, z, w); // 4, 5, 6, 7
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "w").WithArguments("y", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(10, 23),
// (11,14): warning CS8620: Nullability of reference types in argument of type 'I<object>' doesn't match target type 'I<object?>' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(y: x, x: y);
// (11,14): warning CS8620: Argument of type 'I<object>' cannot be used for parameter 'y' of type 'I<object?>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// G(y: x, x: y); // 8, 9
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "x").WithArguments("I<object>", "I<object?>", "y", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(11, 14),
// (11,20): warning CS8620: Nullability of reference types in argument of type 'I<object?>' doesn't match target type 'I<object>' for parameter 'x' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(y: x, x: y);
// (11,20): warning CS8620: Argument of type 'I<object?>' cannot be used for parameter 'x' of type 'I<object>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// G(y: x, x: y); // 8, 9
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "y").WithArguments("I<object?>", "I<object>", "x", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(11, 20),
// (13,14): warning CS8604: Possible null reference argument for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(x, a);
// G(x, a); // 10
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "a").WithArguments("y", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(13, 14),
// (15,14): warning CS8620: Nullability of reference types in argument of type 'I<object>[]' doesn't match target type 'I<object?>[]' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(x, new[] { x, x });
// (15,14): warning CS8620: Argument of type 'I<object>[]' cannot be used for parameter 'y' of type 'I<object?>[]' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// G(x, new[] { x, x }); // 11
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "new[] { x, x }").WithArguments("I<object>[]", "I<object?>[]", "y", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(15, 14),
// (17,14): warning CS8620: Nullability of reference types in argument of type 'I<object>?[]' doesn't match target type 'I<object?>[]' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(x, new[] { x, y, z });
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "new[] { x, y, z }").WithArguments("I<object>?[]", "I<object?>[]", "y", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(17, 14),
// (17,25): warning CS8619: Nullability of reference types in value of type 'I<object?>' doesn't match target type 'I<object>'.
// G(x, new[] { x, y, z });
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "y").WithArguments("I<object?>", "I<object>").WithLocation(17, 25),
// (18,14): warning CS8620: Nullability of reference types in argument of type 'I<object>[]' doesn't match target type 'I<object?>[]' for parameter 'y' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(y: new[] { x, x }, x: y);
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "new[] { x, x }").WithArguments("I<object>[]", "I<object?>[]", "y", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(18, 14),
// (18,33): warning CS8620: Nullability of reference types in argument of type 'I<object?>' doesn't match target type 'I<object>' for parameter 'x' in 'void C.G(I<object> x, params I<object?>[] y)'.
// G(y: new[] { x, x }, x: y);
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "y").WithArguments("I<object?>", "I<object>", "x", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(18, 33)
// (19,14): warning CS8620: Argument of type 'I<object>[]' cannot be used for parameter 'y' of type 'I<object?>[]' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// G(x, new[] { x, y, z }); // 12, 13
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "new[] { x, y, z }").WithArguments("I<object>[]", "I<object?>[]", "y", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(19, 14),
// (19,25): warning CS8619: Nullability of reference types in value of type 'I<object?>' doesn't match target type 'I<object>'.
// G(x, new[] { x, y, z }); // 12, 13
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "y").WithArguments("I<object?>", "I<object>").WithLocation(19, 25),
// (20,14): warning CS8620: Argument of type 'I<object>[]' cannot be used for parameter 'y' of type 'I<object?>[]' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// G(y: new[] { x, x }, x: y); // 14, 15
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "new[] { x, x }").WithArguments("I<object>[]", "I<object?>[]", "y", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(20, 14),
// (20,33): warning CS8620: Argument of type 'I<object?>' cannot be used for parameter 'x' of type 'I<object>' in 'void C.G(I<object> x, params I<object?>[] y)' due to differences in the nullability of reference types.
// G(y: new[] { x, x }, x: y); // 14, 15
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "y").WithArguments("I<object?>", "I<object>", "x", "void C.G(I<object> x, params I<object?>[] y)").WithLocation(20, 33)
);
}
......@@ -18723,9 +18725,9 @@ static int F(object x)
{
Missing(F(null)); // 1
Missing(F(x = null)); // 2
x.ToString(); // 3
x.ToString();
Missing(F(x = this)); // 4
Missing(F(x = this)); // 3
x.ToString();
return 0;
}
......@@ -18748,14 +18750,11 @@ static int F(object x)
// (7,23): warning CS8600: Converting null literal or possible null value to non-nullable type.
// Missing(F(x = null)); // 2
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(7, 23),
// (8,9): warning CS8602: Dereference of a possibly null reference.
// x.ToString(); // 3
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(8, 9),
// (10,9): error CS0103: The name 'Missing' does not exist in the current context
// Missing(F(x = this)); // 4
// Missing(F(x = this)); // 3
Diagnostic(ErrorCode.ERR_NameNotInContext, "Missing").WithArguments("Missing").WithLocation(10, 9),
// (10,23): error CS0026: Keyword 'this' is not valid in a static property, static method, or static field initializer
// Missing(F(x = this)); // 4
// Missing(F(x = this)); // 3
Diagnostic(ErrorCode.ERR_ThisInStaticMeth, "this").WithLocation(10, 23)
);
}
......@@ -18771,9 +18770,9 @@ static int F(object x)
{
bad.Missing(F(null)); // 1
bad.Missing(F(x = null)); // 2
x.ToString(); // 3
x.ToString();
bad.Missing(F(x = this)); // 4
bad.Missing(F(x = this)); // 3
x.ToString();
return 0;
}
......@@ -18796,14 +18795,11 @@ static int F(object x)
// (7,27): warning CS8600: Converting null literal or possible null value to non-nullable type.
// bad.Missing(F(x = null)); // 2
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(7, 27),
// (8,9): warning CS8602: Dereference of a possibly null reference.
// x.ToString(); // 3
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(8, 9),
// (10,9): error CS0103: The name 'bad' does not exist in the current context
// bad.Missing(F(x = this)); // 4
// bad.Missing(F(x = this)); // 3
Diagnostic(ErrorCode.ERR_NameNotInContext, "bad").WithArguments("bad").WithLocation(10, 9),
// (10,27): error CS0026: Keyword 'this' is not valid in a static property, static method, or static field initializer
// bad.Missing(F(x = this)); // 4
// bad.Missing(F(x = this)); // 3
Diagnostic(ErrorCode.ERR_ThisInStaticMeth, "this").WithLocation(10, 27)
);
}
......@@ -18819,7 +18815,7 @@ object F(object x)
{
Missing(
F(x = null) /*warn*/,
x.ToString() /*warn*/,
x.ToString(),
F(x = this),
x.ToString());
return x;
......@@ -18836,10 +18832,7 @@ object F(object x)
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x = null").WithArguments("x", "object C.F(object x)").WithLocation(7, 15),
// (7,19): warning CS8600: Converting null literal or possible null value to non-nullable type.
// F(x = null) /*warn*/,
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(7, 19),
// (8,13): warning CS8602: Dereference of a possibly null reference.
// x.ToString() /*warn*/,
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(8, 13)
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(7, 19)
);
}
......@@ -18854,11 +18847,11 @@ int F(object x)
{
if (G(F(x = null)))
{
x.ToString(); // 1
x.ToString();
}
else
{
x.ToString(); // 2
x.ToString();
}
return 0;
}
......@@ -18874,13 +18867,7 @@ int F(object x)
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x = null").WithArguments("x", "int C.F(object x)").WithLocation(6, 17),
// (6,21): warning CS8600: Converting null literal or possible null value to non-nullable type.
// if (G(F(x = null)))
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(6, 21),
// (8,13): warning CS8602: Dereference of a possibly null reference.
// x.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(8, 13),
// (12,13): warning CS8602: Dereference of a possibly null reference.
// x.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(12, 13)
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(6, 21)
);
}
......@@ -19051,12 +19038,12 @@ public void ParamsArgument_NotArray()
var source =
@"class C
{
static void F1(object x, object? y)
static void F1(bool b, object x, object? y)
{
F2(y);
F2(x, y);
F3(y, x);
F3(x, y, x);
if (b) F2(y);
if (b) F2(x, y);
if (b) F3(y, x);
if (b) F3(x, y, x);
}
static void F2(params object x)
{
......@@ -19076,18 +19063,18 @@ static void F3(params object x, params object[] y)
// (13,20): error CS0231: A params parameter must be the last parameter in a formal parameter list
// static void F3(params object x, params object[] y)
Diagnostic(ErrorCode.ERR_ParamsLast, "params object x").WithLocation(13, 20),
// (6,9): error CS1501: No overload for method 'F2' takes 2 arguments
// F2(x, y);
Diagnostic(ErrorCode.ERR_BadArgCount, "F2").WithArguments("F2", "2").WithLocation(6, 9),
// (5,12): warning CS8604: Possible null reference argument for parameter 'x' in 'void C.F2(params object x)'.
// F2(y);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y").WithArguments("x", "void C.F2(params object x)").WithLocation(5, 12),
// (7,12): warning CS8604: Possible null reference argument for parameter 'x' in 'void C.F3(params object x, params object[] y)'.
// F3(y, x);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y").WithArguments("x", "void C.F3(params object x, params object[] y)").WithLocation(7, 12),
// (8,15): warning CS8604: Possible null reference argument for parameter 'y' in 'void C.F3(params object x, params object[] y)'.
// F3(x, y, x);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y").WithArguments("y", "void C.F3(params object x, params object[] y)").WithLocation(8, 15));
// (6,16): error CS1501: No overload for method 'F2' takes 2 arguments
// if (b) F2(x, y);
Diagnostic(ErrorCode.ERR_BadArgCount, "F2").WithArguments("F2", "2").WithLocation(6, 16),
// (5,19): warning CS8604: Possible null reference argument for parameter 'x' in 'void C.F2(params object x)'.
// if (b) F2(y);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y").WithArguments("x", "void C.F2(params object x)").WithLocation(5, 19),
// (7,19): warning CS8604: Possible null reference argument for parameter 'x' in 'void C.F3(params object x, params object[] y)'.
// if (b) F3(y, x);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y").WithArguments("x", "void C.F3(params object x, params object[] y)").WithLocation(7, 19),
// (8,22): warning CS8604: Possible null reference argument for parameter 'y' in 'void C.F3(params object x, params object[] y)'.
// if (b) F3(x, y, x);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y").WithArguments("y", "void C.F3(params object x, params object[] y)").WithLocation(8, 22));
}
[Fact]
......@@ -19365,10 +19352,7 @@ static void G(out object x, ref object y, in object z)
Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "y").WithLocation(5, 22),
// (5,28): warning CS8604: Possible null reference argument for parameter 'z' in 'void C.G(out object x, ref object y, in object z)'.
// G(out x, ref y, in z);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "z").WithArguments("z", "void C.G(out object x, ref object y, in object z)").WithLocation(5, 28),
// (8,9): warning CS8602: Dereference of a possibly null reference.
// z.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z").WithLocation(8, 9)
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "z").WithArguments("z", "void C.G(out object x, ref object y, in object z)").WithLocation(5, 28)
);
}
......@@ -29429,10 +29413,10 @@ public void RefParameter_EvaluationOrder()
var c = CreateCompilation(@"
class C<T>
{
public void M()
public void M(bool b)
{
string? s = """";
var v1 = M(ref s, s);
var v1 = M(ref s, b ? s : """");
s.ToString(); // 1
v1.ToString();
......@@ -29689,10 +29673,10 @@ public void OutParameter_EvaluationOrder()
var c = CreateCompilation(@"
class C<T>
{
public void M()
public void M(bool b)
{
string? s = """";
var v1 = M(out s, s);
var v1 = M(out s, b ? s : """");
s.ToString(); // 1
v1.ToString();
......@@ -34456,6 +34440,455 @@ public class Derived : Base
comp.VerifyDiagnostics();
}
[Fact]
public void UnannotatedParam_UpdatesArgumentState()
{
var source = @"
public class Program
{
void M0(string s0) { }
void M1(string? s1)
{
M0(s1); // 1
_ = s1.ToString();
}
}
";
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (8,12): warning CS8604: Possible null reference argument for parameter 's0' in 'void Program.M0(string s0)'.
// M0(s1); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "s1").WithArguments("s0", "void Program.M0(string s0)").WithLocation(8, 12)
);
}
[Fact]
public void UnannotatedTypeArgument_UpdatesArgumentState()
{
var source = @"
public class Program
{
void M0<T>(T t) { }
void M1<T>(T? t) where T : class
{
M0<T>(t); // 1
_ = t.ToString();
}
}
";
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (8,15): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.M0<T>(T t)'.
// M0<T>(t); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t").WithArguments("t", "void Program.M0<T>(T t)").WithLocation(8, 15)
);
}
[Fact]
public void UnannotatedTypeArgument_NullableClassConstrained_DoesNotUpdateArgumentState()
{
var source = @"
public class Program
{
void M0<T>(T t) { }
void M1<T>(T t) where T : class?
{
M0(t);
_ = t.ToString(); // 1
}
}
";
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (9,13): warning CS8602: Dereference of a possibly null reference.
// _ = t.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t").WithLocation(9, 13)
);
}
[Fact]
public void UnannotatedParam_SuppressedArgument_DoesNotUpdateArgumentState()
{
var source = @"
public class Program
{
void M0(string s0) { }
void M1(string? s1)
{
M0(s1!);
_ = s1.ToString(); // 1
}
}
";
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (9,13): warning CS8602: Dereference of a possibly null reference.
// _ = s1.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s1").WithLocation(9, 13)
);
}
[Fact]
public void AnnotatedParam_DoesNotUpdateArgumentState()
{
var source = @"
public class Program
{
void M0(string? s0) { }
void M1(string? s1)
{
M0(s1);
_ = s1.ToString(); // 1
}
}
";
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (9,13): warning CS8602: Dereference of a possibly null reference.
// _ = s1.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s1").WithLocation(9, 13)
);
}
[Fact]
public void Params_AnnotatedElement_UnannotatedArray_DoesNotUpdateArgumentState()
{
var source = @"
public class Program
{
void M0(params string?[] s0) { }
void M1(string? s1)
{
M0(s1);
_ = s1.ToString(); // 1
}
}
";
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (9,13): warning CS8602: Dereference of a possibly null reference.
// _ = s1.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s1").WithLocation(9, 13)
);
}
[Fact]
public void Params_UnannotatedElement_AnnotatedArray_UpdatesArgumentState()
{
var source = @"
public class Program
{
void M0(params string[]? s0) { }
void M1(string? s1)
{
M0(s1); // 1
_ = s1.ToString();
}
}
";
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (8,12): warning CS8604: Possible null reference argument for parameter 's0' in 'void Program.M0(params string[]? s0)'.
// M0(s1); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "s1").WithArguments("s0", "void Program.M0(params string[]? s0)").WithLocation(8, 12)
);
}
[Fact]
public void ObliviousParam_DoesNotUpdateArgumentState()
{
var source = @"
public class Program
{
#nullable disable
void M0(string s0) { }
#nullable enable
void M1(string? s1)
{
M0(s1);
_ = s1.ToString(); // 1
}
}
";
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (10,13): warning CS8602: Dereference of a possibly null reference.
// _ = s1.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s1").WithLocation(10, 13)
);
}
[Fact]
public void UnannotatedParam_MaybeNull_UpdatesArgumentState()
{
var source = @"
using System.Diagnostics.CodeAnalysis;
public class Program
{
void M0([MaybeNull] string s0) { }
void M1(string s1)
{
M0(s1);
_ = s1.ToString(); // 1
}
}
";
var comp = CreateNullableCompilation(new[] { source, MaybeNullAttributeDefinition });
comp.VerifyDiagnostics(
// (11,13): warning CS8602: Dereference of a possibly null reference.
// _ = s1.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s1").WithLocation(11, 13)
);
}
[Fact]
public void UnannotatedParam_MaybeNullWhen_UpdatesArgumentState()
{
var source = @"
using System.Diagnostics.CodeAnalysis;
public class Program
{
bool M0([MaybeNullWhen(true)] string s0) => false;
void M1(string s1)
{
M0(s1);
_ = s1.ToString(); // 1
}
void M2(string s1)
{
_ = M0(s1)
? s1.ToString() // 2
: s1.ToString();
}
}
";
var comp = CreateNullableCompilation(new[] { source, MaybeNullWhenAttributeDefinition });
comp.VerifyDiagnostics(
// (11,13): warning CS8602: Dereference of a possibly null reference.
// _ = s1.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s1").WithLocation(11, 13),
// (17,15): warning CS8602: Dereference of a possibly null reference.
// ? s1.ToString() // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s1").WithLocation(17, 15)
);
}
[Fact]
public void AnnotatedParam_DisallowNull_UpdatesArgumentState()
{
var source = @"
using System.Diagnostics.CodeAnalysis;
public class Program
{
void M0([DisallowNull] int? i) { }
void M1(int? i1)
{
M0(i1); // 1
_ = i1.Value.ToString();
}
}
";
var comp = CreateNullableCompilation(new[] { source, DisallowNullAttributeDefinition });
comp.VerifyDiagnostics(
// (10,12): warning CS8607: A possible null value may not be used for a type marked with [NotNull] or [DisallowNull]
// M0(i1); // 1
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "i1").WithLocation(10, 12)
);
}
[Fact]
public void AnnotatedTypeParam_DisallowNull_UpdatesArgumentState()
{
var source = @"
using System.Diagnostics.CodeAnalysis;
public class Program<T>
{
void M0([DisallowNull] T? t) { }
void M1(T t)
{
M0(t); // 1
_ = t.ToString();
}
}
";
var comp = CreateNullableCompilation(new[] { source, DisallowNullAttributeDefinition }, parseOptions: TestOptions.RegularPreview);
comp.VerifyDiagnostics(
// (10,12): warning CS8607: A possible null value may not be used for a type marked with [NotNull] or [DisallowNull]
// M0(t); // 1
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t").WithLocation(10, 12)
);
}
[Fact]
public void UnconstrainedTypeParam_DoesNotUpdateArgumentState()
{
var source = @"
public class Program<T>
{
void M0(T t) { }
void M1()
{
T t = default;
M0(t); // 1
M0(t); // 2
_ = t.ToString(); // 3
}
}
";
// Note that we pass 't' as an argument multiple times to demonstrate that the call does not update the argument state from MaybeDefault to MaybeNull.
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (9,12): warning CS8604: Possible null reference argument for parameter 't' in 'void Program<T>.M0(T t)'.
// M0(t); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t").WithArguments("t", "void Program<T>.M0(T t)").WithLocation(9, 12),
// (10,12): warning CS8604: Possible null reference argument for parameter 't' in 'void Program<T>.M0(T t)'.
// M0(t); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t").WithArguments("t", "void Program<T>.M0(T t)").WithLocation(10, 12),
// (11,13): warning CS8602: Dereference of a possibly null reference.
// _ = t.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t").WithLocation(11, 13)
);
}
[Fact]
public void NotnullTypeParam_UpdatesArgumentState()
{
var source = @"
public class Program<T> where T : notnull
{
void M0(T t) { }
void M1()
{
T t = default;
M0(t); // 2
_ = t.ToString();
}
}
";
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (9,12): warning CS8604: Possible null reference argument for parameter 't' in 'void Program<T>.M0(T t)'.
// M0(t); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t").WithArguments("t", "void Program<T>.M0(T t)").WithLocation(9, 12)
);
}
[Fact]
public void NotNullConstrainedParam_DoesNotUpdateArgumentState()
{
var source = @"
public class Program
{
void M0<T>(T t) where T : notnull { }
void M1(string? s)
{
M0(s); // 1
_ = s.ToString(); // 2
}
}
";
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (8,9): warning CS8714: The type 'string?' cannot be used as type parameter 'T' in the generic type or method 'Program.M0<T>(T)'. Nullability of type argument 'string?' doesn't match 'notnull' constraint.
// M0(s); // 1
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterNotNullConstraint, "M0").WithArguments("Program.M0<T>(T)", "T", "string?").WithLocation(8, 9),
// (9,13): warning CS8602: Dereference of a possibly null reference.
// _ = s.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s").WithLocation(9, 13)
);
}
[Fact]
public void NotNullConstrainedParam_SuppressedArgument_DoesNotUpdateArgumentState()
{
var source = @"
public class Program
{
void M0<T>(T t) where T : notnull { }
void M1(string? s)
{
M0(s!);
_ = s.ToString(); // 1
}
}
";
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (9,13): warning CS8602: Dereference of a possibly null reference.
// _ = s.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s").WithLocation(9, 13)
);
}
[Fact]
public void Params_NotNullConstrainedElement_AnnotatedArray_DoesNotUpdateArgumentState()
{
var source = @"
public class Program
{
void M0<T>(params T[]? s0) where T : notnull { }
void M1(string? s1)
{
M0(s1); // 1
_ = s1.ToString(); // 2
}
}
";
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (8,9): warning CS8714: The type 'string?' cannot be used as type parameter 'T' in the generic type or method 'Program.M0<T>(params T[]?)'. Nullability of type argument 'string?' doesn't match 'notnull' constraint.
// M0(s1); // 1
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterNotNullConstraint, "M0").WithArguments("Program.M0<T>(params T[]?)", "T", "string?").WithLocation(8, 9),
// (9,13): warning CS8602: Dereference of a possibly null reference.
// _ = s1.ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s1").WithLocation(9, 13)
);
}
[Fact]
public void Params_NotNullTypeArgument_AnnotatedArray_UpdatesArgumentState()
{
var source = @"
public class Program
{
void M0<T>(params T[]? s0) where T : notnull { }
void M1(string? s1)
{
M0<string>(s1); // 1
_ = s1.ToString();
}
}
";
var comp = CreateNullableCompilation(source);
comp.VerifyDiagnostics(
// (8,20): warning CS8604: Possible null reference argument for parameter 's0' in 'void Program.M0<string>(params string[]? s0)'.
// M0<string>(s1); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "s1").WithArguments("s0", "void Program.M0<string>(params string[]? s0)").WithLocation(8, 20)
);
}
[Fact]
public void NotNull_Parameter_Generic()
{
......@@ -35935,6 +36368,7 @@ public void DisallowNull_Parameter_01()
@"using System.Diagnostics.CodeAnalysis;
class Program
{
static bool b = false;
static void F1<T>([DisallowNull]T t) { }
static void F2<T>([DisallowNull]T t) where T : class { }
static void F3<T>([DisallowNull]T? t) where T : class { }
......@@ -35950,15 +36384,15 @@ static void M1<T>(T t1)
F2(t2);
F3(t2);
t2 = null; // 1
F1(t2); // 2
F2(t2); // 3, 4
F3(t2); // 5
if (b) F1(t2); // 2
if (b) F2(t2); // 3, 4
if (b) F3(t2); // 5
}
static void M3<T>(T? t3) where T : class
{
F1(t3); // 6
F2(t3); // 7, 8
F3(t3); // 9
if (b) F1(t3); // 6
if (b) F2(t3); // 7, 8
if (b) F3(t3); // 9
if (t3 == null) return;
F1(t3);
F2(t3);
......@@ -35971,8 +36405,8 @@ static void M1<T>(T t1)
}
static void M5<T>(T? t5) where T : struct
{
F1(t5); // 10
F5(t5); // 11
if (b) F1(t5); // 10
if (b) F5(t5); // 11
if (t5 == null) return;
F1(t5);
F5(t5);
......@@ -35980,42 +36414,42 @@ static void M1<T>(T t1)
}";
var comp = CreateCompilation(new[] { DisallowNullAttributeDefinition, source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (11,12): warning CS8607: A possible null value may not be used for a type marked with [NotNull] or [DisallowNull]
// (12,12): warning CS8607: A possible null value may not be used for a type marked with [NotNull] or [DisallowNull]
// F1(t1); // 0
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t1").WithLocation(11, 12),
// (18,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t1").WithLocation(12, 12),
// (19,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
// t2 = null; // 1
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(18, 14),
// (19,12): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F1<T?>(T? t)'.
// F1(t2); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F1<T?>(T? t)").WithLocation(19, 12),
// (20,9): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// F2(t2); // 3, 4
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterReferenceTypeConstraint, "F2").WithArguments("Program.F2<T>(T)", "T", "T?").WithLocation(20, 9),
// (20,12): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F2<T?>(T? t)'.
// F2(t2); // 3, 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F2<T?>(T? t)").WithLocation(20, 12),
// (21,12): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F3<T>(T? t)'.
// F3(t2); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F3<T>(T? t)").WithLocation(21, 12),
// (25,12): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F1<T?>(T? t)'.
// F1(t3); // 6
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F1<T?>(T? t)").WithLocation(25, 12),
// (26,9): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// F2(t3); // 7, 8
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterReferenceTypeConstraint, "F2").WithArguments("Program.F2<T>(T)", "T", "T?").WithLocation(26, 9),
// (26,12): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F2<T?>(T? t)'.
// F2(t3); // 7, 8
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F2<T?>(T? t)").WithLocation(26, 12),
// (27,12): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F3<T>(T? t)'.
// F3(t3); // 9
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F3<T>(T? t)").WithLocation(27, 12),
// (40,12): warning CS8607: A possible null value may not be assigned to a target marked with the [DisallowNull] attribute
// F1(t5); // 10
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t5").WithLocation(40, 12),
// (41,12): warning CS8607: A possible null value may not be assigned to a target marked with the [DisallowNull] attribute
// F5(t5); // 11
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t5").WithLocation(41, 12)
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(19, 14),
// (20,19): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F1<T?>(T? t)'.
// if (b) F1(t2); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F1<T?>(T? t)").WithLocation(20, 19),
// (21,16): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// if (b) F2(t2); // 3, 4
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterReferenceTypeConstraint, "F2").WithArguments("Program.F2<T>(T)", "T", "T?").WithLocation(21, 16),
// (21,19): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F2<T?>(T? t)'.
// if (b) F2(t2); // 3, 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F2<T?>(T? t)").WithLocation(21, 19),
// (22,19): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F3<T>(T? t)'.
// if (b) F3(t2); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F3<T>(T? t)").WithLocation(22, 19),
// (26,19): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F1<T?>(T? t)'.
// if (b) F1(t3); // 6
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F1<T?>(T? t)").WithLocation(26, 19),
// (27,16): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// if (b) F2(t3); // 7, 8
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterReferenceTypeConstraint, "F2").WithArguments("Program.F2<T>(T)", "T", "T?").WithLocation(27, 16),
// (27,19): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F2<T?>(T? t)'.
// if (b) F2(t3); // 7, 8
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F2<T?>(T? t)").WithLocation(27, 19),
// (28,19): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F3<T>(T? t)'.
// if (b) F3(t3); // 9
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F3<T>(T? t)").WithLocation(28, 19),
// (41,19): warning CS8607: A possible null value may not be used for a type marked with [NotNull] or [DisallowNull]
// if (b) F1(t5); // 10
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t5").WithLocation(41, 19),
// (42,19): warning CS8607: A possible null value may not be used for a type marked with [NotNull] or [DisallowNull]
// if (b) F5(t5); // 11
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t5").WithLocation(42, 19)
);
}
......@@ -36123,6 +36557,7 @@ public void DisallowNull_Parameter_01_WithAllowNull()
@"using System.Diagnostics.CodeAnalysis;
class Program
{
static bool b = false;
static void F1<T>([DisallowNull, AllowNull]T t) { }
static void F2<T>([DisallowNull, AllowNull]T t) where T : class { }
static void F3<T>([DisallowNull, AllowNull]T? t) where T : class { }
......@@ -36138,15 +36573,15 @@ static void M1<T>(T t1)
F2(t2);
F3(t2);
t2 = null; // 1
F1(t2); // 2
F2(t2); // 3, 4
F3(t2); // 5
if (b) F1(t2); // 2
if (b) F2(t2); // 3, 4
if (b) F3(t2); // 5
}
static void M3<T>(T? t3) where T : class
{
F1(t3); // 6
F2(t3); // 7, 8
F3(t3); // 9
if (b) F1(t3); // 6
if (b) F2(t3); // 7, 8
if (b) F3(t3); // 9
if (t3 == null) return;
F1(t3);
F2(t3);
......@@ -36159,8 +36594,8 @@ static void M1<T>(T t1)
}
static void M5<T>(T? t5) where T : struct
{
F1(t5); // 10
F5(t5); // 11
if (b) F1(t5); // 10
if (b) F5(t5); // 11
if (t5 == null) return;
F1(t5);
F5(t5);
......@@ -36168,42 +36603,42 @@ static void M1<T>(T t1)
}";
var comp = CreateCompilation(new[] { DisallowNullAttributeDefinition, AllowNullAttributeDefinition, source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (11,12): warning CS8607: A possible null value may not be used for a type marked with [NotNull] or [DisallowNull]
// (12,12): warning CS8607: A possible null value may not be used for a type marked with [NotNull] or [DisallowNull]
// F1(t1); // 0
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t1").WithLocation(11, 12),
// (18,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t1").WithLocation(12, 12),
// (19,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
// t2 = null; // 1
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(18, 14),
// (19,12): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F1<T?>(T? t)'.
// F1(t2); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F1<T?>(T? t)").WithLocation(19, 12),
// (20,9): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// F2(t2); // 3, 4
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterReferenceTypeConstraint, "F2").WithArguments("Program.F2<T>(T)", "T", "T?").WithLocation(20, 9),
// (20,12): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F2<T?>(T? t)'.
// F2(t2); // 3, 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F2<T?>(T? t)").WithLocation(20, 12),
// (21,12): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F3<T>(T? t)'.
// F3(t2); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F3<T>(T? t)").WithLocation(21, 12),
// (25,12): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F1<T?>(T? t)'.
// F1(t3); // 6
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F1<T?>(T? t)").WithLocation(25, 12),
// (26,9): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// F2(t3); // 7, 8
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterReferenceTypeConstraint, "F2").WithArguments("Program.F2<T>(T)", "T", "T?").WithLocation(26, 9),
// (26,12): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F2<T?>(T? t)'.
// F2(t3); // 7, 8
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F2<T?>(T? t)").WithLocation(26, 12),
// (27,12): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F3<T>(T? t)'.
// F3(t3); // 9
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F3<T>(T? t)").WithLocation(27, 12),
// (40,12): warning CS8607: A possible null value may not be assigned to a target marked with the [DisallowNull] attribute
// F1(t5); // 10
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t5").WithLocation(40, 12),
// (41,12): warning CS8607: A possible null value may not be assigned to a target marked with the [DisallowNull] attribute
// F5(t5); // 11
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t5").WithLocation(41, 12)
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(19, 14),
// (20,19): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F1<T?>(T? t)'.
// if (b) F1(t2); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F1<T?>(T? t)").WithLocation(20, 19),
// (21,16): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// if (b) F2(t2); // 3, 4
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterReferenceTypeConstraint, "F2").WithArguments("Program.F2<T>(T)", "T", "T?").WithLocation(21, 16),
// (21,19): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F2<T?>(T? t)'.
// if (b) F2(t2); // 3, 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F2<T?>(T? t)").WithLocation(21, 19),
// (22,19): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F3<T>(T? t)'.
// if (b) F3(t2); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F3<T>(T? t)").WithLocation(22, 19),
// (26,19): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F1<T?>(T? t)'.
// if (b) F1(t3); // 6
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F1<T?>(T? t)").WithLocation(26, 19),
// (27,16): warning CS8634: The type 'T?' cannot be used as type parameter 'T' in the generic type or method 'Program.F2<T>(T)'. Nullability of type argument 'T?' doesn't match 'class' constraint.
// if (b) F2(t3); // 7, 8
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterReferenceTypeConstraint, "F2").WithArguments("Program.F2<T>(T)", "T", "T?").WithLocation(27, 16),
// (27,19): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F2<T?>(T? t)'.
// if (b) F2(t3); // 7, 8
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F2<T?>(T? t)").WithLocation(27, 19),
// (28,19): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F3<T>(T? t)'.
// if (b) F3(t3); // 9
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F3<T>(T? t)").WithLocation(28, 19),
// (41,19): warning CS8607: A possible null value may not be used for a type marked with [NotNull] or [DisallowNull]
// if (b) F1(t5); // 10
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t5").WithLocation(41, 19),
// (42,19): warning CS8607: A possible null value may not be used for a type marked with [NotNull] or [DisallowNull]
// if (b) F5(t5); // 11
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t5").WithLocation(42, 19)
);
}
......@@ -36214,6 +36649,7 @@ public void DisallowNull_Parameter_02()
@"using System.Diagnostics.CodeAnalysis;
class Program
{
static bool b = false;
static void F1<T>([DisallowNull]T t) { }
static void F2<T>([DisallowNull]T t) where T : class { }
static void F3<T>([DisallowNull]T? t) where T : class { }
......@@ -36229,15 +36665,15 @@ static void M1<T>(T t1)
F2<T>(t2);
F3<T>(t2);
t2 = null; // 1
F1<T>(t2); // 2
F2<T>(t2); // 3
F3<T>(t2); // 4
if (b) F1<T>(t2); // 2
if (b) F2<T>(t2); // 3
if (b) F3<T>(t2); // 4
}
static void M3<T>(T? t3) where T : class
{
F1<T>(t3); // 5
F2<T>(t3); // 6
F3<T>(t3); // 7
if (b) F1<T>(t3); // 5
if (b) F2<T>(t3); // 6
if (b) F3<T>(t3); // 7
if (t3 == null) return;
F1<T>(t3);
F2<T>(t3);
......@@ -36259,36 +36695,33 @@ static void M1<T>(T t1)
}";
var comp = CreateCompilation(new[] { DisallowNullAttributeDefinition, source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (11,15): warning CS8607: A possible null value may not be used for a type marked with [NotNull] or [DisallowNull]
// (12,15): warning CS8607: A possible null value may not be used for a type marked with [NotNull] or [DisallowNull]
// F1<T>(t1); // 0
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t1").WithLocation(11, 15),
// (18,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t1").WithLocation(12, 15),
// (19,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
// t2 = null; // 1
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(18, 14),
// (19,15): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F1<T>(T t)'.
// F1<T>(t2); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F1<T>(T t)").WithLocation(19, 15),
// (20,15): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F2<T>(T t)'.
// F2<T>(t2); // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F2<T>(T t)").WithLocation(20, 15),
// (21,15): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F3<T>(T? t)'.
// F3<T>(t2); // 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F3<T>(T? t)").WithLocation(21, 15),
// (25,15): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F1<T>(T t)'.
// F1<T>(t3); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F1<T>(T t)").WithLocation(25, 15),
// (26,15): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F2<T>(T t)'.
// F2<T>(t3); // 6
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F2<T>(T t)").WithLocation(26, 15),
// (27,15): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F3<T>(T? t)'.
// F3<T>(t3); // 7
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F3<T>(T? t)").WithLocation(27, 15),
// (40,16): warning CS8607: A possible null value may not be assigned to a target marked with the [DisallowNull] attribute
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(19, 14),
// (20,22): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F1<T>(T t)'.
// if (b) F1<T>(t2); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F1<T>(T t)").WithLocation(20, 22),
// (21,22): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F2<T>(T t)'.
// if (b) F2<T>(t2); // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F2<T>(T t)").WithLocation(21, 22),
// (22,22): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F3<T>(T? t)'.
// if (b) F3<T>(t2); // 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F3<T>(T? t)").WithLocation(22, 22),
// (26,22): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F1<T>(T t)'.
// if (b) F1<T>(t3); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F1<T>(T t)").WithLocation(26, 22),
// (27,22): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F2<T>(T t)'.
// if (b) F2<T>(t3); // 6
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F2<T>(T t)").WithLocation(27, 22),
// (28,22): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F3<T>(T? t)'.
// if (b) F3<T>(t3); // 7
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F3<T>(T? t)").WithLocation(28, 22),
// (41,16): warning CS8607: A possible null value may not be used for a type marked with [NotNull] or [DisallowNull]
// F1<T?>(t5); // 8
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t5").WithLocation(40, 16),
// (41,15): warning CS8607: A possible null value may not be assigned to a target marked with the [DisallowNull] attribute
// F5<T>(t5); // 9
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t5").WithLocation(41, 15)
Diagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, "t5").WithLocation(41, 16)
);
}
......@@ -36300,6 +36733,7 @@ public void DisallowNull_Parameter_03()
@"using System.Diagnostics.CodeAnalysis;
class Program
{
static bool b = false;
static void F1([DisallowNull]string s) { }
static void F2([DisallowNull]string? s) { }
static void M1(string s1)
......@@ -36307,13 +36741,13 @@ static void M1(string s1)
F1(s1);
F2(s1);
s1 = null; // 1
F1(s1); // 2
F2(s1); // 3
if (b) F1(s1); // 2
if (b) F2(s1); // 3
}
static void M2(string? s2)
{
F1(s2); // 4
F2(s2); // 5
if (b) F1(s2); // 4
if (b) F2(s2); // 5
if (s2 == null) return;
F1(s2);
F2(s2);
......@@ -36321,21 +36755,21 @@ static void M2(string? s2)
}";
var comp = CreateCompilation(new[] { DisallowNullAttributeDefinition, source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (10,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
// (11,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
// s1 = null; // 1
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(10, 14),
// (11,12): warning CS8604: Possible null reference argument for parameter 's' in 'void Program.F1(string s)'.
// F1(s1); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "s1").WithArguments("s", "void Program.F1(string s)").WithLocation(11, 12),
// (12,12): warning CS8604: Possible null reference argument for parameter 's' in 'void Program.F2(string? s)'.
// F2(s1); // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "s1").WithArguments("s", "void Program.F2(string? s)").WithLocation(12, 12),
// (16,12): warning CS8604: Possible null reference argument for parameter 's' in 'void Program.F1(string s)'.
// F1(s2); // 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "s2").WithArguments("s", "void Program.F1(string s)").WithLocation(16, 12),
// (17,12): warning CS8604: Possible null reference argument for parameter 's' in 'void Program.F2(string? s)'.
// F2(s2); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "s2").WithArguments("s", "void Program.F2(string? s)").WithLocation(17, 12));
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(11, 14),
// (12,19): warning CS8604: Possible null reference argument for parameter 's' in 'void Program.F1(string s)'.
// if (b) F1(s1); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "s1").WithArguments("s", "void Program.F1(string s)").WithLocation(12, 19),
// (13,19): warning CS8604: Possible null reference argument for parameter 's' in 'void Program.F2(string? s)'.
// if (b) F2(s1); // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "s1").WithArguments("s", "void Program.F2(string? s)").WithLocation(13, 19),
// (17,19): warning CS8604: Possible null reference argument for parameter 's' in 'void Program.F1(string s)'.
// if (b) F1(s2); // 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "s2").WithArguments("s", "void Program.F1(string s)").WithLocation(17, 19),
// (18,19): warning CS8604: Possible null reference argument for parameter 's' in 'void Program.F2(string? s)'.
// if (b) F2(s2); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "s2").WithArguments("s", "void Program.F2(string? s)").WithLocation(18, 19));
}
[Fact, WorkItem(36009, "https://github.com/dotnet/roslyn/issues/36009")]
......@@ -37975,21 +38409,21 @@ static void M1<T>(T t1)
{
F1<T>(t1).ToString(); // 1
}
static void M2<T>(T t2) where T : class
static void M2<T>(bool b, T t2) where T : class
{
F1<T>(t2).ToString(); // 2
F2<T>(t2).ToString(); // 3
F3<T>(t2).ToString(); // 4
t2 = null; // 5
F1<T>(t2).ToString(); // 6
F2<T>(t2).ToString(); // 7
F3<T>(t2).ToString(); // 8
F1<T>(b ? t2 : default).ToString(); // 6
F2<T>(b ? t2 : default).ToString(); // 7
F3<T>(b ? t2 : default).ToString(); // 8
}
static void M3<T>(T? t3) where T : class
static void M3<T>(bool b, T? t3) where T : class
{
F1<T>(t3).ToString(); // 9
F2<T>(t3).ToString(); // 10
F3<T>(t3).ToString(); // 11
F1<T>(b ? t3 : default).ToString(); // 9
F2<T>(b ? t3 : default).ToString(); // 10
F3<T>(b ? t3 : default).ToString(); // 11
if (t3 == null) return;
F1<T>(t3).ToString(); // 12
F2<T>(t3).ToString(); // 13
......@@ -38027,41 +38461,41 @@ static void M1<T>(T t1)
// t2 = null; // 5
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(18, 14),
// (19,9): warning CS8602: Dereference of a possibly null reference.
// F1<T>(t2).ToString(); // 6
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F1<T>(t2)").WithLocation(19, 9),
// F1<T>(b ? t2 : default).ToString(); // 6
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F1<T>(b ? t2 : default)").WithLocation(19, 9),
// (19,15): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F1<T>(T t)'.
// F1<T>(t2).ToString(); // 6
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "T Program.F1<T>(T t)").WithLocation(19, 15),
// F1<T>(b ? t2 : default).ToString(); // 6
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t2 : default").WithArguments("t", "T Program.F1<T>(T t)").WithLocation(19, 15),
// (20,9): warning CS8602: Dereference of a possibly null reference.
// F2<T>(t2).ToString(); // 7
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F2<T>(t2)").WithLocation(20, 9),
// F2<T>(b ? t2 : default).ToString(); // 7
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F2<T>(b ? t2 : default)").WithLocation(20, 9),
// (20,15): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F2<T>(T t)'.
// F2<T>(t2).ToString(); // 7
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "T Program.F2<T>(T t)").WithLocation(20, 15),
// F2<T>(b ? t2 : default).ToString(); // 7
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t2 : default").WithArguments("t", "T Program.F2<T>(T t)").WithLocation(20, 15),
// (21,9): warning CS8602: Dereference of a possibly null reference.
// F3<T>(t2).ToString(); // 8
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F3<T>(t2)").WithLocation(21, 9),
// F3<T>(b ? t2 : default).ToString(); // 8
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F3<T>(b ? t2 : default)").WithLocation(21, 9),
// (21,15): warning CS8604: Possible null reference argument for parameter 't' in 'T? Program.F3<T>(T t)'.
// F3<T>(t2).ToString(); // 8
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "T? Program.F3<T>(T t)").WithLocation(21, 15),
// F3<T>(b ? t2 : default).ToString(); // 8
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t2 : default").WithArguments("t", "T? Program.F3<T>(T t)").WithLocation(21, 15),
// (25,9): warning CS8602: Dereference of a possibly null reference.
// F1<T>(t3).ToString(); // 9
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F1<T>(t3)").WithLocation(25, 9),
// F1<T>(b ? t3 : default).ToString(); // 9
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F1<T>(b ? t3 : default)").WithLocation(25, 9),
// (25,15): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F1<T>(T t)'.
// F1<T>(t3).ToString(); // 9
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "T Program.F1<T>(T t)").WithLocation(25, 15),
// F1<T>(b ? t3 : default).ToString(); // 9
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t3 : default").WithArguments("t", "T Program.F1<T>(T t)").WithLocation(25, 15),
// (26,9): warning CS8602: Dereference of a possibly null reference.
// F2<T>(t3).ToString(); // 10
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F2<T>(t3)").WithLocation(26, 9),
// F2<T>(b ? t3 : default).ToString(); // 10
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F2<T>(b ? t3 : default)").WithLocation(26, 9),
// (26,15): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F2<T>(T t)'.
// F2<T>(t3).ToString(); // 10
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "T Program.F2<T>(T t)").WithLocation(26, 15),
// F2<T>(b ? t3 : default).ToString(); // 10
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t3 : default").WithArguments("t", "T Program.F2<T>(T t)").WithLocation(26, 15),
// (27,9): warning CS8602: Dereference of a possibly null reference.
// F3<T>(t3).ToString(); // 11
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F3<T>(t3)").WithLocation(27, 9),
// F3<T>(b ? t3 : default).ToString(); // 11
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F3<T>(b ? t3 : default)").WithLocation(27, 9),
// (27,15): warning CS8604: Possible null reference argument for parameter 't' in 'T? Program.F3<T>(T t)'.
// F3<T>(t3).ToString(); // 11
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "T? Program.F3<T>(T t)").WithLocation(27, 15),
// F3<T>(b ? t3 : default).ToString(); // 11
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t3 : default").WithArguments("t", "T? Program.F3<T>(T t)").WithLocation(27, 15),
// (29,9): warning CS8602: Dereference of a possibly null reference.
// F1<T>(t3).ToString(); // 12
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F1<T>(t3)").WithLocation(29, 9),
......@@ -38101,21 +38535,21 @@ static void M1<T>(T t1)
{
F1<T>(t1).ToString(); // 0, 1
}
static void M2<T>(T t2) where T : class
static void M2<T>(bool b, T t2) where T : class
{
F1<T>(t2).ToString(); // 2
F2<T>(t2).ToString(); // 3
F3<T>(t2).ToString(); // 4
t2 = null; // 5
F1<T>(t2).ToString(); // 6
F2<T>(t2).ToString(); // 7
F3<T>(t2).ToString(); // 8
F1<T>(b ? t2 : default).ToString(); // 6
F2<T>(b ? t2 : default).ToString(); // 7
F3<T>(b ? t2 : default).ToString(); // 8
}
static void M3<T>(T? t3) where T : class
static void M3<T>(bool b, T? t3) where T : class
{
F1<T>(t3).ToString(); // 9
F2<T>(t3).ToString(); // 10
F3<T>(t3).ToString(); // 11
F1<T>(b ? t3 : default).ToString(); // 9
F2<T>(b ? t3 : default).ToString(); // 10
F3<T>(b ? t3 : default).ToString(); // 11
if (t3 == null) return;
F1<T>(t3).ToString(); // 12
F2<T>(t3).ToString(); // 13
......@@ -38159,41 +38593,41 @@ static void M1<T>(T t1)
// t2 = null; // 5
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(18, 14),
// (19,9): warning CS8602: Dereference of a possibly null reference.
// F1<T>(t2).ToString(); // 6
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F1<T>(t2)").WithLocation(19, 9),
// F1<T>(b ? t2 : default).ToString(); // 6
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F1<T>(b ? t2 : default)").WithLocation(19, 9),
// (19,15): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F1<T>(T t)'.
// F1<T>(t2).ToString(); // 6
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "T Program.F1<T>(T t)").WithLocation(19, 15),
// F1<T>(b ? t2 : default).ToString(); // 6
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t2 : default").WithArguments("t", "T Program.F1<T>(T t)").WithLocation(19, 15),
// (20,9): warning CS8602: Dereference of a possibly null reference.
// F2<T>(t2).ToString(); // 7
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F2<T>(t2)").WithLocation(20, 9),
// F2<T>(b ? t2 : default).ToString(); // 7
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F2<T>(b ? t2 : default)").WithLocation(20, 9),
// (20,15): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F2<T>(T t)'.
// F2<T>(t2).ToString(); // 7
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "T Program.F2<T>(T t)").WithLocation(20, 15),
// F2<T>(b ? t2 : default).ToString(); // 7
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t2 : default").WithArguments("t", "T Program.F2<T>(T t)").WithLocation(20, 15),
// (21,9): warning CS8602: Dereference of a possibly null reference.
// F3<T>(t2).ToString(); // 8
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F3<T>(t2)").WithLocation(21, 9),
// F3<T>(b ? t2 : default).ToString(); // 8
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F3<T>(b ? t2 : default)").WithLocation(21, 9),
// (21,15): warning CS8604: Possible null reference argument for parameter 't' in 'T? Program.F3<T>(T t)'.
// F3<T>(t2).ToString(); // 8
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "T? Program.F3<T>(T t)").WithLocation(21, 15),
// F3<T>(b ? t2 : default).ToString(); // 8
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t2 : default").WithArguments("t", "T? Program.F3<T>(T t)").WithLocation(21, 15),
// (25,9): warning CS8602: Dereference of a possibly null reference.
// F1<T>(t3).ToString(); // 9
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F1<T>(t3)").WithLocation(25, 9),
// F1<T>(b ? t3 : default).ToString(); // 9
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F1<T>(b ? t3 : default)").WithLocation(25, 9),
// (25,15): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F1<T>(T t)'.
// F1<T>(t3).ToString(); // 9
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "T Program.F1<T>(T t)").WithLocation(25, 15),
// F1<T>(b ? t3 : default).ToString(); // 9
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t3 : default").WithArguments("t", "T Program.F1<T>(T t)").WithLocation(25, 15),
// (26,9): warning CS8602: Dereference of a possibly null reference.
// F2<T>(t3).ToString(); // 10
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F2<T>(t3)").WithLocation(26, 9),
// F2<T>(b ? t3 : default).ToString(); // 10
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F2<T>(b ? t3 : default)").WithLocation(26, 9),
// (26,15): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F2<T>(T t)'.
// F2<T>(t3).ToString(); // 10
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "T Program.F2<T>(T t)").WithLocation(26, 15),
// F2<T>(b ? t3 : default).ToString(); // 10
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t3 : default").WithArguments("t", "T Program.F2<T>(T t)").WithLocation(26, 15),
// (27,9): warning CS8602: Dereference of a possibly null reference.
// F3<T>(t3).ToString(); // 11
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F3<T>(t3)").WithLocation(27, 9),
// F3<T>(b ? t3 : default).ToString(); // 11
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F3<T>(b ? t3 : default)").WithLocation(27, 9),
// (27,15): warning CS8604: Possible null reference argument for parameter 't' in 'T? Program.F3<T>(T t)'.
// F3<T>(t3).ToString(); // 11
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "T? Program.F3<T>(T t)").WithLocation(27, 15),
// F3<T>(b ? t3 : default).ToString(); // 11
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t3 : default").WithArguments("t", "T? Program.F3<T>(T t)").WithLocation(27, 15),
// (29,9): warning CS8602: Dereference of a possibly null reference.
// F1<T>(t3).ToString(); // 12
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "F1<T>(t3)").WithLocation(29, 9),
......@@ -38623,7 +39057,7 @@ static void M1<T>(T t1)
F1<T>(t1, out var s1); // 0
s1.ToString(); // 1
}
static void M2<T>(T t2) where T : class
static void M2<T>(bool b, T t2) where T : class
{
F1<T>(t2, out var s2);
s2.ToString(); // 2
......@@ -38635,24 +39069,24 @@ static void M1<T>(T t1)
s4.ToString(); // 4
t2 = null; // 5
F1<T>(t2, out var s5); // 6
F1<T>(b ? t2 : default, out var s5); // 6
s5.ToString(); // 6B
F2<T>(t2, out var s6); // 7
F2<T>(b ? t2 : default, out var s6); // 7
s6.ToString(); // 7B
F3<T>(t2, out var s7); // 8
F3<T>(b ? t2 : default, out var s7); // 8
s7.ToString(); // 8B
}
static void M3<T>(T? t3) where T : class
static void M3<T>(bool b, T? t3) where T : class
{
F1<T>(t3, out var s8); // 9
F1<T>(b ? t3 : default, out var s8); // 9
s8.ToString(); // 9B
F2<T>(t3, out var s9); // 10
F2<T>(b ? t3 : default, out var s9); // 10
s9.ToString(); // 10B
F3<T>(t3, out var s10); // 11
F3<T>(b ? t3 : default, out var s10); // 11
s10.ToString(); // 11B
if (t3 == null) return;
......@@ -38707,38 +39141,38 @@ static void M1<T>(T t1)
// t2 = null; // 5
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(25, 14),
// (26,15): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F1<T>(T t, out T t2)'.
// F1<T>(t2, out var s5); // 6
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F1<T>(T t, out T t2)").WithLocation(26, 15),
// F1<T>(b ? t2 : default, out var s5); // 6
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t2 : default").WithArguments("t", "void Program.F1<T>(T t, out T t2)").WithLocation(26, 15),
// (27,9): warning CS8602: Dereference of a possibly null reference.
// s5.ToString(); // 6B
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s5").WithLocation(27, 9),
// (29,15): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F2<T>(T t, out T t2)'.
// F2<T>(t2, out var s6); // 7
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F2<T>(T t, out T t2)").WithLocation(29, 15),
// F2<T>(b ? t2 : default, out var s6); // 7
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t2 : default").WithArguments("t", "void Program.F2<T>(T t, out T t2)").WithLocation(29, 15),
// (30,9): warning CS8602: Dereference of a possibly null reference.
// s6.ToString(); // 7B
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s6").WithLocation(30, 9),
// (32,15): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F3<T>(T t, out T? t2)'.
// F3<T>(t2, out var s7); // 8
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void Program.F3<T>(T t, out T? t2)").WithLocation(32, 15),
// F3<T>(b ? t2 : default, out var s7); // 8
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t2 : default").WithArguments("t", "void Program.F3<T>(T t, out T? t2)").WithLocation(32, 15),
// (33,9): warning CS8602: Dereference of a possibly null reference.
// s7.ToString(); // 8B
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s7").WithLocation(33, 9),
// (37,15): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F1<T>(T t, out T t2)'.
// F1<T>(t3, out var s8); // 9
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F1<T>(T t, out T t2)").WithLocation(37, 15),
// F1<T>(b ? t3 : default, out var s8); // 9
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t3 : default").WithArguments("t", "void Program.F1<T>(T t, out T t2)").WithLocation(37, 15),
// (38,9): warning CS8602: Dereference of a possibly null reference.
// s8.ToString(); // 9B
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s8").WithLocation(38, 9),
// (40,15): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F2<T>(T t, out T t2)'.
// F2<T>(t3, out var s9); // 10
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F2<T>(T t, out T t2)").WithLocation(40, 15),
// F2<T>(b ? t3 : default, out var s9); // 10
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t3 : default").WithArguments("t", "void Program.F2<T>(T t, out T t2)").WithLocation(40, 15),
// (41,9): warning CS8602: Dereference of a possibly null reference.
// s9.ToString(); // 10B
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s9").WithLocation(41, 9),
// (43,15): warning CS8604: Possible null reference argument for parameter 't' in 'void Program.F3<T>(T t, out T? t2)'.
// F3<T>(t3, out var s10); // 11
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void Program.F3<T>(T t, out T? t2)").WithLocation(43, 15),
// F3<T>(b ? t3 : default, out var s10); // 11
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? t3 : default").WithArguments("t", "void Program.F3<T>(T t, out T? t2)").WithLocation(43, 15),
// (44,9): warning CS8602: Dereference of a possibly null reference.
// s10.ToString(); // 11B
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s10").WithLocation(44, 9),
......@@ -39382,21 +39816,21 @@ static void M1<T>(T t1)
{
F1<T>(t1).ToString();
}
static void M2<T>(T t2) where T : class
static void M2<T>(bool b, T t2) where T : class
{
F1<T>(t2).ToString();
F2<T>(t2).ToString();
F3<T>(t2).ToString();
t2 = null; // 1
F1<T>(t2).ToString(); // 2
F2<T>(t2).ToString(); // 3
F3<T>(t2).ToString(); // 4
if (b) F1<T>(t2).ToString(); // 2
if (b) F2<T>(t2).ToString(); // 3
if (b) F3<T>(t2).ToString(); // 4
}
static void M3<T>(T? t3) where T : class
static void M3<T>(bool b, T? t3) where T : class
{
F1<T>(t3).ToString(); // 5
F2<T>(t3).ToString(); // 6
F3<T>(t3).ToString(); // 7
if (b) F1<T>(t3).ToString(); // 5
if (b) F2<T>(t3).ToString(); // 6
if (b) F3<T>(t3).ToString(); // 7
if (t3 == null) return;
F1<T>(t3).ToString();
F2<T>(t3).ToString();
......@@ -39427,24 +39861,24 @@ static void M1<T>(T t1)
// (18,14): warning CS8600: Converting null literal or possible null value to non-nullable type.
// t2 = null; // 1
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(18, 14),
// (19,15): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F1<T>(T t)'.
// F1<T>(t2).ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "T Program.F1<T>(T t)").WithLocation(19, 15),
// (20,15): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F2<T>(T t)'.
// F2<T>(t2).ToString(); // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "T Program.F2<T>(T t)").WithLocation(20, 15),
// (21,15): warning CS8604: Possible null reference argument for parameter 't' in 'T? Program.F3<T>(T t)'.
// F3<T>(t2).ToString(); // 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "T? Program.F3<T>(T t)").WithLocation(21, 15),
// (25,15): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F1<T>(T t)'.
// F1<T>(t3).ToString(); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "T Program.F1<T>(T t)").WithLocation(25, 15),
// (26,15): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F2<T>(T t)'.
// F2<T>(t3).ToString(); // 6
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "T Program.F2<T>(T t)").WithLocation(26, 15),
// (27,15): warning CS8604: Possible null reference argument for parameter 't' in 'T? Program.F3<T>(T t)'.
// F3<T>(t3).ToString(); // 7
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "T? Program.F3<T>(T t)").WithLocation(27, 15));
// (19,22): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F1<T>(T t)'.
// if (b) F1<T>(t2).ToString(); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "T Program.F1<T>(T t)").WithLocation(19, 22),
// (20,22): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F2<T>(T t)'.
// if (b) F2<T>(t2).ToString(); // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "T Program.F2<T>(T t)").WithLocation(20, 22),
// (21,22): warning CS8604: Possible null reference argument for parameter 't' in 'T? Program.F3<T>(T t)'.
// if (b) F3<T>(t2).ToString(); // 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "T? Program.F3<T>(T t)").WithLocation(21, 22),
// (25,22): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F1<T>(T t)'.
// if (b) F1<T>(t3).ToString(); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "T Program.F1<T>(T t)").WithLocation(25, 22),
// (26,22): warning CS8604: Possible null reference argument for parameter 't' in 'T Program.F2<T>(T t)'.
// if (b) F2<T>(t3).ToString(); // 6
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "T Program.F2<T>(T t)").WithLocation(26, 22),
// (27,22): warning CS8604: Possible null reference argument for parameter 't' in 'T? Program.F3<T>(T t)'.
// if (b) F3<T>(t3).ToString(); // 7
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "T? Program.F3<T>(T t)").WithLocation(27, 22));
}
[Fact]
......@@ -48336,10 +48770,7 @@ class CL1
Diagnostic(ErrorCode.ERR_UseDefViolationOut, "x3").WithArguments("x3").WithLocation(25, 29),
// (27,29): warning CS8604: Possible null reference argument for parameter 'x' in 'CL1? CL1.M1(CL1 x)'.
// z3.M1(y3.p1);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y3.p1").WithArguments("x", "CL1? CL1.M1(CL1 x)").WithLocation(27, 29),
// (28,18): warning CS8600: Converting null literal or possible null value to non-nullable type.
// CL1 v3 = y3.p1;
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "y3.p1").WithLocation(28, 18));
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y3.p1").WithArguments("x", "CL1? CL1.M1(CL1 x)").WithLocation(27, 29));
}
[Fact]
......@@ -58993,9 +59424,13 @@ class B { public static implicit operator A(B? b) => new A(); }
class C
{
void Test(A a, B? b)
void Test1(A a, B? b)
{
A t1 = M<A>(a, b);
}
void Test2(A a, B? b)
{
A t2 = M(a, b); // unexpected
}
......@@ -59005,9 +59440,9 @@ void Test(A a, B? b)
// There should be no diagnostic. See https://github.com/dotnet/roslyn/issues/36132
comp.VerifyDiagnostics(
// (10,16): warning CS8600: Converting null literal or possible null value to non-nullable type.
// (14,16): warning CS8600: Converting null literal or possible null value to non-nullable type.
// A t2 = M(a, b); // unexpected
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "M(a, b)").WithLocation(10, 16)
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "M(a, b)").WithLocation(14, 16)
);
}
......@@ -61371,22 +61806,19 @@ static void F(D<object?> y)
}";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (19,28): warning CS8620: Nullability of reference types in argument of type 'D<object?>' doesn't match target type 'D<object>' for parameter 'key' in 'void D<int>.Add(D<object>? key, params D<object>?[] value)'.
// _ = new D<int>() { y, y }; // warn 5 and 6
// (19,28): warning CS8620: Argument of type 'D<object?>' cannot be used for parameter 'key' of type 'D<object>' in 'void D<int>.Add(D<object>? key, params D<object>?[] value)' due to differences in the nullability of reference types.
// _ = new D<int>() { y, y }; // warn 5 and 6
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "y").WithArguments("D<object?>", "D<object>", "key", "void D<int>.Add(D<object>? key, params D<object>?[] value)").WithLocation(19, 28),
// (19,32): warning CS8620: Nullability of reference types in argument of type 'D<object?>' doesn't match target type 'D<object>' for parameter 'key' in 'void D<int>.Add(D<object>? key, params D<object>?[] value)'.
// _ = new D<int>() { y, y }; // warn 5 and 6
// (19,32): warning CS8620: Argument of type 'D<object?>' cannot be used for parameter 'key' of type 'D<object>' in 'void D<int>.Add(D<object>? key, params D<object>?[] value)' due to differences in the nullability of reference types.
// _ = new D<int>() { y, y }; // warn 5 and 6
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "y").WithArguments("D<object?>", "D<object>", "key", "void D<int>.Add(D<object>? key, params D<object>?[] value)").WithLocation(19, 32),
// (9,28): warning CS8604: Possible null reference argument for parameter 'key' in 'void C<int>.Add(C<object?> key, params C<object?>[] value)'.
// _ = new C<int>() { x, x }; // warn 1 and 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x").WithArguments("key", "void C<int>.Add(C<object?> key, params C<object?>[] value)").WithLocation(9, 28),
// (9,28): warning CS8620: Nullability of reference types in argument of type 'C<object>' doesn't match target type 'C<object?>' for parameter 'key' in 'void C<int>.Add(C<object?> key, params C<object?>[] value)'.
// (9,28): warning CS8620: Argument of type 'C<object>' cannot be used for parameter 'key' of type 'C<object?>' in 'void C<int>.Add(C<object?> key, params C<object?>[] value)' due to differences in the nullability of reference types.
// _ = new C<int>() { x, x }; // warn 1 and 2
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "x").WithArguments("C<object>", "C<object?>", "key", "void C<int>.Add(C<object?> key, params C<object?>[] value)").WithLocation(9, 28),
// (9,31): warning CS8604: Possible null reference argument for parameter 'key' in 'void C<int>.Add(C<object?> key, params C<object?>[] value)'.
// _ = new C<int>() { x, x }; // warn 1 and 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x").WithArguments("key", "void C<int>.Add(C<object?> key, params C<object?>[] value)").WithLocation(9, 31),
// (9,31): warning CS8620: Nullability of reference types in argument of type 'C<object>' doesn't match target type 'C<object?>' for parameter 'key' in 'void C<int>.Add(C<object?> key, params C<object?>[] value)'.
// (9,31): warning CS8620: Argument of type 'C<object>' cannot be used for parameter 'key' of type 'C<object?>' in 'void C<int>.Add(C<object?> key, params C<object?>[] value)' due to differences in the nullability of reference types.
// _ = new C<int>() { x, x }; // warn 1 and 2
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "x").WithArguments("C<object>", "C<object?>", "key", "void C<int>.Add(C<object?> key, params C<object?>[] value)").WithLocation(9, 31)
);
......@@ -66364,11 +66796,11 @@ static void F(bool b, object x, object? y)
x,
x,
x,
y, // 1
b ? y : null!, // 1
y,
new ValueTuple<object, object?>(y, x), // 2
new ValueTuple<object, object?>(b ? y : null!, x), // 2
x,
new ValueTuple<object, object?, object>(x, y, y))/*T:(object?, object!, object?, object!, object?, (object!, object?), object!, object!, object?, object!)*/; // 3
new ValueTuple<object, object?, object>(x, b ? y : null!, b ? y : null!))/*T:(object?, object!, object?, object!, object?, (object!, object?), object!, object!, object?, object!)*/; // 3
t.Item1.ToString();
t.Item2.ToString();
t.Item3.ToString();
......@@ -66393,36 +66825,36 @@ static void F(bool b, object x, object? y)
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (10,13): warning CS8604: Possible null reference argument for parameter 'item4' in '(object?, object, object?, object, object?, (object, object?), object, object, object?, object).ValueTuple(object? item1, object item2, object? item3, object item4, object? item5, (object, object?) item6, object item7, (object, object?, object) rest)'.
// y, // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y").WithArguments("item4", "(object?, object, object?, object, object?, (object, object?), object, object, object?, object).ValueTuple(object? item1, object item2, object? item3, object item4, object? item5, (object, object?) item6, object item7, (object, object?, object) rest)").WithLocation(10, 13),
// (12,45): warning CS8604: Possible null reference argument for parameter 'item1' in '(object, object?).ValueTuple(object item1, object? item2)'.
// new ValueTuple<object, object?>(y, x), // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y").WithArguments("item1", "(object, object?).ValueTuple(object item1, object? item2)").WithLocation(12, 45),
// (14,59): warning CS8604: Possible null reference argument for parameter 'item3' in '(object, object?, object).ValueTuple(object item1, object? item2, object item3)'.
// new ValueTuple<object, object?, object>(x, y, y))/*T:(object?, object!, object?, object!, object?, (object!, object?), object!, object!, object?, object!)*/; // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y").WithArguments("item3", "(object, object?, object).ValueTuple(object item1, object? item2, object item3)").WithLocation(14, 59),
// (18,9): warning CS8602: Dereference of a possibly null reference.
// t.Item4.ToString(); // 4
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t.Item4").WithLocation(18, 9),
// (19,9): warning CS8602: Dereference of a possibly null reference.
// t.Item5.ToString(); // 5
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t.Item5").WithLocation(19, 9),
// (20,9): warning CS8602: Dereference of a possibly null reference.
// t.Item6.Item1.ToString(); // 6
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t.Item6.Item1").WithLocation(20, 9),
// (26,13): warning CS8602: Dereference of a possibly null reference.
// t.Item9.ToString(); // 7
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t.Item9").WithLocation(26, 13),
// (27,13): warning CS8602: Dereference of a possibly null reference.
// t.Item10.ToString(); // 8
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t.Item10").WithLocation(27, 13),
// (32,13): warning CS8602: Dereference of a possibly null reference.
// t.Rest.Item2.ToString(); // 9
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t.Rest.Item2").WithLocation(32, 13),
// (33,13): warning CS8602: Dereference of a possibly null reference.
// t.Rest.Item3.ToString(); // 10
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t.Rest.Item3").WithLocation(33, 13));
// (10,13): warning CS8604: Possible null reference argument for parameter 'item4' in '(object?, object, object?, object, object?, (object, object?), object, object, object?, object).ValueTuple(object? item1, object item2, object? item3, object item4, object? item5, (object, object?) item6, object item7, (object, object?, object) rest)'.
// b ? y : null!, // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? y : null!").WithArguments("item4", "(object?, object, object?, object, object?, (object, object?), object, object, object?, object).ValueTuple(object? item1, object item2, object? item3, object item4, object? item5, (object, object?) item6, object item7, (object, object?, object) rest)").WithLocation(10, 13),
// (12,45): warning CS8604: Possible null reference argument for parameter 'item1' in '(object, object?).ValueTuple(object item1, object? item2)'.
// new ValueTuple<object, object?>(b ? y : null!, x), // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? y : null!").WithArguments("item1", "(object, object?).ValueTuple(object item1, object? item2)").WithLocation(12, 45),
// (14,71): warning CS8604: Possible null reference argument for parameter 'item3' in '(object, object?, object).ValueTuple(object item1, object? item2, object item3)'.
// new ValueTuple<object, object?, object>(x, b ? y : null!, b ? y : null!))/*T:(object?, object!, object?, object!, object?, (object!, object?), object!, object!, object?, object!)*/; // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b ? y : null!").WithArguments("item3", "(object, object?, object).ValueTuple(object item1, object? item2, object item3)").WithLocation(14, 71),
// (18,9): warning CS8602: Dereference of a possibly null reference.
// t.Item4.ToString(); // 4
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t.Item4").WithLocation(18, 9),
// (19,9): warning CS8602: Dereference of a possibly null reference.
// t.Item5.ToString(); // 5
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t.Item5").WithLocation(19, 9),
// (20,9): warning CS8602: Dereference of a possibly null reference.
// t.Item6.Item1.ToString(); // 6
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t.Item6.Item1").WithLocation(20, 9),
// (26,13): warning CS8602: Dereference of a possibly null reference.
// t.Item9.ToString(); // 7
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t.Item9").WithLocation(26, 13),
// (27,13): warning CS8602: Dereference of a possibly null reference.
// t.Item10.ToString(); // 8
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t.Item10").WithLocation(27, 13),
// (32,13): warning CS8602: Dereference of a possibly null reference.
// t.Rest.Item2.ToString(); // 9
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t.Rest.Item2").WithLocation(32, 13),
// (33,13): warning CS8602: Dereference of a possibly null reference.
// t.Rest.Item3.ToString(); // 10
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t.Rest.Item3").WithLocation(33, 13));
comp.VerifyTypes();
}
......@@ -89306,40 +89738,40 @@ public void UnconstrainedTypeParameter_MayBeNullable_01()
static void F(object o)
{
}
static void F1<T1>(T1 t1)
static void F1<T1>(bool b, T1 t1)
{
F(t1);
F((object)t1);
if (b) F(t1);
if (b) F((object)t1);
t1.ToString();
}
static void F2<T2>(T2 t2) where T2 : struct
static void F2<T2>(bool b, T2 t2) where T2 : struct
{
F(t2);
F((object)t2);
if (b) F(t2);
if (b) F((object)t2);
t2.ToString();
}
static void F3<T3>(T3 t3) where T3 : class
static void F3<T3>(bool b, T3 t3) where T3 : class
{
F(t3);
F((object)t3);
if (b) F(t3);
if (b) F((object)t3);
t3.ToString();
}
static void F4<T4>(T4 t4) where T4 : new()
static void F4<T4>(bool b, T4 t4) where T4 : new()
{
F(t4);
F((object)t4);
if (b) F(t4);
if (b) F((object)t4);
t4.ToString();
}
static void F5<T5>(T5 t5) where T5 : I
static void F5<T5>(bool b, T5 t5) where T5 : I
{
F(t5);
F((object)t5);
if (b) F(t5);
if (b) F((object)t5);
t5.ToString();
}
static void F6<T6>(T6 t6) where T6 : A
static void F6<T6>(bool b, T6 t6) where T6 : A
{
F(t6);
F((object)t6);
if (b) F(t6);
if (b) F((object)t6);
t6.ToString();
}
}
......@@ -89351,27 +89783,27 @@ class A
}";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (8,11): warning CS8604: Possible null reference argument for parameter 'o' in 'void C.F(object o)'.
// F(t1);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t1").WithArguments("o", "void C.F(object o)").WithLocation(8, 11),
// (9,11): warning CS8600: Converting null literal or possible null value to non-nullable type.
// F((object)t1);
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(object)t1").WithLocation(9, 11),
// (9,11): warning CS8604: Possible null reference argument for parameter 'o' in 'void C.F(object o)'.
// F((object)t1);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "(object)t1").WithArguments("o", "void C.F(object o)").WithLocation(9, 11),
// (8,18): warning CS8604: Possible null reference argument for parameter 'o' in 'void C.F(object o)'.
// if (b) F(t1);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t1").WithArguments("o", "void C.F(object o)").WithLocation(8, 18),
// (9,18): warning CS8600: Converting null literal or possible null value to non-nullable type.
// if (b) F((object)t1);
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(object)t1").WithLocation(9, 18),
// (9,18): warning CS8604: Possible null reference argument for parameter 'o' in 'void C.F(object o)'.
// if (b) F((object)t1);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "(object)t1").WithArguments("o", "void C.F(object o)").WithLocation(9, 18),
// (10,9): warning CS8602: Dereference of a possibly null reference.
// t1.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t1").WithLocation(10, 9),
// (26,11): warning CS8604: Possible null reference argument for parameter 'o' in 'void C.F(object o)'.
// F(t4);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t4").WithArguments("o", "void C.F(object o)").WithLocation(26, 11),
// (27,11): warning CS8600: Converting null literal or possible null value to non-nullable type.
// F((object)t4);
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(object)t4").WithLocation(27, 11),
// (27,11): warning CS8604: Possible null reference argument for parameter 'o' in 'void C.F(object o)'.
// F((object)t4);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "(object)t4").WithArguments("o", "void C.F(object o)").WithLocation(27, 11),
// (26,18): warning CS8604: Possible null reference argument for parameter 'o' in 'void C.F(object o)'.
// if (b) F(t4);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t4").WithArguments("o", "void C.F(object o)").WithLocation(26, 18),
// (27,18): warning CS8600: Converting null literal or possible null value to non-nullable type.
// if (b) F((object)t4);
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(object)t4").WithLocation(27, 18),
// (27,18): warning CS8604: Possible null reference argument for parameter 'o' in 'void C.F(object o)'.
// if (b) F((object)t4);
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "(object)t4").WithArguments("o", "void C.F(object o)").WithLocation(27, 18),
// (28,9): warning CS8602: Dereference of a possibly null reference.
// t4.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "t4").WithLocation(28, 9)
......@@ -94784,10 +95216,10 @@ static void G1(C? x)
x!.F1(x);
x.F1(x);
}
static void G2(C? y)
static void G2(bool b, C? y)
{
y?.F2(y);
y!.F2(y); // 3
if (b) y!.F2(y); // 3
y.F2(y); // 4, 5
}
}
......@@ -94797,9 +95229,9 @@ static class E
}";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (13,15): warning CS8604: Possible null reference argument for parameter 'y' in 'void E.F2(C x, C y)'.
// y!.F2(y); // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y").WithArguments("y", "void E.F2(C x, C y)").WithLocation(13, 15),
// (13,22): warning CS8604: Possible null reference argument for parameter 'y' in 'void E.F2(C x, C y)'.
// if (b) y!.F2(y); // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y").WithArguments("y", "void E.F2(C x, C y)").WithLocation(13, 22),
// (14,9): warning CS8604: Possible null reference argument for parameter 'x' in 'void E.F2(C x, C y)'.
// y.F2(y); // 4, 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y").WithArguments("x", "void E.F2(C x, C y)").WithLocation(14, 9),
......@@ -104507,19 +104939,20 @@ class C<T>
}
class Program
{
static bool b = false;
static void F1(string? s)
{
var a1 = new C<string>() { F = s };
F(a1.F/*T:string?*/); // 1
var a1 = new C<string>() { F = s }; // 0
if (b) F(a1.F/*T:string?*/); // 1
var b1 = a1;
F(b1.F/*T:string?*/); // 2
if (b) F(b1.F/*T:string?*/); // 2
}
static void F2<T>(T? t) where T : class
{
var a2 = new C<T>() { F = t };
F(a2.F/*T:T?*/); // 3
var a2 = new C<T>() { F = t }; // 3
if (b) F(a2.F/*T:T?*/); // 4
var b2 = a2;
F(b2.F/*T:T?*/); // 4
if (b) F(b2.F/*T:T?*/); // 5
}
static void F(object o)
{
......@@ -104527,24 +104960,24 @@ static void F(object o)
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (10,40): warning CS8601: Possible null reference assignment.
// var a1 = new C<string>() { F = s };
Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "s").WithLocation(10, 40),
// (11,11): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// F(a1.F/*T:string?*/); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "a1.F").WithArguments("o", "void Program.F(object o)").WithLocation(11, 11),
// (13,11): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// F(b1.F/*T:string?*/); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b1.F").WithArguments("o", "void Program.F(object o)").WithLocation(13, 11),
// (17,35): warning CS8601: Possible null reference assignment.
// var a2 = new C<T>() { F = t };
Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "t").WithLocation(17, 35),
// (18,11): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// F(a2.F/*T:T?*/); // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "a2.F").WithArguments("o", "void Program.F(object o)").WithLocation(18, 11),
// (20,11): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// F(b2.F/*T:T?*/); // 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b2.F").WithArguments("o", "void Program.F(object o)").WithLocation(20, 11));
// (11,40): warning CS8601: Possible null reference assignment.
// var a1 = new C<string>() { F = s }; // 0
Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "s").WithLocation(11, 40),
// (12,18): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// if (b) F(a1.F/*T:string?*/); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "a1.F").WithArguments("o", "void Program.F(object o)").WithLocation(12, 18),
// (14,18): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// if (b) F(b1.F/*T:string?*/); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b1.F").WithArguments("o", "void Program.F(object o)").WithLocation(14, 18),
// (18,35): warning CS8601: Possible null reference assignment.
// var a2 = new C<T>() { F = t }; // 3
Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "t").WithLocation(18, 35),
// (19,18): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// if (b) F(a2.F/*T:T?*/); // 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "a2.F").WithArguments("o", "void Program.F(object o)").WithLocation(19, 18),
// (21,18): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// if (b) F(b2.F/*T:T?*/); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b2.F").WithArguments("o", "void Program.F(object o)").WithLocation(21, 18));
comp.VerifyTypes();
}
......@@ -104559,19 +104992,20 @@ struct S<T>
}
class Program
{
static bool b = false;
static void F1(string? s)
{
var a1 = new S<string>() { F = s };
F(a1.F/*T:string?*/); // 1
var a1 = new S<string>() { F = s }; // 0
if (b) F(a1.F/*T:string?*/); // 1
var b1 = a1;
F(b1.F/*T:string?*/); // 2
if (b) F(b1.F/*T:string?*/); // 2
}
static void F2<T>(T? t) where T : class
{
var a2 = new S<T>() { F = t };
F(a2.F/*T:T?*/); // 3
var a2 = new S<T>() { F = t }; // 3
if (b) F(a2.F/*T:T?*/); // 4
var b2 = a2;
F(b2.F/*T:T?*/); // 4
if (b) F(b2.F/*T:T?*/); // 5
}
static void F(object o)
{
......@@ -104579,24 +105013,24 @@ static void F(object o)
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (10,40): warning CS8601: Possible null reference assignment.
// var a1 = new S<string>() { F = s };
Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "s").WithLocation(10, 40),
// (11,11): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// F(a1.F/*T:string?*/); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "a1.F").WithArguments("o", "void Program.F(object o)").WithLocation(11, 11),
// (13,11): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// F(b1.F/*T:string?*/); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b1.F").WithArguments("o", "void Program.F(object o)").WithLocation(13, 11),
// (17,35): warning CS8601: Possible null reference assignment.
// var a2 = new S<T>() { F = t };
Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "t").WithLocation(17, 35),
// (18,11): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// F(a2.F/*T:T?*/); // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "a2.F").WithArguments("o", "void Program.F(object o)").WithLocation(18, 11),
// (20,11): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// F(b2.F/*T:T?*/); // 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b2.F").WithArguments("o", "void Program.F(object o)").WithLocation(20, 11));
// (11,40): warning CS8601: Possible null reference assignment.
// var a1 = new S<string>() { F = s }; // 0
Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "s").WithLocation(11, 40),
// (12,18): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// if (b) F(a1.F/*T:string?*/); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "a1.F").WithArguments("o", "void Program.F(object o)").WithLocation(12, 18),
// (14,18): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// if (b) F(b1.F/*T:string?*/); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b1.F").WithArguments("o", "void Program.F(object o)").WithLocation(14, 18),
// (18,35): warning CS8601: Possible null reference assignment.
// var a2 = new S<T>() { F = t }; // 3
Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "t").WithLocation(18, 35),
// (19,18): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// if (b) F(a2.F/*T:T?*/); // 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "a2.F").WithArguments("o", "void Program.F(object o)").WithLocation(19, 18),
// (21,18): warning CS8604: Possible null reference argument for parameter 'o' in 'void Program.F(object o)'.
// if (b) F(b2.F/*T:T?*/); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "b2.F").WithArguments("o", "void Program.F(object o)").WithLocation(21, 18));
comp.VerifyTypes();
}
......@@ -115514,6 +115948,7 @@ interface IB<T> : IA<T>
}
class Program
{
static bool b = false;
static IB<T> CreateB<T>(T t)
{
throw null!;
......@@ -115523,9 +115958,9 @@ static void F1<T>(T x1)
if (x1 == null) return;
T y1 = default;
var ix1 = CreateB(x1);
var iy1 = CreateB(y1);
ix1.A(y1); // 1
ix1.B(y1); // 2
var iy1 = b ? CreateB(y1) : null!;
if (b) ix1.A(y1); // 1
if (b) ix1.B(y1); // 2
iy1.A(x1);
iy1.B(x1);
}
......@@ -115535,10 +115970,10 @@ static void F2<T>() where T : class, new()
T? y2 = new T();
var ix2 = CreateB(x2);
var iy2 = CreateB(y2);
ix2.A(y2);
ix2.B(y2);
iy2.A(x2); // 4
iy2.B(x2); // 5
if (b) ix2.A(y2);
if (b) ix2.B(y2);
if (b) iy2.A(x2); // 4
if (b) iy2.B(x2); // 5
}
static void F3<T>() where T : struct
{
......@@ -115554,21 +115989,21 @@ static void F2<T>() where T : class, new()
}";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (21,15): warning CS8604: Possible null reference argument for parameter 't' in 'void IA<T>.A(T t)'.
// ix1.A(y1); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y1").WithArguments("t", "void IA<T>.A(T t)").WithLocation(21, 15),
// (22,15): warning CS8604: Possible null reference argument for parameter 't' in 'void IB<T>.B(T t)'.
// ix1.B(y1); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y1").WithArguments("t", "void IB<T>.B(T t)").WithLocation(22, 15),
// (28,16): warning CS8600: Converting null literal or possible null value to non-nullable type.
// (22,22): warning CS8604: Possible null reference argument for parameter 't' in 'void IA<T>.A(T t)'.
// if (b) ix1.A(y1); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y1").WithArguments("t", "void IA<T>.A(T t)").WithLocation(22, 22),
// (23,22): warning CS8604: Possible null reference argument for parameter 't' in 'void IB<T>.B(T t)'.
// if (b) ix1.B(y1); // 2
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y1").WithArguments("t", "void IB<T>.B(T t)").WithLocation(23, 22),
// (29,16): warning CS8600: Converting null literal or possible null value to non-nullable type.
// T x2 = null; // 3
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(28, 16),
// (34,15): warning CS8604: Possible null reference argument for parameter 't' in 'void IA<T>.A(T t)'.
// iy2.A(x2); // 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x2").WithArguments("t", "void IA<T>.A(T t)").WithLocation(34, 15),
// (35,15): warning CS8604: Possible null reference argument for parameter 't' in 'void IB<T>.B(T t)'.
// iy2.B(x2); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x2").WithArguments("t", "void IB<T>.B(T t)").WithLocation(35, 15));
Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(29, 16),
// (35,22): warning CS8604: Possible null reference argument for parameter 't' in 'void IA<T>.A(T t)'.
// if (b) iy2.A(x2); // 4
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x2").WithArguments("t", "void IA<T>.A(T t)").WithLocation(35, 22),
// (36,22): warning CS8604: Possible null reference argument for parameter 't' in 'void IB<T>.B(T t)'.
// if (b) iy2.B(x2); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "x2").WithArguments("t", "void IB<T>.B(T t)").WithLocation(36, 22));
}
[Fact]
......@@ -115926,7 +116361,7 @@ class G<T>
{
public static void Test(bool b, object? o, G<string?> g)
{
M1(o); // 1
if (b) M1(o); // 1
M2(g); // 2
if (false) M1(o);
if (false) M2(g);
......@@ -115946,13 +116381,13 @@ public static void Test(bool b, object? o, G<string?> g)
}";
var comp = CreateCompilation(new[] { source }, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (7,12): warning CS8604: Possible null reference argument for parameter 'o' in 'bool G<T>.M1(object o)'.
// M1(o); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "o").WithArguments("o", "bool G<T>.M1(object o)").WithLocation(7, 12),
// (8,12): warning CS8620: Argument of type 'G<string?>' cannot be used as an input of type 'G<string>' for parameter 'o' in 'bool G<T>.M2(G<string> o)' due to differences in the nullability of reference types.
// (7,19): warning CS8604: Possible null reference argument for parameter 'o' in 'bool G<T>.M1(object o)'.
// if (b) M1(o); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "o").WithArguments("o", "bool G<T>.M1(object o)").WithLocation(7, 19),
// (8,12): warning CS8620: Argument of type 'G<string?>' cannot be used for parameter 'o' of type 'G<string>' in 'bool G<T>.M2(G<string> o)' due to differences in the nullability of reference types.
// M2(g); // 2
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "g").WithArguments("G<string?>", "G<string>", "o", "bool G<T>.M2(G<string> o)").WithLocation(8, 12),
// (14,16): warning CS8620: Argument of type 'G<string?>' cannot be used as an input of type 'G<string>' for parameter 'o' in 'bool G<T>.M2(G<string> o)' due to differences in the nullability of reference types.
// (14,16): warning CS8620: Argument of type 'G<string?>' cannot be used for parameter 'o' of type 'G<string>' in 'bool G<T>.M2(G<string> o)' due to differences in the nullability of reference types.
// M2(g); // 3
Diagnostic(ErrorCode.WRN_NullabilityMismatchInArgument, "g").WithArguments("G<string?>", "G<string>", "o", "bool G<T>.M2(G<string> o)").WithLocation(14, 16),
// (16,16): warning CS8604: Possible null reference argument for parameter 'o' in 'bool G<T>.M1(object o)'.
......@@ -125953,6 +126388,7 @@ public void MaybeNullT_Meet_02()
@"#nullable enable
class C<T> where T : new()
{
static bool b = false;
static void F0(T t)
{
}
......@@ -125967,7 +126403,7 @@ static T F2(T t)
}
finally
{
F0(t2); // 1
if (b) F0(t2); // 1
r2 = t2;
}
return r2; // 2
......@@ -125983,7 +126419,7 @@ static T F3(T t)
}
finally
{
F0(t3); // 3
if (b) F0(t3); // 3
r3 = t3;
}
return r3; // 4
......@@ -125999,7 +126435,7 @@ static T F4(T t)
}
finally
{
F0(t4); // 5
if (b) F0(t4); // 5
r4 = t4;
}
return r4; // 6
......@@ -126031,7 +126467,7 @@ static T F7(T t)
}
finally
{
F0(t7); // 7
if (b) F0(t7); // 7
r7 = t7;
}
return r7; // 8
......@@ -126057,30 +126493,30 @@ static T F8(T t)
// 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<T>.F0(T t)'.
// F0(t2); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void C<T>.F0(T t)").WithLocation(18, 16),
// (21,16): warning CS8603: Possible null reference return.
// (19,23): warning CS8604: Possible null reference argument for parameter 't' in 'void C<T>.F0(T t)'.
// if (b) F0(t2); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t2").WithArguments("t", "void C<T>.F0(T t)").WithLocation(19, 23),
// (22,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<T>.F0(T t)'.
// F0(t3); // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void C<T>.F0(T t)").WithLocation(34, 16),
// (37,16): warning CS8603: Possible null reference return.
Diagnostic(ErrorCode.WRN_NullReferenceReturn, "r2").WithLocation(22, 16),
// (35,23): warning CS8604: Possible null reference argument for parameter 't' in 'void C<T>.F0(T t)'.
// if (b) F0(t3); // 3
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t3").WithArguments("t", "void C<T>.F0(T t)").WithLocation(35, 23),
// (38,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<T>.F0(T t)'.
// F0(t4); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t4").WithArguments("t", "void C<T>.F0(T t)").WithLocation(50, 16),
// (53,16): warning CS8603: Possible null reference return.
Diagnostic(ErrorCode.WRN_NullReferenceReturn, "r3").WithLocation(38, 16),
// (51,23): warning CS8604: Possible null reference argument for parameter 't' in 'void C<T>.F0(T t)'.
// if (b) F0(t4); // 5
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t4").WithArguments("t", "void C<T>.F0(T t)").WithLocation(51, 23),
// (54,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<T>.F0(T t)'.
// F0(t7); // 7
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t7").WithArguments("t", "void C<T>.F0(T t)").WithLocation(82, 16),
// (85,16): warning CS8603: Possible null reference return.
Diagnostic(ErrorCode.WRN_NullReferenceReturn, "r4").WithLocation(54, 16),
// (83,23): warning CS8604: Possible null reference argument for parameter 't' in 'void C<T>.F0(T t)'.
// if (b) F0(t7); // 7
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "t7").WithArguments("t", "void C<T>.F0(T t)").WithLocation(83, 23),
// (86,16): warning CS8603: Possible null reference return.
// return r7; // 8
Diagnostic(ErrorCode.WRN_NullReferenceReturn, "r7").WithLocation(85, 16));
Diagnostic(ErrorCode.WRN_NullReferenceReturn, "r7").WithLocation(86, 16));
}
[Fact]
......@@ -131997,12 +132433,12 @@ public class A<T>
$@"#nullable enable
class B
{{
static void M1<T, U>(U x, U? y) {fullConstraint} where U : T
static void M1<T, U>(bool b, U x, U? y) {fullConstraint} where U : T
{{
A<T>.F<U>(x);
A<T>.F<U>(y); // 1
A<T>.F<U?>(x); // 2
A<T>.F<U?>(y); // 3
if (b) A<T>.F<U>(x);
if (b) A<T>.F<U>(y); // 1
if (b) A<T>.F<U?>(x); // 2
if (b) A<T>.F<U?>(y); // 3
A<T>.F(x);
A<T>.F(y); // 4
}}
......@@ -132018,15 +132454,15 @@ class B
}}";
comp = CreateCompilation(sourceB, references: new[] { refA }, parseOptions: TestOptions.RegularPreview);
comp.VerifyDiagnostics(
// (7,19): warning CS8604: Possible null reference argument for parameter 'u' in 'void A<T>.F<U>(U u)'.
// A<T>.F<U>(y); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y").WithArguments("u", "void A<T>.F<U>(U u)").WithLocation(7, 19),
// (8,9): warning CS8631: The type 'U?' cannot be used as type parameter 'U' in the generic type or method 'A<T>.F<U>(U)'. Nullability of type argument 'U?' doesn't match constraint type 'T'.
// A<T>.F<U?>(x); // 2
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterConstraint, "A<T>.F<U?>").WithArguments("A<T>.F<U>(U)", "T", "U", "U?").WithLocation(8, 9),
// (9,9): warning CS8631: The type 'U?' cannot be used as type parameter 'U' in the generic type or method 'A<T>.F<U>(U)'. Nullability of type argument 'U?' doesn't match constraint type 'T'.
// A<T>.F<U?>(y); // 3
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterConstraint, "A<T>.F<U?>").WithArguments("A<T>.F<U>(U)", "T", "U", "U?").WithLocation(9, 9),
// (7,26): warning CS8604: Possible null reference argument for parameter 'u' in 'void A<T>.F<U>(U u)'.
// if (b) A<T>.F<U>(y); // 1
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "y").WithArguments("u", "void A<T>.F<U>(U u)").WithLocation(7, 26),
// (8,16): warning CS8631: The type 'U?' cannot be used as type parameter 'U' in the generic type or method 'A<T>.F<U>(U)'. Nullability of type argument 'U?' doesn't match constraint type 'T'.
// if (b) A<T>.F<U?>(x); // 2
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterConstraint, "A<T>.F<U?>").WithArguments("A<T>.F<U>(U)", "T", "U", "U?").WithLocation(8, 16),
// (9,16): warning CS8631: The type 'U?' cannot be used as type parameter 'U' in the generic type or method 'A<T>.F<U>(U)'. Nullability of type argument 'U?' doesn't match constraint type 'T'.
// if (b) A<T>.F<U?>(y); // 3
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterConstraint, "A<T>.F<U?>").WithArguments("A<T>.F<U>(U)", "T", "U", "U?").WithLocation(9, 16),
// (11,9): warning CS8631: The type 'U?' cannot be used as type parameter 'U' in the generic type or method 'A<T>.F<U>(U)'. Nullability of type argument 'U?' doesn't match constraint type 'T'.
// A<T>.F(y); // 4
Diagnostic(ErrorCode.WRN_NullabilityMismatchInTypeParameterConstraint, "A<T>.F").WithArguments("A<T>.F<U>(U)", "T", "U", "U?").WithLocation(11, 9),
......@@ -3001,19 +3001,19 @@ class C<T, U>
{
public T this[U u] { get => throw null!; set => throw null!; }
public static void M(object? o1, object o2)
public static void M(bool b, object? o1, object o2)
{
var c1 = CExt.Create(o1, o2);
c1[o1] = o2;
_ = c1[o1];
if (b) c1[o1] = o2;
if (b) _ = c1[o1];
var c2 = CExt.Create(o2, o1);
c2[o2] = o1;
_ = c2[o2];
if (b) c2[o2] = o1;
if (b) _ = c2[o2];
var c3 = CExt.Create(o1 ?? o2, o2);
c3[o1] = o2;
_ = c3[o1];
if (b) c3[o1] = o2;
if (b) _ = c3[o1];
}
}
static class CExt
......@@ -3023,21 +3023,21 @@ static class CExt
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (9,12): warning CS8604: Possible null reference argument for parameter 'u' in 'object? C<object?, object>.this[object u]'.
// c1[o1] = o2;
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "o1").WithArguments("u", "object? C<object?, object>.this[object u]").WithLocation(9, 12),
// (10,16): warning CS8604: Possible null reference argument for parameter 'u' in 'object? C<object?, object>.this[object u]'.
// _ = c1[o1];
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "o1").WithArguments("u", "object? C<object?, object>.this[object u]").WithLocation(10, 16),
// (13,18): warning CS8601: Possible null reference assignment.
// c2[o2] = o1;
Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "o1").WithLocation(13, 18),
// (17,12): warning CS8604: Possible null reference argument for parameter 'u' in 'object C<object, object>.this[object u]'.
// c3[o1] = o2;
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "o1").WithArguments("u", "object C<object, object>.this[object u]").WithLocation(17, 12),
// (18,16): warning CS8604: Possible null reference argument for parameter 'u' in 'object C<object, object>.this[object u]'.
// _ = c3[o1];
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "o1").WithArguments("u", "object C<object, object>.this[object u]").WithLocation(18, 16));
// (9,19): warning CS8604: Possible null reference argument for parameter 'u' in 'object? C<object?, object>.this[object u]'.
// if (b) c1[o1] = o2;
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "o1").WithArguments("u", "object? C<object?, object>.this[object u]").WithLocation(9, 19),
// (10,23): warning CS8604: Possible null reference argument for parameter 'u' in 'object? C<object?, object>.this[object u]'.
// if (b) _ = c1[o1];
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "o1").WithArguments("u", "object? C<object?, object>.this[object u]").WithLocation(10, 23),
// (13,25): warning CS8601: Possible null reference assignment.
// if (b) c2[o2] = o1;
Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "o1").WithLocation(13, 25),
// (17,19): warning CS8604: Possible null reference argument for parameter 'u' in 'object C<object, object>.this[object u]'.
// if (b) c3[o1] = o2;
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "o1").WithArguments("u", "object C<object, object>.this[object u]").WithLocation(17, 19),
// (18,23): warning CS8604: Possible null reference argument for parameter 'u' in 'object C<object, object>.this[object u]'.
// if (b) _ = c3[o1];
Diagnostic(ErrorCode.WRN_NullReferenceArgument, "o1").WithArguments("u", "object C<object, object>.this[object u]").WithLocation(18, 23));
var syntaxTree = comp.SyntaxTrees[0];
var root = syntaxTree.GetRoot();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册