Skip to content

Commit 43626c3

Browse files
committed
Fix parameterless constructors not being generated for structs
1 parent 03874e7 commit 43626c3

File tree

7 files changed

+43
-11
lines changed

7 files changed

+43
-11
lines changed

src/Generator/AST/Utils.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static bool CheckIgnoreMethod(Method method)
3030
var isEmptyCtor = method.IsConstructor && method.Parameters.Count == 0;
3131

3232
var @class = method.Namespace as Class;
33-
if (@class != null && @class.IsValueType && isEmptyCtor)
33+
if (@class != null && @class.IsValueType && isEmptyCtor && method.IsDefaulted)
3434
return true;
3535

3636
if (method.IsDestructor)

src/Generator/Generators/CLI/CLIHeaders.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,8 @@ public void GenerateFunction(Function function)
798798
public static bool FunctionIgnored(Function function)
799799
{
800800
return TypeIgnored(function.ReturnType.Type) ||
801-
function.Parameters.Any(param => TypeIgnored(param.Type));
801+
function.Parameters.Any(param => TypeIgnored(param.Type)) ||
802+
function is Method { IsConstructor: true, Parameters: { Count: 0 }, Namespace: Class { IsValueType: true } };
802803
}
803804

804805
public static bool TypeIgnored(CppSharp.AST.Type type)

src/Generator/Generators/CSharp/CSharpSources.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2678,7 +2678,7 @@ public void GenerateMethod(Method method, Class @class)
26782678
if (hasBase && !@class.IsValueType)
26792679
WriteLineIndent($": this({(method != null ? "(void*) null" : "native")})");
26802680

2681-
if (@class.IsValueType)
2681+
if (@class.IsValueType && method.Parameters.Count > 0)
26822682
WriteLineIndent(": this()");
26832683
}
26842684

src/Generator/Types/Std/Stdlib.CSharp.cs

-3
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,6 @@ public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
331331
string var;
332332
if (ctx.ReturnVarName.LastIndexOf('.') > ctx.ReturnVarName.LastIndexOf("->"))
333333
{
334-
ctx.Before.WriteLine("throw new NotImplementedException(\"This method cannot currently be called because it would " +
335-
"leave the object in an invalid state. See https://github.com/mono/CppSharp/issues/1777\");");
336-
337334
var = Generator.GeneratedIdentifier(ctx.ArgName);
338335
ctx.Before.WriteLine($"fixed (void* {var} = &{ctx.ReturnVarName})");
339336
ctx.Before.WriteOpenBraceAndIndent();

tests/dotnet/CSharp/CSharp.Tests.cs

+27-2
Original file line numberDiff line numberDiff line change
@@ -2025,8 +2025,7 @@ public void TestOptionalIntPtr()
20252025
}
20262026

20272027
[Test]
2028-
[Ignore("https://github.com/mono/CppSharp/issues/1730")]
2029-
public void TestString()
2028+
public void TestValueTypeStringMember()
20302029
{
20312030
var test = new CSharp.ValueType();
20322031
Assert.AreEqual(string.Empty, test.StringMember);
@@ -2036,4 +2035,30 @@ public void TestString()
20362035
Assert.AreEqual("test", test.StringMember);
20372036
Assert.AreEqual("test2", test.CharPtrMember);
20382037
}
2038+
2039+
[Test]
2040+
[Ignore("https://github.com/mono/CppSharp/issues/1777")]
2041+
public void TestValueTypeStringMemberDefaulted()
2042+
{
2043+
CSharp.ValueType test = default;
2044+
Assert.AreEqual(string.Empty, test.StringMember);
2045+
Assert.AreEqual(null, test.CharPtrMember);
2046+
test.StringMember = "test";
2047+
test.CharPtrMember = "test2";
2048+
Assert.AreEqual("test", test.StringMember);
2049+
Assert.AreEqual("test2", test.CharPtrMember);
2050+
}
2051+
2052+
[Test]
2053+
[Ignore("https://github.com/mono/CppSharp/issues/1777")]
2054+
public void TestValueTypeStringMemberDefaultedCtor()
2055+
{
2056+
var test = new CSharp.ValueTypeNoCtor();
2057+
Assert.AreEqual(string.Empty, test.StringMember);
2058+
Assert.AreEqual(null, test.CharPtrMember);
2059+
test.StringMember = "test";
2060+
test.CharPtrMember = "test2";
2061+
Assert.AreEqual("test", test.StringMember);
2062+
Assert.AreEqual("test2", test.CharPtrMember);
2063+
}
20392064
}

tests/dotnet/CSharp/CSharp.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -1644,9 +1644,14 @@ inline void DLL_API InstantiateOptionalTemplate(Optional<unsigned int>, Optional
16441644

16451645
CS_VALUE_TYPE class DLL_API ValueType {
16461646
public:
1647-
// Parameterless ctors are currently not generated for value types.
1648-
ValueType(int) { }
1647+
ValueType() { }
16491648

16501649
std::string string_member;
16511650
const char* char_ptr_member;
16521651
};
1652+
1653+
CS_VALUE_TYPE class DLL_API ValueTypeNoCtor {
1654+
public:
1655+
std::string string_member;
1656+
const char* char_ptr_member;
1657+
};
+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
<Project Sdk="Microsoft.NET.Sdk" />
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<LangVersion>10.0</LangVersion>
4+
</PropertyGroup>
5+
</Project>

0 commit comments

Comments
 (0)