Skip to content

Merge branch 'UserDefinedCompoundAssignment' into 'extensions' #78302

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

Open
wants to merge 21 commits into
base: features/extensions
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3d90c17
Support declaration of instance increment operators (#76991)
AlekseyTs Feb 6, 2025
c3beaf8
Support consumption of instance increment operators (#77098)
AlekseyTs Feb 14, 2025
06476c7
Merge 'main' into UserDefinedCompoundAssignment
AlekseyTs Mar 27, 2025
2c74f00
Merge 'main' into UserDefinedCompoundAssignment (#77858)
AlekseyTs Mar 27, 2025
46178b5
Detect regular method vs. operator mismatch during overriding and int…
AlekseyTs Apr 1, 2025
6514158
Parse compound assignment operator declarations (#77943)
AlekseyTs Apr 2, 2025
b264ee2
Bind compound assignment operator declarations (#77999)
AlekseyTs Apr 9, 2025
2b1abe9
Support consumption of instance compound assignment operators (#78087)
AlekseyTs Apr 11, 2025
fa9d7be
Merge 'main' into UserDefinedCompoundAssignment
AlekseyTs Apr 11, 2025
3064172
Merge 'main' into UserDefinedCompoundAssignment
AlekseyTs Apr 12, 2025
d3ff877
Merge 'main' into UserDefinedCompoundAssignment (#78113)
AlekseyTs Apr 12, 2025
baea40e
Implement CRef binding for compound assignment operators (#78111)
AlekseyTs Apr 15, 2025
d63dc75
Allow `readonly` modifier for instance operators (#78151)
AlekseyTs Apr 15, 2025
6c8b098
Test some IDE scenarios for instance operators (#78186)
AlekseyTs Apr 18, 2025
0155691
Add support for user defined compound assignment operators to syntax …
AlekseyTs Apr 18, 2025
d0d2c14
Emit CompilerFeatureRequiredAttribute for instance operators (#78163)
AlekseyTs Apr 21, 2025
96a2dcd
Implement ref safety analysis for instance compound assignment operat…
AlekseyTs Apr 22, 2025
3ca1d1e
Merge 'main' into UserDefinedCompoundAssignment
AlekseyTs Apr 22, 2025
7fb0864
Merge 'main' into UserDefinedCompoundAssignment (#78262)
AlekseyTs Apr 23, 2025
6c7bda1
Address some PROTOTYPE comments (#78241)
AlekseyTs Apr 23, 2025
aec4186
Merge branch 'UserDefinedCompoundAssignment' into 'extensions'
AlekseyTs Apr 24, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ public sealed class ImplementInterfaceTests
{
private readonly NamingStylesTestOptionSets _options = new(LanguageNames.CSharp);

private const string CompilerFeatureRequiredAttribute = """

namespace System.Runtime.CompilerServices
{
public sealed class CompilerFeatureRequiredAttribute : Attribute
{
public CompilerFeatureRequiredAttribute(string featureName)
{
}
}
}
""";

private static OptionsCollection AllOptionsOff
=> new(LanguageNames.CSharp)
{
Expand Down Expand Up @@ -11243,6 +11256,78 @@ class C : ITest
}.RunAsync();
}

[Theory(Skip = "Yes")] // PROTOTYPE: Something doesn't work
[CombinatorialData]
public async Task TestInstanceIncrementOperator_ImplementExplicitly([CombinatorialValues("++", "--")] string op)
{
await new VerifyCS.Test
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net60,
LanguageVersion = LanguageVersion.Preview,
TestCode = $$$"""
interface ITest
{
abstract void operator {{{op}}}();
}
class C : {|CS0535:ITest|}
{
}
""" + CompilerFeatureRequiredAttribute,
FixedCode = $$$"""
interface ITest
{
abstract void operator {{{op}}}();
}
class C : ITest
{
void ITest.operator {{{op}}}()
{
throw new System.NotImplementedException();
}
}
""" + CompilerFeatureRequiredAttribute,
CodeActionVerifier = (codeAction, verifier) => verifier.Equal(CodeFixesResources.Implement_all_members_explicitly, codeAction.Title),
CodeActionEquivalenceKey = "True;False;False:global::ITest;Microsoft.CodeAnalysis.ImplementInterface.AbstractImplementInterfaceService+ImplementInterfaceCodeAction;",
CodeActionIndex = 0,
}.RunAsync();
}

[Theory(Skip = "Yes")] // PROTOTYPE: Something doesn't work
[CombinatorialData]
public async Task TestInstanceCompoundAssignmentOperator_ImplementExplicitly([CombinatorialValues("+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>=", ">>>=")] string op)
{
await new VerifyCS.Test
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net60,
LanguageVersion = LanguageVersion.Preview,
TestCode = $$$"""
interface ITest
{
abstract void operator {{{op}}}(int y);
}
class C : {|CS0535:ITest|}
{
}
""" + CompilerFeatureRequiredAttribute,
FixedCode = $$$"""
interface ITest
{
abstract void operator {{{op}}}(int y);
}
class C : ITest
{
void ITest.operator {{{op}}}(int y)
{
throw new System.NotImplementedException();
}
}
""" + CompilerFeatureRequiredAttribute,
CodeActionVerifier = (codeAction, verifier) => verifier.Equal(CodeFixesResources.Implement_all_members_explicitly, codeAction.Title),
CodeActionEquivalenceKey = "True;False;False:global::ITest;Microsoft.CodeAnalysis.ImplementInterface.AbstractImplementInterfaceService+ImplementInterfaceCodeAction;",
CodeActionIndex = 0,
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/53927")]
public async Task TestStaticAbstractInterfaceOperator_ImplementImplicitly()
{
Expand Down Expand Up @@ -11320,6 +11405,80 @@ class C : ITest<C>
}.RunAsync();
}

[Theory]
[CombinatorialData]
public async Task TestInstanceIncrementOperator_ImplementImplicitly([CombinatorialValues("++", "--")] string op)
{
await new VerifyCS.Test
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net60,
LanguageVersion = LanguageVersion.Preview,
TestCode = $$$"""
interface ITest<T> where T : ITest<T>
{
void operator {{{op}}}();
}
class C : {|CS0535:ITest<C>|}
{
}
""" + CompilerFeatureRequiredAttribute,
// PROTOTYPE: The 'static' modifier shouldn't be added
FixedCode = $$$"""
interface ITest<T> where T : ITest<T>
{
void operator {{{op}}}();
}
class C : {|CS0736:ITest<C>|}
{
public static void operator {|CS1535:{{{op}}}|}()
{
throw new System.NotImplementedException();
}
}
""" + CompilerFeatureRequiredAttribute,
CodeActionVerifier = (codeAction, verifier) => verifier.Equal(CodeFixesResources.Implement_interface, codeAction.Title),
CodeActionEquivalenceKey = "False;False;True:global::ITest<global::C>;Microsoft.CodeAnalysis.ImplementInterface.AbstractImplementInterfaceService+ImplementInterfaceCodeAction;",
CodeActionIndex = 0,
}.RunAsync();
}

[Theory]
[CombinatorialData]
public async Task TestInstanceCompoundAssignmentOperator_ImplementImplicitly([CombinatorialValues("+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>=", ">>>=")] string op)
{
await new VerifyCS.Test
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net60,
LanguageVersion = LanguageVersion.Preview,
TestCode = $$$"""
interface ITest<T> where T : ITest<T>
{
void operator {{{op}}}(int y);
}
class C : {|CS0535:ITest<C>|}
{
}
""" + CompilerFeatureRequiredAttribute,
// PROTOTYPE: The 'static' modifier shouldn't be added
FixedCode = $$$"""
interface ITest<T> where T : ITest<T>
{
void operator {{{op}}}(int y);
}
class C : ITest<C>
{
public static void operator {|CS0106:{{{op}}}|}(int y)
{
throw new System.NotImplementedException();
}
}
""" + CompilerFeatureRequiredAttribute,
CodeActionVerifier = (codeAction, verifier) => verifier.Equal(CodeFixesResources.Implement_interface, codeAction.Title),
CodeActionEquivalenceKey = "False;False;True:global::ITest<global::C>;Microsoft.CodeAnalysis.ImplementInterface.AbstractImplementInterfaceService+ImplementInterfaceCodeAction;",
CodeActionIndex = 0,
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/53927")]
public async Task TestStaticAbstractInterfaceOperator_ImplementExplicitly()
{
Expand Down
64 changes: 40 additions & 24 deletions src/Compilers/CSharp/Portable/Binder/Binder.ValueChecks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4489,18 +4489,27 @@ internal SafeContext GetValEscape(BoundExpression expr, SafeContext localScopeDe
case BoundKind.CompoundAssignmentOperator:
var compound = (BoundCompoundAssignmentOperator)expr;

// https://github.com/dotnet/roslyn/issues/78198 It looks like we don't have a single test demonstrating significance of the code below.

if (compound.Operator.Method is { } compoundMethod)
{
return GetInvocationEscapeScope(
MethodInfo.Create(compoundMethod),
receiver: null,
receiverIsSubjectToCloning: ThreeState.Unknown,
compoundMethod.Parameters,
argsOpt: [compound.Left, compound.Right],
argRefKindsOpt: default,
argsToParamsOpt: default,
localScopeDepth: localScopeDepth,
isRefEscape: false);
if (compoundMethod.IsStatic)
{
return GetInvocationEscapeScope(
MethodInfo.Create(compoundMethod),
receiver: null,
receiverIsSubjectToCloning: ThreeState.Unknown,
compoundMethod.Parameters,
argsOpt: [compound.Left, compound.Right],
argRefKindsOpt: default,
argsToParamsOpt: default,
localScopeDepth: localScopeDepth,
isRefEscape: false);
}
else
{
return GetValEscape(compound.Left, localScopeDepth);
}
}

return GetValEscape(compound.Left, localScopeDepth)
Expand Down Expand Up @@ -5278,20 +5287,27 @@ internal bool CheckValEscape(SyntaxNode node, BoundExpression expr, SafeContext

if (compound.Operator.Method is { } compoundMethod)
{
return CheckInvocationEscape(
compound.Syntax,
MethodInfo.Create(compoundMethod),
receiver: null,
receiverIsSubjectToCloning: ThreeState.Unknown,
compoundMethod.Parameters,
argsOpt: [compound.Left, compound.Right],
argRefKindsOpt: default,
argsToParamsOpt: default,
checkingReceiver: checkingReceiver,
escapeFrom: escapeFrom,
escapeTo: escapeTo,
diagnostics,
isRefEscape: false);
if (compoundMethod.IsStatic)
{
return CheckInvocationEscape(
compound.Syntax,
MethodInfo.Create(compoundMethod),
receiver: null,
receiverIsSubjectToCloning: ThreeState.Unknown,
compoundMethod.Parameters,
argsOpt: [compound.Left, compound.Right],
argRefKindsOpt: default,
argsToParamsOpt: default,
checkingReceiver: checkingReceiver,
escapeFrom: escapeFrom,
escapeTo: escapeTo,
diagnostics,
isRefEscape: false);
}
else
{
return CheckValEscape(compound.Left.Syntax, compound.Left, escapeFrom, escapeTo, checkingReceiver: false, diagnostics: diagnostics);
}
}

return CheckValEscape(compound.Left.Syntax, compound.Left, escapeFrom, escapeTo, checkingReceiver: false, diagnostics: diagnostics) &&
Expand Down
23 changes: 16 additions & 7 deletions src/Compilers/CSharp/Portable/Binder/Binder_Crefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,24 @@ private ImmutableArray<Symbol> BindOperatorMemberCref(OperatorMemberCrefSyntax s
CrefParameterListSyntax? parameterListSyntax = syntax.Parameters;
bool isChecked = syntax.CheckedKeyword.IsKind(SyntaxKind.CheckedKeyword);

// NOTE: Prefer binary to unary, unless there is exactly one parameter.
// CONSIDER: we're following dev11 by never using a binary operator name if there's
// exactly one parameter, but doing so would allow us to match single-parameter constructors.
SyntaxKind operatorTokenKind = syntax.OperatorToken.Kind();
string? memberName = parameterListSyntax != null && parameterListSyntax.Parameters.Count == 1
? null
: OperatorFacts.BinaryOperatorNameFromSyntaxKindIfAny(operatorTokenKind, isChecked);
string? memberName;

memberName = memberName ?? OperatorFacts.UnaryOperatorNameFromSyntaxKindIfAny(operatorTokenKind, isChecked: isChecked);
if (SyntaxFacts.IsOverloadableCompoundAssignmentOperator(operatorTokenKind))
{
memberName = OperatorFacts.CompoundAssignmentOperatorNameFromSyntaxKind(operatorTokenKind, isChecked);
}
else
{
// NOTE: Prefer binary to unary, unless there is exactly one parameter.
// CONSIDER: we're following dev11 by never using a binary operator name if there's
// exactly one parameter, but doing so would allow us to match single-parameter constructors.
memberName = parameterListSyntax != null && parameterListSyntax.Parameters.Count == 1
? null
: OperatorFacts.BinaryOperatorNameFromSyntaxKindIfAny(operatorTokenKind, isChecked);

memberName = memberName ?? OperatorFacts.UnaryOperatorNameFromSyntaxKindIfAny(operatorTokenKind, isChecked: isChecked);
}

if (memberName == null ||
(isChecked && !syntax.OperatorToken.IsMissing && !SyntaxFacts.IsCheckedOperator(memberName))) // the operator cannot be checked
Expand Down
69 changes: 37 additions & 32 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11364,38 +11364,7 @@ private BoundConditionalAccess BindConditionalAccessExpression(ConditionalAccess
// For improved diagnostics we detect the cases where the value will be used and produce a
// more specific (though not technically correct) diagnostic here:
// "Error CS0023: Operator '?' cannot be applied to operand of type 'T'"
bool resultIsUsed = true;
CSharpSyntaxNode parent = node.Parent;

if (parent != null)
{
switch (parent.Kind())
{
case SyntaxKind.ExpressionStatement:
resultIsUsed = ((ExpressionStatementSyntax)parent).Expression != node;
break;

case SyntaxKind.SimpleLambdaExpression:
resultIsUsed = (((SimpleLambdaExpressionSyntax)parent).Body != node) || MethodOrLambdaRequiresValue(ContainingMemberOrLambda, Compilation);
break;

case SyntaxKind.ParenthesizedLambdaExpression:
resultIsUsed = (((ParenthesizedLambdaExpressionSyntax)parent).Body != node) || MethodOrLambdaRequiresValue(ContainingMemberOrLambda, Compilation);
break;

case SyntaxKind.ArrowExpressionClause:
resultIsUsed = (((ArrowExpressionClauseSyntax)parent).Expression != node) || MethodOrLambdaRequiresValue(ContainingMemberOrLambda, Compilation);
break;

case SyntaxKind.ForStatement:
// Incrementors and Initializers doesn't have to produce a value
var loop = (ForStatementSyntax)parent;
resultIsUsed = !loop.Incrementors.Contains(node) && !loop.Initializers.Contains(node);
break;
}
}

if (resultIsUsed)
if (ResultIsUsed(node))
{
return GenerateBadConditionalAccessNodeError(node, receiver, access, diagnostics);
}
Expand All @@ -11414,6 +11383,42 @@ private BoundConditionalAccess BindConditionalAccessExpression(ConditionalAccess
return new BoundConditionalAccess(node, receiver, access, accessType);
}

private bool ResultIsUsed(ExpressionSyntax node)
{
bool resultIsUsed = true;
CSharpSyntaxNode parent = node.Parent;

if (parent != null)
{
switch (parent.Kind())
{
case SyntaxKind.ExpressionStatement:
resultIsUsed = ((ExpressionStatementSyntax)parent).Expression != node;
break;

case SyntaxKind.SimpleLambdaExpression:
resultIsUsed = (((SimpleLambdaExpressionSyntax)parent).Body != node) || MethodOrLambdaRequiresValue(ContainingMemberOrLambda, Compilation);
break;

case SyntaxKind.ParenthesizedLambdaExpression:
resultIsUsed = (((ParenthesizedLambdaExpressionSyntax)parent).Body != node) || MethodOrLambdaRequiresValue(ContainingMemberOrLambda, Compilation);
break;

case SyntaxKind.ArrowExpressionClause:
resultIsUsed = (((ArrowExpressionClauseSyntax)parent).Expression != node) || MethodOrLambdaRequiresValue(ContainingMemberOrLambda, Compilation);
break;

case SyntaxKind.ForStatement:
// Incrementors and Initializers doesn't have to produce a value
var loop = (ForStatementSyntax)parent;
resultIsUsed = !loop.Incrementors.Contains(node) && !loop.Initializers.Contains(node);
break;
}
}

return resultIsUsed;
}

internal static bool MethodOrLambdaRequiresValue(Symbol symbol, CSharpCompilation compilation)
{
return symbol is MethodSymbol method &&
Expand Down
6 changes: 5 additions & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Lookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,10 @@ internal SingleLookupResult CheckViability(Symbol symbol, int arity, LookupOptio
{
return LookupResult.Empty();
}
else if ((options & LookupOptions.MustBeOperator) != 0 && unwrappedSymbol is not MethodSymbol { MethodKind: MethodKind.UserDefinedOperator })
{
return LookupResult.Empty();
}
else if (!IsInScopeOfAssociatedSyntaxTree(unwrappedSymbol))
{
return LookupResult.Empty();
Expand All @@ -1503,7 +1507,7 @@ internal SingleLookupResult CheckViability(Symbol symbol, int arity, LookupOptio
{
return LookupResult.WrongArity(symbol, diagInfo);
}
else if (!InCref && !unwrappedSymbol.CanBeReferencedByNameIgnoringIllegalCharacters)
else if (!InCref && !unwrappedSymbol.CanBeReferencedByNameIgnoringIllegalCharacters && (options & LookupOptions.MustBeOperator) == 0)
{
// Strictly speaking, this test should actually check CanBeReferencedByName.
// However, we don't want to pay that cost in cases where the lookup is based
Expand Down
Loading