Skip to content

Add a warn form that takes a callback#7685

Merged
headius merged 2 commits intojruby:masterfrom
headius:warning_callback_for_message
Feb 28, 2023
Merged

Add a warn form that takes a callback#7685
headius merged 2 commits intojruby:masterfrom
headius:warning_callback_for_message

Conversation

@headius
Copy link
Member

@headius headius commented Feb 21, 2023

Callback receives the warning's stack trace element and must return a formatted message. This allows reusing the same single trace element rather than generating it twice to build the message elsewhere.

@headius headius added this to the JRuby 9.4.2.0 milestone Feb 21, 2023
@headius headius marked this pull request as ready for review February 21, 2023 23:18
@headius headius requested a review from enebo February 21, 2023 23:19
@headius headius force-pushed the warning_callback_for_message branch from cd1bf63 to 536b068 Compare February 21, 2023 23:19
@headius headius linked an issue Feb 21, 2023 that may be closed by this pull request
Copy link
Member

@enebo enebo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the ID should get removed. The idea of it is nice and was originally useful for ruby-ide but we have long stopped using our main parser for that. We do not use ID for anything internally and most new warns do what you just did and mark it miscellaneous.

If we do ever have a need for a progmatic warn system I think we should consider completely rewriting our warn system.

I do really like the notion of making all warns lazy like this to reduce accidental overhead of forumlating a warn string to just not use it because warn is off.

@headius
Copy link
Member Author

headius commented Feb 24, 2023

@enebo This does avoid building the string until the last moment, but it introduces the construction of a lambda object. The callback form could be expanded to pass in a few key objects to avoid that allocation, like we do for Hash and lock callbacks elsewhere.

I'll see if I can cut the ID stuff out of the new code.

@headius headius force-pushed the warning_callback_for_message branch from 536b068 to 004f5dd Compare February 24, 2023 05:28
@headius
Copy link
Member Author

headius commented Feb 24, 2023

@enebo I cut out the ID path from this and actually fixed it to only generate a single backtrace frame (it was still doing two before).

I also pushed a change in 06f7b7d that makes this path zero-alloc for most common cases (adds two passthrough values for the lambda).

Callback receives the warning's stack trace element and must
return a formatted message. This allows reusing the same single
trace element rather than generating it twice to build the message
elsewhere.

Fixes jruby#7675
Usually all we need to build a message for the warning is the
execution context (usually will be ThreadContext) and some state
(a Ruby object in this case). Pass them through so the lambda can
be stateless.
@headius headius force-pushed the warning_callback_for_message branch from 06f7b7d to 8cf5130 Compare February 24, 2023 05:44
@headius
Copy link
Member Author

headius commented Feb 24, 2023

My commits got a little mixed up; see new hashes.

@headius headius merged commit 3a2134e into jruby:master Feb 28, 2023
@headius headius deleted the warning_callback_for_message branch February 28, 2023 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JIT loses line numbers in some warnings?

2 participants