Commit graph

118 commits

Author SHA1 Message Date
Jean Boussier
09d8c99cdc Ensure test suite is compatible with --frozen-string-literal
As preparation for https://bugs.ruby-lang.org/issues/20205
making sure the test suite is compatible with frozen string
literals is making things easier.
2024-03-14 17:56:15 +01:00
Jeremy Evans
5899f6aa55 Keep hidden local variables when dumping and loading iseqs
Fixes [Bug #19975]
2024-03-04 09:49:55 -08:00
Yusuke Endoh
25d74b9527 Do not include a backtick in error messages and backtraces
[Feature #16495]
2024-02-15 18:42:31 +09:00
Peter Zhu
a6e924cf5f [PRISM] Fix crash in compile_prism
If the argument is not a file or a string, it assumes it's a string
which will crash because RSTRING_PTR and RSTRING_LEN assumes it's a
string.
2024-01-17 15:51:44 -05:00
Peter Zhu
3a7ad808b1 [PRISM] Fix test_compile_prism_with_file
The test should be testing RubyVM::InstructionSequence.compile_prism
with a file but it is instead passing the file path (which is a string)
which raises a SyntaxError because it is not Ruby syntax.
2024-01-17 10:15:02 -05:00
Kevin Newton
44d0c5ae3f [PRISM] Raise syntax errors when found 2024-01-11 14:59:37 -05:00
eileencodes
049a9bd62f [PRISM] Fix compile_prism when src is a file
`compile_prism` can take a source and file (and other arguments) or a
file as the source. `compile` checks if the source is a file and if it
is converts it. `compile_prism` is now doing the same thing.

On the Ruby side `compile` handles a file
[here](https://github.com/ruby/ruby/blob/master/iseq.c#L1159-L1162).

Before:

```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(26,21)>
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] name@0
0000 putstring                              "Prism"                   (  25)[Li]
0002 setlocal                               name@0, 0
0005 putself                                                          (  26)[Li]
0006 putobject                              "hello, "
0008 getlocal                               name@0, 0
0011 dup
0012 objtostring                            <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
0014 anytostring
0015 concatstrings                          2
0017 send                                   <calldata!mid:puts, argc:1, FCALL|ARGS_SIMPLE>, nil
0020 leave
hello, Prism

"********* PRISM *************"
./test.rb:13:in `compile_prism': wrong argument type File (expected String) (TypeError)
	from ./test.rb:13:in `<main>'
make: *** [run] Error 1
```

After:

```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(26,21)>
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] name@0
0000 putstring                              "Prism"                   (  25)[Li]
0002 setlocal                               name@0, 0
0005 putself                                                          (  26)[Li]
0006 putobject                              "hello, "
0008 getlocal                               name@0, 0
0011 dup
0012 objtostring                            <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
0014 anytostring
0015 concatstrings                          2
0017 send                                   <calldata!mid:puts, argc:1, FCALL|ARGS_SIMPLE>, nil
0020 leave

"********* PRISM *************"
== disasm: #<ISeq:<compiled>@test_code.rb:24 (24,0)-(25,21)>
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] name@0
0000 putstring                              "Prism"                   (  24)[Li]
0002 setlocal                               name@0, 0
0005 putself                                                          (  25)[Li]
0006 putobject                              "hello, "
0008 getlocal                               name@0, 0
0011 dup
0012 objtostring                            <calldata!mid:to_s, argc:0, FCALL|ARGS_SIMPLE>
0014 anytostring
0015 concatstrings                          2
0017 send                                   <calldata!mid:puts, argc:1, FCALL|ARGS_SIMPLE>, nil
0020 leave                                                            (  24)
```

Fixes ruby/prism#1609
2023-12-15 10:27:44 -05:00
Nobuyoshi Nakada
7fe7b7bc5a
Fix portability of bignum in ISeq Binary Format
- Unless `sizeof(BDIGIT) == 4`, (8-byte integer not available), the
  size to be loaded was wrong.
- Since `BDIGIT`s are dumped as raw binary, the loaded byte order was
  inverted unless little-endian.
2023-11-26 11:42:02 +09:00
Nobuyoshi Nakada
ec3d81629f
[Bug #19906] Add the test 2023-10-01 23:58:24 +09:00
Nobuyoshi Nakada
435b243978
Suppress an unused variable warning 2023-09-13 10:24:55 +09:00
Nobuyoshi Nakada
6e64d43704 [Bug #19862] Skip compiled result of never reachable expression 2023-09-13 08:30:37 +09:00
Koichi Sasada
d68c01fd31 support rescue event for TracePoint
fix [Feature #19572]
2023-08-01 22:46:17 +09:00
Nobuyoshi Nakada
c7bacf84f0 [Bug #19611] Remove never-reachable branch in logical expression 2023-04-27 14:52:22 +09:00
lukeg
d2c6dca8f4 Fix small issues concerning namespacing in test-all suite
* Fix temporary methods on Object leaking across test cases.
* Remove temporary classes/modules leaking across test cases.
2023-03-17 08:46:37 +09:00
Aaron Patterson
dba61f487c return early if there is no is_entries buffer
If there is a compilation error, is_entries may not be allocated, but
ic_size could be greater than 0.  If we don't have a buffer to iterate
over, just return early.  Otherwise GC could segv

[Bug #19173]
2022-12-03 13:03:51 -06:00
Hiroshi SHIBATA
47bf64a26d
Use omit instead of skip: test/ruby/**/*.rb 2022-01-04 17:25:30 +09:00
Jeremy Evans
f53dfab95c Add support for anonymous rest and keyword rest argument forwarding
This allows for the following syntax:

```ruby
def foo(*)
  bar(*)
end
def baz(**)
  quux(**)
end
```

This is a natural addition after the introduction of anonymous
block forwarding.  Anonymous rest and keyword rest arguments were
already supported in method parameters, this just allows them to
be used as arguments to other methods.  The same advantages of
anonymous block forwarding apply to rest and keyword rest argument
forwarding.

This has some minor changes to #parameters output.  Now, instead
of `[:rest], [:keyrest]`, you get `[:rest, :*], [:keyrest, :**]`.
These were already used for `...` forwarding, so I think it makes
it more consistent to include them in other cases.  If we want to
use `[:rest], [:keyrest]` in both cases, that is also possible.

I don't think the previous behavior of `[:rest], [:keyrest]` in
the non-... case and `[:rest, :*], [:keyrest, :**]` in the ...
case makes sense, but if we did want that behavior, we'll have to
make more substantial changes, such as using a different ID in the
... forwarding case.

Implements [Feature #18351]
2021-12-30 14:37:42 -08:00
Koichi Sasada
df48db987d mandatory_only_cme should not be in def
`def` (`rb_method_definition_t`) is shared by multiple callable
method entries (cme, `rb_callable_method_entry_t`).

There are two issues:

* old -> young reference: `cme1->def->mandatory_only_cme = monly_cme`
  if `cme1` is young and `monly_cme` is young, there is no problem.
  Howevr, another old `cme2` can refer `def`, in this case, old `cme2`
  points young `monly_cme` and it violates gengc assumption.
* cme can have different `defined_class` but `monly_cme` only has
  one `defined_class`. It does not make sense and `monly_cme`
  should be created for a cme (not `def`).

To solve these issues, this patch allocates `monly_cme` per `cme`.
`cme` does not have another room to store a pointer to the `monly_cme`,
so this patch introduces `overloaded_cme_table`, which is weak key map
`[cme] -> [monly_cme]`.

`def::body::iseqptr::monly_cme` is deleted.

The first issue is reported by Alan Wu.
2021-12-21 11:03:09 +09:00
Koichi Sasada
cce331272b Ractor.make_shareable checks proc's sefl
`Ractor.make_shareable(proc_obj)` raises an `IsolationError`
if the self of `proc_obj` is not a shareable object.

[Bug #18243]
2021-12-09 16:20:04 +09:00
Koichi Sasada
e984c2a9ea fix to choose correct callcache
It should retun general `cc`, not for overloaded (mandatory only)
method call cache.

This issue is reported by @shugo and @ktou
https://twitter.com/shugomaeda/status/1463699797182119936
2021-11-26 10:56:03 +09:00
Yusuke Endoh
b5aa161383 test/ruby/test_iseq.rb: Avoid pollution of method namespace
20211125T003004Z.log.html.gz
```
[ 4780/21204] TestISeq#test_super_with_anonymous_block/home/chkbuild/chkbuild/tmp/build/20211125T003004Z/ruby/test/ruby/test_iseq.rb:141: warning: method redefined; discarding old touch3
/home/chkbuild/chkbuild/tmp/build/20211125T003004Z/ruby/test/ruby/test_iseq.rb:121: warning: previous definition of touch3 was here
 = 0.00 s
```
2021-11-25 11:01:27 +09:00
Yusuke Endoh
3454a456d1 test/ruby/test_iseq.rb: Use __LINE__ to make the error log easy to see 2021-11-24 15:42:00 +09:00
Jean Boussier
eb301d8aec Add an extra failing test case for [Bug #18250]
The parameter being called `req` specifically causes an assertion error:

```
Assertion failed: (key != 0), function hash_table_raw_insert, file id_table.c, line 153.
```

Renaming the parameter or removing the `*` doesn't reproduce.
2021-11-23 21:03:19 +09:00
Jeremy Evans
3c92516519 Fix test_super_with_anonymous_block test to use anonymous block 2021-11-19 11:05:09 -08:00
Jeremy Evans
4adb012926 Anonymous block forwarding allows a method to forward a passed
block to another method without having to provide a name for the
block parameter.

Implements [Feature #11256]

Co-authored-by: Yusuke Endoh mame@ruby-lang.org
Co-authored-by: Nobuyoshi Nakada nobu@ruby-lang.org
2021-11-18 14:17:57 -08:00
Yusuke Endoh
54bda176c6 test/ruby/test_iseq.rb: Prevent a warning
20211109T063003Z.log.html.gz
```
[ 9898/21145] TestISeq#test_super_with_block_and_kwrest/home/chkbuild/chkbuild/tmp/build/20211109T063003Z/ruby/test/ruby/test_iseq.rb:1: warning: method redefined; discarding old touch
/home/chkbuild/chkbuild/tmp/build/20211109T063003Z/ruby/test/ruby/test_iseq.rb:1: warning: previous definition of touch was here
 = 0.00 s
```
2021-11-09 17:31:17 +09:00
Nobuyoshi Nakada
ec657f44dc Refine the error message for hidden variables 2021-11-07 12:40:27 +09:00
Nobuyoshi Nakada
334b69e504 rb_id_serial_to_id: return unregistered ID as an internal ID
```ruby
def foo(*); ->{ super }; end
```

This code makes anonymous parameters which is not registered as an
ID.  The problem is that when Ractors try to scan `getlocal`
instructions, it puts the Symbol corresponding to the parameter
in to a hash.  Since it is not registered, we end up with a
strange exception.  This commit wraps the unregistered ID in an
internal ID so that we get the same exception for `...` as `*`.

Co-Authored-By: Aaron Patterson <tenderlove@ruby-lang.org>
Co-Authored-By: John Hawthorn <john@hawthorn.email>
2021-11-07 12:40:27 +09:00
Nobuyoshi Nakada
a202408180
Fix typos 2021-11-02 19:17:37 +09:00
Nobuyoshi Nakada
5e52fd4bd1
Preserve the encoding of message from outer local variable
In the case of read-only but refering an unshareable object.
2021-10-29 21:40:11 +09:00
Nobuyoshi Nakada
ec2e04c90d Preserve the encoding of message from outer local variables 2021-10-29 18:53:57 +09:00
Nobuyoshi Nakada
9b751db99c Fix script_lines in loaded iseq as nil 2021-10-29 06:39:57 +09:00
Aaron Patterson
217df51f0e Dump outer variables tables when dumping an iseq to binary
This commit dumps the outer variables table when dumping an iseq to
binary.  This fixes a case where Ractors aren't able to tell what outer
variables belong to a lambda after the lambda is loaded via ISeq.load_from_binary

[Bug #18232] [ruby-core:105504]
2021-10-07 15:39:47 -07:00
Alan Wu
b2fc592c30
Build CDHASH properly when loading iseq from binary
Before this change, CDHASH operands were built as plain hashes when
loaded from binary. Without setting up the hash with the correct
st_table type, the hash can sometimes be an ar_table. When the hash is
an ar_table, lookups can call the `eql?` method on keys of the hash,
which makes the `opt_case_dispatch` instruction not "leaf" as it
implicitly declares.

The following script trips the stack canary for checking the leaf
attribute for `opt_case_dispatch` on VM_CHECK_MODE > 0 (enabled by
default with RUBY_DEBUG).

    rb_vm_iseq = RubyVM::InstructionSequence

    iseq = rb_vm_iseq.compile(<<-EOF)
      case Class.new(String).new("foo")
      when "foo"
        42
      end
    EOF

    puts rb_vm_iseq.load_from_binary(iseq.to_binary).eval

This commit changes the binary loading logic to build CDHASH with the
right st_table type. The dumping logic and the dump format stays the
same
2021-05-21 12:13:55 -04:00
Nobuyoshi Nakada
555bd83a8e
Raise when loading unprovided builtin function [Bug #17192] 2020-11-30 15:19:49 +09:00
Nobuyoshi Nakada
44ad72fa21
Added assertions 2020-11-19 15:41:53 +09:00
Jeremy Evans
4a5c42db88 Make RubyVM::InstructionSequence.compile_file use same encoding as load
This switches the internal function from rb_parser_compile_file_path
to rb_parser_load_file, which is the same internal method that
Kernel#load uses.

Fixes [Bug #17308]
2020-11-19 07:12:50 +09:00
Nobuyoshi Nakada
7b2bea42a2
Unfreeze string-literal-only interpolated string-literal
[Feature #17104]
2020-09-30 22:15:28 +09:00
Benoit Daloze
9b535f3ff7 Interpolated strings are no longer frozen with frozen-string-literal: true
* Remove freezestring instruction since this was the only usage for it.
* [Feature #17104]
2020-09-15 21:32:35 +02:00
Takashi Kokubun
50c63ab38d
Fix a random test failure by TracePoint
A test worker process may already be enabling TracePoint, which results
in changing the insn name in this test.

http://ci.rvm.jp/results/trunk-random0@phosphorus-docker/3022750
2020-06-24 00:29:53 -07:00
Nobuyoshi Nakada
a9567cc2bf Added test for debug_level: option of RubyVM::InstructionSequence.compile 2020-04-15 16:06:48 +09:00
Koichi Sasada
251930cea0 support multi-run for test/ruby/test_iseq.rb
need to remove a Constant.
2020-01-28 15:23:51 +09:00
Koichi Sasada
c3693bbaaa catch up last commit.
Array#pack uses `opt_invokebuiltin_delegate_leave` now.
2019-11-18 10:24:38 +09:00
Takashi Kokubun
5c168c7e7f
Support RB_BUILTIN in ISeq#to_a 2019-11-09 21:40:38 -08:00
Alan Wu
99d3043bd8 Iseq#to_binary: dump flag for **nil (#2508)
RUBY_ISEQ_DUMP_DEBUG=to_binary and the attached test case was failing.
Dump the flag to make sure `**nil` can round-trip properly.
2019-10-02 16:05:40 +09:00
Nobuyoshi Nakada
3cee99808d [EXPERIMENTAL] Expression with modifier in
[Feature #15865]
2019-09-26 15:10:48 +09:00
Alan Wu
050b932152
Iseq#to_binary: Add support for NoMatchingPatternError and TypeError
Binary dumping the iseq for `case foo in []; end` used to crash as
there was no handling for these exception classes.

Pattern matching generates these classes as operands to `putobject`.

[Bug #16088]
Closes: https://github.com/ruby/ruby/pull/2325
2019-08-09 01:29:53 +09:00
Nobuyoshi Nakada
cc68084652
Suppress method redefinition warnings 2019-06-30 11:49:41 +09:00
Nobuyoshi Nakada
409b0ec4c3
Suppress unused variable warnings 2019-06-30 11:45:37 +09:00
Nobuyoshi Nakada
f8b64e5f2c
Use assigned local variable 2019-06-28 15:04:25 +09:00