Skip to content

Commit 45b5285

Browse files
committed
Fix the exception type raised when type coercion via to_sym raises a NoMethodError.
1 parent 33d10f5 commit 45b5285

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

spec/ruby/core/module/alias_method_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ def uno_refined_method
8181
-> { @class.make_alias mock('x'), :public_one }.should raise_error(TypeError)
8282
end
8383

84+
it "raises a NoMethodError if the given name raises a NoMethodError during type coercion using to_str" do
85+
obj = mock("mock-name")
86+
obj.should_receive(:to_str).and_raise(NoMethodError)
87+
-> { @class.make_alias obj, :public_one }.should raise_error(NoMethodError)
88+
end
89+
8490
it "is a public method" do
8591
Module.should have_public_instance_method(:alias_method, false)
8692
end

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.truffleruby.language.control.RaiseException;
2525
import org.truffleruby.language.dispatch.DispatchNode;
2626
import org.truffleruby.language.library.RubyStringLibrary;
27-
import org.truffleruby.utils.Utils;
2827

2928
@GenerateUncached
3029
@NodeChild(value = "valueNode", type = RubyBaseNodeWithExecute.class)
@@ -85,22 +84,15 @@ protected RubySymbol rubyStringUncached(Object str,
8584
@Specialization(guards = { "!isRubySymbol(object)", "!isString(object)", "isNotRubyString(object)" })
8685
protected RubySymbol toStr(Object object,
8786
@Cached BranchProfile errorProfile,
88-
@Cached DispatchNode toStr,
87+
@Cached DispatchNode toStrNode,
8988
@Cached RubyStringLibrary libString,
9089
@Cached ToSymbolNode toSymbolNode) {
91-
final Object coerced;
92-
try {
93-
coerced = toStr.call(object, "to_str");
94-
} catch (RaiseException e) {
95-
errorProfile.enter();
96-
if (e.getException().getLogicalClass() == coreLibrary().noMethodErrorClass) {
97-
throw new RaiseException(getContext(), coreExceptions().typeError(
98-
Utils.concat(object, " is not a symbol nor a string"),
99-
this));
100-
} else {
101-
throw e;
102-
}
103-
}
90+
var coerced = toStrNode.call(
91+
coreLibrary().truffleTypeModule,
92+
"rb_convert_type",
93+
object,
94+
coreLibrary().stringClass,
95+
coreSymbols().TO_STR);
10496

10597
if (libString.isRubyString(coerced)) {
10698
return toSymbolNode.execute(coerced);

0 commit comments

Comments
 (0)