merge revision(s) 58918788ab: [Backport #20342]

[Bug #20342] Consider wrapped load in `main` methods
This commit is contained in:
nagachika 2024-07-15 17:56:01 +09:00
parent bfdb6d5a9d
commit b72deb7ca1
6 changed files with 37 additions and 22 deletions

18
eval.c
View file

@ -1777,6 +1777,16 @@ rb_obj_extend(int argc, VALUE *argv, VALUE obj)
return obj;
}
VALUE
rb_top_main_class(const char *method)
{
VALUE klass = GET_THREAD()->top_wrapper;
if (!klass) return rb_cObject;
rb_warning("main.%s in the wrapped load is effective only in wrapper module", method);
return klass;
}
/*
* call-seq:
* include(module, ...) -> self
@ -1789,13 +1799,7 @@ rb_obj_extend(int argc, VALUE *argv, VALUE obj)
static VALUE
top_include(int argc, VALUE *argv, VALUE self)
{
rb_thread_t *th = GET_THREAD();
if (th->top_wrapper) {
rb_warning("main.include in the wrapped load is effective only in wrapper module");
return rb_mod_include(argc, argv, th->top_wrapper);
}
return rb_mod_include(argc, argv, rb_cObject);
return rb_mod_include(argc, argv, rb_top_main_class("include"));
}
/*

View file

@ -21,6 +21,7 @@ extern ID ruby_static_id_status;
VALUE rb_refinement_module_get_refined_class(VALUE module);
void rb_class_modify_check(VALUE);
NORETURN(VALUE rb_f_raise(int argc, VALUE *argv));
VALUE rb_top_main_class(const char *method);
/* eval_error.c */
VALUE rb_get_backtrace(VALUE info);

12
proc.c
View file

@ -2382,17 +2382,7 @@ rb_obj_define_method(int argc, VALUE *argv, VALUE obj)
static VALUE
top_define_method(int argc, VALUE *argv, VALUE obj)
{
rb_thread_t *th = GET_THREAD();
VALUE klass;
klass = th->top_wrapper;
if (klass) {
rb_warning("main.define_method in the wrapped load is effective only in wrapper module");
}
else {
klass = rb_cObject;
}
return rb_mod_define_method(argc, argv, klass);
return rb_mod_define_method(argc, argv, rb_top_main_class("define_method"));
}
/*

View file

@ -369,6 +369,26 @@ class TestRequire < Test::Unit::TestCase
end
end
def test_public_in_wrapped_load
Tempfile.create(["test_public_in_wrapped_load", ".rb"]) do |t|
t.puts "def foo; end", "public :foo"
t.close
assert_warning(/main\.public/) do
assert load(t.path, true)
end
end
end
def test_private_in_wrapped_load
Tempfile.create(["test_private_in_wrapped_load", ".rb"]) do |t|
t.puts "def foo; end", "private :foo"
t.close
assert_warning(/main\.private/) do
assert load(t.path, true)
end
end
end
def test_load_scope
bug1982 = '[ruby-core:25039] [Bug #1982]'
Tempfile.create(["test_ruby_test_require", ".rb"]) {|t|

View file

@ -11,7 +11,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 4
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
#define RUBY_PATCHLEVEL 195
#define RUBY_PATCHLEVEL 196
#include "ruby/version.h"
#include "ruby/internal/abi.h"

View file

@ -2552,7 +2552,7 @@ rb_mod_private_method(int argc, VALUE *argv, VALUE obj)
static VALUE
top_public(int argc, VALUE *argv, VALUE _)
{
return rb_mod_public(argc, argv, rb_cObject);
return rb_mod_public(argc, argv, rb_top_main_class("public"));
}
/*
@ -2572,7 +2572,7 @@ top_public(int argc, VALUE *argv, VALUE _)
static VALUE
top_private(int argc, VALUE *argv, VALUE _)
{
return rb_mod_private(argc, argv, rb_cObject);
return rb_mod_private(argc, argv, rb_top_main_class("private"));
}
/*
@ -2585,7 +2585,7 @@ top_private(int argc, VALUE *argv, VALUE _)
static VALUE
top_ruby2_keywords(int argc, VALUE *argv, VALUE module)
{
return rb_mod_ruby2_keywords(argc, argv, rb_cObject);
return rb_mod_ruby2_keywords(argc, argv, rb_top_main_class("ruby2_keywords"));
}
/*