-
Notifications
You must be signed in to change notification settings - Fork 194
Refactor Kernel#respond_to? specializations #2132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Kernel#respond_to? specializations #2132
Conversation
if (notSymbolOrStringProfile.profile(!RubyGuards.isRubySymbolOrString(name))) { | ||
throw new RaiseException( | ||
getContext(), | ||
coreExceptions().typeErrorIsNotAOrB(object.toString(), "symbol", "string", this)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toString()
should not be called in PE code, because the receiver class is not known so it would be a virtual call, which is a performance warning.
So I'd suggest passing object
to typeErrorIsNotAOrB()
(then toString() is called behind a TruffleBoundary and that's OK).
We should probably use the new CoreException#inspectReceiver()
helper there instead of toString(), but the PR adding it is not merged yet (should be soon).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, it seems we have a few places with similar.toString()
:
truffleruby/src/main/java/org/truffleruby/core/cast/LongCastNode.java
Lines 47 to 51 in 314f8ae
protected long doBasicObject(Object value, | |
@CachedContext(RubyLanguage.class) RubyContext context) { | |
throw new RaiseException( | |
context, | |
context.getCoreExceptions().typeErrorIsNotA(value.toString(), "Integer (fitting in long)", this)); |
and
truffleruby/src/main/java/org/truffleruby/core/cast/BigIntegerCastNode.java
Lines 68 to 70 in 4acd2a8
private RubyException notAnInteger(RubyContext context, Object object) { | |
return context.getCoreExceptions().typeErrorIsNotA( | |
object.toString(), |
here we use Utils.toString()
:
protected int doBasicObject(Object value, | |
@CachedContext(RubyLanguage.class) RubyContext context) { | |
throw new RaiseException( | |
context, | |
context.getCoreExceptions().typeErrorIsNotA(Utils.toString(value), "Integer (fitting in int)", this)); |
Is this will be a part of
We should probably use the new CoreException#inspectReceiver() helper there instead of toString(), but the PR adding it is not merged yet (should be soon).
or we need to refactor these places too (probably not in this PR)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those cases are behind a @TruffleBoundary
, so compilation-wise they are fine. The case here isn't.
I think -H:+TruffleCheckBlackListedMethods
which is used in jt build --env native
would actually detect as an error at image build time.
In terms of error message, inspectReceiver() is better. I used that to replace toString()
in CoreExceptions, but not in other places (yet).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR I was talking about has been merged now, so if you rebase you can use inspectReceiver()
:
25eacca
As usual, new code should use the correct approach, but fixing old code is out of scope.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those cases are behind a @TruffleBoundary, so compilation-wise they are fine
Ah, that was obvious, sorry
The PR I was talking about has been merged now, so if you rebase you can use inspectReceiver():
No problem, but I should still pass Object
over String
, right?:
public RubyException typeErrorIsNotAOrB(Object value, String expectedTypeA, String expectedTypeB, Node currentNode) {
# use inspectReceiver(value) instead of value.toString()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think in the future we should remove those cases where the receiver is typed as String
, conversation should be done in CoreExeptions or later.
You could add one like
(the previous message was essentially a bug) |
618fc20
to
c1fec78
Compare
@eregon the PR ready for the final review |
Thank you for the refactoring! |
PullRequest: truffleruby/2108
Related to #2120 (comment)
The test output before the PR changes:
Not sure if I should add a note to the Changelog