Skip to content

Commit 338ecc7

Browse files
committed
Fix Regexp.new - warn when 3d argument has unsupported value
1 parent b83ba83 commit 338ecc7

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Compatibility:
2222
* Range literals of integers are now created at parse time like in CRuby (#2622, @aardvark179).
2323
* Fix `IO.pipe` - allow overriding `IO.new` that is used to create new pipes (#2692, @andykonchin).
2424
* Fix exception message when there are missing or extra keyword arguments - it contains all the missing/extra keywords now (#1522, @andrykonchin).
25+
* Fix `Regexp.new` to coerce non-String arguments (#2705, @andrykonchin).
2526

2627
Performance:
2728

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
fails:Regexp.compile given a String ignores the third argument if it is 'e' or 'euc' (case-insensitive)
2-
fails:Regexp.compile given a String ignores the third argument if it is 's' or 'sjis' (case-insensitive)
3-
fails:Regexp.compile given a String ignores the third argument if it is 'u' or 'utf8' (case-insensitive)
41
fails(immutable regexp):Regexp.compile works by default for subclasses with overridden #initialize

spec/tags/core/regexp/new_tags.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
fails:Regexp.new given a String ignores the third argument if it is 'e' or 'euc' (case-insensitive)
2-
fails:Regexp.new given a String ignores the third argument if it is 's' or 'sjis' (case-insensitive)
3-
fails:Regexp.new given a String ignores the third argument if it is 'u' or 'utf8' (case-insensitive)
41
fails(immutable regexp):Regexp.new works by default for subclasses with overridden #initialize

src/main/ruby/truffleruby/core/regexp.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,11 @@ def self.union(*patterns)
130130
end
131131
Truffle::Graal.always_split(method(:union))
132132

133-
def self.new(pattern, opts=undefined, lang=nil)
133+
def self.new(pattern, opts=undefined, encoding=nil)
134134
if Primitive.object_kind_of?(pattern, Regexp)
135135
warn 'flags ignored' unless Primitive.undefined?(opts)
136136
opts = pattern.options
137137
pattern = pattern.source
138-
elsif Primitive.object_kind_of?(pattern, String)
139138
else
140139
pattern = Truffle::Type.rb_convert_type pattern, String, :to_str
141140
end
@@ -148,8 +147,15 @@ def self.new(pattern, opts=undefined, lang=nil)
148147
opts = 0
149148
end
150149

151-
code = lang[0] if lang
152-
opts |= NOENCODING if code == ?n or code == ?N
150+
if encoding
151+
encoding = Truffle::Type.rb_convert_type encoding, String, :to_str
152+
code = encoding[0]
153+
if code == ?n or code == ?N
154+
opts |= NOENCODING
155+
else
156+
warn "encoding option is ignored - #{encoding}"
157+
end
158+
end
153159

154160
Primitive.regexp_compile pattern, opts # may be overridden by subclasses
155161
end

0 commit comments

Comments
 (0)