Skip to content

Commit

Permalink
NameError#receiver of uninitialized constant
Browse files Browse the repository at this point in the history
* error.c (name_err_mesg_to_str): quote the name if unprintable.
* object.c (check_setter_id): use rb_check_id to convert names.
* variable.c (uninitialized_constant): use NameError::message to
  keep the receiver of uninitialized constant.  [Feature ruby#10881]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52321 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Oct 28, 2015
1 parent 7a92972 commit 72ff61f
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 237 deletions.
9 changes: 8 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
Wed Oct 28 15:23:14 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
Wed Oct 28 15:24:09 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>

* error.c (name_err_mesg_to_str): quote the name if unprintable.

* object.c (check_setter_id): use rb_check_id to convert names.

* variable.c (uninitialized_constant): use NameError::message to
keep the receiver of uninitialized constant. [Feature #10881]

* error.c (rb_name_err_new): new function to create NameError
exception instance. [Feature #10881]
Expand Down
2 changes: 1 addition & 1 deletion error.c
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ name_err_mesg_to_str(VALUE obj)
else {
c = s = FAKE_CSTR(&s_str, "");
}
args[0] = ptr[NAME_ERR_MESG__NAME];
args[0] = QUOTE(rb_obj_as_string(ptr[NAME_ERR_MESG__NAME]));
args[1] = d;
args[2] = s;
args[3] = c;
Expand Down
60 changes: 33 additions & 27 deletions eval_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,47 +208,53 @@ ruby_error_print(void)
error_print();
}

static const char *
method_visibility_name(rb_method_visibility_t visi)
{
switch (visi) {
case METHOD_VISI_UNDEF:
case METHOD_VISI_PUBLIC: return "";
case METHOD_VISI_PRIVATE: return " private";
case METHOD_VISI_PROTECTED: return " protected";
}
rb_bug("method_visibility_name: unreachable (%d)", (int)visi);
}
#define undef_mesg_for(v, k) rb_fstring_cstr("undefined"v" method `%1$s' for "k" `%$s'")
#define undef_mesg(v) ( \
is_mod ? \
undef_mesg_for(v, "module") : \
undef_mesg_for(v, "class"))

void
rb_print_undef(VALUE klass, ID id, int visi)
{
const char *v = method_visibility_name(visi);

rb_name_error(id, "undefined%s method `%"PRIsVALUE"' for %s `% "PRIsVALUE"'", v,
QUOTE_ID(id),
(RB_TYPE_P(klass, T_MODULE)) ? "module" : "class",
rb_class_name(klass));
const int is_mod = RB_TYPE_P(klass, T_MODULE);
VALUE mesg;
switch (visi & METHOD_VISI_MASK) {
case METHOD_VISI_UNDEF:
case METHOD_VISI_PUBLIC: mesg = undef_mesg(""); break;
case METHOD_VISI_PRIVATE: mesg = undef_mesg(" private"); break;
case METHOD_VISI_PROTECTED: mesg = undef_mesg(" protected"); break;
default: UNREACHABLE;
}
rb_name_err_raise_str(mesg, klass, ID2SYM(id));
}

void
rb_print_undef_str(VALUE klass, VALUE name)
{
rb_name_error_str(name, "undefined method `%"PRIsVALUE"' for %s `% "PRIsVALUE"'",
QUOTE(name),
(RB_TYPE_P(klass, T_MODULE)) ? "module" : "class",
rb_class_name(klass));
const int is_mod = RB_TYPE_P(klass, T_MODULE);
rb_name_err_raise_str(undef_mesg(""), klass, name);
}

#define inaccessible_mesg_for(v, k) rb_fstring_cstr("method `%1$s' for "k" `%2$s' is "v)
#define inaccessible_mesg(v) ( \
is_mod ? \
inaccessible_mesg_for(v, "module") : \
inaccessible_mesg_for(v, "class"))

void
rb_print_inaccessible(VALUE klass, ID id, rb_method_visibility_t visi)
{
const char *v = method_visibility_name(visi);
rb_name_error(id, "method `%"PRIsVALUE"' for %s `% "PRIsVALUE"' is %s",
QUOTE_ID(id),
(RB_TYPE_P(klass, T_MODULE)) ? "module" : "class",
rb_class_name(klass),
v);
const int is_mod = RB_TYPE_P(klass, T_MODULE);
VALUE mesg;
switch (visi & METHOD_VISI_MASK) {
case METHOD_VISI_UNDEF:
case METHOD_VISI_PUBLIC: mesg = inaccessible_mesg(""); break;
case METHOD_VISI_PRIVATE: mesg = inaccessible_mesg(" private"); break;
case METHOD_VISI_PROTECTED: mesg = inaccessible_mesg(" protected"); break;
default: UNREACHABLE;
}
rb_name_err_raise_str(mesg, klass, ID2SYM(id));
}

static int
Expand Down
4 changes: 3 additions & 1 deletion method.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ typedef enum {
METHOD_VISI_UNDEF = 0x00,
METHOD_VISI_PUBLIC = 0x01,
METHOD_VISI_PRIVATE = 0x02,
METHOD_VISI_PROTECTED = 0x03
METHOD_VISI_PROTECTED = 0x03,

METHOD_VISI_MASK = 0x03
} rb_method_visibility_t;

typedef struct rb_scope_visi_struct {
Expand Down
Loading

0 comments on commit 72ff61f

Please sign in to comment.