Commit 4b380748

4b380748cf20Merged

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

Description

Run Mutex/Monitor#synchronize and Thread.new blocks, carry their value

In Spinel's single-threaded AOT model these concurrency primitives have trivially-satisfied semantics, but they were lowered as unresolved calls on the int-0 that Mutex.new/Monitor.new/Thread.new produce: the block was silently dropped (synchronize) or the result was 0 (Thread#value), with no diagnostic.

Wire them through the existing value-returning-block machinery used by then/StringIO.open:

  • mutex.synchronize { body } / monitor.synchronize { body } run the block and return its last-expression value (the lock is always held). The receiver is still evaluated for side effects. Works at top level and for ivar receivers inside instance methods (the bundler/rake idiom).
  • Thread.new { body } runs the block eagerly and models the Thread value as the block's result; t.value (no args) reads it back as the identity on a scalar receiver, guarded so struct/hash/object .value accessors are never hijacked.

The compiler itself uses none of these primitives, so the change is purely additive and self-host-safe. Full suite: no new regressions.

Closes #1360

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
4b380748cf20
Parent
0c21b9c76a0a

spinel_analyze.rbmodifiedView file

@@ -5134,6 +5134,53 @@
51345134 end
51355135 return "int"
51365136 end
5137+ # `mutex.synchronize { body }`: single-threaded, so the result is the
5138+ # block's last-expression value (the lock is trivially held). No block
5139+ # param to bind -- synchronize yields nothing.
5140+ if mname == "synchronize"
5141+ blk = @nd_block[nid]
5142+ if blk >= 0
5143+ bbody = @nd_body[blk]
5144+ if bbody >= 0
5145+ bbs = get_stmts(bbody)
5146+ if bbs.length > 0
5147+ return infer_type(bbs.last)
5148+ end
5149+ end
5150+ return "int"
5151+ end
5152+ end
5153+ # `Thread.new { body }`: single-threaded, so run the block eagerly;
5154+ # the Thread value is modelled directly as the block's return value.
5155+ # `t.value` / `t.join` then read that value back (identity below).
5156+ if mname == "new" && constructor_class_name(recv) == "Thread"
5157+ blk = @nd_block[nid]
5158+ if blk >= 0
5159+ bbody = @nd_body[blk]
5160+ if bbody >= 0
5161+ bbs = get_stmts(bbody)
5162+ if bbs.length > 0
5163+ return infer_type(bbs.last)
5164+ end
5165+ end
5166+ end
5167+ return "int"
5168+ end
5169+ # `t.value` on a Thread (modelled as its eager block value): return the
5170+ # receiver's type so the carried value reads back unchanged. In a
5171+ # single-threaded model the Thread *is* its result value, so `value` is
5172+ # the identity. A no-arg `value` is otherwise an unresolved call on the
5173+ # carried scalar; treating it as identity is strictly better than the
5174+ # int-0 fallback (and `value` on a real Integer is a NoMethodError in
5175+ # CRuby anyway, so no valid program loses). Restricted to scalar
5176+ # receivers so a struct/hash/array `value` access is never hijacked.
5177+ if mname == "value" && recv >= 0 && @nd_arguments[nid] < 0
5178+ rtv = infer_type(recv)
5179+ bt = base_type(rtv)
5180+ if bt == "int" || bt == "string" || bt == "float" || bt == "bool"
5181+ return rtv
5182+ end
5183+ end
51375184 # `StringIO.open(str) { |io| ... }` returns the block's value (and
51385185 # closes the io). Same value-returning-block shape as `then`, but the
51395186 # block param is a freshly-constructed StringIO, not the receiver.