Skip to content

Commit d5ee315

Browse files
committed
[fix] Struct#initialize to handle kwargs (call-info)
1 parent 4d95401 commit d5ee315

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

core/src/main/java/org/jruby/RubyStruct.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -480,24 +480,24 @@ private void checkSize(ThreadContext context, int length) {
480480
if (length > values.length) throw argumentError(context, "struct size differs (" + length +" for " + values.length + ")");
481481
}
482482

483-
private void checkForKeywords(ThreadContext context, boolean keywordInit) {
484-
if (hasKeywords(ThreadContext.resetCallInfo(context)) && !keywordInit) {
483+
private void checkForKeywords(ThreadContext context, int callInfo, boolean keywordInit) {
484+
if (hasKeywords(callInfo) && !keywordInit) {
485485
warn(context, "Passing only keyword arguments to Struct#initialize will behave differently from Ruby 3.2. Please use a Hash literal like .new({k: v}) instead of .new(k: v).");
486486
}
487487
}
488488

489489
@JRubyMethod(rest = true, visibility = PRIVATE, keywords = true)
490490
public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
491+
final int callInfo = ThreadContext.resetCallInfo(context);
491492
IRubyObject keywordInit = RubyStruct.getInternalVariable(context, classOf(), KEYWORD_INIT_VAR);
492-
checkForKeywords(context, !keywordInit.isNil());
493-
ThreadContext.resetCallInfo(context);
493+
checkForKeywords(context, callInfo, !keywordInit.isNil());
494494
modify(context);
495495
checkSize(context, args.length);
496496

497497
if (keywordInit.isTrue()) {
498498
if (args.length != 1) throw argumentError(context, args.length, 0);
499499

500-
return initialize(context, args[0]);
500+
return initialize(context, args[0], callInfo);
501501
} else {
502502
System.arraycopy(args, 0, values, 0, args.length);
503503
Helpers.fillNil(context, values, args.length, values.length);
@@ -530,9 +530,13 @@ public IRubyObject initialize(ThreadContext context) {
530530
return initializeInternal(context, 0, nil, nil, nil);
531531
}
532532

533-
@JRubyMethod(visibility = PRIVATE)
533+
@JRubyMethod(visibility = PRIVATE, keywords = true)
534534
public IRubyObject initialize(ThreadContext context, IRubyObject arg0) {
535-
boolean keywords = hasNonemptyKeywords(ThreadContext.resetCallInfo(context));
535+
return initialize(context, arg0, ThreadContext.resetCallInfo(context));
536+
}
537+
538+
private IRubyObject initialize(ThreadContext context, IRubyObject arg0, final int callInfo) {
539+
final boolean keywords = hasNonemptyKeywords(callInfo);
536540

537541
if (keywords && arg0 instanceof RubyHash hash) {
538542
return setupStructValuesFromHash(context, hash);

0 commit comments

Comments
 (0)