Skip to content

Commit 3603e70

Browse files
committed
C#: fix translation of enum.to_i
Fixes kaitai-io/kaitai_struct#802 Fixes the following tests in CI: ``` * ➖ 1 tests removed: * EnumToIClassBorder2 -> {"status"=>"format_build_failed", "elapsed"=>0} * 🟢 3 tests fixed: * EnumToI: {"status"=>"format_build_failed"} -> {"status"=>"passed"} * EnumToIClassBorder1: {"status"=>"format_build_failed"} -> {"status"=>"passed"} * EnumToIInvalid: {"status"=>"format_build_failed"} -> {"status"=>"passed"} ```
1 parent 12dbc32 commit 3603e70

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

shared/src/main/scala/io/kaitai/struct/translators/CSharpTranslator.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,16 @@ class CSharpTranslator(provider: TypeProvider, importList: ImportList) extends B
9797
s"Convert.ToInt64(${translate(s)}, ${translate(base)})"
9898
}
9999
override def enumToInt(v: expr, et: EnumType): String =
100-
translate(v)
100+
// Always casting to `int` works fine at the time of writing this, because the
101+
// enums we generate for C# are `int`-based (we generate `public enum $enumClass
102+
// { ... }`, see `CSharpCompiler.enumDeclaration`, and according to
103+
// <https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/enum>:
104+
// "By default, the associated constant values of enum members are of type
105+
// `int`").
106+
//
107+
// However, once we start generating enums with underlying types other than
108+
// `int`, we will have to change this.
109+
s"((int) ${translate(v, METHOD_PRECEDENCE)})"
101110
override def floatToInt(v: expr): String =
102111
s"(long) (${translate(v)})"
103112
override def intToStr(i: expr): String =

0 commit comments

Comments
 (0)