aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ast.c4
-rw-r--r--parse.y21
-rw-r--r--test/ruby/test_ast.rb14
3 files changed, 33 insertions, 6 deletions
diff --git a/ast.c b/ast.c
index 3d09756225..4e2f05c2b7 100644
--- a/ast.c
+++ b/ast.c
@@ -83,7 +83,7 @@ rb_ast_parse_str(VALUE str)
str = rb_check_string_type(str);
rb_parser_set_context(parser, NULL, 0);
- ast = rb_parser_compile_string_path(parser, rb_str_new_cstr("no file name"), str, 1);
+ ast = rb_parser_compile_string_path(parser, Qnil, str, 1);
if (!ast->body.root) {
rb_ast_dispose(ast);
@@ -127,7 +127,7 @@ rb_ast_parse_file(VALUE path)
f = rb_file_open_str(path, "r");
rb_funcall(f, rb_intern("set_encoding"), 2, rb_enc_from_encoding(enc), rb_str_new_cstr("-"));
rb_parser_set_context(parser, NULL, 0);
- ast = rb_parser_compile_file_path(parser, path, f, 1);
+ ast = rb_parser_compile_file_path(parser, Qnil, f, 1);
rb_io_close(f);
diff --git a/parse.y b/parse.y
index aed392822e..71392ea00c 100644
--- a/parse.y
+++ b/parse.y
@@ -4873,7 +4873,7 @@ yycompile0(VALUE arg)
struct parser_params *p = (struct parser_params *)arg;
VALUE cov = Qfalse;
- if (!compile_for_eval && rb_safe_level() == 0) {
+ if (!compile_for_eval && rb_safe_level() == 0 && !NIL_P(p->ruby_sourcefile_string)) {
p->debug_lines = debug_lines(p->ruby_sourcefile_string);
if (p->debug_lines && p->ruby_sourceline > 0) {
VALUE str = STR_NEW0();
@@ -4933,8 +4933,14 @@ static rb_ast_t *
yycompile(VALUE vparser, struct parser_params *p, VALUE fname, int line)
{
rb_ast_t *ast;
- p->ruby_sourcefile_string = rb_str_new_frozen(fname);
- p->ruby_sourcefile = StringValueCStr(fname);
+ if (NIL_P(fname)) {
+ p->ruby_sourcefile_string = Qnil;
+ p->ruby_sourcefile = "(none)";
+ }
+ else {
+ p->ruby_sourcefile_string = rb_str_new_frozen(fname);
+ p->ruby_sourcefile = StringValueCStr(fname);
+ }
p->ruby_sourceline = line - 1;
p->ast = ast = rb_ast_new();
@@ -8716,7 +8722,14 @@ gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
return NEW_FALSE(loc);
case keyword__FILE__:
WARN_LOCATION("__FILE__");
- node = NEW_STR(add_mark_object(p, rb_str_dup(p->ruby_sourcefile_string)), loc);
+ {
+ VALUE file = p->ruby_sourcefile_string;
+ if (NIL_P(file))
+ file = rb_str_new(0, 0);
+ else
+ file = rb_str_dup(file);
+ node = NEW_STR(add_mark_object(p, file), loc);
+ }
return node;
case keyword__LINE__:
WARN_LOCATION("__LINE__");
diff --git a/test/ruby/test_ast.rb b/test/ruby/test_ast.rb
index e8b25fd7d6..05f4211cba 100644
--- a/test/ruby/test_ast.rb
+++ b/test/ruby/test_ast.rb
@@ -180,6 +180,20 @@ class TestAst < Test::Unit::TestCase
assert_instance_of(RubyVM::AbstractSyntaxTree::Node, node_proc)
assert_instance_of(RubyVM::AbstractSyntaxTree::Node, node_method)
assert_raise(TypeError) { RubyVM::AbstractSyntaxTree.of("1 + 2") }
+
+ Tempfile.create(%w"test_of .rb") do |tmp|
+ tmp.print "#{<<-"begin;"}\n#{<<-'end;'}"
+ begin;
+ SCRIPT_LINES__ = {}
+ assert_instance_of(RubyVM::AbstractSyntaxTree::Node, RubyVM::AbstractSyntaxTree.of(proc {|x| x}))
+ end;
+ tmp.close
+ assert_separately(["-", tmp.path], "#{<<~"begin;"}\n#{<<~'end;'}")
+ begin;
+ load ARGV[0]
+ assert_empty(SCRIPT_LINES__)
+ end;
+ end
end
def test_scope_local_variables