Commit e7bbaf3e

e7bbaf3efdd4Merged

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

Description

fix(codegen): dispatch nullable receivers as their base type

compile_call_expr compared recv_type strictly against "string", "mutable_str", etc. A local widened to a nullable variant via def f(file_path = nil) later being passed a string ("nil + string" → "string?" via detect_poly_in_node) missed every per-type dispatcher and fell through to the "0" fallback at the bottom of compile_call_expr.

Strip the ? from recv_type once after the early constructor / operator / fiber dispatch. The null check is the caller's responsibility, matching Ruby's NoMethodError on a nil receiver. The is_obj_type dispatcher already operates this way (it accepts both obj_T and obj_T? and uses base_type internally).

Add test/optional_string_param_method.rb covering the issue's exact shape plus the alternate branches (file_path absent, longer source).

Closes #60

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

Details

Commit
e7bbaf3efdd4
Parent
d06c99ef5a0f

spinel_codegen.rbmodifiedView file

@@ -13311,6 +13311,15 @@
1331113311 end
1331213312
1331313313 recv_type = infer_type(recv)
13314+ # Nullable receiver: dispatch identically to the base type. The
13315+ # null check is the caller's responsibility, matching Ruby's
13316+ # NoMethodError semantics on a nil receiver. Without this, a
13317+ # local typed `string?` (e.g. an opt param `f = nil` later passed
13318+ # a string) misses every per-type dispatcher and falls through
13319+ # to the "0" fallback. Issue #60.
13320+ if is_nullable_type(recv_type) == 1
13321+ recv_type = base_type(recv_type)
13322+ end
1331413323 rc = compile_expr_gc_rooted(recv)
1331513324 # Root receiver if it may be collected during argument evaluation
1331613325 if expr_may_gc(recv) == 1 && type_is_pointer(recv_type) == 1