Skip to content

Commit

Permalink
CSharpExpressionPrinter: Wrap expression in parenthesis
Browse files Browse the repository at this point in the history
Wrap default parameter expressions in parentheses to ensure whole expressions are casted instead of only the first operand.
  • Loading branch information
trungnt2910 committed Jul 9, 2023
1 parent 9923d79 commit 26e4984
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs
Expand Up @@ -27,11 +27,11 @@ public string VisitParameter(Parameter parameter)
if (desugared.IsPrimitiveType() &&
(parameter.DefaultArgument.Declaration != null ||
parameter.DefaultArgument.Class == StatementClass.BinaryOperator))
return $"({desugared.Visit(typePrinter)}) {expression}";
return $"({desugared.Visit(typePrinter)}) ({expression})";
var finalType = (desugared.GetFinalPointee() ?? desugared).Desugar();
if (finalType.TryGetClass(out var @class) && @class.IsInterface)
return $@"({@class.Visit(typePrinter)}) ({
@class.OriginalClass.Visit(typePrinter)}) {expression}";
@class.OriginalClass.Visit(typePrinter)}) ({expression})";
return expression;
}

Expand Down Expand Up @@ -88,4 +88,4 @@ public string ToString(Type type)

private readonly TypePrinter typePrinter;
}
}
}

0 comments on commit 26e4984

Please sign in to comment.