From: divide via ruby-core Date: 2026-11-23T11:28:51+00:00 Subject: [ruby-core:127680] [Ruby Bug#22629] TracePoint :class / :end events are not fired for class << self when compiled with Prism Issue #22629 has been updated by divide (Rafa�� Rzepecki). The pull request is at https://github.com/ruby/ruby/pull/20095 Note I can't seem to be able to edit the backports field, but I'd appreciate a backport to 3.4 and 4.0 because it breaks tracing for an instrumentation project I'm working on, which people could use with multiple versions of Ruby. ---------------------------------------- Bug #22629: TracePoint :class / :end events are not fired for class << self when compiled with Prism https://bugs.ruby-lang.org/issues/22629#change-120187 * Author: divide (Rafa�� Rzepecki) * Status: Open * ruby -v: ruby 4.1.0dev (2026-11-22T16:35:30Z master bf888872d4) +PRISM [x86_64-linux] * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- When code is compiled with Prism (the default since Ruby 3.4), entering and leaving a singleton class body (`class << obj ... end`) does not fire the `:class` and `:end` TracePoint events. The parse.y compiler does fire them. ## Reproduction ```ruby events = [] tp = TracePoint.new(:class, :end) { |t| events << [t.event, t.self] } tp.enable do class Foo class << self def bar; end end end end events.each { |event, mod| puts "#{event} #{mod.inspect}" } puts RUBY_DESCRIPTION ``` ### Expected (`--parser=parse.y`) ``` class Foo class # end # end Foo ``` ### Actual (`--parser=prism`, the default) ``` class Foo end Foo ``` ## Affected versions | version | parse.y | prism | |---|---|---| | 3.3.6 | 4 events | 0 events (the Prism compiler was experimental then and fired no `:class`/`:end` at all) | | 3.4.10 | 4 events | 2 events | | 4.0.10 | 4 events | 2 events | | master (bf888872d4) | 4 events | 2 events | ## Cause `compile.c` emits `RUBY_EVENT_CLASS` and `RUBY_EVENT_END` for every `ISEQ_TYPE_CLASS` iseq. That covers `class`, `module` and singleton class bodies. `pm_compile_scope_node` in `prism_compile.c` keys these events on the AST node type instead. It checks only `PM_CLASS_NODE` and `PM_MODULE_NODE` and leaves out `PM_SINGLETON_CLASS_NODE`. This looks like a gap left by the fix for https://github.com/ruby/prism/issues/2662. ## Fix Adding `PM_SINGLETON_CLASS_NODE` to both conditions makes the events and their `lineno` values match parse.y. A patch with a regression test in `test/ruby/test_settracefunc.rb` is attached and will also be submitted as a pull request. ```diff - if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE)) { + if (PM_NODE_TYPE_P(scope_node->ast_node, PM_CLASS_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_MODULE_NODE) || PM_NODE_TYPE_P(scope_node->ast_node, PM_SINGLETON_CLASS_NODE)) { ``` (The same change applies to both the `RUBY_EVENT_CLASS` site and the `RUBY_EVENT_END` site.) ---Files-------------------------------- 0001-Bug-XXXXX-Fire-class-end-TracePoint-events-for-class.patch (2.9 KB) -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/