Skip to content

Commit 7de0ac3

Browse files
committed
Revert "Skip initializing optional arguments to nil" and add test
This reverts commit 1596853. Expressions that run when the local is not given can observe the initial state of the optional parameter locals.
1 parent d8353e1 commit 7de0ac3

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

bootstraptest/test_method.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,3 +1434,9 @@ def x = [1]
14341434
def forwarder(...) = target(*x, 2, ...)
14351435
forwarder(3).inspect
14361436
}, '[Bug #21832] post-splat args before forwarding'
1437+
1438+
assert_equal '[nil, nil]', %q{
1439+
def self_reading(a = a, kw:) = a
1440+
def through_binding(a = binding.local_variable_get(:a), kw:) = a
1441+
[self_reading(kw: 1), through_binding(kw: 1)]
1442+
}, 'nil initialization of optional parameters'

vm_args.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ args_setup_opt_parameters(struct args_info *args, int opt_max, VALUE *locals)
247247
i = opt_max;
248248
}
249249
else {
250+
int j;
250251
i = args->argc;
251252
args->argc = 0;
252253

@@ -258,6 +259,11 @@ args_setup_opt_parameters(struct args_info *args, int opt_max, VALUE *locals)
258259
locals[i] = argv[args->rest_index];
259260
}
260261
}
262+
263+
/* initialize by nil */
264+
for (j=i; j<opt_max; j++) {
265+
locals[j] = Qnil;
266+
}
261267
}
262268

263269
return i;

0 commit comments

Comments
 (0)