Skip to content

Commit faff809

Browse files
committed
Fix HashCastNode to always return a RubyHash or raise a TypeError
1 parent 7ca8ede commit faff809

File tree

1 file changed

+9
-40
lines changed

1 file changed

+9
-40
lines changed

src/main/java/org/truffleruby/core/cast/HashCastNode.java

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
package org.truffleruby.core.cast;
1111

1212
import org.truffleruby.core.hash.RubyHash;
13-
import org.truffleruby.core.numeric.RubyBignum;
14-
import org.truffleruby.language.Nil;
1513
import org.truffleruby.language.RubyContextSourceNode;
16-
import org.truffleruby.language.RubyDynamicObject;
1714
import org.truffleruby.language.RubyGuards;
1815
import org.truffleruby.language.RubyNode;
1916
import org.truffleruby.language.control.RaiseException;
@@ -24,55 +21,27 @@
2421
import com.oracle.truffle.api.frame.VirtualFrame;
2522
import com.oracle.truffle.api.profiles.BranchProfile;
2623

27-
// TODO(CS): copy and paste of ArrayCastNode
2824
@NodeChild(value = "child", type = RubyNode.class)
2925
public abstract class HashCastNode extends RubyContextSourceNode {
3026

3127
protected abstract RubyNode getChild();
3228

3329
@Specialization
34-
protected Object cast(boolean value) {
35-
return nil;
36-
}
37-
38-
@Specialization
39-
protected Object cast(int value) {
40-
return nil;
41-
}
42-
43-
@Specialization
44-
protected Object cast(long value) {
45-
return nil;
46-
}
47-
48-
@Specialization
49-
protected Object cast(double value) {
50-
return nil;
51-
}
52-
53-
@Specialization
54-
protected Object castNil(Nil nil) {
55-
return nil;
56-
}
57-
58-
@Specialization
59-
protected Object castBignum(RubyBignum value) {
60-
return nil;
61-
}
62-
63-
@Specialization
64-
protected Object castHash(RubyHash hash) {
30+
protected RubyHash castHash(RubyHash hash) {
6531
return hash;
6632
}
6733

68-
@Specialization(guards = { "!isRubyBignum(object)", "!isRubyHash(object)" })
69-
protected Object cast(RubyDynamicObject object,
34+
@Specialization(guards = "!isRubyHash(object)")
35+
protected RubyHash cast(Object object,
7036
@Cached BranchProfile errorProfile,
7137
@Cached(parameters = "PRIVATE_RETURN_MISSING") DispatchNode toHashNode) {
72-
7338
final Object result = toHashNode.call(object, "to_hash");
39+
7440
if (result == DispatchNode.MISSING) {
75-
return nil;
41+
errorProfile.enter();
42+
throw new RaiseException(
43+
getContext(),
44+
coreExceptions().typeErrorNoImplicitConversion(object, "Hash", this));
7645
}
7746

7847
if (!RubyGuards.isRubyHash(result)) {
@@ -82,7 +51,7 @@ protected Object cast(RubyDynamicObject object,
8251
coreExceptions().typeErrorCantConvertTo(object, "Hash", "to_hash", result, this));
8352
}
8453

85-
return result;
54+
return (RubyHash) result;
8655
}
8756

8857
@Override

0 commit comments

Comments
 (0)