Skip to content
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

Implement control flow graph for Lock object feature #71987

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -2052,7 +2052,22 @@ private ILockOperation CreateBoundLockStatementOperation(BoundLockStatement boun
SyntaxNode syntax = boundLockStatement.Syntax;
bool isImplicit = boundLockStatement.WasCompilerGenerated;

return new LockOperation(lockedValue, body, lockTakenSymbol, _semanticModel, syntax, isImplicit);
(IOperation, IMethodSymbol)? lockTypeInfo =
boundLockStatement.Argument is { Type: { } argType, Syntax: var argSyntax } argExpression &&
argType.IsWellKnownTypeLock() &&
LockBinder.TryFindLockTypeInfo(argType, CSharp.BindingDiagnosticBag.Discarded, argSyntax) is { } binderInfo
? (CreateBoundCallOperation(BoundCall.Synthesized(
argSyntax,
argExpression,
initialBindingReceiverIsSubjectToCloning: ThreeState.Unknown,
binderInfo.EnterLockScopeMethod)),
binderInfo.ScopeDisposeMethod.GetPublicSymbol())
: null;

return new LockOperation(lockedValue, body, lockTakenSymbol, _semanticModel, syntax, isImplicit)
{
LockTypeInfo = lockTypeInfo,
};
}

private IInvalidOperation CreateBoundBadStatementOperation(BoundBadStatement boundBadStatement)
Expand Down
112 changes: 112 additions & 0 deletions src/Compilers/CSharp/Test/Emit2/Semantics/LockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,118 @@ public void LangVersion()
verify: Verification.FailsILVerify).VerifyDiagnostics();
}

[Fact]
public void OperationTree()
{
var source = """
using System;
using System.Threading;

Lock l = new Lock();
/*<bind>*/lock (l)
{
}/*</bind>*/
""";

var expectedOperationTree = """
ILockOperation (OperationKind.Lock, Type: null) (Syntax: 'lock (l) ... }')
Expression:
ILocalReferenceOperation: l (OperationKind.LocalReference, Type: System.Threading.Lock) (Syntax: 'l')
Body:
IBlockOperation (0 statements) (OperationKind.Block, Type: null) (Syntax: '{ ... }')
""";

var expectedDiagnostics = DiagnosticDescription.None;

VerifyOperationTreeAndDiagnosticsForTest<LockStatementSyntax>([source, LockTypeDefinition], expectedOperationTree, expectedDiagnostics);
}

[Fact]
public void FlowGraph()
jjonescz marked this conversation as resolved.
Show resolved Hide resolved
jjonescz marked this conversation as resolved.
Show resolved Hide resolved
{
var source = """
using System;
using System.Threading;

class C
{
void M()
/*<bind>*/{
Lock l = new Lock();
lock (l)
jjonescz marked this conversation as resolved.
Show resolved Hide resolved
{
}
}/*</bind>*/
}
""";

var expectedFlowGraph = """
Block[B0] - Entry
Statements (0)
Next (Regular) Block[B1]
Entering: {R1}
.locals {R1}
{
Locals: [System.Threading.Lock l]
Block[B1] - Block
Predecessors: [B0]
Statements (1)
ISimpleAssignmentOperation (OperationKind.SimpleAssignment, Type: System.Threading.Lock, IsImplicit) (Syntax: 'l = new Lock()')
Left:
ILocalReferenceOperation: l (IsDeclaration: True) (OperationKind.LocalReference, Type: System.Threading.Lock, IsImplicit) (Syntax: 'l = new Lock()')
Right:
IObjectCreationOperation (Constructor: System.Threading.Lock..ctor()) (OperationKind.ObjectCreation, Type: System.Threading.Lock) (Syntax: 'new Lock()')
Arguments(0)
Initializer:
null
Next (Regular) Block[B2]
Entering: {R2}
.locals {R2}
{
CaptureIds: [0]
Block[B2] - Block
Predecessors: [B1]
Statements (1)
IFlowCaptureOperation: 0 (OperationKind.FlowCapture, Type: null, IsImplicit) (Syntax: 'l')
Value:
IInvocationOperation ( System.Threading.Lock.Scope System.Threading.Lock.EnterLockScope()) (OperationKind.Invocation, Type: System.Threading.Lock.Scope, IsImplicit) (Syntax: 'l')
Instance Receiver:
ILocalReferenceOperation: l (OperationKind.LocalReference, Type: System.Threading.Lock) (Syntax: 'l')
Arguments(0)
Next (Regular) Block[B3]
Entering: {R3} {R4}
.try {R3, R4}
{
Block[B3] - Block
Predecessors: [B2]
Statements (0)
Next (Regular) Block[B5]
Finalizing: {R5}
Leaving: {R4} {R3} {R2} {R1}
}
.finally {R5}
{
Block[B4] - Block
Predecessors (0)
Statements (1)
IInvocationOperation ( void System.Threading.Lock.Scope.Dispose()) (OperationKind.Invocation, Type: System.Void, IsImplicit) (Syntax: 'l')
Instance Receiver:
IFlowCaptureReferenceOperation: 0 (OperationKind.FlowCaptureReference, Type: System.Threading.Lock.Scope, IsImplicit) (Syntax: 'l')
Arguments(0)
Next (StructuredExceptionHandling) Block[null]
}
}
}
Block[B5] - Exit
Predecessors: [B3]
Statements (0)
""";

var expectedDiagnostics = DiagnosticDescription.None;

VerifyFlowGraphAndDiagnosticsForTest<BlockSyntax>([source, LockTypeDefinition], expectedFlowGraph, expectedDiagnostics);
}
jjonescz marked this conversation as resolved.
Show resolved Hide resolved

[Fact]
public void InPlace()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4200,6 +4200,22 @@ private IOperation ConvertToIDisposable(IOperation operand, ITypeSymbol iDisposa
{
StartVisitingStatement(operation);

var lockStatement = (LockOperation)operation;

// `lock (l) { }` on value of type `System.Threading.Lock` is lowered to `using (l.EnterLockScope()) { }`.
if (lockStatement.LockTypeInfo is (var enterLockScope, var disposeMethod))
jjonescz marked this conversation as resolved.
Show resolved Hide resolved
{
HandleUsingOperationParts(
resources: enterLockScope,
jjonescz marked this conversation as resolved.
Show resolved Hide resolved
body: lockStatement.Body,
disposeMethod: disposeMethod,
jjonescz marked this conversation as resolved.
Show resolved Hide resolved
disposeArguments: ImmutableArray<IArgumentOperation>.Empty,
locals: ImmutableArray<ILocalSymbol>.Empty,
isAsynchronous: false);

return FinishVisitingStatement(operation);
}

ITypeSymbol objectType = _compilation.GetSpecialType(SpecialType.System_Object);

// If Monitor.Enter(object, ref bool) is available:
Expand Down Expand Up @@ -4233,7 +4249,6 @@ private IOperation ConvertToIDisposable(IOperation operand, ITypeSymbol iDisposa
// Microsoft.VisualBasic.CompilerServices.ObjectFlowControl.CheckForSyncLockOnValueType to ensure no value type is
// used.
// For simplicity, we will not synthesize this call because its presence is unlikely to affect graph analysis.
var lockStatement = (LockOperation)operation;

var lockRegion = new RegionBuilder(ControlFlowRegionKind.LocalLifetime,
locals: lockStatement.LockTakenSymbol != null ?
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/Core/Portable/Operations/OperationNodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -701,4 +701,9 @@ private BlockOperation(ImmutableArray<IOperation> statements, SemanticModel sema
Locals = ImmutableArray<ILocalSymbol>.Empty;
}
}

internal sealed partial class LockOperation
{
public (IOperation EnterLockScope, IMethodSymbol DisposeMethod)? LockTypeInfo { get; init; }
jjonescz marked this conversation as resolved.
Show resolved Hide resolved
}
}