mirror of https://github.com/icsharpcode/ILSpy.git
21 changed files with 1086 additions and 49 deletions
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
// Copyright (c) 2026 Siegfried Pammer
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||
// software and associated documentation files (the "Software"), to deal in the Software
|
||||
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all copies or
|
||||
// substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
using System; |
||||
using System.IO; |
||||
|
||||
using ICSharpCode.Decompiler.Metadata; |
||||
using ICSharpCode.Decompiler.Tests.Helpers; |
||||
using ICSharpCode.Decompiler.Tests.TypeSystem; |
||||
using ICSharpCode.Decompiler.TypeSystem; |
||||
using ICSharpCode.Decompiler.TypeSystem.Implementation; |
||||
|
||||
namespace ICSharpCode.Decompiler.Tests.Semantics |
||||
{ |
||||
/// <summary>
|
||||
/// A compilation over a .NET reference assembly plus the test assembly, shared by all tests
|
||||
/// whose subject types postdate the legacy mscorlib the main test compilation resolves
|
||||
/// against - System.ValueTuple and Span<T>/ReadOnlySpan<T>. The reference assembly
|
||||
/// is read once for the whole test run.
|
||||
/// </summary>
|
||||
static class RefAssemblyCompilation |
||||
{ |
||||
public static ICompilation Instance => instance.Value; |
||||
|
||||
static readonly Lazy<ICompilation> instance = new Lazy<ICompilation>( |
||||
delegate { |
||||
string path = Path.Combine( |
||||
Tester.RefAssembliesToolset.GetPath(".NETCoreApp,Version=v5.0"), "System.Runtime.dll"); |
||||
return new SimpleCompilation(TypeSystemLoaderTests.TestAssembly, |
||||
new PEFile(path, new FileStream(path, FileMode.Open, FileAccess.Read))); |
||||
}); |
||||
} |
||||
} |
||||
@ -0,0 +1,17 @@
@@ -0,0 +1,17 @@
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.Decompiler.Tests.TestCases.ILPretty |
||||
{ |
||||
public class SpanConversionOperatorMismatch |
||||
{ |
||||
public static implicit operator ReadOnlySpan<char>(object o) |
||||
{ |
||||
return default(ReadOnlySpan<char>); |
||||
} |
||||
|
||||
public static ReadOnlySpan<char> ConvertString(string s) |
||||
{ |
||||
return (ReadOnlySpan<char>)(object)s; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
// Regression fixture: an implicit span conversion must not be folded into a call to a conversion |
||||
// operator that does not perform it. |
||||
// |
||||
// C# cannot declare 'implicit operator ReadOnlySpan<char>(object)' - neither operand type is the |
||||
// declaring type - but IL can. An implicit span conversion from string to ReadOnlySpan<char> does |
||||
// exist, yet the compiler emits it as MemoryExtensions.AsSpan, not as this operator, so the call |
||||
// below is a user-defined conversion and the cast to its parameter type has to survive. |
||||
|
||||
.assembly extern System.Runtime |
||||
{ |
||||
.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A ) |
||||
.ver 4:0:0:0 |
||||
} |
||||
.assembly SpanConversionOperatorMismatch |
||||
{ |
||||
.custom instance void [System.Runtime]System.Runtime.Versioning.TargetFrameworkAttribute::.ctor(string) = { |
||||
string('.NETCoreApp,Version=11.0') |
||||
} |
||||
.hash algorithm 0x00008004 |
||||
.ver 1:0:0:0 |
||||
} |
||||
.module SpanConversionOperatorMismatch.dll |
||||
|
||||
.class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.ILPretty.SpanConversionOperatorMismatch |
||||
extends [System.Runtime]System.Object |
||||
{ |
||||
.method public hidebysig specialname static |
||||
valuetype [System.Runtime]System.ReadOnlySpan`1<char> |
||||
op_Implicit(object o) cil managed |
||||
{ |
||||
.maxstack 1 |
||||
.locals init (valuetype [System.Runtime]System.ReadOnlySpan`1<char> V_0) |
||||
IL_0000: ldloca.s V_0 |
||||
IL_0002: initobj valuetype [System.Runtime]System.ReadOnlySpan`1<char> |
||||
IL_0008: ldloc.0 |
||||
IL_0009: ret |
||||
} |
||||
|
||||
.method public hidebysig static |
||||
valuetype [System.Runtime]System.ReadOnlySpan`1<char> |
||||
ConvertString(string s) cil managed |
||||
{ |
||||
.maxstack 8 |
||||
IL_0000: ldarg.0 |
||||
IL_0001: call valuetype [System.Runtime]System.ReadOnlySpan`1<char> ICSharpCode.Decompiler.Tests.TestCases.ILPretty.SpanConversionOperatorMismatch::op_Implicit(object) |
||||
IL_0006: ret |
||||
} |
||||
|
||||
.method public hidebysig specialname rtspecialname |
||||
instance void .ctor() cil managed |
||||
{ |
||||
.maxstack 8 |
||||
IL_0000: ldarg.0 |
||||
IL_0001: call instance void [System.Runtime]System.Object::.ctor() |
||||
IL_0006: ret |
||||
} |
||||
} |
||||
@ -0,0 +1,152 @@
@@ -0,0 +1,152 @@
|
||||
// Copyright (c) 2026 Siegfried Pammer
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||
// software and associated documentation files (the "Software"), to deal in the Software
|
||||
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all copies or
|
||||
// substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty |
||||
{ |
||||
internal static class FirstClassSpanConversions |
||||
{ |
||||
internal class Base |
||||
{ |
||||
} |
||||
|
||||
internal class Derived : Base |
||||
{ |
||||
} |
||||
|
||||
internal class SpanConvertible |
||||
{ |
||||
public static implicit operator ReadOnlySpan<int>(SpanConvertible c) |
||||
{ |
||||
return default(ReadOnlySpan<int>); |
||||
} |
||||
} |
||||
|
||||
internal class DerivedSpanConvertible : SpanConvertible |
||||
{ |
||||
} |
||||
|
||||
public static void AcceptReadOnlySpanChar(ReadOnlySpan<char> s) |
||||
{ |
||||
} |
||||
|
||||
public static void AcceptReadOnlySpanBase(ReadOnlySpan<Base> s) |
||||
{ |
||||
} |
||||
|
||||
public static void AcceptInReadOnlySpan(in ReadOnlySpan<int> s) |
||||
{ |
||||
} |
||||
|
||||
public static void ObjectOrReadOnlySpanChar(object a) |
||||
{ |
||||
} |
||||
|
||||
public static void ObjectOrReadOnlySpanChar(ReadOnlySpan<char> a) |
||||
{ |
||||
} |
||||
|
||||
public static void StringArgument(string s) |
||||
{ |
||||
AcceptReadOnlySpanChar(s); |
||||
ObjectOrReadOnlySpanChar(s); |
||||
s.ExtensionOnReadOnlySpanChar(); |
||||
} |
||||
|
||||
public static ReadOnlySpan<char> StringToReadOnlySpanCharReturn(string s) |
||||
{ |
||||
return s; |
||||
} |
||||
|
||||
public static int StringToReadOnlySpanCharLocal(string s) |
||||
{ |
||||
// The local is read twice so it survives decompilation; a single-use span local is
|
||||
// inlined into its consumer by general decompiler policy, independent of this feature.
|
||||
ReadOnlySpan<char> readOnlySpan = s; |
||||
return readOnlySpan.Length + readOnlySpan.Length; |
||||
} |
||||
|
||||
public static void VarianceReadOnlySpan(ReadOnlySpan<Derived> s) |
||||
{ |
||||
AcceptReadOnlySpanBase(s); |
||||
} |
||||
|
||||
public static void VarianceSpan(Span<Derived> s) |
||||
{ |
||||
AcceptReadOnlySpanBase(s); |
||||
} |
||||
|
||||
public static ReadOnlySpan<Base> VarianceReturn(ReadOnlySpan<Derived> s) |
||||
{ |
||||
return s; |
||||
} |
||||
|
||||
public static void CovariantArrayToReadOnlySpan(Derived[] a) |
||||
{ |
||||
AcceptReadOnlySpanBase(a); |
||||
} |
||||
|
||||
public static ReadOnlySpan<Base> CovariantArrayThroughOperator(Derived[] a) |
||||
{ |
||||
// ReadOnlySpan<Base>.op_Implicit(Base[]) applied to a Derived[]: array covariance
|
||||
// means the IL passes the argument without a cast instruction, and the span
|
||||
// conversion the call performs relates exactly these two types.
|
||||
return a; |
||||
} |
||||
|
||||
public static ReadOnlySpan<int> UserDefinedOperatorToSpan(DerivedSpanConvertible c) |
||||
{ |
||||
// The operator is declared on the base type, so the conversion is user-defined even
|
||||
// though its target is a span type. Only the user-defined conversion that resolves
|
||||
// to this very operator may be folded into the cast; a span conversion never applies
|
||||
// to a source type outside the language's own span-convertible set.
|
||||
return c; |
||||
} |
||||
|
||||
public static void InArgument(int[] a) |
||||
{ |
||||
AcceptInReadOnlySpan(a); |
||||
} |
||||
|
||||
public static void ByValueOrIn(ReadOnlySpan<int> s) |
||||
{ |
||||
} |
||||
|
||||
public static void ByValueOrIn(in ReadOnlySpan<int> s) |
||||
{ |
||||
} |
||||
|
||||
public static void CallByValueOrIn(ReadOnlySpan<int> s, int[] a) |
||||
{ |
||||
// Without 'in' the by-value overload is the better parameter-passing choice, also
|
||||
// through the span conversion; with 'in' only the in-overload binds, so the
|
||||
// keyword must survive decompilation.
|
||||
ByValueOrIn(s); |
||||
ByValueOrIn(in s); |
||||
ByValueOrIn(a); |
||||
} |
||||
} |
||||
|
||||
internal static class FirstClassSpanConversionsExtensions |
||||
{ |
||||
public static void ExtensionOnReadOnlySpanChar(this ReadOnlySpan<char> s) |
||||
{ |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,216 @@
@@ -0,0 +1,216 @@
|
||||
// Copyright (c) 2026 Siegfried Pammer
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||
// software and associated documentation files (the "Software"), to deal in the Software
|
||||
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all copies or
|
||||
// substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
|
||||
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty |
||||
{ |
||||
internal static class FirstClassSpanTypes |
||||
{ |
||||
public static void ArrayOrReadOnlySpan(int[] a) |
||||
{ |
||||
} |
||||
|
||||
public static void ArrayOrReadOnlySpan(ReadOnlySpan<int> a) |
||||
{ |
||||
} |
||||
|
||||
public static void ArrayOrSpan(int[] a) |
||||
{ |
||||
} |
||||
|
||||
public static void ArrayOrSpan(Span<int> a) |
||||
{ |
||||
} |
||||
|
||||
public static void SpanOrReadOnlySpan(Span<int> a) |
||||
{ |
||||
} |
||||
|
||||
public static void SpanOrReadOnlySpan(ReadOnlySpan<int> a) |
||||
{ |
||||
} |
||||
|
||||
public static void ObjectOrReadOnlySpan(object a) |
||||
{ |
||||
} |
||||
|
||||
public static void ObjectOrReadOnlySpan(ReadOnlySpan<int> a) |
||||
{ |
||||
} |
||||
|
||||
public static void ObjectOrReadOnlySpanChar(object a) |
||||
{ |
||||
} |
||||
|
||||
public static void ObjectOrReadOnlySpanChar(ReadOnlySpan<char> a) |
||||
{ |
||||
} |
||||
|
||||
public static void EnumerableOrReadOnlySpan(IEnumerable<int> a) |
||||
{ |
||||
} |
||||
|
||||
public static void EnumerableOrReadOnlySpan(ReadOnlySpan<int> a) |
||||
{ |
||||
} |
||||
|
||||
public static void CovariantArrayOrReadOnlySpan(object[] a) |
||||
{ |
||||
} |
||||
|
||||
public static void CovariantArrayOrReadOnlySpan(ReadOnlySpan<string> a) |
||||
{ |
||||
} |
||||
|
||||
public static void ReadOnlySpanOfObjectOrString(ReadOnlySpan<object> a) |
||||
{ |
||||
} |
||||
|
||||
public static void ReadOnlySpanOfObjectOrString(ReadOnlySpan<string> a) |
||||
{ |
||||
} |
||||
|
||||
public static void StringOrReadOnlySpanChar(string a) |
||||
{ |
||||
} |
||||
|
||||
public static void StringOrReadOnlySpanChar(ReadOnlySpan<char> a) |
||||
{ |
||||
} |
||||
|
||||
public static void ParamsArrayOrParamsReadOnlySpan(params int[] a) |
||||
{ |
||||
} |
||||
|
||||
public static void ParamsArrayOrParamsReadOnlySpan(params ReadOnlySpan<int> a) |
||||
{ |
||||
} |
||||
|
||||
public static void RefSpanOrByValue(ref ReadOnlySpan<int> s) |
||||
{ |
||||
} |
||||
|
||||
public static void RefSpanOrByValue(ReadOnlySpan<int> s) |
||||
{ |
||||
} |
||||
|
||||
public static void OutSpanOrByValue(out ReadOnlySpan<int> s) |
||||
{ |
||||
s = default(ReadOnlySpan<int>); |
||||
} |
||||
|
||||
public static void OutSpanOrByValue(ReadOnlySpan<int> s) |
||||
{ |
||||
} |
||||
|
||||
public static void GenericArrayOrReadOnlySpan<T>(T[] a) |
||||
{ |
||||
} |
||||
|
||||
public static void GenericArrayOrReadOnlySpan<T>(ReadOnlySpan<T> a) |
||||
{ |
||||
} |
||||
|
||||
public static void InferFromReadOnlySpan<T>(ReadOnlySpan<T> a) |
||||
{ |
||||
} |
||||
|
||||
public static ReadOnlySpan<int> ArrayToReadOnlySpanReturn(int[] a) |
||||
{ |
||||
return a; |
||||
} |
||||
|
||||
public static ReadOnlySpan<int> TernaryArrayOrSpan(bool b, int[] a, Span<int> s) |
||||
{ |
||||
#if OPT
|
||||
if (!b) |
||||
{ |
||||
return s; |
||||
} |
||||
return a; |
||||
#else
|
||||
return b ? ((ReadOnlySpan<int>)a) : ((ReadOnlySpan<int>)s); |
||||
#endif
|
||||
} |
||||
|
||||
public static bool SpanExtensionContains(int[] a) |
||||
{ |
||||
// binds to MemoryExtensions.Contains under C# 14 first-class span conversions
|
||||
return a.Contains(2); |
||||
} |
||||
|
||||
public static bool LinqContains(int[] a) |
||||
{ |
||||
// Enumerable.Contains loses against MemoryExtensions.Contains under C# 14;
|
||||
// extension method syntax must not be used here
|
||||
return Enumerable.Contains(a, 2); |
||||
} |
||||
|
||||
public static void CallWinners(int[] arr, Span<int> span, string str, string[] strArr) |
||||
{ |
||||
ArrayOrReadOnlySpan(arr); |
||||
ArrayOrSpan(arr); |
||||
SpanOrReadOnlySpan(arr); |
||||
SpanOrReadOnlySpan(span); |
||||
ObjectOrReadOnlySpan(arr); |
||||
EnumerableOrReadOnlySpan(arr); |
||||
CovariantArrayOrReadOnlySpan(strArr); |
||||
ReadOnlySpanOfObjectOrString(strArr); |
||||
StringOrReadOnlySpanChar(str); |
||||
ParamsArrayOrParamsReadOnlySpan(arr); |
||||
ParamsArrayOrParamsReadOnlySpan(1, 2, 3); |
||||
GenericArrayOrReadOnlySpan(arr); |
||||
InferFromReadOnlySpan(arr); |
||||
InferFromReadOnlySpan(span); |
||||
arr.ExtensionOnReadOnlySpan(); |
||||
} |
||||
|
||||
public static void CallRefOutOrByValue(int[] arr) |
||||
{ |
||||
// A span conversion never binds a ref or out parameter: without the keyword the
|
||||
// by-value overload wins, with the keyword only the ref/out overload is
|
||||
// applicable and the keyword must survive decompilation.
|
||||
ReadOnlySpan<int> s = arr; |
||||
RefSpanOrByValue(arr); |
||||
RefSpanOrByValue(ref s); |
||||
OutSpanOrByValue(arr); |
||||
OutSpanOrByValue(out s); |
||||
} |
||||
|
||||
public static void CallLosersWithExplicitConversions(int[] arr, string str) |
||||
{ |
||||
ArrayOrReadOnlySpan((ReadOnlySpan<int>)arr); |
||||
ArrayOrSpan((Span<int>)arr); |
||||
SpanOrReadOnlySpan((Span<int>)arr); |
||||
ObjectOrReadOnlySpan((object)arr); |
||||
ObjectOrReadOnlySpanChar((object)str); |
||||
EnumerableOrReadOnlySpan((IEnumerable<int>)arr); |
||||
StringOrReadOnlySpanChar((ReadOnlySpan<char>)str); |
||||
} |
||||
} |
||||
|
||||
internal static class FirstClassSpanTypesExtensions |
||||
{ |
||||
public static void ExtensionOnReadOnlySpan<T>(this ReadOnlySpan<T> s) |
||||
{ |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue