Commit 2008568b

2008568b153bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Run module/class body side effects at definition time

Side-effecting statements in a module/class body (e.g. a puts deprecation notice) were silently dropped: only def/const/macro declarations were processed. Emit each body with definition-time side effects as a sp_clsbody_<nid>() function and call it from main at the definition site, so the body runs top-to-bottom in source order, in its own scope.

Only bare calls (no explicit receiver) that aren't class macros or the class's own methods are emitted. A call to one of the class's own methods is a compile-time DSL that analysis already interprets (e.g. optcarrot's op), so it stays unemitted to avoid running twice; unresolved DSL calls (e.g. FFI declarations) degrade to a no-op body. A call with a receiver (self.x = ..., M.use(...)) is left to the existing handling.

Covers the puts/side-effect form of the report. The singleton-attr form (self.x = [2,3] via class << self; attr_accessor) needs self-receiver dispatch and stays as before.

Refs #1256

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
2008568b153b
Parent
ad486706fe5f

spinel_codegen.rbmodifiedView file

@@ -393,6 +393,12 @@
393393 # reverse-of-source-order END execution.
394394 @post_execution_blocks = []
395395
396+ # Class/module nodes whose body has definition-time side effects
397+ # (emitted as `sp_clsbody_<nid>()` functions, called from main at
398+ # the definition site). Parallel arrays: node id and lexical scope.
399+ @clsbody_func_ids = "".split(",", -1)
400+ @clsbody_func_scopes = "".split(",", -1)
401+
396402 # ---- Scope stack for local variables ----
397403 @scope_names = "".split(",", -1)
398404 @scope_types = "".split(",", -1)
@@ -8723,6 +8729,9 @@
87238729 # them in source order; atexit invokes handlers LIFO, matching
87248730 # CRuby's reverse-order END execution.
87258731 emit_post_execution_funcs
8732+ # Definition-time class/module body side effects: emit one function
8733+ # per body so emit_main can run them at the definition site.
8734+ emit_clsbody_funcs
87268735 # poly_poly_hash needs eql?-aware OBJ-tag dispatch. Emit a
87278736 # cls_id-keyed shim that knows about Method specifically (compares
87288737 # bound receiver + fn_ptr) and falls through to pointer identity
@@ -12351,6 +12360,202 @@
1235112360 end
1235212361 end
1235312362
12363+ # Names a class/module body call resolves at compile time (definition
12364+ # macros). Analysis already consumes these, so they must not re-run as
12365+ # definition-time side effects.
12366+ def clsbody_call_is_macro(mn)
12367+ if mn == "attr_accessor" || mn == "attr_reader" || mn == "attr_writer" || mn == "attr"
12368+ return 1
12369+ end
12370+ if mn == "include" || mn == "extend" || mn == "prepend"
12371+ return 1
12372+ end
12373+ if mn == "private" || mn == "public" || mn == "protected" || mn == "module_function"
12374+ return 1
12375+ end
12376+ if mn == "define_method" || mn == "alias_method" || mn == "alias"
12377+ return 1
12378+ end
12379+ if mn == "private_constant" || mn == "private_class_method" || mn == "public_class_method"
12380+ return 1
12381+ end
12382+ if mn == "using" || mn == "refine"
12383+ return 1
12384+ end
12385+ if mn == "require" || mn == "require_relative" || mn == "load" || mn == "autoload"
12386+ return 1
12387+ end
12388+ 0
12389+ end
12390+
12391+ # Does this class/module body statement run at definition time? Only
12392+ # genuine side-effecting method calls qualify (e.g. `puts`).
12393+ # Definitions, constants, and class macros are handled elsewhere, and
12394+ # a call to one of the class's own methods is treated as a
12395+ # compile-time DSL (analysis interprets it, e.g. optcarrot's `op`),
12396+ # so it stays unemitted to avoid double execution.
12397+ def clsbody_stmt_emittable(sid, ci)
12398+ if @nd_type[sid] != "CallNode"
12399+ return 0
12400+ end
12401+ # Only bare calls (no explicit receiver) run as definition-time side
12402+ # effects -- e.g. `puts`. A call with a receiver (`M.use(...)`,
12403+ # `self.x = ...`) is a self-referential / metaprogramming shape left
12404+ # to the existing compile-time handling.
12405+ recv = @nd_receiver[sid]
12406+ if recv >= 0
12407+ return 0
12408+ end
12409+ mn = @nd_name[sid]
12410+ if clsbody_call_is_macro(mn) == 1
12411+ return 0
12412+ end
12413+ # A bare call to one of the class's own methods is a compile-time DSL
12414+ # that analysis already interprets (e.g. optcarrot's `op`); leave it
12415+ # unemitted so it does not run twice.
12416+ if ci >= 0
12417+ if cls_method_owner_for(ci, mn, "cmeth") >= 0
12418+ return 0
12419+ end
12420+ if cls_method_owner_for(ci, mn, "imeth") >= 0
12421+ return 0
12422+ end
12423+ end
12424+ 1
12425+ end
12426+
12427+ # The fully scoped name (`Outer_Inner`) of a class/module node given
12428+ # its enclosing lexical scope, used for the class-index lookup.
12429+ def clsbody_scoped_name(nid, scope)
12430+ cpath = @nd_constant_path[nid]
12431+ simple = ""
12432+ if cpath >= 0
12433+ simple = @nd_name[cpath]
12434+ end
12435+ if simple == ""
12436+ return scope
12437+ end
12438+ if scope == ""
12439+ return simple
12440+ end
12441+ scope + "_" + simple
12442+ end
12443+
12444+ def clsbody_has_func(nid)
12445+ j = 0
12446+ while j < @clsbody_func_ids.length
12447+ if @clsbody_func_ids[j] == nid.to_s
12448+ return 1
12449+ end
12450+ j = j + 1
12451+ end
12452+ 0
12453+ end
12454+
12455+ # Collect (depth-first, source order) every class/module node whose
12456+ # body has definition-time side effects, directly or via a nested
12457+ # class/module. Children are pushed before parents so a parent's
12458+ # forward-declared call always resolves. Returns 1 if `nid` got a func.
12459+ def collect_clsbody_funcs(nid, scope)
12460+ t = @nd_type[nid]
12461+ if t != "ClassNode" && t != "ModuleNode"
12462+ return 0
12463+ end
12464+ body = @nd_body[nid]
12465+ if body < 0
12466+ return 0
12467+ end
12468+ cscoped = clsbody_scoped_name(nid, scope)
12469+ ci = find_class_idx(cscoped)
12470+ stmts = get_stmts(body)
12471+ has_emittable = 0
12472+ k = 0
12473+ while k < stmts.length
12474+ sid = stmts[k]
12475+ st = @nd_type[sid]
12476+ if st == "ClassNode" || st == "ModuleNode"
12477+ if collect_clsbody_funcs(sid, cscoped) == 1
12478+ has_emittable = 1
12479+ end
12480+ else
12481+ if clsbody_stmt_emittable(sid, ci) == 1
12482+ has_emittable = 1
12483+ end
12484+ end
12485+ k = k + 1
12486+ end
12487+ if has_emittable == 1
12488+ @clsbody_func_ids.push(nid.to_s)
12489+ @clsbody_func_scopes.push(scope)
12490+ return 1
12491+ end
12492+ 0
12493+ end
12494+
12495+ def emit_clsbody_func_body(nid, scope)
12496+ body = @nd_body[nid]
12497+ if body < 0
12498+ return
12499+ end
12500+ cscoped = clsbody_scoped_name(nid, scope)
12501+ ci = find_class_idx(cscoped)
12502+ emit_raw("static void sp_clsbody_" + nid.to_s + "(void) {")
12503+ @indent = 1
12504+ @in_main = 1
12505+ saved_ci = @current_class_idx
12506+ saved_lex = @current_lexical_scope
12507+ @current_class_idx = ci
12508+ @current_lexical_scope = cscoped
12509+ push_scope
12510+ stmts = get_stmts(body)
12511+ k = 0
12512+ while k < stmts.length
12513+ sid = stmts[k]
12514+ st = @nd_type[sid]
12515+ if st == "ClassNode" || st == "ModuleNode"
12516+ if clsbody_has_func(sid) == 1
12517+ emit(" sp_clsbody_" + sid.to_s + "();")
12518+ end
12519+ else
12520+ if clsbody_stmt_emittable(sid, ci) == 1
12521+ compile_stmt(sid)
12522+ end
12523+ end
12524+ k = k + 1
12525+ end
12526+ pop_scope
12527+ @current_class_idx = saved_ci
12528+ @current_lexical_scope = saved_lex
12529+ @in_main = 0
12530+ emit_raw("}")
12531+ emit_raw("")
12532+ end
12533+
12534+ # Emit one `static void sp_clsbody_<nid>(void)` per class/module body
12535+ # with definition-time side effects. emit_main calls them at each
12536+ # definition site so the body runs in source order, in its own scope.
12537+ def emit_clsbody_funcs
12538+ stmts = get_body_stmts(@root_id)
12539+ k = 0
12540+ while k < stmts.length
12541+ collect_clsbody_funcs(stmts[k], "")
12542+ k = k + 1
12543+ end
12544+ if @clsbody_func_ids.length == 0
12545+ return
12546+ end
12547+ fi = 0
12548+ while fi < @clsbody_func_ids.length
12549+ emit_raw("static void sp_clsbody_" + @clsbody_func_ids[fi] + "(void);")
12550+ fi = fi + 1
12551+ end
12552+ fi = 0
12553+ while fi < @clsbody_func_ids.length
12554+ emit_clsbody_func_body(@clsbody_func_ids[fi].to_i, @clsbody_func_scopes[fi])
12555+ fi = fi + 1
12556+ end
12557+ end
12558+
1235412559 def emit_toplevel_method(mi)
1235512560 mfullname = @meth_names[mi]
1235612561 @current_method_name = mfullname
@@ -13502,7 +13707,13 @@
1350213707 # an RHS that reads from a main local, so the pre-loop deliberately
1350313708 # left them out and we emit them here in source order.
1350413709 stmts.each { |sid|
13505- if @nd_type[sid] != "DefNode"
13710+ if @nd_type[sid] == "ClassNode" || @nd_type[sid] == "ModuleNode"
13711+ # The class/module is defined statically; run its definition-time
13712+ # body side effects (if any) at this source position.
13713+ if clsbody_has_func(sid) == 1
13714+ emit(" sp_clsbody_" + sid.to_s + "();")
13715+ end
13716+ elsif @nd_type[sid] != "DefNode"
1350613717 if @nd_type[sid] != "ClassNode"
1350713718 if @nd_type[sid] != "ConstantWriteNode"
1350813719 compile_stmt(sid)