From b88bd27ece5962619ffaffeb5ac18b16ab67d64f Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Tue, 30 Jul 2019 16:15:19 -0700 Subject: [PATCH] Make eval(code, binding) use (eval) as __FILE__ and 1 as __LINE__ This removes the warning that was added in 3802fb92ff8c83eed3e867db20f72c53932f542d, and switches the behavior so that the eval does not use the binding's __FILE__ and __LINE__ implicitly. Fixes [Bug #4352] --- parse.y | 18 ------ spec/ruby/core/binding/eval_spec.rb | 85 +++++++++++++++++++-------- spec/ruby/core/kernel/__dir___spec.rb | 16 ++++- spec/ruby/core/kernel/eval_spec.rb | 29 ++++++--- test/ruby/test_eval.rb | 9 ++- test/ruby/test_method.rb | 2 +- vm_eval.c | 6 -- 7 files changed, 104 insertions(+), 61 deletions(-) diff --git a/parse.y b/parse.y index 46c263fa7b..b8433067f6 100644 --- a/parse.y +++ b/parse.y @@ -274,7 +274,6 @@ struct parser_params { unsigned int do_loop: 1; unsigned int do_chomp: 1; unsigned int do_split: 1; - unsigned int warn_location: 1; NODE *eval_tree_begin; NODE *eval_tree; @@ -9739,13 +9738,6 @@ past_dvar_p(struct parser_params *p, ID id) } # endif -#define WARN_LOCATION(type) do { \ - if (p->warn_location) { \ - rb_warning0(type" in eval may not return location in binding;" \ - " use Binding#source_location instead"); \ - } \ -} while (0) - static NODE* gettable(struct parser_params *p, ID id, const YYLTYPE *loc) { @@ -9761,7 +9753,6 @@ gettable(struct parser_params *p, ID id, const YYLTYPE *loc) case keyword_false: return NEW_FALSE(loc); case keyword__FILE__: - WARN_LOCATION("__FILE__"); { VALUE file = p->ruby_sourcefile_string; if (NIL_P(file)) @@ -9772,7 +9763,6 @@ gettable(struct parser_params *p, ID id, const YYLTYPE *loc) } return node; case keyword__LINE__: - WARN_LOCATION("__LINE__"); return NEW_LIT(INT2FIX(p->tokline), loc); case keyword__ENCODING__: return NEW_LIT(add_mark_object(p, rb_enc_from_encoding(p->enc)), loc); @@ -11977,14 +11967,6 @@ rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split) p->do_split = split; } -void -rb_parser_warn_location(VALUE vparser, int warn) -{ - struct parser_params *p; - TypedData_Get_Struct(vparser, struct parser_params, &parser_data_type, p); - p->warn_location = warn; -} - static NODE * parser_append_options(struct parser_params *p, NODE *node) { diff --git a/spec/ruby/core/binding/eval_spec.rb b/spec/ruby/core/binding/eval_spec.rb index c0264192b8..d98715620a 100644 --- a/spec/ruby/core/binding/eval_spec.rb +++ b/spec/ruby/core/binding/eval_spec.rb @@ -23,29 +23,58 @@ bind2.local_variables.should == [] end - it "inherits __LINE__ from the enclosing scope" do - obj = BindingSpecs::Demo.new(1) - bind = obj.get_binding - bind.eval("__LINE__").should == obj.get_line_of_binding - end + ruby_version_is ""..."2.7" do + it "inherits __LINE__ from the enclosing scope" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + bind.eval("__LINE__").should == obj.get_line_of_binding + end - it "preserves __LINE__ across multiple calls to eval" do - obj = BindingSpecs::Demo.new(1) - bind = obj.get_binding - bind.eval("__LINE__").should == obj.get_line_of_binding - bind.eval("__LINE__").should == obj.get_line_of_binding - end + it "preserves __LINE__ across multiple calls to eval" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + bind.eval("__LINE__").should == obj.get_line_of_binding + bind.eval("__LINE__").should == obj.get_line_of_binding + end - it "increments __LINE__ on each line of a multiline eval" do - obj = BindingSpecs::Demo.new(1) - bind = obj.get_binding - bind.eval("#foo\n__LINE__").should == obj.get_line_of_binding + 1 + it "increments __LINE__ on each line of a multiline eval" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + bind.eval("#foo\n__LINE__").should == obj.get_line_of_binding + 1 + end + + it "inherits __LINE__ from the enclosing scope even if the Binding is created with #send" do + obj = BindingSpecs::Demo.new(1) + bind, line = obj.get_binding_with_send_and_line + bind.eval("__LINE__").should == line + end end - it "inherits __LINE__ from the enclosing scope even if the Binding is created with #send" do - obj = BindingSpecs::Demo.new(1) - bind, line = obj.get_binding_with_send_and_line - bind.eval("__LINE__").should == line + ruby_version_is "2.7" do + it "starts with line 1 if single argument is given" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + bind.eval("__LINE__").should == 1 + end + + it "preserves __LINE__ across multiple calls to eval" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + bind.eval("__LINE__").should == 1 + bind.eval("__LINE__").should == 1 + end + + it "increments __LINE__ on each line of a multiline eval" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + bind.eval("#foo\n__LINE__").should == 2 + end + + it "starts with line 1 if the Binding is created with #send" do + obj = BindingSpecs::Demo.new(1) + bind, line = obj.get_binding_with_send_and_line + bind.eval("__LINE__").should == 1 + end end it "starts with a __LINE__ of 1 if a filename is passed" do @@ -60,10 +89,20 @@ bind.eval("#foo\n__LINE__", "(test)", 88).should == 89 end - it "inherits __FILE__ from the enclosing scope" do - obj = BindingSpecs::Demo.new(1) - bind = obj.get_binding - bind.eval("__FILE__").should == obj.get_file_of_binding + ruby_version_is ""..."2.7" do + it "inherits __FILE__ from the enclosing scope" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + bind.eval("__FILE__").should == obj.get_file_of_binding + end + end + + ruby_version_is "2.7" do + it "Uses (eval) as __FILE__ if single argument given" do + obj = BindingSpecs::Demo.new(1) + bind = obj.get_binding + bind.eval("__FILE__").should == '(eval)' + end end it "uses the __FILE__ that is passed in" do diff --git a/spec/ruby/core/kernel/__dir___spec.rb b/spec/ruby/core/kernel/__dir___spec.rb index 3c34277277..e2bcc6e65a 100644 --- a/spec/ruby/core/kernel/__dir___spec.rb +++ b/spec/ruby/core/kernel/__dir___spec.rb @@ -12,9 +12,19 @@ end end - context "when used in eval with top level binding" do - it "returns the real name of the directory containing the currently-executing file" do - eval("__dir__", binding).should == File.realpath(File.dirname(__FILE__)) + ruby_version_is ""..."2.7" do + context "when used in eval with top level binding" do + it "returns the real name of the directory containing the currently-executing file" do + eval("__dir__", binding).should == File.realpath(File.dirname(__FILE__)) + end + end + end + + ruby_version_is "2.7" do + context "when used in eval with top level binding" do + it "returns nil" do + eval("__dir__", binding).should == nil + end end end end diff --git a/spec/ruby/core/kernel/eval_spec.rb b/spec/ruby/core/kernel/eval_spec.rb index 9442725eac..3541540a2f 100644 --- a/spec/ruby/core/kernel/eval_spec.rb +++ b/spec/ruby/core/kernel/eval_spec.rb @@ -159,13 +159,28 @@ class Object end end - it "uses the filename of the binding if none is provided" do - eval("__FILE__").should == "(eval)" - eval("__FILE__", binding).should == __FILE__ - eval("__FILE__", binding, "success").should == "success" - eval("eval '__FILE__', binding").should == "(eval)" - eval("eval '__FILE__', binding", binding).should == __FILE__ - eval("eval '__FILE__', binding", binding, 'success').should == 'success' + ruby_version_is ""..."2.7" do + it "uses the filename of the binding if none is provided" do + eval("__FILE__").should == "(eval)" + eval("__FILE__", binding).should == __FILE__ + eval("__FILE__", binding, "success").should == "success" + eval("eval '__FILE__', binding").should == "(eval)" + eval("eval '__FILE__', binding", binding).should == __FILE__ + eval("eval '__FILE__', binding", binding, 'success').should == 'success' + eval("eval '__FILE__', binding, 'success'", binding).should == 'success' + end + end + + ruby_version_is "2.7" do + it "uses (eval) filename if none is provided" do + eval("__FILE__").should == "(eval)" + eval("__FILE__", binding).should == "(eval)" + eval("__FILE__", binding, "success").should == "success" + eval("eval '__FILE__', binding").should == "(eval)" + eval("eval '__FILE__', binding", binding).should == "(eval)" + eval("eval '__FILE__', binding", binding, 'success').should == '(eval)' + eval("eval '__FILE__', binding, 'success'", binding).should == 'success' + end end # Found via Rubinius bug github:#149 diff --git a/test/ruby/test_eval.rb b/test/ruby/test_eval.rb index 49bb3305e4..8b67c07e25 100644 --- a/test/ruby/test_eval.rb +++ b/test/ruby/test_eval.rb @@ -470,9 +470,12 @@ def test_eval_location_fstring end def test_eval_location_binding - assert_warning(/__FILE__ in eval/) do - assert_equal(__FILE__, eval("__FILE__", binding)) - end + assert_equal(['(eval)', 1], eval("[__FILE__, __LINE__]", nil)) + assert_equal(['(eval)', 1], eval("[__FILE__, __LINE__]", binding)) + assert_equal(['foo', 1], eval("[__FILE__, __LINE__]", nil, 'foo')) + assert_equal(['foo', 1], eval("[__FILE__, __LINE__]", binding, 'foo')) + assert_equal(['foo', 2], eval("[__FILE__, __LINE__]", nil, 'foo', 2)) + assert_equal(['foo', 2], eval("[__FILE__, __LINE__]", binding, 'foo', 2)) end def test_fstring_instance_eval diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb index ba425a4517..77f9367b8d 100644 --- a/test/ruby/test_method.rb +++ b/test/ruby/test_method.rb @@ -722,7 +722,7 @@ def test___dir__ assert_instance_of String, __dir__ assert_equal(File.dirname(File.realpath(__FILE__)), __dir__) bug8436 = '[ruby-core:55123] [Bug #8436]' - assert_equal(__dir__, eval("__dir__", binding), bug8436) + assert_equal(__dir__, eval("__dir__", binding, *binding.source_location), bug8436) bug8662 = '[ruby-core:56099] [Bug #8662]' assert_equal("arbitrary", eval("__dir__", binding, "arbitrary/file.rb"), bug8662) assert_equal("arbitrary", Object.new.instance_eval("__dir__", "arbitrary/file.rb"), bug8662) diff --git a/vm_eval.c b/vm_eval.c index 9281ebd40e..cf7d43cc11 100644 --- a/vm_eval.c +++ b/vm_eval.c @@ -1286,12 +1286,6 @@ eval_make_iseq(VALUE src, VALUE fname, int line, const rb_binding_t *bind, if (fname != Qundef) { realpath = fname; } - else if (bind) { - fname = pathobj_path(bind->pathobj); - realpath = pathobj_realpath(bind->pathobj); - line = bind->first_lineno; - rb_parser_warn_location(parser, TRUE); - } else { fname = rb_usascii_str_new_cstr("(eval)"); } -- 2.21.0