Skip to content

Commit

Permalink
Preserve CustomObsoleteDiagnosticInfo when changing severity (#47777)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkiGibson authored Sep 18, 2020
1 parent 7495df1 commit b523cbe
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
Expand Down Expand Up @@ -8657,6 +8658,108 @@ void verify(MetadataReference reference)
}
}

[Fact, WorkItem(42119, "https://github.com/dotnet/roslyn/issues/42119")]
public void Obsolete_CustomDiagnosticId_FromMetadata_05()
{
var source1 = @"
using System;
#pragma warning disable 436
public class C1
{
[Obsolete(DiagnosticId = ""TEST1"", UrlFormat = ""https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/{0}"")]
public void M1() { }
}
";

var source2 = @"
class C2 : C1
{
void M2()
{
M1(); // 1
}
}";
var comp1 = CreateCompilation(new[] { ObsoleteAttributeSource, source1 });
comp1.VerifyDiagnostics();

// WithGeneralDiagnosticOption
verify(TestOptions.DebugDll.WithGeneralDiagnosticOption(ReportDiagnostic.Warn),
// (6,9): warning TEST1: 'C1.M1()' is obsolete
// M1(); // 1
Diagnostic("TEST1", "M1()", isSuppressed: false).WithArguments("C1.M1()").WithLocation(6, 9)
);

verify(TestOptions.DebugDll.WithGeneralDiagnosticOption(ReportDiagnostic.Error),
// (6,9): error TEST1: 'C1.M1()' is obsolete
// M1(); // 1
Diagnostic("TEST1", "M1()", isSuppressed: false).WithArguments("C1.M1()").WithLocation(6, 9).WithWarningAsError(true)
);

verify(TestOptions.DebugDll.WithGeneralDiagnosticOption(ReportDiagnostic.Hidden),
// (6,9): hidden TEST1: 'C1.M1()' is obsolete
// M1(); // 1
Diagnostic("TEST1", "M1()", isSuppressed: false).WithArguments("C1.M1()").WithLocation(6, 9));

verify(TestOptions.DebugDll.WithGeneralDiagnosticOption(ReportDiagnostic.Suppress));


// WithSpecificDiagnosticOption for id TEST1
verify(TestOptions.DebugDll.WithSpecificDiagnosticOptions(ImmutableDictionary<string, ReportDiagnostic>.Empty.Add("TEST1", ReportDiagnostic.Warn)),
// (6,9): warning TEST1: 'C1.M1()' is obsolete
// M1(); // 1
Diagnostic("TEST1", "M1()", isSuppressed: false).WithArguments("C1.M1()").WithLocation(6, 9)
);

verify(TestOptions.DebugDll.WithSpecificDiagnosticOptions(ImmutableDictionary<string, ReportDiagnostic>.Empty.Add("TEST1", ReportDiagnostic.Error)),
// (6,9): error TEST1: 'C1.M1()' is obsolete
// M1(); // 1
Diagnostic("TEST1", "M1()", isSuppressed: false).WithArguments("C1.M1()").WithLocation(6, 9).WithWarningAsError(true)
);

verify(TestOptions.DebugDll.WithSpecificDiagnosticOptions(ImmutableDictionary<string, ReportDiagnostic>.Empty.Add("TEST1", ReportDiagnostic.Hidden)),
// (6,9): hidden TEST1: 'C1.M1()' is obsolete
// M1(); // 1
Diagnostic("TEST1", "M1()", isSuppressed: false).WithArguments("C1.M1()").WithLocation(6, 9)
);

verify(TestOptions.DebugDll.WithSpecificDiagnosticOptions(ImmutableDictionary<string, ReportDiagnostic>.Empty.Add("TEST1", ReportDiagnostic.Suppress)));


// WithSpecificDiagnosticOption for id CS0618
verify(TestOptions.DebugDll.WithSpecificDiagnosticOptions(ImmutableDictionary<string, ReportDiagnostic>.Empty.Add("CS0618", ReportDiagnostic.Error)),
// (6,9): warning TEST1: 'C1.M1()' is obsolete
// M1(); // 1
Diagnostic("TEST1", "M1()", isSuppressed: false).WithArguments("C1.M1()").WithLocation(6, 9)
);

verify(TestOptions.DebugDll.WithSpecificDiagnosticOptions(ImmutableDictionary<string, ReportDiagnostic>.Empty.Add("CS0618", ReportDiagnostic.Suppress)),
// (6,9): warning TEST1: 'C1.M1()' is obsolete
// M1(); // 1
Diagnostic("TEST1", "M1()", isSuppressed: false).WithArguments("C1.M1()").WithLocation(6, 9)
);

void verify(CSharpCompilationOptions options, params DiagnosticDescription[] expectedDiagnostics)
{
verifyReference(comp1.ToMetadataReference(), options, expectedDiagnostics);
verifyReference(comp1.EmitToImageReference(), options, expectedDiagnostics);
}

void verifyReference(MetadataReference reference, CSharpCompilationOptions options, DiagnosticDescription[] expectedDiagnostics)
{
var comp2 = CreateCompilation(source2, references: new[] { reference }, options: options);
var diags = comp2.GetDiagnostics();

if (expectedDiagnostics.Any())
{
var diag = diags.Single();
Assert.Equal("https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/TEST1", diag.Descriptor.HelpLinkUri);
}

diags.Verify(expectedDiagnostics);
}
}

[Fact, WorkItem(42119, "https://github.com/dotnet/roslyn/issues/42119")]
public void Obsolete_CustomDiagnosticId_BadMetadata_01()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ internal CustomObsoleteDiagnosticInfo(CommonMessageProvider messageProvider, int
Data = data;
}

private CustomObsoleteDiagnosticInfo(CustomObsoleteDiagnosticInfo baseInfo, DiagnosticSeverity effectiveSeverity)
: base(baseInfo, effectiveSeverity)
{
Data = baseInfo.Data;
}

public override string MessageIdentifier
{
get
Expand Down Expand Up @@ -50,6 +56,11 @@ public override DiagnosticDescriptor Descriptor
}
}

internal override DiagnosticInfo GetInstanceWithSeverity(DiagnosticSeverity severity)
{
return new CustomObsoleteDiagnosticInfo(this, severity);
}

private DiagnosticDescriptor CreateDescriptor()
{
var baseDescriptor = base.Descriptor;
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/Core/Portable/Diagnostic/DiagnosticInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal DiagnosticInfo(CommonMessageProvider messageProvider, int errorCode, pa
_arguments = arguments;
}

private DiagnosticInfo(DiagnosticInfo original, DiagnosticSeverity overriddenSeverity)
protected DiagnosticInfo(DiagnosticInfo original, DiagnosticSeverity overriddenSeverity)
{
_messageProvider = original.MessageProvider;
_errorCode = original._errorCode;
Expand Down Expand Up @@ -137,7 +137,7 @@ internal DiagnosticInfo(CommonMessageProvider messageProvider, bool isWarningAsE
}

// Create a copy of this instance with a explicit overridden severity
internal DiagnosticInfo GetInstanceWithSeverity(DiagnosticSeverity severity)
internal virtual DiagnosticInfo GetInstanceWithSeverity(DiagnosticSeverity severity)
{
return new DiagnosticInfo(this, severity);
}
Expand Down

0 comments on commit b523cbe

Please sign in to comment.