[#108552] [Ruby master Bug#18782] Race conditions in autoload when loading the same feature with multiple threads. — "ioquatix (Samuel Williams)" <noreply@...>
Issue #18782 has been reported by ioquatix (Samuel Williams).
11 messages
2022/05/14
[ruby-core:108655] [Ruby master Bug#18797] Third argument to Regexp.new is a bit broken
From:
janosch-x <noreply@...>
Date:
2022-05-23 10:11:21 UTC
List:
ruby-core #108655
Issue #18797 has been reported by janosch-x (Janosch Müller).
----------------------------------------
Bug #18797: Third argument to Regexp.new is a bit broken
https://bugs.ruby-lang.org/issues/18797
* Author: janosch-x (Janosch Müller)
* Status: Open
* Priority: Normal
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
## Situation
'n' or 'N' can be passed as a third argument to `Regexp.new`. However, the behavior is not the same as the literal `n`-flag or the `Regexp::NOENCODING` option, and it makes the `#encoding` of `Regexp` and `Regexp#source` diverge:
```ruby
//n # => SyntaxError
Regexp.new('', Regexp::NOENCODING) # => RegexpError
re = Regexp.new('', nil, 'n') # => //
re.options == Regexp::NOENCODING # => true
re.encoding # => ASCII-8BIT
re.source.encoding # => UTF-8
re =~ '' # => Encoding::CompatibilityError
```
## Code
[Here](https://github.com/ruby/ruby/blob/b41de3a1e8c36a5cc336b6f7cd3cb71126cf1a60/re.c#L3622-L3658). There is also a test for the resulting encoding [here](https://github.com/ruby/ruby/blob/cf2bbcfff2985c116552967c7c4522f4630f2d18/test/ruby/test_regexp.rb#L564), but it is a no-op because the whole file is set to that encoding via magic comment anyway.
The third argument was added when ASCII was still the default Ruby encoding, so I guess Regexp and source encoding still matched at that point.
## Solution
It could be fixed, but my impression is that it is not useful anymore.
It was probably only added because `Regexp::NOENCODING` wasn't available at the time, so I think it could be deprecated like so:
> Passing a third argument to Regexp.new is deprecated. Use `Regexp::NOENCODING` as second argument instead.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>