Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate spread suppression #75596

Merged
merged 2 commits into from
Oct 25, 2024
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 @@ -991,6 +991,7 @@ BoundNode bindSpreadElement(BoundCollectionExpressionSpreadElement element, Type

var expressionSyntax = element.Expression.Syntax;
var elementPlaceholder = new BoundValuePlaceholder(expressionSyntax, enumeratorInfo.ElementType) { WasCompilerGenerated = true };
elementPlaceholder = (BoundValuePlaceholder)elementPlaceholder.WithSuppression(element.Expression.IsSuppressed);
var convertElement = CreateConversion(
expressionSyntax,
elementPlaceholder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14178,6 +14178,72 @@ public void Add(T t) { }
CompileAndVerify(comp, expectedOutput: "[],");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75560")]
public void Nullable_Spread_01()
{
var source = """
#nullable enable
using System.Collections.Generic;
{
IEnumerable<object?> a = [null];
object[] b = [..a];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a

I would strengthen the tests and used a different local here in order to avoid a chance that the error was not reported here because it was reported for the same local on the previous line.

}
{
IEnumerable<object?> a = [null];
object[] b = [..a!];
}
""";
CreateCompilation(source).VerifyDiagnostics(
// (5,21): warning CS8601: Possible null reference assignment.
// object[] b = [..a];
Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "a").WithLocation(5, 21));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75560")]
public void Nullable_Spread_02()
{
var source = """
#nullable enable
using System.Collections.Generic;
{
IEnumerable<object>? a = null;
object[] b = [..a];
}
{
IEnumerable<object>? a = null;
object[] b = [..a!];
}
""";
CreateCompilation(source).VerifyDiagnostics(
// (5,21): warning CS8602: Dereference of a possibly null reference.
// object[] b = [..a];
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a").WithLocation(5, 21));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75560")]
public void Nullable_Spread_03()
{
var source = """
#nullable enable
using System.Collections.Generic;
{
IEnumerable<object?> a1 = [null];
IEnumerable<object?> a2 = [null];
object[] b = [..(m() ? a1 : a2)];
}
{
IEnumerable<object?> a1 = [null];
IEnumerable<object?> a2 = [null];
object[] b = [..(m() ? a1 : a2)!];
}
bool m() => throw null!;
""";
CreateCompilation(source).VerifyDiagnostics(
// (6,22): warning CS8601: Possible null reference assignment.
// object[] b = [..(m() ? a1 : a2)];
Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "m() ? a1 : a2").WithLocation(6, 22));
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/69447")]
public void NullableValueType_ImplicitConversion()
{
Expand Down
Loading