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

Test Lock with .NET 9 runtime #74367

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 56 additions & 0 deletions src/Compilers/CSharp/Test/Emit2/Semantics/LockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,62 @@ .locals init (System.Threading.Lock.Scope V_0)
verifier.VerifyIL("C.M1", il);
}

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

static class C
{
static readonly Lock _lock = new();

static void Main()
{
Console.Write("1");
lock (_lock)
{
Console.Write("2");
}
Console.Write("3");
}
}
""";
var verifier = CompileAndVerify(source,
expectedOutput: ExecutionConditionUtil.IsMonoOrCoreClr ? "123" : null,
targetFramework: TargetFramework.Net90,
verify: Verification.FailsPEVerify);
verifier.VerifyDiagnostics();
verifier.VerifyIL("C.Main", """
{
// Code size 52 (0x34)
.maxstack 1
.locals init (System.Threading.Lock.Scope V_0)
IL_0000: ldstr "1"
IL_0005: call "void System.Console.Write(string)"
IL_000a: ldsfld "System.Threading.Lock C._lock"
IL_000f: callvirt "System.Threading.Lock.Scope System.Threading.Lock.EnterScope()"
IL_0014: stloc.0
.try
{
IL_0015: ldstr "2"
IL_001a: call "void System.Console.Write(string)"
IL_001f: leave.s IL_0029
}
finally
{
IL_0021: ldloca.s V_0
IL_0023: call "void System.Threading.Lock.Scope.Dispose()"
IL_0028: endfinally
}
IL_0029: ldstr "3"
IL_002e: call "void System.Console.Write(string)"
IL_0033: ret
}
""");
}

[Fact]
public void SemanticModel()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,53 @@ End Namespace
")
End Sub

<Fact>
Public Sub LockType_InSyncLock_Net9()
Dim source = "
Module Program
Sub Main()
Dim l = New System.Threading.Lock()
SyncLock l
End SyncLock
End Sub
End Module
"
CreateCompilation(source, targetFramework:=TargetFramework.Net90).AssertTheseDiagnostics(
"BC37329: A value of type 'System.Threading.Lock' is not supported in SyncLock. Consider manually calling 'Enter' and 'Exit' methods in a Try/Finally block instead.
SyncLock l
~
")
End Sub

''' <summary>
''' Verifies that the suggestion from the error in the test above (to manually call 'Enter' and 'Exit') compiles.
''' </summary>
<Fact>
Public Sub LockType_InSyncLock_ManualEnterExit()
Dim source = <![CDATA[
Imports System
Imports System.Threading
Module Program
Sub Main()
Dim l = New Lock()
l.Enter()
Console.Write("1")
Try
Console.Write("2")
Finally
Console.Write("3")
l.Exit()
Console.Write("4")
End Try
Console.Write("5")
End Sub
End Module
]]>.Value
Dim comp = CreateCompilation(source, options:=TestOptions.ReleaseExe, targetFramework:=TargetFramework.Net90)
CompileAndVerify(comp, expectedOutput:=If(ExecutionConditionUtil.IsMonoOrCoreClr, "12345", Nothing),
verify:=Verification.FailsPEVerify).VerifyDiagnostics()
End Sub

<Fact>
Public Sub LockType_Generic()
Dim source = "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>$(NetRoslyn);net472</TargetFrameworks>
<TargetFrameworks>$(NetRoslynNext);net472</TargetFrameworks>
<RootNamespace></RootNamespace>

<!--
Expand All @@ -21,6 +21,7 @@
<ProjectReference Include="..\..\..\Test\Utilities\VisualBasic\Microsoft.CodeAnalysis.VisualBasic.Test.Utilities.vbproj" />
<ProjectReference Include="..\..\Portable\Microsoft.CodeAnalysis.VisualBasic.vbproj" />
<PackageReference Include="Basic.Reference.Assemblies.Net60" />
<PackageReference Include="Basic.Reference.Assemblies.Net90" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Condition="$(TargetFrameworkIdentifier) == '.NETCoreApp'" />
Expand Down