90661 Commits

Author SHA1 Message Date
Benoit Daloze
65bc0ec62d [ruby/prism] Fix fork check in ractor_test.rb
https://github.com/ruby/prism/commit/0073266cad
2025-03-20 21:24:19 +00:00
Kevin Newton
e47078fb30 [ruby/prism] Update ractor_test.rb per review
https://github.com/ruby/prism/commit/fd96a6821f
2025-03-20 17:23:36 -04:00
Earlopain
ab8b199be8 [ruby/prism] Add Prism::Translation::ParserCurrent
It's not my favorite api but for users that currently use the same thing
from `parser`, moving over is more difficult
than it needs to be.

If you plan to support both old and new ruby versions, you definitly need to
branch somewhere on the ruby version
to either choose prism or parser.
But with prism you then need to enumerate all the versions again and choose the correct one.

Also, don't recommend to use `Prism::Translation::Parser` in docs. It's version-less
but actually always just uses Ruby 3.4 which is probably
not what the user intended.

Note: parser also warns when the patch version doesn't match what it expects. But I don't think prism has such a concept,
and anyways it would require releases anytime ruby releases, which I don't think is very desirable

https://github.com/ruby/prism/commit/77177f9e92
2025-03-20 21:20:23 +00:00
John Hawthorn
bfe6068417 Use atomic for method reference count [Bug #20934]
This changes reference_count on rb_method_definition_struct into an
atomic.

Ractors can create additional references as part of `bind_call` or
(presumably) similar. Because this can be done inside Ractors, we should
use a lock or atomics so that we don't race and avoid incrementing.

Co-authored-by: wanabe <s.wanabe@gmail.com>
2025-03-20 13:09:40 -07:00
Aaron Patterson
62cc3464d9 Remove leading nop from block when we don't need it
Blocks insert a leading `nop` instruction in order to execute a "block
call" tracepoint. Block compilation unconditionally inserts a leading
`nop` plus a label after the instruction:

  641f15b1c6/prism_compile.c (L6867-L6869)

This `nop` instruction is used entirely for firing the block entry
tracepoint.  The label exists so that the block can contain a loop but
the block entry tracepoint is executed only once.

For example, the following code is an infinite loop, but should only
execute the b_call tracepoint once:

```ruby
-> { redo }.call
```

Previous to this commit, we would eliminate the `nop` instruction, but
only if there were no other jump instructions inside the block.  This
means that the following code would still contain a leading `nop` even
though the label following the `nop` is unused:

```ruby
-> { nil if bar }
```

```
== disasm: #<ISeq:block in <main>@test.rb:1 (1,2)-(1,17)> (catch: FALSE)
0000 nop                                                              (   1)[Bc]
0001 putself                                [Li]
0002 opt_send_without_block                 <calldata!mid:bar, argc:0, FCALL|VCALL|ARGS_SIMPLE>
0004 branchunless                           8
0006 putnil
0007 leave                                  [Br]
0008 putnil
0009 leave                                  [Br]
```

This commit checks to see if the label inserted after the `nop` is
actually a jump target.  If it's not a jump target, then we should be
safe to eliminate the leading `nop`:

```
> build-master/miniruby --dump=insns test.rb
== disasm: #<ISeq:<main>@test.rb:1 (1,0)-(1,17)>
0000 putspecialobject                       1                         (   1)[Li]
0002 send                                   <calldata!mid:lambda, argc:0, FCALL>, block in <main>
0005 leave

== disasm: #<ISeq:block in <main>@test.rb:1 (1,2)-(1,17)>
0000 putself                                                          (   1)[LiBc]
0001 opt_send_without_block                 <calldata!mid:bar, argc:0, FCALL|VCALL|ARGS_SIMPLE>
0003 branchunless                           7
0005 putnil
0006 leave                                  [Br]
0007 putnil
0008 leave                                  [Br]
```

We have a test for b_call tracepoints that use `redo` here:

  aebf96f371/test/ruby/test_settracefunc.rb (L1728-L1736)
2025-03-20 11:49:13 -07:00
Sam Bostock
f07af59a2f [ruby/prism] Dynamically register events to dispatch
Instead of requiring the consumer to provide a list of all events which
they wish to handle, we can give them to option of dynamically detecting
them, by scanning the listener's public methods.

This approach is similar to that used by Minitest (scanning for `test_`
methods) and Rails generators (running all public methods in the order
they are defined).

While this is slower than specifying a hard coded list, the penalty is
only during registration. There is no change the the behaviour of
dispatching the events.

https://github.com/ruby/prism/commit/781ebed743
2025-03-20 17:28:59 +00:00
Jean Boussier
de097fbe5f Trigger inherited and const_set callbacks after const has been defined
[Misc #21143]
[Bug #21193]

The previous change caused a backward compatibility issue with code
that called `Object.const_source_location` from the `inherited` callback.

To fix this, the order is now:

- Define the constant
- Invoke `inherited`
- Invoke `const_set`
2025-03-20 18:18:11 +01:00
Nobuyoshi Nakada
a51364f54b
Close reader pipes 2025-03-20 20:59:42 +09:00
Nobuyoshi Nakada
820c541671 [Bug #21026] no singleton method on pseudo variable literal 2025-03-20 17:32:26 +09:00
Kevin Newton
641f15b1c6 [ruby/prism] Mark Prism as ractor-safe
https://github.com/ruby/prism/commit/c02429765b
2025-03-19 21:11:57 +00:00
Kevin Newton
050ffab82b [ruby/prism] Polyfill Kernel#warn category parameter
https://github.com/ruby/prism/commit/d85c72a1b9
2025-03-19 21:03:18 +00:00
Earlopain
b5e9a2da4c [ruby/prism] Remove category keyword from warn call
`category` is only supported from Ruby 3.0 onwards and prism can still run with Ruyb 2.7

https://github.com/ruby/prism/commit/335a193851
2025-03-19 21:03:17 +00:00
Earlopain
e5e160475b [ruby/prism] Warn when the parser translator receives an incompatible builder class
In https://github.com/ruby/prism/pull/3494 I added a bit of code
so that using the new builder doesn't break stuff.
This code can be dropped when it is enforced that builder
is _always_ the correct subclass (and makes future issues like that unlikely).

https://github.com/ruby/prism/commit/193d4b806d
2025-03-19 21:03:17 +00:00
git
4e11ea4268 Update default gems list at 265dcd1733ede8f3e4cb13322392d6 [ci skip] 2025-03-19 08:01:00 +00:00
Nobuyoshi Nakada
265dcd1733 [ruby/optparse] bump up to 0.7.0.dev.2
https://github.com/ruby/optparse/commit/8c2c7a4903
2025-03-19 08:00:01 +00:00
Nobuyoshi Nakada
00c84f4d49
A comment for TestRubyOptions::ExpectedStderrList [ci skip] 2025-03-19 15:19:06 +09:00
Nobuyoshi Nakada
6c7f721f1e
Source path may or may not exist 2025-03-19 15:08:20 +09:00
Yusuke Endoh
3eb802fb56 Loosen SEGV message testing
Since `rb_bug` does not always take Ruby frame info during SEGV, the
source file path may not be output.

```
  1) Failure:
TestRubyOptions#test_crash_report_script [/tmp/ruby/src/trunk_gcc11/test/ruby/test_rubyoptions.rb:907]:
Expected /
        bug\.rb:(?:1:)?\s\[BUG\]\sSegmentation\sfault.*\n
      /x
to match
  "[BUG] Segmentation fault at 0x000003e900328766\n"+
```
http://ci.rvm.jp/results/trunk_gcc11@ruby-sp2-noble-docker/5663880
2025-03-19 14:57:15 +09:00
git
e391c33734 Update default gems list at 6e9568d202389dc5f820b024315cf8 [ci skip] 2025-03-18 19:07:40 +00:00
Kevin Newton
6e9568d202 [ruby/prism] Bump to v1.4.0
https://github.com/ruby/prism/commit/71d31db496
2025-03-18 19:06:34 +00:00
Kevin Newton
adaaa7878e Handle void expressions in defined?
[Bug #21029]
2025-03-18 14:44:28 -04:00
Kevin Newton
b003d40194 Fix up merge conflicts for prism sync 2025-03-18 13:36:53 -04:00
Kevin Newton
33aaa069a4 [ruby/prism] Update truffleruby version
https://github.com/ruby/prism/commit/2afe89f8ce
2025-03-18 13:36:53 -04:00
Earlopain
90d38ddb47 [ruby/prism] Fix merge mishap
Caused by https://github.com/ruby/prism/pull/3478 and https://github.com/ruby/prism/pull/3443

I also made the builder reference more explicit to clearly distinquish
between `::Parser` and `Prism::Translation::Parser`

https://github.com/ruby/prism/commit/d52aaa75b6
2025-03-18 13:36:53 -04:00
Kevin Newton
dc48c1aca3 [ruby/prism] Add a multiple statements flag to parentheses
This can get triggered even if the list of statements only contains
a single statement. This is necessary to properly support compiling

```ruby
defined? (;a)
defined? (a;)
```

as "expression". Previously these were parsed as statements lists
with single statements in them.

https://github.com/ruby/prism/commit/b63b5d67a9
2025-03-18 13:36:53 -04:00
Earlopain
e3c8464630 [ruby/prism] Only unnest parser mlhs nodes when no rest argument is provided
```
(a,), = []

PARSER====================
s(:masgn,
  s(:mlhs,
    s(:mlhs,
      s(:lvasgn, :a))),
  s(:array))
PRISM====================
s(:masgn,
  s(:mlhs,
    s(:lvasgn, :a)),
  s(:array))
```

https://github.com/ruby/prism/commit/8aa1f4690e
2025-03-18 13:36:53 -04:00
Earlopain
94e12ffa39 [ruby/prism] Fix parser translator multiline interpolated symbols
In 2637007929 I added tests but didn't modify them correctly

https://github.com/ruby/prism/commit/de021e74de
2025-03-18 13:36:53 -04:00
Earlopain
a8adf5e006 [ruby/prism] Further refine string handling in the parser translator
Mostly around newlines and line continuation.
* percent arrays need special backslash handling in the ast
* Fix offset issue for heredocs with many line continuations (used wrong variable as index access)
* More refined rules on when to simplify string tokens
* Handle line continuations in squiggly heredocs
* Correctly dedent squiggly heredocs with interpolation
* Consider `':foo:` and `%s[foo]` to not be interpolation

https://github.com/ruby/prism/commit/4edfe9d981
2025-03-18 13:36:53 -04:00
Earlopain
fc14d3ac7d [ruby/prism] Allow to test a custom fixtures path during testing
Of course, these won't really be fixtures, but it allows to test against whole codebases
without copying them, doing symlinks or something like that.

For example, I can tell that over the whole RuboCop codebase, there are only 8 files that produce mismatched ast.
Telling what the problem is is a different problem. The ast for real files can and will be huge so I haven't checked yet
(maybe parser bug) but it's nice for discoverability regardless

https://github.com/ruby/prism/commit/2184d82ba6
2025-03-18 13:36:53 -04:00
Kevin Newton
0b4604d5a0 [ruby/prism] Use Set.new over to_set
https://github.com/ruby/prism/commit/422d5c4c64
2025-03-18 13:36:53 -04:00
Earlopain
ad478de3f0 [ruby/prism] Optimize array inclusion checks in the parser translator
I see `Array.include?` as 2.4% runtime. Probably because of `LPAREN_CONVERSION_TOKEN_TYPES` but
the others will be faster as well.

Also remove some inline array checks. They are specifically optimized in Ruby since 3.4, but for now prism is for >= 2.7

https://github.com/ruby/prism/commit/ca9500a3fc
2025-03-18 13:36:53 -04:00
Earlopain
d5503444fd [ruby/prism] Fix parser translator crash for certain octal escapes
`Integer#chr` performs some validation that we don't want/need. Octal escapes can go above 255, where it will then raise trying to convert.

`append_as_bytes` actually allows to pass a number, so we can just skip that call.
Although, on older rubies of course we still need to handle this in the polyfill.
I don't really like using `pack` but don't know of another way to do so.

For the utf-8 escapes, this is not an issue. Invalid utf-8 in these is simply a syntax error.

https://github.com/ruby/prism/commit/161c606b1f
2025-03-18 13:36:53 -04:00
Kevin Newton
1944247a0e [ruby/prism] Handle control and meta escapes in parser translation
https://github.com/ruby/prism/commit/09c59a3aa5
2025-03-18 13:36:53 -04:00
Earlopain
fd7a10cf4a [ruby/prism] Further refine string handling in the parser translator
Mostly around newlines and line continuation.
* percent arrays need special backslash handling in the ast
* Fix offset issue for heredocs with many line continuations (used wrong variable as index access)
* More refined rules on when to simplify string tokens
* Handle line continuations in squiggly heredocs
* Correctly dedent squiggly heredocs with interpolation
* Consider `':foo:` and `%s[foo]` to not be interpolation

https://github.com/ruby/prism/commit/4edfe9d981
2025-03-18 13:36:53 -04:00
Earlopain
5d138f2b43 [ruby/prism] Better handle regexp in the parser translator
Turns out, it was already almost correct. If you disregard \c and \M style escapes, only a single character is allowed to be escaped in a regex so most tests passed already.

There was also a mistake where the wrong value was constructed for the ast, this is now fixed.
One test fails because of this, but I'm fairly sure it is because of a parser bug. For `/\“/`, the backslash is supposed to be removed because it is a multibyte character. But tbh,
I don't entirely understand all the rules.

Fixes more than half of the remaining ast differences for rubocop tests

https://github.com/ruby/prism/commit/e1c75f304b
2025-03-18 13:36:53 -04:00
Earlopain
177adf6fa5 [ruby/prism] Fix parser translator tokens for %-arrays with whitespace escapes
Also fixes a token incompatibility for the word separator. parser only considers whitespace until the first newline

https://github.com/ruby/prism/commit/bd3dd2b62a
2025-03-18 13:36:53 -04:00
Earlopain
ac728389e2 [ruby/prism] Fix parser translator edge-case when multiline string ends with \n
When the line contains no real newline but contains unescaped ones, then there will be one less entry

https://github.com/ruby/prism/commit/4ef093b600
2025-03-18 13:36:53 -04:00
Earlopain
0fcb7fc21d [ruby/prism] Better handle all kinds of multiline strings in the parser translator
This is a followup to #3373, where the implementation
was extracted

https://github.com/ruby/prism/commit/2637007929
2025-03-18 13:36:53 -04:00
Earlopain
acf404e20e [ruby/prism] Fix an incompatibility with the parser translator
The offset cache contains an entry for each byte so it can't be accessed via the string length.

Adds tests for all variants except for this:
```
"fo
o" "ba
’"
```

For some reason, this still has the wrong offset.

https://github.com/ruby/prism/commit/a651126458
2025-03-18 13:36:53 -04:00
Earlopain
f49a0114e3 [ruby/prism] Fix parser translator rescue location with semicolon body
There are a few other locations that should be included in that check.
I think the end location must always be present but I left it in to be safe (maybe implicit begin somehow?)

https://github.com/ruby/prism/commit/545d07ddc3
2025-03-18 13:36:53 -04:00
Kevin Newton
12541d2cc0 [ruby/prism] Track then keyword on rescue nodes
https://github.com/ruby/prism/commit/bde8ccc038
2025-03-18 13:36:53 -04:00
Earlopain
a679597547 [ruby/prism] Fix parser translator crash for certain octal escapes
`Integer#chr` performs some validation that we don't want/need. Octal escapes can go above 255, where it will then raise trying to convert.

`append_as_bytes` actually allows to pass a number, so we can just skip that call.
Although, on older rubies of course we still need to handle this in the polyfill.
I don't really like using `pack` but don't know of another way to do so.

For the utf-8 escapes, this is not an issue. Invalid utf-8 in these is simply a syntax error.

https://github.com/ruby/prism/commit/161c606b1f
2025-03-18 13:36:53 -04:00
Earlopain
bc506295a3 [ruby/prism] Further refine string handling in the parser translator
Mostly around newlines and line continuation.
* percent arrays need special backslash handling in the ast
* Fix offset issue for heredocs with many line continuations (used wrong variable as index access)
* More refined rules on when to simplify string tokens
* Handle line continuations in squiggly heredocs
* Correctly dedent squiggly heredocs with interpolation
* Consider `':foo:` and `%s[foo]` to not be interpolation

https://github.com/ruby/prism/commit/4edfe9d981
2025-03-18 13:36:53 -04:00
Earlopain
9e5e3f1bed [ruby/prism] Add a custom builder class for the parser translator
I want to add new node types to the parser translator, for example `itblock`. The bulk of the work is already done by prism itself. In the `parser`
builder, this would be a 5-line change at most but we don't control that here.

Instead, we can add our own builder and either overwrite the few methods we need,
or just inline the complete builder. I'm not sure yet which would be better.

`rubocop-ast` uses its own builder for `parser`. For this to correctly work, it must explicitly choose to extend the
prism builder and use it, same as it currently chooses to use a different parser when prism is used.

I'd like to enforce that the builder for prism extends its custom one since it will lead to
some pretty weird issues otherwise. But first, I'd like to change `rubocop-ast` to make use of this.

https://github.com/ruby/prism/commit/b080e608a8
2025-03-18 13:36:53 -04:00
Kevin Newton
fcd6e53693 Remove incorrectly committed snapshots 2025-03-18 13:36:53 -04:00
Earlopain
705bd6fadb [ruby/prism] Fix parser translator when unescaping invalid utf8
1. The string starts out as binary
2. `ち` is appended, forcing it back into utf-8
3. Some invalid byte sequences are tried to append

> incompatible character encodings: UTF-8 and BINARY (ASCII-8BIT)

This makes use of my wish to use `append_as_bytes`. Unfortunatly that method is rather new
so it needs a fallback

https://github.com/ruby/prism/commit/e31e94a775
2025-03-18 13:36:53 -04:00
YO4
9c71b5901c fix rb_w32_strerror when errno < 0
change SystemCallError.new(-1) message on Windows

(Bug #21083)
https://bugs.ruby-lang.org/issues/21083
2025-03-19 01:28:59 +09:00
YO4
c717dbdc81 avoid platform dependent message 2025-03-19 01:28:59 +09:00
YO4
f220866c39 Explicitly place a regular expression
Co-authored-by: Nobuyoshi Nakada <nobu.nakada@gmail.com>
2025-03-19 01:28:59 +09:00
YO4
0f6c647b1a avoid platform dependent message
(Bug #21083)
https://bugs.ruby-lang.org/issues/21083
2025-03-19 01:28:59 +09:00