Skip to content

Commit 07e9180

Browse files
Fix: Incorrect casting when assigning Enum to custom type
- Modified TypeConversionAnalyzer.cs to use EnumCastThenConversion for all non-enum types, ensuring that the necessary cast through the underlying type is generated (e.g. `(MyType)(int)MyEnum.Value1`). - Added unit test Issue1211_EnumToCustomTypeImplicitConversionAsync to ensure regressions don't occur. Co-authored-by: GrahamTheCoder <2490482+GrahamTheCoder@users.noreply.github.com>
1 parent fadac1b commit 07e9180

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

CodeConverter/CSharp/TypeConversionAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public TypeConversionKind AnalyzeConversion(VBSyntax.ExpressionSyntax vbNode, bo
165165
return TypeConversionKind.Identity;
166166
}
167167

168-
if (vbConvertedType.SpecialType == SpecialType.System_String) {
168+
if (!vbConvertedType.IsEnumType()) {
169169
return TypeConversionKind.EnumCastThenConversion;
170170
}
171171
return TypeConversionKind.Conversion;

Tests/CSharp/ExpressionTests/ExpressionTests.cs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2931,4 +2931,72 @@ public object Edit(bool flag2 = false, CrashEnum? crashEnum = default)
29312931
}
29322932
}");
29332933
}
2934-
}
2934+
2935+
[Fact]
2936+
public async Task Issue1211_EnumToCustomTypeImplicitConversionAsync()
2937+
{
2938+
await TestConversionVisualBasicToCSharpAsync(
2939+
@"Public Class Class1
2940+
Enum MyEnum
2941+
Value1 = 1
2942+
End Enum
2943+
2944+
Public Structure MyType
2945+
Public val As Integer
2946+
Public Shared Widening Operator CType(ByVal p As Integer) As MyType
2947+
Return New MyType With {.val = p}
2948+
End Operator
2949+
Public Shared Operator =(ByVal p As MyType, ByVal q As MyType) As Boolean
2950+
Return p.val = q.val
2951+
End Operator
2952+
Public Shared Operator <>(ByVal p As MyType, ByVal q As MyType) As Boolean
2953+
Return p.val <> q.val
2954+
End Operator
2955+
End Structure
2956+
2957+
Public Function Col(name As String) As MyType
2958+
Return Nothing
2959+
End Function
2960+
2961+
Public Sub Foo()
2962+
Dim b = Col(""foo"") = MyEnum.Value1
2963+
End Sub
2964+
End Class",
2965+
@"
2966+
public partial class Class1
2967+
{
2968+
public enum MyEnum
2969+
{
2970+
Value1 = 1
2971+
}
2972+
2973+
public partial struct MyType
2974+
{
2975+
public int val;
2976+
public static implicit operator MyType(int p)
2977+
{
2978+
return new MyType() { val = p };
2979+
}
2980+
public static bool operator ==(MyType p, MyType q)
2981+
{
2982+
return p.val == q.val;
2983+
}
2984+
public static bool operator !=(MyType p, MyType q)
2985+
{
2986+
return p.val != q.val;
2987+
}
2988+
}
2989+
2990+
public MyType Col(string name)
2991+
{
2992+
return default;
2993+
}
2994+
2995+
public void Foo()
2996+
{
2997+
bool b = Col(""foo"") == (MyType)(int)MyEnum.Value1;
2998+
}
2999+
}");
3000+
}
3001+
3002+
}

0 commit comments

Comments
 (0)