Commit graph

20692 commits

Author SHA1 Message Date
Stan Lo
b315826377 [ruby/irb] Support repeating debugger input by passing empty input
to it
(https://github.com/ruby/irb/pull/856)

* Test IRB's behaviour with empty input

* Handle empty input and pass it to debugger

Since `rdbg` accepts empty input to repeat the previous command, IRB
should take empty input in `irb:rdbg` sessions and pass them to the
debugger.

Currently, IRB simply ignores empty input and does nothing. This commit
creates `EmptyInput` to represent empty input so it can fit into the
current IRB's input processing flow in `Irb#eval_input`.

0e9db419be
2024-02-16 16:12:54 +00:00
Haldun Bayhantopcu
f4f57e1162 [ruby/prism] Add warning for assignments to literals in conditionals
ee87ed08fb
2024-02-16 15:33:07 +00:00
Max Prokopiev
f012ce0d18 [ruby/prism] Fix lexing of foo! when it's a first thing to parse
7597aca76a
2024-02-16 14:08:56 +00:00
Hiroshi SHIBATA
cf16a70448 [rubygems/rubygems] Relax regexp for Ruby 3.3 or before
89d37e670c
2024-02-16 05:38:42 +00:00
Hiroshi SHIBATA
1c41dc40d8 [ruby/forwardable] Relax regexp for Ruby 3.3 or before
42b50c2111
2024-02-16 02:12:08 +00:00
Kevin Newton
14a7277da1 [ruby/prism] Speed up creating Ruby AST
When creating the Ruby AST, we were previously allocating Location
objects for every node and every inner location. Instead, this
commit changes it to pack both the start offset and length into a
single u64 and pass that into the nodes. Then, when the locations
are requested via a reader method, we lazily allocate the Location
objects.

de203dca83

Co-Authored-By: Aaron Patterson <tenderlove@ruby-lang.org>
2024-02-15 20:39:50 +00:00
Max Prokopiev
49ddbbf39f [ruby/prism] Fix opening loc for an empty symbol
241d0169da
2024-02-15 20:28:17 +00:00
Noah Gibbs
a5cee8fa79 [ruby/prism] Handle more aliases. Better testing of prism ripper CLI and a test for it.
cfd4f28cb3
2024-02-15 20:26:31 +00:00
Haldun Bayhantopcu
a021702596 [ruby/prism] Fix parsing rescue modifier
b7407ae3c0
2024-02-15 16:40:36 +00:00
Yusuke Endoh
a7718c914a Do not show an anonymous class as a receiver 2024-02-15 20:43:11 +09:00
Yusuke Endoh
9d1b000bd1 Show the method owner in backtraces
```
test.rb:1:in 'Object#toplevel_meth': unhandled exception
        from test.rb:4:in 'Foo.class_meth'
        from test.rb:6:in 'Foo#instance_meth'
        from test.rb:11:in 'singleton_meth'
        from test.rb:13:in '<main>'
```

[Feature #19117]
2024-02-15 19:11:58 +09:00
Yusuke Endoh
d5c16ddfcb Temporarily update the error message format in prism 2024-02-15 18:42:31 +09:00
Yusuke Endoh
25d74b9527 Do not include a backtick in error messages and backtraces
[Feature #16495]
2024-02-15 18:42:31 +09:00
Nobuyoshi Nakada
c57880e68d
Show the invalid source encoding in messages 2024-02-15 13:39:33 +09:00
Kevin Newton
9933377c34 [PRISM] Correctly hook up line numbers for eval 2024-02-14 15:29:26 -05:00
Kevin Newton
fc2c128e7e [PRISM] Set eval encoding based on string encoding 2024-02-14 13:24:43 -05:00
Alan Wu
ee3b4bec0e
YJIT: Simplify Kernel#send guards and admit more cases (#9956)
Previously, our compile time check rejected dynamic symbols (e.g. what
String#to_sym could return) even though we could handle them just fine.
The runtime guards for the type of method name was also overly
restrictive and didn't accept dynamic symbols.

Fold the type check into the rb_get_symbol_id() and take advantage of
the guard already checking for 0. This also avoids generating the same
call twice in case the same method name is presented as different
types.
2024-02-14 11:19:04 -05:00
Benoit Daloze
c2d8d6eba6 Initialize the Prism::Source directly with all 3 fields for the C extension
* Faster that way:
  $ ruby -Ilib -rprism -rbenchmark/ips -e 'Benchmark.ips { |x| x.report("parse") { Prism.parse("1 + 2") } }'
  195.722k (± 0.5%) i/s
  rb_iv_set():
  179.609k (± 0.5%) i/s
  rb_funcall():
  190.030k (± 0.3%) i/s
  before this PR:
  183.319k (± 0.4%) i/s
2024-02-14 15:48:33 +00:00
Stan Lo
c84581f1d9 [ruby/irb] Repurpose the help command to display the help message
(https://github.com/ruby/irb/pull/872)

See #787 for more details.

d9192d92d0
2024-02-14 13:47:45 +00:00
tomoya ishida
d0412599e0 [ruby/irb] Improve constant lookup in SourceFinder
(https://github.com/ruby/irb/pull/871)

87c279ccf2
2024-02-14 13:46:49 +00:00
Kevin Newton
1fc7c79dea
[PRISM] Exclude additional tests from rebase 2024-02-13 21:40:47 -05:00
Kevin Newton
5de3e3057a [PRISM] Add temporary test exclusions to get eval passing 2024-02-13 21:19:12 -05:00
Matt Valentine-House
edb755c84b [PRISM] Add eval tests 2024-02-13 21:19:12 -05:00
Kevin Newton
b1964a9204 [ruby/prism] Add code unit APIs to location
LSPs need this because the protocol dictates that you return code
units for offsets. None of our existing APIs provided that
information, and since we hid the source it's not nearly as useful
for them. Now they can pass an encoding directly to:

* Location#start_code_units_offset
* Location#end_code_units_offset
* Location#start_code_units_column
* Location#end_code_units_column

4757a2cc06

Co-Authored-By: Vinicius Stock <vinicius.stock@shopify.com>
2024-02-13 20:10:25 +00:00
Kevin Newton
29d04bb0c4 [ruby/prism] Introduce version: "3.4.0"
This is effectively an alias for "latest" right now. In the future
it will change to be its own enum value.

2c86036022
2024-02-13 18:26:28 +00:00
Koichi ITO
246005f5bd [ruby/prism] Fix an error when specifying the parsing version latest
This PR fixes following error when using `version: latest` argument.

```console
$ ruby -rprism -e "p Prism.parse('-> { it }', version: 'latest')"
-e:1:in `parse': invalid version: latest (ArgumentError)

p Prism.parse('-> { it }', version: 'latest')
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        from -e:1:in `<main>'
```

The argument `version: latest` in the added test is commented as potentially being
better replaced with `version: 3.4.0` in the future.

27b5c933cb
2024-02-13 18:15:48 +00:00
Peter Zhu
a71d1ed838 Fix memory leak when parsing invalid hash symbol
For example:

    10.times do
      100_000.times do
        eval('{"\xC3": 1}')
      rescue EncodingError
      end

      puts `ps -o rss= -p #{$$}`
    end

Before:

    32032
    48464
    66112
    84192
    100592
    117520
    134096
    150656
    167168
    183760

After:

    17120
    17120
    17120
    17120
    18560
    18560
    18560
    18560
    18560
    18560
2024-02-13 11:05:56 -05:00
Nikita Vasilevsky
d357d50f0a [PRISM] Fix lambda start column number
Co-Authored-By: Kevin Newton <kddnewton@gmail.com>
2024-02-13 09:39:28 -05:00
Vinicius Stock
dedca31804 [ruby/prism] Fix full_name for constant path targets
84c10f3a2d
2024-02-13 14:09:19 +00:00
Stan Lo
2f0f95235a [ruby/irb] Fix SourceFinder's constant evaluation issue
(https://github.com/ruby/irb/pull/869)

Currently, if the signature's constant part is not defined, a NameError
would be raised.

```
irb(main):001> show_source Foo
(eval):1:in `<top (required)>': uninitialized constant Foo (NameError)

Foo
^^^
        from (irb):1:in `<main>'
```

This commit fixes the issue and simplifies the `edit` command's implementation.

8c16e029d1
2024-02-13 13:36:32 +00:00
Stan Lo
ec26786b1a [ruby/irb] Refactor eval_path and SourceFinder::Source
(https://github.com/ruby/irb/pull/870)

* Assign `@eval_path` through `irb_path=` method

This simplifies the original caching logic for the `eval_path` method
and makes it easier to understand.

* Refactor SourceFinder::Source

c63e4c4035
2024-02-13 13:33:36 +00:00
Otávio Schwanck dos Santos
e848848b58
[ruby/reline] C for vi mode
(https://github.com/ruby/reline/pull/472)

d197be7c44
2024-02-13 18:57:17 +09:00
Hiroya Fujinami
4a6384ed93
Fix [Bug #20246]: Don't set next_head_exact if a capture is called (#9897) 2024-02-13 09:53:03 +09:00
yui-knk
8a345860d3 Warn duplication of __ENCODING__ on the hash
```
$ ruby -e 'h = { __ENCODING__ => 1, __ENCODING__ => 2 }'
-e:1: warning: key #<Encoding:UTF-8> is duplicated and overwritten on line 1
```
2024-02-13 08:40:14 +09:00
Alan Wu
2131d04f43 YJIT: Add support for **kwrest parameters
Now that `...` uses `**kwrest` instead of regular splat and
ruby2keywords, we need to support these type of methods to
support `...` well.
2024-02-12 13:57:24 -05:00
tomoya ishida
7af97dc71f [ruby/irb] Powerup show_source by enabling RubyVM.keep_script_lines
(https://github.com/ruby/irb/pull/862)

* Powerup show_source by enabling RubyVM.keep_script_lines

* Add file_content field to avoid reading file twice while show_source

* Change path passed to eval, don't change irb_path.

* Encapsulate source coloring logic and binary file check insode class Source

* Add edit command testcase when irb_path does not exist

* Memoize irb_path existence to reduce file existence check calculating eval_path

239683a937
2024-02-12 18:38:30 +00:00
Alan Wu
e878bbd641 Allow foo(**nil, &block_arg)
Previously, `**nil` by itself worked, but if you add a block argument,
it raised a conversion error. The presence of the block argument
shouldn't change how keyword splat works.

See: <https://bugs.ruby-lang.org/issues/20064>
2024-02-12 13:02:50 -05:00
Kevin Newton
e08c128417 [ruby/prism] Error messages closer to CRuby
19ffa0b980
2024-02-12 18:01:45 +00:00
Kevin Newton
94bc5ad30a [ruby/prism] ruby_parser translator
1925b970c7
2024-02-12 17:54:54 +00:00
Kevin Newton
c3886c12dc [ruby/prism] Fix unary not location
861689f6d1
2024-02-12 16:27:49 +00:00
Noah Gibbs
16b39072a5 [ruby/prism] Move Prism::RipperCompat to Prism::Translation::Ripper
c0331abe4f
2024-02-12 15:57:57 +00:00
Kevin Newton
78deba1aa1 [ruby/prism] Unary not name location
78190d2999
2024-02-12 15:57:17 +00:00
tomoya ishida
06995eb45b [ruby/irb] Fix exit! command warning and method behavior
(https://github.com/ruby/irb/pull/868)

* Fix exit! command warning and method behavior

* Remove arg(0) from Kernel.exit and Kernel.exit!

372bc59bf5
2024-02-12 11:28:54 +00:00
Jeremy Evans
c20e819e8b Fix crash when passing large keyword splat to method accepting keywords and keyword splat
The following code previously caused a crash:

```ruby
h = {}
1000000.times{|i| h[i.to_s.to_sym] = i}
def f(kw: 1, **kws) end
f(**h)
```

Inside a thread or fiber, the size of the keyword splat could be much smaller
and still cause a crash.

I found this issue while optimizing method calling by reducing implicit
allocations.  Given the following code:

```ruby
def f(kw: , **kws) end
kw = {kw: 1}
f(**kw)
```

The `f(**kw)` call previously allocated two hashes callee side instead of a
single hash.  This is because `setup_parameters_complex` would extract the
keywords from the keyword splat hash to the C stack, to attempt to mirror
the case when literal keywords are passed without a keyword splat.  Then,
`make_rest_kw_hash` would build a new hash based on the extracted keywords
that weren't used for literal keywords.

Switch the implementation so that if a keyword splat is passed, literal keywords
are deleted from the keyword splat hash (or a copy of the hash if the hash is
not mutable).

In addition to avoiding the crash, this new approach is much more
efficient in all cases.  With the included benchmark:

```
                                1
            miniruby:   5247879.9 i/s
     miniruby-before:   2474050.2 i/s - 2.12x  slower

                        1_mutable
            miniruby:   1797036.5 i/s
     miniruby-before:   1239543.3 i/s - 1.45x  slower

                               10
            miniruby:   1094750.1 i/s
     miniruby-before:    365529.6 i/s - 2.99x  slower

                       10_mutable
            miniruby:    407781.7 i/s
     miniruby-before:    225364.0 i/s - 1.81x  slower

                              100
            miniruby:    100992.3 i/s
     miniruby-before:     32703.6 i/s - 3.09x  slower

                      100_mutable
            miniruby:     40092.3 i/s
     miniruby-before:     21266.9 i/s - 1.89x  slower

                             1000
            miniruby:     21694.2 i/s
     miniruby-before:      4949.8 i/s - 4.38x  slower

                     1000_mutable
            miniruby:      5819.5 i/s
     miniruby-before:      2995.0 i/s - 1.94x  slower
```
2024-02-11 22:48:38 -08:00
eileencodes
a3ceb69168 [PRISM] Fix error handling in pm_parse_prism
Following changes made in ruby/prism#2365 this implements error handling
for when `pm_string_mapped_init` returns `false`.

Related: ruby/prism#2207
2024-02-11 09:41:20 -05:00
Stan Lo
5c4657f883 [ruby/irb] Polish the exit! command and its tests
(https://github.com/ruby/irb/pull/867)

* Remove IRB.irb_exit! method

It's not necessary to introduce a new method just for the exit! command
at this moment.

* Rename ExitForcedAction to ForceExit

* Move force exit tests to a dedicated file

* Fix nested history saving with exit! command

Because we switched to use `Kernel#exit` instead of `exit!`, the outer
session's ensure block in `Irb#run` will be run, which will save the
history. This means the separate check to save history when force exiting
is no longer necessary.

* execute_lines helper should also capture IRB setup's output

This prevents setup warnings from being printed to test output
while allowing those output to be tested.

* Update readme

899d10ade1
2024-02-11 05:17:40 +00:00
Ignacio Chiazzo Cardarello
429eeb09f2 [ruby/irb] Introduce exit! command
(https://github.com/ruby/irb/pull/851)

* Added failing test for when writing history on exit

* Save history on exit

* Exit early when calling Kernel.exit

* use status 0 for kernel.exit

* Added test for nested sessions

* Update lib/irb.rb

---------

c0a5f31679

Co-authored-by: Stan Lo <stan001212@gmail.com>
2024-02-10 22:07:53 +00:00
yui-knk
ea91ab696e Fix constant name of Ractor::IsolationError message
`dest` of `const_decl_path` is `NODE_COLON2` or `NODE_COLON3` in some cases.
For example, `B::C ||= [“Not ” + “shareable”]` passes `NODE_COLON2`
and `::C ||= [“Not ” + “shareable”]` passes `NODE_COLON3`.
This commit fixes `Ractor::IsolationError` message for such case.

```
# shareable_constant_value: literal
::C ||= ["Not " + "shareable"]

# Before
# => cannot assign unshareable object to C (Ractor::IsolationError)

# After
# => cannot assign unshareable object to ::C (Ractor::IsolationError)
```
2024-02-10 09:23:17 +09:00
yui-knk
bf72cb84ca Include the first constant name into Ractor::IsolationError message
If lhs of assignment is top-level constant reference, the first
constant name is omitted from error message.
This commit fixes it.

```
# shareable_constant_value: literal
::C = ["Not " + "shareable"]

# Before
# => cannot assign unshareable object to  (Ractor::IsolationError)

# After
# => cannot assign unshareable object to ::C (Ractor::IsolationError)
```
2024-02-10 09:23:17 +09:00
Kevin Newton
e96c838ca4 [PRISM] Fix flaky memory in scope nodes 2024-02-09 16:30:07 -05:00