Skip to content

Commit 98a2830

Browse files
committed
Fix #68
1 parent 899f4ac commit 98a2830

10 files changed

Lines changed: 23 additions & 22 deletions

File tree

Library/CodeWriterVisitors/CSharpWriterVisitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected override void WriteUnary(ExpressionType nodeType, string operandPath,
100100
case ExpressionType.Convert:
101101
case ConvertChecked:
102102
case Unbox:
103-
if (!type.IsAssignableFrom(operand.Type)) {
103+
if (!operand.Type.HasImplicitConversionTo(type)) {
104104
Write($"({type.FriendlyName(language)})");
105105
}
106106
Parens(nodeType, operandPath, operand);
@@ -762,7 +762,7 @@ private int GetPrecedence(Expression node, Type? parentType = null) {
762762
(nodeType, type) = node;
763763
}
764764

765-
var renderConversion = parentType is null || !parentType.IsAssignableFrom(type);
765+
var renderConversion = parentType is null || !type.HasImplicitConversionTo(parentType);
766766
return nodeType switch
767767
{
768768
Conditional when type != typeof(void) => 15,

Library/CodeWriterVisitors/VBWriterVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ protected override void WriteUnary(ExpressionType nodeType, string operandPath,
146146
case ExpressionType.Convert:
147147
case ConvertChecked:
148148
case Unbox:
149-
if (type.IsAssignableFrom(operand.Type)) {
149+
if (operand.Type.HasImplicitConversionTo(type)) {
150150
WriteNode(operandPath, operand);
151151
} else if (conversionFunctions.TryGetValue(type, out var conversionFunction)) {
152152
Write(conversionFunction);

Library/DynamicLinqWriterVisitor.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,9 @@ protected override void WriteUnary(UnaryExpression expr) {
157157
case ExpressionType.Convert:
158158
case ConvertChecked:
159159
case Unbox:
160-
bool renderConversion =
161-
expr.Type != expr.Operand.Type && !(
162-
expr.Type.IsAssignableFrom(expr.Operand.Type) ||
163-
(expr.Type.IsNullable() && expr.Type.UnderlyingIfNullable() == expr.Operand.Type) ||
164-
HasImplicitConversions(expr.Operand.Type, expr.Type)
165-
);
160+
bool renderConversion =
161+
expr.Type != expr.Operand.Type &&
162+
!expr.Operand.Type.HasImplicitConversionTo(expr.Type);
166163
if (renderConversion) { Write($"{TypeName(expr.Type)}("); }
167164
WriteNode("Operand", expr.Operand);
168165
if (renderConversion) { Write(")"); }

Library/Library.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<ItemGroup>
3333
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
34-
<PackageReference Include="ZSpitz.Util" Version="0.1.67" />
34+
<PackageReference Include="ZSpitz.Util" Version="0.1.69" />
3535
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
3636
</ItemGroup>
3737

TestObjects/CSCompiler/Unary.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using static ExpressionTreeTestObjects.Categories;
44
using static ExpressionTreeTestObjects.Functions;
55

6-
76
namespace ExpressionTreeTestObjects {
87
partial class CSCompiler {
98
[TestObject(Unary)]

Tests.DataGenerator/Program.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ static void Main(string[] args) {
2424
var language = key == VisualBasic ? Language.VisualBasic : Language.CSharp;
2525

2626
const string dlinq = nameof(DynamicLinqTestObjects);
27-
var objects = Objects.Get()
28-
.Where(x => key == DynamicLinq ? x.source == dlinq : x.source != dlinq)
29-
.Where(x => !ordering.ContainsKey($"{x.source}.{x.name}"));
30-
3127
//var objects = Objects.Get()
3228
// .Where(x => key == DynamicLinq ? x.source == dlinq : x.source != dlinq)
33-
// .OrderBy(x => ordering.TryGetValue($"{x.source}.{x.name}", out var order) ? order : -1);
29+
// .Where(x => !ordering.ContainsKey($"{x.source}.{x.name}"));
30+
31+
var objects = Objects.Get()
32+
.Where(x => key == DynamicLinq ? x.source == dlinq : x.source != dlinq)
33+
.OrderBy(x => ordering.TryGetValue($"{x.source}.{x.name}", out var order) ? order : -1);
3434

3535
foreach (var (category, source, name, o) in objects) {
3636
var toWrite = o switch

Tests/Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<PrivateAssets>all</PrivateAssets>
2626
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2727
</PackageReference>
28-
<PackageReference Include="ZSpitz.Util" Version="0.1.67" />
28+
<PackageReference Include="ZSpitz.Util" Version="0.1.69" />
2929
</ItemGroup>
3030

3131
<ItemGroup>

Tests/expectedResults/csharp-testdata.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ s1 ?? s2
322322
---- FactoryMethods.ConstructConvert
323323
arr
324324
---- FactoryMethods.ConstructConvertChecked
325-
(float)5
325+
5
326326
---- FactoryMethods.ConstructConvertCheckedForReferenceType
327327
arr
328328
---- FactoryMethods.ConstructDecrement

Tests/expectedResults/visualbasic-testdata.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ If(s1, s2)
313313
---- FactoryMethods.ConstructConvert
314314
arr
315315
---- FactoryMethods.ConstructConvertChecked
316-
CSng(5)
316+
5
317317
---- FactoryMethods.ConstructConvertCheckedForReferenceType
318318
arr
319319
---- FactoryMethods.ConstructDecrement

_tests/Program.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,14 @@ static void Main() {
148148
// )
149149
//);
150150

151-
int i = 5;
152-
Expression<Func<string>> expr = () => i.ToString();
153-
Console.WriteLine(expr.ToString("Factory methods", "C#"));
151+
//int i = 5;
152+
//Expression<Func<string>> expr = () => i.ToString();
153+
//Console.WriteLine(expr.ToString("Factory methods", "C#"));
154+
155+
int i = 5;
156+
Expression<Func<long>> expr = () => (long)i;
157+
Console.WriteLine(expr.ToString("Textual tree", "C#"));
158+
Console.WriteLine(expr.ToString("C#"));
154159
}
155160

156161
static PropertyInfo debugView = typeof(Expression).GetProperty("DebugView", BindingFlags.NonPublic | BindingFlags.Instance);

0 commit comments

Comments
 (0)