Skip to content

Commit 6dfabb6

Browse files
committed
Generate valid C# for specializations in default args
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 8a75cee commit 6dfabb6

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,19 @@ public string VisitParameter(Parameter parameter)
3131
return $"({desugared.Visit(typePrinter)}) {expression}";
3232
var finalType = (desugared.GetFinalPointee() ?? desugared).Desugar();
3333
if (finalType.TryGetClass(out var @class) && @class.IsInterface)
34-
return $@"({@class.Visit(typePrinter)}) ({
35-
@class.OriginalClass.Visit(typePrinter)}) {expression}";
34+
{
35+
string cast;
36+
if (parameter.DefaultArgument.Declaration is Method method &&
37+
method.IsConstructor && method.Namespace == @class.OriginalClass)
38+
{
39+
cast = string.Empty;
40+
}
41+
else
42+
{
43+
cast = $"({@class.OriginalClass.Visit(typePrinter)}) ";
44+
}
45+
return cast + expression;
46+
}
3647
return expression;
3748
}
3849

src/Generator/Passes/ExpressionHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ private static bool CheckForDefaultPointer(BindingContext context, Type desugare
237237
var typePrinter = new CSharpTypePrinter(context);
238238
typePrinter.PushMarshalKind(MarshalKind.DefaultExpression);
239239

240-
var typePrinterResult = type.Visit(typePrinter).Type;
240+
var typePrinterResult = type.Visit(typePrinter);
241241

242242
TypeMap typeMap;
243243
if (context.TypeMaps.FindTypeMap(type, out typeMap))

tests/CSharp/CSharp.Tests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ public void TestDefaultArguments()
341341
methodsWithDefaultValues.DefaultWithCharFromInt();
342342
methodsWithDefaultValues.DefaultWithFreeConstantInNameSpace();
343343
methodsWithDefaultValues.DefaultWithStdNumericLimits(10, 5);
344+
methodsWithDefaultValues.DefaultWithSpecialization();
344345
methodsWithDefaultValues.DefaultWithParamNamedSameAsMethod(5);
345346
}
346347
}

tests/CSharp/CSharp.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,10 @@ void MethodsWithDefaultValues::defaultWithParamRequiringRename(_ClassWithLeading
818818
{
819819
}
820820

821+
void MethodsWithDefaultValues::defaultWithSpecialization(IndependentFields<int> specialization)
822+
{
823+
}
824+
821825
int MethodsWithDefaultValues::DefaultWithParamNamedSameAsMethod(int DefaultWithParamNamedSameAsMethod, const Foo& defaultArg)
822826
{
823827
return 1;

tests/CSharp/CSharp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ class DLL_API MethodsWithDefaultValues : public Quux
482482
void defaultWithFreeConstantInNameSpace(int c = HasFreeConstant::FREE_CONSTANT_IN_NAMESPACE);
483483
void defaultWithStdNumericLimits(double d = 1.0, int i = std::numeric_limits<double>::infinity());
484484
void defaultWithParamRequiringRename(_ClassWithLeadingUnderscore* ptr = nullptr);
485+
void defaultWithSpecialization(IndependentFields<int> specialization = IndependentFields<int>());
485486
int DefaultWithParamNamedSameAsMethod(int DefaultWithParamNamedSameAsMethod, const Foo& defaultArg = Foo());
486487
int getA();
487488
private:

0 commit comments

Comments
 (0)