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

System.Text.Json emits uncompilable code #103565

Merged
merged 3 commits into from
Jun 17, 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 @@ -838,7 +838,7 @@ private void GenerateFastPathFuncForObject(SourceWriter writer, ContextGeneratio
switch (defaultCheckType)
{
case SerializedValueCheckType.IgnoreWhenNull:
writer.WriteLine($"if ({propValueExpr} != null)");
writer.WriteLine($"if ({propValueExpr} is not null)");
writer.WriteLine('{');
writer.Indentation++;

Expand Down Expand Up @@ -974,7 +974,7 @@ private static void GenerateFastPathFuncHeader(SourceWriter writer, TypeGenerati
if (!skipNullCheck && typeGenSpec.TypeRef.CanBeNull)
{
writer.WriteLine($$"""
if ({{ValueVarName}} == null)
if ({{ValueVarName}} is null)
{
{{WriterVarName}}.WriteNullValue();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,40 @@ internal partial class JsonContext : JsonSerializerContext
result.AssertContainsType("int");
}

[Fact]
public static void NoErrorsWhenUsingTypesWithMultipleEqualsOperators()
{
// Regression test for https://github.com/dotnet/runtime/issues/103515
string source = """
using System.Text.Json.Serialization;

namespace Test
{
public class Foo
{
public override bool Equals(object obj) => false;

public static bool operator ==(Foo left, Foo right) => false;
public static bool operator !=(Foo left, Foo right) => false;

public static bool operator ==(Foo left, string right) => false;
public static bool operator !=(Foo left, string right) => false;

public override int GetHashCode() => 1;
}

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Foo))]
internal partial class JsonSourceGenerationContext : JsonSerializerContext
{
}
}
""";

Compilation compilation = CompilationHelper.CreateCompilation(source);
CompilationHelper.RunJsonSourceGenerator(compilation);
}

[Fact]
public static void NoErrorsWhenUsingIgnoredReservedCSharpKeywords()
{
Expand Down
Loading