Files
mruby-mruby/mrbgems/mruby-bin-mrbc/bintest/mrbc.rb
T
Yukihiro "Matz" Matsumoto 0d87198c92 mruby-compiler: preserve line number across multi-file boundary
When mrbc compiles multiple input files (e.g. `mrbc -g -o out.mrb
a.rb b.rb`), the bison parser's one-token lookahead can buffer the
final token of one file before partial_hook switches to the next.
By the time bison reduces that token into an AST node,
`mrb_parser_set_filename` has already reset `p->lineno` to 0, so
init_var_header recorded lineno=0 for the previous file's last
statement and codegen propagated the previous instruction's line.

Save the lineno into `prev_file_lineno` immediately before the
reset so init_var_header can restore the correct value when it
detects the lookahead edge case (lineno==0 && filename_index>0).

close #1316

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-11 10:38:02 +09:00

48 lines
1.7 KiB
Ruby

require 'tempfile'
assert('Compiling multiple files without new line in last line. #2361') do
a, b, out = Tempfile.new('a.rb'), Tempfile.new('b.rb'), Tempfile.new('out.mrb')
a.write('module A; end')
a.flush
b.write('module B; end')
b.flush
result = `#{cmd('mrbc')} -c -o #{out.path} #{a.path} #{b.path} 2>&1`
assert_equal "#{cmd_bin('mrbc')}:#{a.path}:Syntax OK", result.chomp
assert_equal 0, $?.exitstatus
end
assert('parsing function with void argument') do
a, out = Tempfile.new('a.rb'), Tempfile.new('out.mrb')
a.write('f ()')
a.flush
result = `#{cmd('mrbc')} -c -o #{out.path} #{a.path} 2>&1`
assert_equal "#{cmd_bin('mrbc')}:#{a.path}:Syntax OK", result.chomp
assert_equal 0, $?.exitstatus
end
assert('embedded document with invalid terminator') do
a, out = Tempfile.new('a.rb'), Tempfile.new('out.mrb')
a.write("=begin\n=endx\n")
a.flush
result = `#{cmd('mrbc')} -c -o #{out.path} #{a.path} 2>&1`
assert_equal "#{a.path}:3:0: embedded document meets end of file", result.chomp
assert_equal 1, $?.exitstatus
end
assert('debug info preserves line/filename across multiple inputs. #1316') do
a = Tempfile.new(['a', '.rb'])
b = Tempfile.new(['b', '.rb'])
out = Tempfile.new(['out', '.mrb'])
a.write("# line 1\n# line 2\nputs \"from a\"\n# line 4\nundefined_in_a\n")
a.flush
b.write("# b line 1\nputs \"from b\"\n")
b.flush
`#{cmd('mrbc')} -g -o #{out.path} #{a.path} #{b.path}`
assert_equal 0, $?.exitstatus
result = `#{cmd('mruby')} -b #{out.path} 2>&1`
# Error should point at a.rb line 5 (the `undefined_in_a` line),
# not b.rb or a different line within a.rb.
assert_include result, "#{a.path}:5:"
assert_not_include result, b.path
end