dotnet / dotnet/roslyn

OverloadResolutionPriority is not respected on some attribute constructors

Open
#75,985 3 comments 0 reactions 1 assignee Claimed by @333fred View on GitHub
Area-Compilers Feature - Overload Resolution Priority
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

The unit-tests reflect the current behavior:
```
[Fact]
public void CycleOnOverloadResolutionPriorityConstructor_07()
{
var source = """
using System.Runtime.CompilerServices;

namespace System
{
public class ObsoleteAttribute : Attribute
{
public ObsoleteAttribute(string x){}

[OverloadResolutionPriority(1)]
public ObsoleteAttribute(string x, bool y = false){}

}
}

#pragma warning disable CS0436 // The type 'ObsoleteAttribute' in '' conflicts with the imported type 'ObsoleteAttribute'

[System.Obsolete("Test")]
public class C {}

public class D
{
public C x;
}
""";

var verifier = CompileAndVerify([source, OverloadResolutionPriorityAttributeDefinition],
symbolValidator: (m) =>
{
AssertEx.Equal("System.ObsoleteAttribute..ctor(System.String x)",
m.ContainingAssembly.GetTypeByMetadataName("C")!.GetAttributes().Single().AttributeConstructor.ToTestDisplayString());
});
verifier.VerifyDiagnostics(
// (22,12): warning CS0618: 'C' is obsolete: 'Test'
// public C x;
Diagnostic(ErrorCode.WRN_DeprecatedSymbolStr, "C").WithArguments("C", "Test").WithLocation(22, 12)
);
}

[Fact]
public void CycleOnOverloadResolutionPriorityConstructor_08()
{
var source = """
using System.Runtime.CompilerServices;

namespace System
{
public class ObsoleteAttribute : Attribute
{
public ObsoleteAttribute(string x){}

[OverloadResolutionPriority(1)]
public ObsoleteAttribute(string x, bool y = true){}

}
}

#pragma warning disable CS0436 // The type 'ObsoleteAttribute' in '' conflicts with the imported type 'ObsoleteAttribute'

[System.Obsolete("Test")]
public class C {}

public class D
{
public C x;
}
""";

var verifier = CompileAndVerify([source, OverloadResolutionPriorityAttributeDefinition],
symbolValidator: (m) =>
{
AssertEx.Equal("System.ObsoleteAttribute..ctor(System.String x)",
m.ContainingAssembly.GetTypeByMetadataName("C")!.GetAttributes().Single().AttributeConstructor.ToTestDisplayString());
});
verifier.VerifyDiagnostics(
// (22,12): warning CS0618: 'C' is obsolete: 'Test'
// public C x;
Diagnostic(ErrorCode.WRN_DeprecatedSymbolStr, "C").WithArguments("C", "Test").WithLocation(22, 12)
);
}
```

Observed:
``` [System.Obsolete("Test")]``` binds to ```System.ObsoleteAttribute..ctor(System.String x)```

Expected:
``` [System.Obsolete("Test")]``` binds to ```System.ObsoleteAttribute..ctor(System.String x, System.Boolean y)```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.