Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5921,6 +5921,7 @@ private void VisitConditionalAccess(BoundConditionalAccess node, out PossiblyCon
// Therefore we do a VisitPossibleConditionalAccess here which unconditionally includes the "after receiver" state in State
// and includes the "after subsequent conditional accesses" in stateWhenNotNull
VisitPossibleConditionalAccess(node.AccessExpression, out stateWhenNotNull);
Unsplit();
}
else
{
Expand Down
7 changes: 4 additions & 3 deletions src/Compilers/CSharp/Test/Emit3/FlowAnalysis/FlowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2387,8 +2387,9 @@ void M2(C c1)
Diagnostic(ErrorCode.ERR_UseDefViolation, "x").WithArguments("x").WithLocation(17, 15));
}

[Fact]
public void NullCoalescing_CondAccess_NonNullConstantLeft()
[Theory, CombinatorialData, WorkItem("https://github.com/dotnet/roslyn/issues/78386")]
public void NullCoalescing_CondAccess_NonNullConstantLeft(
[CombinatorialValues(TargetFramework.Standard, TargetFramework.NetCoreApp)] TargetFramework targetFramework)
{
var source = @"
#nullable enable
Expand All @@ -2414,7 +2415,7 @@ static void M2()
}
}
";
CreateCompilation(source).VerifyDiagnostics(
CreateCompilation(source, targetFramework: targetFramework).VerifyDiagnostics(
// (21,30): error CS0165: Use of unassigned local variable 'z'
// : x.ToString() + z.ToString(); // 1
Diagnostic(ErrorCode.ERR_UseDefViolation, "z").WithArguments("z").WithLocation(21, 30)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54206,8 +54206,9 @@ static void M2(E? e, object? x)
}

// <remarks>Ported from <see cref="FlowTests.NullCoalescing_CondAccess_NonNullConstantLeft"/>.</remarks>
[Fact]
public void NullCoalescing_CondAccess_NonNullConstantLeft()
[Theory, CombinatorialData, WorkItem("https://github.com/dotnet/roslyn/issues/78386")]
public void NullCoalescing_CondAccess_NonNullConstantLeft(
[CombinatorialValues(TargetFramework.Standard, TargetFramework.NetCoreApp)] TargetFramework targetFramework)
{
var source = @"
static class C
Expand All @@ -54229,12 +54230,53 @@ static void M2(object? x, object? y)
}
}
";
CreateNullableCompilation(source).VerifyDiagnostics(
CreateCompilation(source, targetFramework: targetFramework, options: WithNullableEnable()).VerifyDiagnostics(
// (17,30): warning CS8602: Dereference of a possibly null reference.
// : x.ToString() + y.ToString(); // 1
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "y").WithLocation(17, 30));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/78386")]
public void CondAccess_PostConditionInArgument_01()
{
var source = """
using System.Diagnostics.CodeAnalysis;
static class C
{
static bool M1(this string s, [NotNullWhen(true)] string? x) => true;
static void M2(string? x)
{
_ = ""?.M1(x = "s") ?? false
? x.ToString()
: x.ToString();
}
}
""";
CreateCompilation([source, NotNullWhenAttributeDefinition], options: WithNullableEnable()).VerifyDiagnostics();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/78386")]
public void CondAccess_PostConditionInArgument_02()
{
var source = """
using System.Diagnostics.CodeAnalysis;
static class C
{
static bool M1(this string s, [NotNullWhen(true)] string? x) => true;
static void M2(string? x)
{
_ = ""?.M1(x) ?? false
? x.ToString()
: x.ToString();
}
}
""";
CreateCompilation([source, NotNullWhenAttributeDefinition], options: WithNullableEnable()).VerifyDiagnostics(
// (9,15): warning CS8602: Dereference of a possibly null reference.
// : x.ToString();
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "x").WithLocation(9, 15));
}

// <remarks>Ported from <see cref="FlowTests.NullCoalescing_NonNullConstantLeft"/>.</remarks>
[Fact]
public void NullCoalescing_NonNullConstantLeft()
Expand Down