mirror of
https://github.com/icsharpcode/ILSpy.git
synced 2026-10-12 02:15:58 +00:00
Shortening default(T) is the same problem as removing the redundant cast around a lambda whose delegate type the context already fixes, so it uses the same mechanism: ConvertTo makes the explicit type implicit when the conversion is an identity conversion and the caller allows an implicit one. The literal keeps the type it was shortened from, so any later conversion to a different type - or any context that requires an explicit type, such as an overload resolution recheck falling back to CastArguments - can spell default(T) out again. That keeps the value intact where the bare literal would change it, e.g. "object o = default(SomeStruct)", which boxes a non-null struct while "default" would be null. Because the shortened literal resolves to DefaultLiteralResolveResult, CallBuilder's existing overload resolution recheck sees a real default literal and rejects ambiguous calls on its own; no separate bookkeeping about which arguments may stay untyped is needed. Only the contexts that supply no target type at all restore the explicit form: an awaited expression, and arguments of operator methods, which later become operator or cast syntax rather than calls. Assisted-by: Claude:claude-opus-5[1m]:Claude Code
228 lines
4.7 KiB
C#
228 lines
4.7 KiB
C#
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
|
|
//
|
|
// 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 System.Runtime.InteropServices;
|
|
using System.Threading;
|
|
|
|
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
|
|
{
|
|
internal class Using
|
|
{
|
|
[StructLayout(LayoutKind.Sequential, Size = 1)]
|
|
private struct UsingStruct : IDisposable
|
|
{
|
|
public UsingStruct(int i)
|
|
{
|
|
Console.WriteLine(i);
|
|
}
|
|
|
|
void IDisposable.Dispose()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
|
|
private struct TypeA_Issue3385 : IDisposable
|
|
{
|
|
private int dummy;
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
|
|
#if !ROSLYN3
|
|
public static implicit operator TypeB_Issue3385(TypeA_Issue3385 a)
|
|
{
|
|
#if CS71
|
|
return default;
|
|
#else
|
|
return default(TypeB_Issue3385);
|
|
#endif
|
|
}
|
|
#else
|
|
public static implicit operator TypeB_Issue3385(in TypeA_Issue3385 a)
|
|
{
|
|
#if CS71
|
|
return default;
|
|
#else
|
|
return default(TypeB_Issue3385);
|
|
#endif
|
|
}
|
|
#endif
|
|
}
|
|
|
|
private struct TypeB_Issue3385
|
|
{
|
|
private int dummy;
|
|
}
|
|
|
|
#if CS80
|
|
[StructLayout(LayoutKind.Sequential, Size = 1)]
|
|
public ref struct UsingRefStruct
|
|
{
|
|
public int i;
|
|
|
|
public UsingRefStruct(int i)
|
|
{
|
|
this.i = i;
|
|
Console.WriteLine(i);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if LEGACY_CSC
|
|
// roslyn optimizes out the try-finally; mcs has a compiler bug on using(null-literal)
|
|
public void SimpleUsingNullStatement()
|
|
{
|
|
using (null)
|
|
{
|
|
Console.WriteLine("using (null)");
|
|
}
|
|
}
|
|
#endif
|
|
|
|
public void SimpleUsingExpressionStatement()
|
|
{
|
|
using (new MemoryStream())
|
|
{
|
|
Console.WriteLine("using-body");
|
|
}
|
|
}
|
|
|
|
public void SimpleUsingExpressionStatementWithDeclaration()
|
|
{
|
|
using (MemoryStream memoryStream = new MemoryStream())
|
|
{
|
|
memoryStream.WriteByte(42);
|
|
Console.WriteLine("using-body: " + memoryStream.Position);
|
|
}
|
|
}
|
|
|
|
public void UsingStatementThatChangesTheVariable()
|
|
{
|
|
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
|
|
using (cancellationTokenSource)
|
|
{
|
|
cancellationTokenSource = new CancellationTokenSource();
|
|
}
|
|
cancellationTokenSource.Cancel();
|
|
}
|
|
|
|
public void UsingStatementOnStruct()
|
|
{
|
|
using (new UsingStruct(1))
|
|
{
|
|
Console.WriteLine("using-body");
|
|
}
|
|
}
|
|
|
|
public void UsingStatementOnStructWithVariable()
|
|
{
|
|
using (UsingStruct usingStruct = new UsingStruct(2))
|
|
{
|
|
Console.WriteLine("using-body: " + usingStruct);
|
|
}
|
|
}
|
|
|
|
private void UsingStatementOnNullableStruct(UsingStruct? us)
|
|
{
|
|
using (us)
|
|
{
|
|
Console.WriteLine("using-body: " + us.ToString());
|
|
}
|
|
}
|
|
|
|
public void GenericUsing<T>(T t) where T : IDisposable
|
|
{
|
|
using (t)
|
|
{
|
|
Console.WriteLine(t);
|
|
}
|
|
}
|
|
|
|
public void GenericStructUsing<T>(T t) where T : struct, IDisposable
|
|
{
|
|
using (t)
|
|
{
|
|
Console.WriteLine(t);
|
|
}
|
|
}
|
|
|
|
public void GenericClassUsing<T>(T t) where T : class, IDisposable
|
|
{
|
|
using (t)
|
|
{
|
|
Console.WriteLine(t);
|
|
}
|
|
}
|
|
|
|
public void GenericNullableUsing<T>(T? t) where T : struct, IDisposable
|
|
{
|
|
using (t)
|
|
{
|
|
Console.WriteLine(t);
|
|
}
|
|
}
|
|
|
|
#if CS80
|
|
public void UsingRefStruct1(UsingRefStruct s)
|
|
{
|
|
using (s)
|
|
{
|
|
Console.WriteLine(s.i);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
public static void Issue3385()
|
|
{
|
|
#if ROSLYN3
|
|
#if CS71
|
|
using (TypeA_Issue3385 a = default)
|
|
#else
|
|
using (TypeA_Issue3385 a = default(TypeA_Issue3385))
|
|
#endif
|
|
#else
|
|
#if CS71
|
|
using (TypeA_Issue3385 typeA_Issue = default)
|
|
#else
|
|
using (TypeA_Issue3385 typeA_Issue = default(TypeA_Issue3385))
|
|
#endif
|
|
#endif
|
|
{
|
|
#if ROSLYN3
|
|
Empty(a);
|
|
#else
|
|
Empty(typeA_Issue);
|
|
#endif
|
|
}
|
|
}
|
|
|
|
private static void Empty(TypeB_Issue3385 b)
|
|
{
|
|
}
|
|
}
|
|
}
|