提交 b43d3e92 编写于 作者: V vsadov

more PR feedback

上级 ef25a749
...@@ -1430,16 +1430,13 @@ public override BoundNode VisitObjectCreationExpression(BoundObjectCreationExpre ...@@ -1430,16 +1430,13 @@ public override BoundNode VisitObjectCreationExpression(BoundObjectCreationExpre
// a nullable value type created with its default constructor is by definition null // a nullable value type created with its default constructor is by definition null
resultState = NullableFlowState.MaybeNull; resultState = NullableFlowState.MaybeNull;
} }
else else if (constructor.ParameterCount == 1)
{ {
// if we deal with one-parameter ctor that takes underlying, then Value state is inferred from the argument. // if we deal with one-parameter ctor that takes underlying, then Value state is inferred from the argument.
if (constructor.ParameterCount == 1)
{
var parameterType = constructor.ParameterTypes[0]; var parameterType = constructor.ParameterTypes[0];
if (AreNullableAndUnderlyingTypes(type, parameterType.TypeSymbol, out TypeSymbolWithAnnotations underlyingType)) if (AreNullableAndUnderlyingTypes(type, parameterType.TypeSymbol, out TypeSymbolWithAnnotations underlyingType))
{ {
TrackNullableStateWhenWrappingUnderlying(node, arguments[0], type, underlyingType); TrackNullableStateOfNullableValue(node, arguments[0], type, underlyingType);
}
} }
} }
} }
...@@ -3671,11 +3668,11 @@ private void TrackNullableStateIfNullableConversion(BoundConversion node) ...@@ -3671,11 +3668,11 @@ private void TrackNullableStateIfNullableConversion(BoundConversion node)
if (AreNullableAndUnderlyingTypes(convertedType, operandType, out TypeSymbolWithAnnotations underlyingType)) if (AreNullableAndUnderlyingTypes(convertedType, operandType, out TypeSymbolWithAnnotations underlyingType))
{ {
// Conversion of T to Nullable<T> is equivalent to new Nullable<T>(t). // Conversion of T to Nullable<T> is equivalent to new Nullable<T>(t).
TrackNullableStateWhenWrappingUnderlying(node, operand, convertedType, underlyingType); TrackNullableStateOfNullableValue(node, operand, convertedType, underlyingType);
} }
} }
private void TrackNullableStateWhenWrappingUnderlying(BoundExpression node, BoundExpression operand, TypeSymbol convertedType, TypeSymbolWithAnnotations underlyingType) private void TrackNullableStateOfNullableValue(BoundExpression node, BoundExpression operand, TypeSymbol convertedType, TypeSymbolWithAnnotations underlyingType)
{ {
int valueSlot = MakeSlot(operand); int valueSlot = MakeSlot(operand);
if (valueSlot > 0) if (valueSlot > 0)
......
...@@ -77944,7 +77944,53 @@ static void F() ...@@ -77944,7 +77944,53 @@ static void F()
// (23,9): warning CS8602: Possible dereference of a null reference. // (23,9): warning CS8602: Possible dereference of a null reference.
// x.Value.F.ToString(); // warning // x.Value.F.ToString(); // warning
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x.Value.F").WithLocation(23, 9) Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x.Value.F").WithLocation(23, 9)
); );
}
[WorkItem(32626, "https://github.com/dotnet/roslyn/issues/32626")]
[Fact]
public void NullableCtorErr()
{
var source =
@"
using System;
struct S
{
internal object? F;
}
class Program
{
static void F()
{
S? x = new S() { F = 2 };
x.Value.F.ToString(); // ok baseline
S? y = new Nullable<S>(1);
y.Value.F.ToString(); // warning 1
S? z = new Nullable<S>(null);
z.Value.F.ToString(); // warning 2
}
}
";
var comp = CreateCompilation(source, options: WithNonNullTypesTrue());
comp.VerifyDiagnostics(
// (16,32): error CS1503: Argument 1: cannot convert from 'int' to 'S'
// S? y = new Nullable<S>(1);
Diagnostic(ErrorCode.ERR_BadArgType, "1").WithArguments("1", "int", "S").WithLocation(16, 32),
// (17,9): warning CS8602: Possible dereference of a null reference.
// y.Value.F.ToString(); // warning 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y.Value.F").WithLocation(17, 9),
// (19,32): error CS1503: Argument 1: cannot convert from '<null>' to 'S'
// S? z = new Nullable<S>(null);
Diagnostic(ErrorCode.ERR_BadArgType, "null").WithArguments("1", "<null>", "S").WithLocation(19, 32),
// (20,9): warning CS8602: Possible dereference of a null reference.
// z.Value.F.ToString(); // warning 2
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "z.Value.F").WithLocation(20, 9)
);
} }
[WorkItem(32626, "https://github.com/dotnet/roslyn/issues/32626")] [WorkItem(32626, "https://github.com/dotnet/roslyn/issues/32626")]
...@@ -77990,8 +78036,6 @@ static void F() ...@@ -77990,8 +78036,6 @@ static void F()
); );
} }
[Fact] [Fact]
public void NullableT_AlwaysTrueOrFalse() public void NullableT_AlwaysTrueOrFalse()
{ {
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册