Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita5afda8

Browse files
authored
Fix parameterless constructors not being generated for structs (#1783)
* Fix parameterless constructors not being generated for structs* Fix implicit non-trivial default ctor* Adjust `Ignore` linked issue
1 parentb16e809 commita5afda8

File tree

7 files changed

+42
-11
lines changed

7 files changed

+42
-11
lines changed

‎src/Generator/AST/Utils.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static bool CheckIgnoreMethod(Method method)
3030
varisEmptyCtor=method.IsConstructor&&method.Parameters.Count==0;
3131

3232
var@class=method.NamespaceasClass;
33-
if(@class!=null&&@class.IsValueType&&isEmptyCtor)
33+
if(@class!=null&&@class.IsValueType&&isEmptyCtor&&!@class.HasNonTrivialDefaultConstructor)
3434
returntrue;
3535

3636
if(method.IsDestructor)

‎src/Generator/Generators/CLI/CLIHeaders.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,8 @@ public void GenerateFunction(Function function)
798798
publicstaticboolFunctionIgnored(Functionfunction)
799799
{
800800
returnTypeIgnored(function.ReturnType.Type)||
801-
function.Parameters.Any(param=>TypeIgnored(param.Type));
801+
function.Parameters.Any(param=>TypeIgnored(param.Type))||
802+
functionisMethod{IsConstructor:true,Parameters:{Count:0},Namespace:Class{IsValueType:true}};
802803
}
803804

804805
publicstaticboolTypeIgnored(CppSharp.AST.Typetype)

‎src/Generator/Generators/CSharp/CSharpSources.cs‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,6 @@ public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
331331
stringvar;
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‎

Lines changed: 26 additions & 2 deletions
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-
publicvoidTestString()
2028+
publicvoidTestValueTypeStringMember()
20302029
{
20312030
vartest=newCSharp.ValueType();
20322031
Assert.AreEqual(string.Empty,test.StringMember);
@@ -2036,4 +2035,29 @@ 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/1786")]
2041+
publicvoidTestValueTypeStringMemberDefaulted()
2042+
{
2043+
CSharp.ValueTypetest=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+
publicvoidTestValueTypeStringMemberDefaultedCtor()
2054+
{
2055+
vartest=newCSharp.ValueTypeNoCtor();
2056+
Assert.AreEqual(string.Empty,test.StringMember);
2057+
Assert.AreEqual(null,test.CharPtrMember);
2058+
test.StringMember="test";
2059+
test.CharPtrMember="test2";
2060+
Assert.AreEqual("test",test.StringMember);
2061+
Assert.AreEqual("test2",test.CharPtrMember);
2062+
}
20392063
}

‎tests/dotnet/CSharp/CSharp.h‎

Lines changed: 7 additions & 2 deletions
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_TYPEclassDLL_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
constchar* char_ptr_member;
16521651
};
1652+
1653+
CS_VALUE_TYPEclassDLL_API ValueTypeNoCtor {
1654+
public:
1655+
std::string string_member;
1656+
constchar* char_ptr_member;
1657+
};
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
<ProjectSdk="Microsoft.NET.Sdk" />
1+
<ProjectSdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<LangVersion>11.0</LangVersion>
4+
</PropertyGroup>
5+
</Project>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp