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 1 commit
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
Prev Previous commit
Next Next commit
PR feedback - add test and fix an instance of != null to is not null
  • Loading branch information
SteveDunn committed Jun 17, 2024
commit 5bdea3ea4240189ccebeea859b9df044f06e220f
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
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,42 @@ internal partial class JsonContext : JsonSerializerContext
result.AssertContainsType("int");
}

[Fact]
public static void NoErrorsWhenUsingTypesWithMultipleEqualsOperators()
{
// Types that override equals operators used to cause the generated to
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
// emit 'Error CS0034 : Operator '==' is ambiguous on operands of type 'Foo' and '<null>''
// This has been changed to use `is null` and `is not null` to disambiguate.
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