Skip to content

Commit

Permalink
Merge branch 'master' into toggle-smooth-scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
Hertzole authored Feb 19, 2025
2 parents 1379d9f + fb2561a commit dcb37f7
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 56 deletions.
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="Microsoft.NETCore.ILAsm" Version="9.0.0" />
<PackageVersion Include="Microsoft.NETCore.ILDAsm" Version="9.0.0" />
<PackageVersion Include="Microsoft.Sbom.Targets" Version="3.0.1" />
<PackageVersion Include="Microsoft.Sbom.Targets" Version="3.0.1" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.135" />
<PackageVersion Include="Mono.Cecil" Version="0.11.6" />
Expand All @@ -46,7 +46,7 @@
<PackageVersion Include="System.Resources.Extensions" Version="9.0.1" />
<PackageVersion Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.0" />
<PackageVersion Include="TomsToolbox.Composition.MicrosoftExtensions" Version="2.22.0" />
<PackageVersion Include="TomsToolbox.Wpf.Composition" Version="2.20.0" />
<PackageVersion Include="TomsToolbox.Wpf.Composition" Version="2.22.0" />
<PackageVersion Include="TomsToolbox.Wpf.Composition.AttributedModel" Version="2.22.0" />
<PackageVersion Include="TomsToolbox.Wpf.Styles" Version="2.22.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.3" />
Expand Down
45 changes: 44 additions & 1 deletion ICSharpCode.Decompiler.Tests/Helpers/Tester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection.PortableExecutable;
Expand Down Expand Up @@ -330,6 +329,45 @@ static string GetTargetFrameworkAttributeSnippetFile()
return tempFile;
}

const string nonEmbeddedAttributesSnippet = @"
using System;
#if !NET60
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
internal sealed class CompilerFeatureRequiredAttribute : Attribute
{
public CompilerFeatureRequiredAttribute(string featureName)
{
}
}
internal class IsExternalInit
{
}
#endif
#if !NET70
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
internal sealed class RequiredMemberAttribute : Attribute
{
}
#endif
#if !NET60
}
#endif
";

static readonly Lazy<string> nonEmbeddedAttributesSnippetFile = new Lazy<string>(GetNonEmbeddedAttributesSnippetFile);

static string GetNonEmbeddedAttributesSnippetFile()
{
// Note: this leaks a temporary file, we're not attempting to delete it, because it is only one.
var tempFile = Path.GetTempFileName();
File.WriteAllText(tempFile, nonEmbeddedAttributesSnippet);
return tempFile;
}

public static List<string> GetPreprocessorSymbols(CompilerOptions flags)
{
var preprocessorSymbols = new List<string>();
Expand Down Expand Up @@ -419,6 +457,11 @@ public static async Task<CompilerResults> CompileCSharp(string sourceFileName, C
sourceFileNames.Add(targetFrameworkAttributeSnippetFile.Value);
}

if (targetNet40)
{
sourceFileNames.Add(nonEmbeddedAttributesSnippetFile.Value);
}

var preprocessorSymbols = GetPreprocessorSymbols(flags);

if ((flags & CompilerOptions.UseMcsMask) == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
<Compile Include="ProjectDecompiler\TargetFrameworkTests.cs" />
<Compile Include="TestAssemblyResolver.cs" />
<Compile Include="TestCases\ILPretty\MonoFixed.cs" />
<Compile Include="TestCases\Pretty\Comparisons.cs" />
<None Include="TestCases\VBPretty\VBAutomaticEvents.vb" />
<Compile Include="TestCases\VBPretty\VBAutomaticEvents.cs" />
<Compile Include="TestCases\VBPretty\VBNonGenericForEach.cs" />
Expand All @@ -151,7 +152,7 @@
<None Include="TestCases\Pretty\CovariantReturns.cs" />
<Compile Include="TestCases\VBPretty\VBPropertiesTest.cs" />
<None Include="TestCases\ILPretty\Issue2260SwitchString.cs" />
<None Include="TestCases\Pretty\Records.cs" />
<Compile Include="TestCases\Pretty\Records.cs" />
<Compile Include="TestCases\VBPretty\Issue2192.cs" />
<Compile Include="Util\FileUtilityTests.cs" />
<Compile Include="TestCases\Pretty\FunctionPointers.cs" />
Expand Down
6 changes: 6 additions & 0 deletions ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,12 @@ public async Task OptionalArguments([ValueSource(nameof(defaultOptions))] Compil
await RunForLibrary(cscOptions: cscOptions);
}

[Test]
public async Task Comparisons([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{
await RunForLibrary(cscOptions: cscOptions);
}

[Test]
public async Task ConstantsTests([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{
Expand Down
18 changes: 18 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/Comparisons.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
public class Comparisons
{
private class A
{
}

private class B
{
}

private bool CompareUnrelatedNeedsCast(A a, B b)
{
return (object)a == b;
}
}
}
24 changes: 24 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/InitializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,19 @@ public interface IData
{
int Property { get; set; }
}

#if CS90
public class Issue3392Type
{
public bool Flag { get; init; }
public List<int> List { get; } = new List<int>();

public Issue3392Type(object x)
{

}
}
#endif
#endregion

private S s1;
Expand Down Expand Up @@ -1010,6 +1023,17 @@ public OtherItem2 Issue1345b()
otherItem.Data2.Nullable = 3m;
return otherItem;
}

#if CS90
public Issue3392Type Issue3392(Issue3392Type x)
{
x = new Issue3392Type(null) {
Flag = false
};
x.List.AddRange(Enumerable.Range(0, 10));
return x;
}
#endif
#if CS60
public OtherItem2 Issue1345c()
{
Expand Down
22 changes: 22 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ public enum MyEnum
B
}

internal class OptionalArgumentTest
{
private static void Test()
{
Test2();
Test3();
Test4();
}

private static void Test2(int a = 0)
{
}

private static void Test3(int a = 0, int? b = null)
{
}

private static void Test4(int? b = null, int a = 0)
{
}
}

public OptionalArguments(string name, int a = 5)
{

Expand Down
28 changes: 2 additions & 26 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/Records.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public record Pair<A, B>
public record PairWithPrimaryCtor<A, B>(A First, B Second);

public record PrimaryCtor(int A, string B);
public record PrimaryCtorWithAttribute([RecordTest("param")] [property: RecordTest("property")][field: RecordTest("field")] int a);
public record PrimaryCtorWithAttribute([RecordTest("param")][property: RecordTest("property")][field: RecordTest("field")] int a);
public record PrimaryCtorWithField(int A, string B)
{
public double C = 1.0;
Expand Down Expand Up @@ -169,7 +169,7 @@ public record struct Pair<A, B>
public record struct PairWithPrimaryCtor<A, B>(A First, B Second);

public record struct PrimaryCtor(int A, string B);
public record struct PrimaryCtorWithAttribute([RecordTest("param")] [property: RecordTest("property")][field: RecordTest("field")] int a);
public record struct PrimaryCtorWithAttribute([RecordTest("param")][property: RecordTest("property")][field: RecordTest("field")] int a);
public record struct PrimaryCtorWithField(int A, string B)
{
public double C = 1.0;
Expand Down Expand Up @@ -242,27 +242,3 @@ public record struct WithRequiredMembers
}
#endif
}
#if !NET60
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
internal sealed class CompilerFeatureRequiredAttribute : Attribute
{
public CompilerFeatureRequiredAttribute(string featureName)
{
}
}

internal class IsExternalInit
{
}
#endif
#if !NET70
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
internal sealed class RequiredMemberAttribute : Attribute
{
}
#endif
#if !NET60
}
#endif
18 changes: 1 addition & 17 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/Structs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,4 @@ public struct StructWithRequiredMembers
public required string LastName { get; set; }
}
#endif
}

namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
internal sealed class CompilerFeatureRequiredAttribute : Attribute
{
public CompilerFeatureRequiredAttribute(string featureName)
{
}
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
internal sealed class RequiredMemberAttribute : Attribute
{
}
}
}
5 changes: 4 additions & 1 deletion ICSharpCode.Decompiler/CSharp/CallBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,10 @@ bool CheckArgument(out int len, out IType t)

bool IsOptionalArgument(IParameter parameter, TranslatedExpression arg)
{
if (!parameter.IsOptional || !arg.ResolveResult.IsCompileTimeConstant)
if (!parameter.IsOptional)
return false;

if (!arg.ResolveResult.IsCompileTimeConstant && arg.ResolveResult is not ConversionResolveResult { Conversion.IsNullLiteralConversion: true })
return false;
if (parameter.GetAttributes().Any(a => a.AttributeType.IsKnownType(KnownAttribute.CallerMemberName)
|| a.AttributeType.IsKnownType(KnownAttribute.CallerFilePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1530,8 +1530,8 @@ public virtual void VisitAttributeSection(AttributeSection attributeSection)
WriteToken(Roles.RBracket);
switch (attributeSection.Parent)
{
case ParameterDeclaration _:
if (attributeSection.NextSibling is AttributeSection)
case ParameterDeclaration pd:
if (pd.Attributes.Last() != attributeSection)
Space(policy.SpaceBetweenParameterAttributeSections);
else
Space();
Expand Down
5 changes: 4 additions & 1 deletion ICSharpCode.Decompiler/CSharp/Resolver/CSharpResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,10 @@ public ResolveResult ResolveBinaryOperator(BinaryOperatorType op, ResolveResult
}
if (op == BinaryOperatorType.Equality || op == BinaryOperatorType.InEquality)
{
if (lhsType.IsReferenceType == true && rhsType.IsReferenceType == true)
if (lhsType.IsReferenceType == true && rhsType.IsReferenceType == true
&& (conversions.IdentityConversion(lhsType, rhsType)
|| conversions.ExplicitConversion(lhsType, rhsType).IsReferenceConversion
|| conversions.ExplicitConversion(rhsType, lhsType).IsReferenceConversion))
{
// If it's a reference comparison
if (op == BinaryOperatorType.Equality)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,27 @@ public class RemoveEmbeddedAttributes : DepthFirstAstVisitor, IAstTransform
"Microsoft.CodeAnalysis.EmbeddedAttribute",
};

internal static readonly HashSet<string> nonEmbeddedAttributeNames = new HashSet<string>() {
// non-embedded attributes, but we still want to remove them
"System.Runtime.CompilerServices.CompilerFeatureRequiredAttribute",
"System.Runtime.CompilerServices.RequiredMemberAttribute",
"System.Runtime.CompilerServices.IsExternalInit",
};

public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration)
{
var typeDefinition = typeDeclaration.GetSymbol() as ITypeDefinition;
if (typeDefinition == null || !attributeNames.Contains(typeDefinition.FullName))
if (typeDefinition == null)
return;
if (!typeDefinition.HasAttribute(KnownAttribute.Embedded))

if (attributeNames.Contains(typeDefinition.FullName))
{
if (!typeDefinition.HasAttribute(KnownAttribute.Embedded))
return;
}
else if (!nonEmbeddedAttributeNames.Contains(typeDefinition.FullName))
return;

if (typeDeclaration.Parent is NamespaceDeclaration ns && ns.Members.Count == 1)
ns.Remove();
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ void IStatementTransform.Run(Block block, int pos, StatementTransformContext con
return;
}
int initializerItemsCount = 0;
bool initializerContainsInitOnlyItems = false;
possibleIndexVariables.Clear();
currentPath.Clear();
isCollection = false;
Expand All @@ -113,13 +114,13 @@ void IStatementTransform.Run(Block block, int pos, StatementTransformContext con
// if the method is a setter we're dealing with an object initializer
// if the method is named Add and has at least 2 arguments we're dealing with a collection/dictionary initializer
while (pos + initializerItemsCount + 1 < block.Instructions.Count
&& IsPartOfInitializer(block.Instructions, pos + initializerItemsCount + 1, v, instType, ref blockKind, context))
&& IsPartOfInitializer(block.Instructions, pos + initializerItemsCount + 1, v, instType, ref blockKind, ref initializerContainsInitOnlyItems, context))
{
initializerItemsCount++;
}
// Do not convert the statements into an initializer if there's an incompatible usage of the initializer variable
// directly after the possible initializer.
if (IsMethodCallOnVariable(block.Instructions[pos + initializerItemsCount + 1], v))
if (!initializerContainsInitOnlyItems && IsMethodCallOnVariable(block.Instructions[pos + initializerItemsCount + 1], v))
return;
// Calculate the correct number of statements inside the initializer:
// All index variables that were used in the initializer have Index set to -1.
Expand Down Expand Up @@ -200,7 +201,7 @@ bool IsMethodCallOnVariable(ILInstruction inst, ILVariable variable)
bool isCollection;
readonly Stack<HashSet<AccessPathElement>> pathStack = new Stack<HashSet<AccessPathElement>>();

bool IsPartOfInitializer(InstructionCollection<ILInstruction> instructions, int pos, ILVariable target, IType rootType, ref BlockKind blockKind, StatementTransformContext context)
bool IsPartOfInitializer(InstructionCollection<ILInstruction> instructions, int pos, ILVariable target, IType rootType, ref BlockKind blockKind, ref bool initializerContainsInitOnlyItems, StatementTransformContext context)
{
// Include any stores to local variables that are single-assigned and do not reference the initializer-variable
// in the list of possible index variables.
Expand Down Expand Up @@ -255,6 +256,7 @@ bool IsPartOfInitializer(InstructionCollection<ILInstruction> instructions, int
return false;
if (blockKind != BlockKind.ObjectInitializer && blockKind != BlockKind.WithInitializer)
blockKind = BlockKind.ObjectInitializer;
initializerContainsInitOnlyItems |= lastElement.Member is IProperty { Setter.IsInitOnly: true };
return true;
default:
return false;
Expand Down
2 changes: 2 additions & 0 deletions ILSpy/AssemblyTree/AssemblyTreeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,8 @@ private void TreeView_SelectionChanged()
}
else
{
// ensure that we are only connected once to the event, else we might get multiple notifications
ContextMenuProvider.ContextMenuClosed -= ContextMenuClosed;
ContextMenuProvider.ContextMenuClosed += ContextMenuClosed;
}
}
Expand Down
1 change: 1 addition & 0 deletions ILSpy/ILSpy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<RollForward>major</RollForward>
<RuntimeIdentifiers>win-x64;win-arm64</RuntimeIdentifiers>
<GenerateAssemblyInfo>False</GenerateAssemblyInfo>
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
Expand Down

0 comments on commit dcb37f7

Please sign in to comment.