Skip to content

Commit

Permalink
Fix crash when using allomorphs as enum values
Browse files Browse the repository at this point in the history
Mixing both NumericEnumeration and StringyEnumeration causes a
conflict with Str method resolution.

Fix by adding NumericStringyEnumeration role and using it for
allomorphs.
  • Loading branch information
zoffixznet committed Dec 12, 2017
1 parent 6530175 commit fc52143
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Perl6/Actions.nqp
Expand Up @@ -4587,12 +4587,19 @@ class Perl6::Actions is HLL::Actions does STDActions {
$type_obj := $*W.pkg_create_mo($/, $*W.resolve_mo($/, 'enum'), :$name, :$base_type);
# Add roles (which will provide the enum-related methods).
$*W.apply_trait($/, '&trait_mod:<does>', $type_obj, $*W.find_symbol(['Enumeration']));

if istype($type_obj, $*W.find_symbol(['Numeric'])) {
$*W.apply_trait($/, '&trait_mod:<does>', $type_obj, $*W.find_symbol(['NumericEnumeration']));
$*W.apply_trait($/, '&trait_mod:<does>', $type_obj, $*W.find_symbol([
istype($type_obj, $*W.find_symbol(['Stringy'])) # handle allomorphs
?? 'NumericStringyEnumeration'
!! 'NumericEnumeration'
]));
}
if istype($type_obj, $*W.find_symbol(['Stringy'])) {
$*W.apply_trait($/, '&trait_mod:<does>', $type_obj, $*W.find_symbol(['StringyEnumeration']));
elsif istype($type_obj, $*W.find_symbol(['Stringy'])) {
$*W.apply_trait($/, '&trait_mod:<does>', $type_obj,
$*W.find_symbol(['StringyEnumeration']));
}

# Apply traits, compose and install package.
$*W.apply_traits($<trait>, $type_obj);
$*W.pkg_compose($/, $type_obj);
Expand Down
5 changes: 5 additions & 0 deletions src/core/Enumeration.pm
Expand Up @@ -81,6 +81,11 @@ my role StringyEnumeration {
self.value
}
}
my role NumericStringyEnumeration {
multi method Str(::?CLASS:D:) {
self.key
}
}

sub ENUM_VALUES(*@args) {
my Mu $prev = -1;
Expand Down

0 comments on commit fc52143

Please sign in to comment.