Compiler: Do not cast enum value to i32 if not specified. Fixes #194 #2703
Conversation
handle #194 |
The error message seems misleading: |
Agree with @lbguilherme, maybe say "enum value must be an Int32" |
you're totally right, updated the PR with the improved exception message.. thank you @lbguilherme and @asterite |
@asterite build passed in case you want to merge it |
if default_value.is_a?(Crystal::NumberLiteral) | ||
enum_base_kind = enum_base_type.kind | ||
node_kind = return_enum_kind(default_value) | ||
if (enum_base_kind == :i32) && (enum_base_kind != node_kind) |
asterite
Jun 1, 2016
Member
How about we remove return_enum_kind
and just use default_value.kind
here?
How about we remove return_enum_kind
and just use default_value.kind
here?
fernandes
Jun 1, 2016
Author
Contributor
I tried doing this, but at compile time, the compiler expects default_value
as a Crystal::ASTNode and #kind
is specific to Crystal::NumberLiteral, so can't call it here,
I created return_enum_kind
with signature expecting (node : NumberLiteral)
then I can call #kind
inside.
I don't know if I can handle another way, if you can point me to a solution I change immediately
I tried doing this, but at compile time, the compiler expects default_value
as a Crystal::ASTNode and #kind
is specific to Crystal::NumberLiteral, so can't call it here,
I created return_enum_kind
with signature expecting (node : NumberLiteral)
then I can call #kind
inside.
I don't know if I can handle another way, if you can point me to a solution I change immediately
asterite
Jun 1, 2016
Member
I just tried the change it and it works fine. The compiler knows default_value
is a NumberLiteral because it's inside if default_value.is_a?(Crystal::NumberLiteral)
. Maybe you didn't have that before?
In any case, doing it with a return_enum_kind
should also give an error because you are not providing overloads for all ASTNode types (which means that the compiler knows that default_value
can only be a NumberLiteral
).
I just tried the change it and it works fine. The compiler knows default_value
is a NumberLiteral because it's inside if default_value.is_a?(Crystal::NumberLiteral)
. Maybe you didn't have that before?
In any case, doing it with a return_enum_kind
should also give an error because you are not providing overloads for all ASTNode types (which means that the compiler knows that default_value
can only be a NumberLiteral
).
fernandes
Jun 1, 2016
Author
Contributor
maybe was my mistake... now I run the specs and it's passing, let me rebuild the compiler and run all spec to make sure build won't break, I'm pushing in a few minutes 👍
maybe was my mistake... now I run the specs and it's passing, let me rebuild the compiler and run all spec to make sure build won't break, I'm pushing in a few minutes
@fernandes Thank you! I just made a comment. After that, I'll merge. |
@asterite my pleasure |
If enum base type is not specified, default is assumed (i32), and if any enum value is specified with a different type it should be casted to i32, but it's an undesired behavior, thats why now an exception is raised to alert developer.
@asterite I think now everything is fine :) |
Perfect, thank you! |
If enum base type is not specified, default is assumed (i32), and if any
enum value is specified with a different type it should be casted to
i32, but it's an undesired behavior, thats why now an exception is
raised to alert developer.