Project

General

Profile

Feature #14111 » add_receiver_and_method_name_to_argument_error_for_application_code.diff

esjee (SJ Stoker), 11/20/2017 09:38 PM

View differences:

spec/ruby/core/exception/arguments_spec.rb
1 1
require File.expand_path('../../../spec_helper', __FILE__)
2 2

  
3 3
describe "ArgumentError" do
4
  class ArgumentErrorDummyClass
5
    def foo(a,b,c:)
6
    end
7
  end
8

  
4 9
  it "is a subclass of StandardError" do
5 10
    StandardError.should be_ancestor_of(ArgumentError)
6 11
  end
......
8 13
  it "gives its own class name as message if it has no message" do
9 14
    ArgumentError.new.message.should == "ArgumentError"
10 15
  end
16

  
17
  it "includes receiver and method name when raised by application code" do
18
    exc = begin
19
      ArgumentErrorDummyClass.new.foo(3)
20
    rescue => exc
21
      exc
22
    end
23

  
24
    exc.instance_variable_get('@receiver').should_not be_nil
25
    exc.instance_variable_get('@receiver').should.class === ArgumentErrorDummyClass
26
    exc.instance_variable_get('@method_name').should == 'foo'
27
  end
11 28
end
vm_args.c
9 9
**********************************************************************/
10 10

  
11 11
NORETURN(static void raise_argument_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const VALUE exc));
12
NORETURN(static void argument_arity_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const int miss_argc, const int min_argc, const int max_argc));
12
NORETURN(static void argument_arity_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, struct rb_calling_info *calling, const int miss_argc, const int min_argc, const int max_argc));
13 13
NORETURN(static void argument_kw_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const char *error, const VALUE keys));
14 14
VALUE rb_keyword_error_new(const char *error, VALUE keys); /* class.c */
15 15
static VALUE method_missing(VALUE obj, ID id, int argc, const VALUE *argv,
......
592 592
		args_extend(args, min_argc);
593 593
	    }
594 594
	    else {
595
		argument_arity_error(ec, iseq, given_argc, min_argc, max_argc);
595
        argument_arity_error(ec, iseq, calling, given_argc, min_argc, max_argc);
596 596
	    }
597 597
	}
598 598
    }
......
614 614
	    given_argc = max_argc;
615 615
	}
616 616
	else {
617
	    argument_arity_error(ec, iseq, given_argc, min_argc, max_argc);
618
	}
617
    argument_arity_error(ec, iseq, calling, given_argc, min_argc, max_argc);
618
  }
619 619
    }
620 620

  
621 621
    if (iseq->body->param.flags.has_lead) {
......
713 713
}
714 714

  
715 715
static void
716
argument_arity_error(rb_execution_context_t *ec, const rb_iseq_t *iseq, const int miss_argc, const int min_argc, const int max_argc)
716
argument_arity_error(rb_execution_context_t *ec, const rb_iseq_t *iseq,
717
                       struct rb_calling_info *calling,
718
                       const int miss_argc, const int min_argc, const int max_argc)
717 719
{
718 720
    VALUE exc = rb_arity_error_new(miss_argc, min_argc, max_argc);
719 721
    if (iseq->body->param.flags.has_kw) {
......
734 736
	    RSTRING_PTR(mesg)[RSTRING_LEN(mesg)-1] = ')';
735 737
	}
736 738
    }
739

  
740
    rb_iv_set(exc, "@receiver", calling->recv);
741
    rb_iv_set(exc, "@method_name", rb_iseq_method_name(iseq));
737 742
    raise_argument_error(ec, iseq, exc);
738 743
}
739 744

  
vm_insnhelper.c
1608 1608
	CALLER_SETUP_ARG(cfp, calling, ci); /* splat arg */
1609 1609

  
1610 1610
	if (calling->argc != iseq->body->param.lead_num) {
1611
	    argument_arity_error(ec, iseq, calling->argc, iseq->body->param.lead_num, iseq->body->param.lead_num);
1611
    argument_arity_error(ec, iseq, calling, calling->argc, iseq->body->param.lead_num, iseq->body->param.lead_num);
1612 1612
	}
1613 1613

  
1614 1614
	CI_SET_FASTPATH(cc, vm_call_iseq_setup_func(ci, param_size, local_size),
......
2593 2593
		}
2594 2594
	    }
2595 2595
	    else {
2596
		argument_arity_error(ec, iseq, calling->argc, iseq->body->param.lead_num, iseq->body->param.lead_num);
2596
        argument_arity_error(ec, iseq, calling, calling->argc, iseq->body->param.lead_num, iseq->body->param.lead_num);
2597 2597
	    }
2598 2598
	}
2599 2599