Skip to content

Commit 829012c

Browse files
committed
[GR-18163] Fix exception message to match CRuby when Integer#chr is called with incorrect codepoint value
PullRequest: truffleruby/4298
2 parents ede6725 + 094804c commit 829012c

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Compatibility:
3030
* Set `RbConfig::CONFIG['host_cpu']` to `arm64` on darwin platform (#3571, @andrykonchin).
3131
* Fix `RegexpError` messages to match CRuby better (#3398, @andrykonchin).
3232
* Fix `Enumerable#reduce` to handle non-Symbol method name parameter (#2931, @andrykonchin).
33+
* Fix `RangeError` message to match CRuby for `Integer#chr` called with invalid codepoint argument (#2795, @andrykonchin).
3334

3435
Performance:
3536

spec/ruby/core/integer/chr_spec.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
end
1111

1212
it "raises a RangeError is self is less than 0" do
13-
-> { -1.chr }.should raise_error(RangeError)
14-
-> { (-bignum_value).chr }.should raise_error(RangeError)
13+
-> { -1.chr }.should raise_error(RangeError, /-1 out of char range/)
14+
-> { (-bignum_value).chr }.should raise_error(RangeError, /bignum out of char range/)
1515
end
1616

1717
it "raises a RangeError if self is too large" do
18-
-> { 2206368128.chr(Encoding::UTF_8) }.should raise_error(RangeError)
18+
-> { 2206368128.chr(Encoding::UTF_8) }.should raise_error(RangeError, /2206368128 out of char range/)
1919
end
2020

2121
describe "when Encoding.default_internal is nil" do
@@ -48,8 +48,8 @@
4848
end
4949

5050
it "raises a RangeError is self is greater than 255" do
51-
-> { 256.chr }.should raise_error(RangeError)
52-
-> { bignum_value.chr }.should raise_error(RangeError)
51+
-> { 256.chr }.should raise_error(RangeError, /256 out of char range/)
52+
-> { bignum_value.chr }.should raise_error(RangeError, /bignum out of char range/)
5353
end
5454
end
5555

@@ -137,7 +137,7 @@
137137
[620, "TIS-620"]
138138
].each do |integer, encoding_name|
139139
Encoding.default_internal = Encoding.find(encoding_name)
140-
-> { integer.chr }.should raise_error(RangeError)
140+
-> { integer.chr }.should raise_error(RangeError, /(invalid codepoint|out of char range)/)
141141
end
142142
end
143143
end
@@ -165,12 +165,12 @@
165165

166166
# http://redmine.ruby-lang.org/issues/4869
167167
it "raises a RangeError is self is less than 0" do
168-
-> { -1.chr(Encoding::UTF_8) }.should raise_error(RangeError)
169-
-> { (-bignum_value).chr(Encoding::EUC_JP) }.should raise_error(RangeError)
168+
-> { -1.chr(Encoding::UTF_8) }.should raise_error(RangeError, /-1 out of char range/)
169+
-> { (-bignum_value).chr(Encoding::EUC_JP) }.should raise_error(RangeError, /bignum out of char range/)
170170
end
171171

172172
it "raises a RangeError if self is too large" do
173-
-> { 2206368128.chr(Encoding::UTF_8) }.should raise_error(RangeError)
173+
-> { 2206368128.chr(Encoding::UTF_8) }.should raise_error(RangeError, /2206368128 out of char range/)
174174
end
175175

176176
it "returns a String with the specified encoding" do

src/main/java/org/truffleruby/core/exception/CoreExceptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,9 +1065,9 @@ public RubyException ioError(IOException exception, Node currentNode) {
10651065
// RangeError
10661066

10671067
@TruffleBoundary
1068-
public RubyException rangeError(long code, RubyEncoding encoding, Node currentNode) {
1068+
public RubyException rangeError(long code, Node currentNode) {
10691069
return rangeError(
1070-
StringUtils.format("invalid codepoint %x in %s", code, encoding),
1070+
StringUtils.format("%d out of char range", code),
10711071
currentNode);
10721072
}
10731073

src/main/java/org/truffleruby/core/string/StringNodes.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3638,7 +3638,7 @@ RubyString stringFromCodepoint(int code, RubyEncoding encoding,
36383638
var tstring = fromCodePointNode.execute(code, encoding.tencoding, false);
36393639
if (tstring == null) {
36403640
errorProfile.enter(this);
3641-
throw new RaiseException(getContext(), coreExceptions().rangeError(code, encoding, this));
3641+
throw new RaiseException(getContext(), coreExceptions().rangeError(code, this));
36423642
}
36433643

36443644
return createString(tstring, encoding);
@@ -3651,15 +3651,15 @@ RubyString stringFromLongCodepoint(long code, RubyEncoding encoding,
36513651
var tstring = fromCodePointNode.execute((int) code, encoding.tencoding, false);
36523652
if (tstring == null) {
36533653
errorProfile.enter(this);
3654-
throw new RaiseException(getContext(), coreExceptions().rangeError(code, encoding, this));
3654+
throw new RaiseException(getContext(), coreExceptions().rangeError(code, this));
36553655
}
36563656

36573657
return createString(tstring, encoding);
36583658
}
36593659

36603660
@Specialization(guards = "!isCodepoint(code)")
36613661
RubyString tooBig(long code, RubyEncoding encoding) {
3662-
throw new RaiseException(getContext(), coreExceptions().rangeError(code, encoding, this));
3662+
throw new RaiseException(getContext(), coreExceptions().rangeError(code, this));
36633663
}
36643664

36653665
protected boolean isCodepoint(long code) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,16 @@ def truncate(precision = 0)
165165

166166
def chr(enc = undefined)
167167
if self < 0 || (self & 0xffff_ffff) != self
168-
raise RangeError, "#{self} is outside of the valid character range"
168+
subject = Primitive.integer_fits_into_int(self) ? self : 'bignum'
169+
raise RangeError, "#{subject} out of char range"
169170
end
170171

171172
if Primitive.undefined? enc
172173
if 0xff < self
173174
enc = Encoding.default_internal
174175
if Primitive.nil? enc
175-
raise RangeError, "#{self} is outside of the valid character range"
176+
subject = Primitive.integer_fits_into_int(self) ? self : 'bignum'
177+
raise RangeError, "#{subject} out of char range"
176178
end
177179
elsif self < 0x80
178180
enc = Encoding::US_ASCII

0 commit comments

Comments
 (0)