Skip to content

Commit b1fbe69

Browse files
jaredparcston
andauthored
Update to latest .NET 7 Roslyn compiler (#76459)
* Patches for scoped locals dotnet/roslyn#64093 This change enforced that `scoped` on a local set the escape scope to the current block where previously it was incorrectly setting to the containing method. * Make return and out equivalent for ref safety dotnet/roslyn#64318 This change allows anything returnable from a method to be assigned to an `out` parameter. In several places had to add `scoped` to `ref` to inform compiler they could not be captured in an `out` parameter. * Warnings on managed pointer types dotnet/roslyn#64294 Compiler now issues warnings for pointer operations involving managed types * Update compiler version * PR feedback * Ref safety rules attribute Co-authored-by: Charles Stoner <[email protected]>
1 parent 07e861a commit b1fbe69

File tree

14 files changed

+56
-24
lines changed

14 files changed

+56
-24
lines changed

eng/Versions.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<!--
5353
TODO: Remove pinned version once arcade supplies a compiler that enables the repo to compile.
5454
-->
55-
<MicrosoftNetCompilersToolsetVersion>4.4.0-3.22452.8</MicrosoftNetCompilersToolsetVersion>
55+
<MicrosoftNetCompilersToolsetVersion>4.4.0-3.22479.16</MicrosoftNetCompilersToolsetVersion>
5656
<StaticCsVersion>0.2.0</StaticCsVersion>
5757
<!-- SDK dependencies -->
5858
<MicrosoftDotNetApiCompatTaskVersion>8.0.100-alpha.1.22462.3</MicrosoftDotNetApiCompatTaskVersion>

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicMethod.CoreCLR.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ Signature LazyCreateSignature()
172172
Span<ParameterCopyBackAction> shouldCopyBackParameters = new(ref argStorage._copyBack0, argCount);
173173

174174
StackAllocatedByRefs byrefStorage = default;
175+
#pragma warning disable CS8500
175176
IntPtr* pByRefStorage = (IntPtr*)&byrefStorage;
177+
#pragma warning restore CS8500
176178

177179
CheckArguments(
178180
copyOfParameters,

src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.CoreCLR.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,9 @@ public override MethodImplAttributes GetMethodImplementationFlags()
316316
Span<ParameterCopyBackAction> shouldCopyBackParameters = new(ref argStorage._copyBack0, 1);
317317

318318
StackAllocatedByRefs byrefStorage = default;
319+
#pragma warning disable 8500
319320
IntPtr* pByRefStorage = (IntPtr*)&byrefStorage;
321+
#pragma warning restore 8500
320322

321323
CheckArguments(
322324
copyOfParameters,

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/DynamicInvokeInfo.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,15 @@ public DynamicInvokeInfo(MethodBase method, IntPtr invokeThunk)
235235
StackAllocedArguments argStorage = default;
236236
StackAllocatedByRefs byrefStorage = default;
237237

238+
#pragma warning disable 8500
238239
CheckArguments(ref argStorage._arg0!, (ByReference*)&byrefStorage, parameters, binderBundle);
240+
#pragma warning restore 8500
239241

240242
try
241243
{
244+
#pragma warning disable 8500
242245
ret = ref RawCalliHelper.Call(InvokeThunk, (void*)methodToCall, ref thisArg, ref ret, &byrefStorage);
246+
#pragma warning restore 8500
243247
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
244248
}
245249
catch (Exception e) when (wrapInTargetInvocationException)
@@ -268,7 +272,9 @@ private unsafe ref byte InvokeWithManyArguments(
268272
IntPtr* pStorage = stackalloc IntPtr[2 * argCount];
269273
NativeMemory.Clear(pStorage, (nuint)(2 * argCount) * (nuint)sizeof(IntPtr));
270274

271-
ByReference* pByRefStorage = (ByReference*)(pStorage + argCount);
275+
#pragma warning disable 8500
276+
void* pByRefStorage = (ByReference*)(pStorage + argCount);
277+
#pragma warning restore 8500
272278

273279
RuntimeImports.GCFrameRegistration regArgStorage = new(pStorage, (uint)argCount, areByRefs: false);
274280
RuntimeImports.GCFrameRegistration regByRefStorage = new(pByRefStorage, (uint)argCount, areByRefs: true);
@@ -326,7 +332,7 @@ private unsafe ref byte InvokeWithManyArguments(
326332

327333
private unsafe void CheckArguments(
328334
ref object copyOfParameters,
329-
ByReference* byrefParameters,
335+
void* byrefParameters,
330336
object?[] parameters,
331337
BinderBundle binderBundle)
332338
{
@@ -398,8 +404,10 @@ private unsafe void CheckArguments(
398404

399405
Unsafe.Add(ref copyOfParameters, i) = arg!;
400406

401-
byrefParameters[i] = new ByReference(ref (argumentInfo.Transform & Transform.Reference) != 0 ?
407+
#pragma warning disable 8500, 9094
408+
((ByReference*)byrefParameters)[i] = new ByReference(ref (argumentInfo.Transform & Transform.Reference) != 0 ?
402409
ref Unsafe.As<object, byte>(ref Unsafe.Add(ref copyOfParameters, i)) : ref arg.GetRawData());
410+
#pragma warning restore 8500, 9094
403411
}
404412
}
405413

src/coreclr/tools/aot/ILCompiler.Compiler.Tests/ILCompiler.Compiler.Tests.Assets/ILCompiler.Compiler.Tests.Assets.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<TargetFramework>netstandard2.0</TargetFramework>
99
<!-- Don't add references to the netstandard platform since this is a core assembly -->
1010
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
11+
<Features>noRefSafetyRulesAttribute=true</Features>
1112
</PropertyGroup>
1213

1314
<ItemGroup>

src/coreclr/tools/aot/ILCompiler.Compiler.Tests/ILCompiler.Compiler.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<Platforms>x86;x64</Platforms>
1515
<PlatformTarget>AnyCPU</PlatformTarget>
1616
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
17+
<Features>noRefSafetyRulesAttribute=true</Features>
1718
</PropertyGroup>
1819

1920
<ItemGroup>

src/libraries/System.Private.CoreLib/src/System/Number.BigInteger.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ internal unsafe ref struct BigInteger
317317
private int _length;
318318
private fixed uint _blocks[MaxBlockCount];
319319

320-
public static void Add(ref BigInteger lhs, ref BigInteger rhs, out BigInteger result)
320+
public static void Add(scoped ref BigInteger lhs, scoped ref BigInteger rhs, out BigInteger result)
321321
{
322322
// determine which operand has the smaller length
323323
ref BigInteger large = ref (lhs._length < rhs._length) ? ref rhs : ref lhs;
@@ -369,7 +369,7 @@ public static void Add(ref BigInteger lhs, ref BigInteger rhs, out BigInteger re
369369
}
370370
}
371371

372-
public static int Compare(ref BigInteger lhs, ref BigInteger rhs)
372+
public static int Compare(scoped ref BigInteger lhs, scoped ref BigInteger rhs)
373373
{
374374
Debug.Assert(unchecked((uint)(lhs._length)) <= MaxBlockCount);
375375
Debug.Assert(unchecked((uint)(rhs._length)) <= MaxBlockCount);
@@ -427,7 +427,7 @@ public static uint CountSignificantBits(ref BigInteger value)
427427
return (lastIndex * BitsPerBlock) + CountSignificantBits(value._blocks[lastIndex]);
428428
}
429429

430-
public static void DivRem(ref BigInteger lhs, ref BigInteger rhs, out BigInteger quo, out BigInteger rem)
430+
public static void DivRem(scoped ref BigInteger lhs, scoped ref BigInteger rhs, out BigInteger quo, out BigInteger rem)
431431
{
432432
// This is modified from the libraries BigIntegerCalculator.DivRem.cs implementation:
433433
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigIntegerCalculator.DivRem.cs
@@ -558,6 +558,11 @@ public static void DivRem(ref BigInteger lhs, ref BigInteger rhs, out BigInteger
558558

559559
if (digit > 0)
560560
{
561+
// rem and rhs have different lifetimes here and compiler is warning
562+
// about potential for one to copy into the other. This is a place
563+
// ref scoped parameters would alleviate.
564+
// https://github.com/dotnet/roslyn/issues/64393
565+
#pragma warning disable CS9080
561566
// Now it's time to subtract our current quotient
562567
uint carry = SubtractDivisor(ref rem, n, ref rhs, digit);
563568

@@ -571,6 +576,7 @@ public static void DivRem(ref BigInteger lhs, ref BigInteger rhs, out BigInteger
571576

572577
Debug.Assert(carry == 1);
573578
}
579+
#pragma warning restore CS9080
574580
}
575581

576582
// We have the digit!
@@ -693,7 +699,7 @@ public static uint HeuristicDivide(ref BigInteger dividend, ref BigInteger divis
693699
return quotient;
694700
}
695701

696-
public static void Multiply(ref BigInteger lhs, uint value, out BigInteger result)
702+
public static void Multiply(scoped ref BigInteger lhs, uint value, out BigInteger result)
697703
{
698704
if (lhs._length <= 1)
699705
{
@@ -739,7 +745,7 @@ public static void Multiply(ref BigInteger lhs, uint value, out BigInteger resul
739745
}
740746
}
741747

742-
public static void Multiply(ref BigInteger lhs, ref BigInteger rhs, out BigInteger result)
748+
public static void Multiply(scoped ref BigInteger lhs, scoped ref BigInteger rhs, out BigInteger result)
743749
{
744750
if (lhs._length <= 1)
745751
{
@@ -1032,7 +1038,7 @@ public void Multiply(uint value)
10321038
Multiply(ref this, value, out this);
10331039
}
10341040

1035-
public void Multiply(ref BigInteger value)
1041+
public void Multiply(scoped ref BigInteger value)
10361042
{
10371043
if (value._length <= 1)
10381044
{
@@ -1115,7 +1121,7 @@ public static void SetUInt64(out BigInteger result, ulong value)
11151121
}
11161122
}
11171123

1118-
public static void SetValue(out BigInteger result, ref BigInteger value)
1124+
public static void SetValue(out BigInteger result, scoped ref BigInteger value)
11191125
{
11201126
int rhsLength = value._length;
11211127
result._length = rhsLength;

src/libraries/System.Private.CoreLib/src/System/Number.NumberToFloatingPointBits.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ public FloatingPointInfo(ushort denormalMantissaBits, ushort exponentBits, int m
794794
0x8e679c2f5e44ff8f, 0x570f09eaa7ea7648
795795
};
796796

797-
private static void AccumulateDecimalDigitsIntoBigInteger(ref NumberBuffer number, uint firstIndex, uint lastIndex, out BigInteger result)
797+
private static void AccumulateDecimalDigitsIntoBigInteger(scoped ref NumberBuffer number, uint firstIndex, uint lastIndex, out BigInteger result)
798798
{
799799
BigInteger.SetZero(out result);
800800

src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ BindingFlags invokeAttr
236236
shouldCopyBack[i] = copyBackArg;
237237
copyOfParameters[i] = arg;
238238

239+
#pragma warning disable 8500
239240
if (isValueType)
240241
{
241242
#if !MONO // Temporary until Mono is updated.
@@ -254,6 +255,7 @@ BindingFlags invokeAttr
254255
ByReference objRef = ByReference.Create(ref copyOfParameters[i]);
255256
*(ByReference*)(byrefParameters + i) = objRef;
256257
}
258+
#pragma warning restore 8500
257259
}
258260
}
259261

src/libraries/System.Private.CoreLib/src/System/Reflection/RuntimeConstructorInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ internal void ThrowNoInvokeException()
146146
Span<ParameterCopyBackAction> shouldCopyBackParameters = new(ref argStorage._copyBack0, argCount);
147147

148148
StackAllocatedByRefs byrefStorage = default;
149+
#pragma warning disable 8500
149150
IntPtr* pByRefStorage = (IntPtr*)&byrefStorage;
151+
#pragma warning restore 8500
150152

151153
CheckArguments(
152154
copyOfParameters,
@@ -299,7 +301,9 @@ public override object Invoke(BindingFlags invokeAttr, Binder? binder, object?[]
299301
Span<ParameterCopyBackAction> shouldCopyBackParameters = new(ref argStorage._copyBack0, argCount);
300302

301303
StackAllocatedByRefs byrefStorage = default;
304+
#pragma warning disable 8500
302305
IntPtr* pByRefStorage = (IntPtr*)&byrefStorage;
306+
#pragma warning restore 8500
303307

304308
CheckArguments(
305309
copyOfParameters,

0 commit comments

Comments
 (0)