Skip to content
Closed
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
15 changes: 13 additions & 2 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,27 @@ private static SourceWriter CreateSourceWriterWithContextHeader(ContextGeneratio
{
var writer = new SourceWriter();

// In release builds of the generator, we suppress all diagnostics stemming from the generated code.
// There's nothing a developer can do about these diagnostics, and they can break the build. In debug
// builds, we allow more warnings in order to catch potential issues early.
#if DEBUG
writer.WriteLine("""
// <auto-generated/>

#nullable enable annotations
#nullable disable warnings
#pragma warning disable CS0612, CS0618 // [Obsolete] member usage

// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0612, CS0618
""");
#else
writer.WriteLine("""
// <auto-generated/>

#nullable enable annotations
#pragma warning disable

""");
#endif

if (contextSpec.Namespace != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ internal sealed class CompilerFeatureRequiredAttribute : Attribute
public CompilerFeatureRequiredAttribute(string featureName) { }
}
}

namespace System.Diagnostics.CodeAnalysis
{
internal sealed class ExperimentalAttribute : Attribute
{
public ExperimentalAttribute(string diagnosticId) => DiagnosticId = diagnosticId;
public string DiagnosticId { get; }
public string UrlFormat { get; set; }
}
}
""";
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,36 @@ public class ClassWithObsolete
CompilationHelper.RunJsonSourceGenerator(compilation);
}

[Fact]
public static void NoWarningsDueToExperimentalMembers()
{
string source = """
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;

namespace Test
{
#pragma warning disable TEST001
[JsonSerializable(typeof(ClassWithExperimental))]
#pragma warning restore TEST001
public partial class JsonContext : JsonSerializerContext { }

public class ClassWithExperimental
{
[Experimental("TEST001")]
public bool Test { get; set; }

[Experimental("TEST002")]
public bool Test2 { get; set; }
}
}
""";

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

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