Skip to content

Commit

Permalink
Support checked operators in explicit interface implementation (#60715)
Browse files Browse the repository at this point in the history
  • Loading branch information
dibarbet authored Apr 14, 2022
1 parent 9938fe3 commit eb3ccc5
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -888,5 +888,110 @@ void I.M(int x)
// TODO: Consider adding the default value too.
await VerifyProviderCommitAsync(markup, "M(int x)", expected, '\t');
}

[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
[WorkItem(60215, "https://github.com/dotnet/roslyn/issues/60215")]
public async Task TestStaticAbstractCheckedUnaryOperator()
{
var markup = @"
interface I1<T> where T : I1<T>
{
abstract static T operator checked -(T x);
abstract static T operator -(T x);
}
class C : I1<C>
{
static C I1<C>.$$
}
";

var expected = @"
interface I1<T> where T : I1<T>
{
abstract static T operator checked -(T x);
abstract static T operator -(T x);
}
class C : I1<C>
{
static C I1<C>.operator checked -(C x)
}
";

await VerifyProviderCommitAsync(markup, "operator checked -(C x)", expected, '\t');
}

[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
[WorkItem(60215, "https://github.com/dotnet/roslyn/issues/60215")]
public async Task TestStaticAbstractCheckedBinaryOperator()
{
var markup = @"
interface I1<T> where T : I1<T>
{
abstract static T operator checked +(T x, T y);
abstract static T operator +(T x, T y);
}
class C : I1<C>
{
static C I1<C>.$$
}
";

var expected = @"
interface I1<T> where T : I1<T>
{
abstract static T operator checked +(T x, T y);
abstract static T operator +(T x, T y);
}
class C : I1<C>
{
static C I1<C>.operator checked +(C x, C y)
}
";

await VerifyProviderCommitAsync(markup, "operator checked +(C x, C y)", expected, '\t');
}

[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
[WorkItem(60215, "https://github.com/dotnet/roslyn/issues/60215")]
public async Task TestStaticAbstractCheckedCastOperator()
{
var markup = @"
interface I1<T> where T : I1<T>
{
abstract static explicit operator checked string(T x);
abstract static explicit operator string(T x);
}
class C3 : I1<C3>
{
static C3 I1<C3>.$$
}
";

var expected = @"
interface I1<T> where T : I1<T>
{
abstract static explicit operator checked string(T x);
abstract static explicit operator string(T x);
}
class C3 : I1<C3>
{
static C3 I1<C3>.operator checked string(C3 x)
}
";

await VerifyProviderCommitAsync(markup, "operator checked string(C3 x)", expected, '\t');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ private static string ToDisplayString(IMethodSymbol symbol)
break;
case MethodKind.UserDefinedOperator:
case MethodKind.BuiltinOperator:
builder.Append("operator ");
AppendOperatorKeywords(symbol, builder);
builder.Append(SyntaxFacts.GetText(SyntaxFacts.GetOperatorKind(symbol.MetadataName)));
break;
case MethodKind.Conversion:
builder.Append("operator ");
AppendOperatorKeywords(symbol, builder);
AddType(symbol.ReturnType, builder);
break;
}
Expand All @@ -73,6 +73,15 @@ private static string ToDisplayString(IMethodSymbol symbol)
AddParameters(symbol.Parameters, builder);
builder.Append(')');
return builder.ToString();

static void AppendOperatorKeywords(IMethodSymbol symbol, StringBuilder builder)
{
builder.Append("operator ");
if (SyntaxFacts.IsCheckedOperator(symbol.MetadataName))
{
builder.Append("checked ");
}
}
}

private static void AddParameters(ImmutableArray<IParameterSymbol> parameters, StringBuilder builder)
Expand Down

0 comments on commit eb3ccc5

Please sign in to comment.