Skip to content

Commit

Permalink
Fix #2921: struct type definition contains duplicate default construc…
Browse files Browse the repository at this point in the history
…tor.
  • Loading branch information
siegfriedpammer committed Apr 4, 2023
1 parent 29ccf6c commit 8b0c7fb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
10 changes: 10 additions & 0 deletions ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,16 @@ public void DefaultConstructorAddedToStruct()
Assert.IsTrue(ctors.All(c => c.Accessibility == Accessibility.Public));
}

[Test]
public void NoDefaultConstructorAddedToStruct()
{
var ctors = compilation.FindType(typeof(MyStructWithDefaultCtor)).GetConstructors();
Assert.AreEqual(1, ctors.Count());
Assert.IsFalse(ctors.Any(c => c.IsStatic));
Assert.IsTrue(ctors.All(c => c.ReturnType.Kind == TypeKind.Void));
Assert.IsTrue(ctors.All(c => c.Accessibility == Accessibility.Public));
}

[Test]
public void NoDefaultConstructorAddedToClass()
{
Expand Down
5 changes: 5 additions & 0 deletions ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ public struct MyStructWithCtor
public MyStructWithCtor(int a) { }
}

public struct MyStructWithDefaultCtor
{
public MyStructWithDefaultCtor() { }
}

public class MyClassWithCtor
{
private MyClassWithCtor(int a) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,23 @@ public IEnumerable<IMethod> Methods {
var methodsCollection = metadata.GetTypeDefinition(handle).GetMethods();
var methodsList = new List<IMethod>(methodsCollection.Count);
var methodSemantics = module.PEFile.MethodSemanticsLookup;
bool hasDefaultCtor = false;
foreach (MethodDefinitionHandle h in methodsCollection)
{
var md = metadata.GetMethodDefinition(h);
if (methodSemantics.GetSemantics(h).Item2 == 0 && module.IsVisible(md.Attributes))
{
methodsList.Add(module.GetDefinition(h));
IMethod method = module.GetDefinition(h);
if (method.SymbolKind == SymbolKind.Constructor && !method.IsStatic && method.Parameters.Count == 0)
{
hasDefaultCtor = true;
}
methodsList.Add(method);
}
}
if (this.Kind == TypeKind.Struct || this.Kind == TypeKind.Enum)
if (!hasDefaultCtor && (this.Kind == TypeKind.Struct || this.Kind == TypeKind.Enum))
{
methodsList.Add(FakeMethod.CreateDummyConstructor(Compilation, this, IsAbstract ? Accessibility.Protected : Accessibility.Public));
methodsList.Add(FakeMethod.CreateDummyConstructor(Compilation, this, Accessibility.Public));
}
if ((module.TypeSystemOptions & TypeSystemOptions.Uncached) != 0)
return methodsList;
Expand Down

0 comments on commit 8b0c7fb

Please sign in to comment.