Skip to content

Commit 683e1b6

Browse files
committed
[GR-16060] Define FrozenError and use it for can't modify frozen object exceptions.
PullRequest: truffleruby/862
2 parents fe95ff5 + ec1595a commit 683e1b6

File tree

6 files changed

+22
-10
lines changed

6 files changed

+22
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Compatibility
2020
* `String#-@` now performs string deduplication (#1608).
2121
* `Hash#merge` now preserves the key order from the original hash for merged values (#1650).
2222
* Coerce values given to `FFI::Pointer` methods.
23+
* `FrozenError` is now defined and is used for `can't modify frozen` object exceptions.
2324

2425
Changes:
2526

spec/ruby/core/kernel/autoload_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def check_autoload(const)
5757
end
5858

5959
describe "when Object is frozen" do
60-
it "raises a FrozenError before defining the constant" do
60+
it "raises a #{frozen_error_class} before defining the constant" do
6161
ruby_exe(fixture(__FILE__, "autoload_frozen.rb")).should == "#{frozen_error_class} - nil"
6262
end
6363
end

spec/tags/core/exception/standard_error_tags.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

spec/truffle.mspec

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,6 @@ if MSpecScript.child_process?
148148
elsif ARGV.include? ":next"
149149
::VersionGuard::FULL_RUBY_VERSION = SpecVersion.new("2.7.0")
150150
end
151-
152-
# We do not use Ruby 2.5's FrozenError yet
153-
def frozen_error_class
154-
RuntimeError
155-
end
156151
end
157152

158153
if i = ARGV.index('slow') and ARGV[i-1] == '--excl-tag' and MSpecScript.child_process?

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public class CoreLibrary {
106106
private final DynamicObjectFactory fiberFactory;
107107
private final DynamicObject floatClass;
108108
private final DynamicObject floatDomainErrorClass;
109+
private final DynamicObject frozenErrorClass;
109110
private final DynamicObject hashClass;
110111
private final DynamicObjectFactory hashFactory;
111112
private final DynamicObject integerClass;
@@ -346,11 +347,14 @@ public CoreLibrary(RubyContext context) {
346347
ioErrorClass = defineClass(standardErrorClass, "IOError");
347348
localJumpErrorClass = defineClass(standardErrorClass, "LocalJumpError");
348349
regexpErrorClass = defineClass(standardErrorClass, "RegexpError");
349-
runtimeErrorClass = defineClass(standardErrorClass, "RuntimeError");
350350
threadErrorClass = defineClass(standardErrorClass, "ThreadError");
351351
typeErrorClass = defineClass(standardErrorClass, "TypeError");
352352
zeroDivisionErrorClass = defineClass(standardErrorClass, "ZeroDivisionError");
353353

354+
// StandardError > RuntimeError
355+
runtimeErrorClass = defineClass(standardErrorClass, "RuntimeError");
356+
frozenErrorClass = defineClass(runtimeErrorClass, "FrozenError");
357+
354358
// StandardError > RangeError
355359
rangeErrorClass = defineClass(standardErrorClass, "RangeError");
356360
floatDomainErrorClass = defineClass(rangeErrorClass, "FloatDomainError");
@@ -1305,6 +1309,10 @@ public DynamicObject getSyntaxErrorClass() {
13051309
return syntaxErrorClass;
13061310
}
13071311

1312+
public DynamicObject getFrozenErrorClass() {
1313+
return frozenErrorClass;
1314+
}
1315+
13081316
public DynamicObject getFloatDomainErrorClass() {
13091317
return floatDomainErrorClass;
13101318
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,23 @@ public DynamicObject argumentError(Rope message, Node currentNode, Throwable jav
198198
return ExceptionOperations.createRubyException(context, exceptionClass, StringOperations.createString(context, message), currentNode, javaThrowable);
199199
}
200200

201-
// RuntimeError
201+
// FrozenError
202202

203203
@TruffleBoundary
204204
public DynamicObject frozenError(Object object, Node currentNode) {
205205
String className = Layouts.MODULE.getFields(context.getCoreLibrary().getLogicalClass(object)).getName();
206-
return runtimeError(StringUtils.format("can't modify frozen %s", className), currentNode);
206+
return frozenError(StringUtils.format("can't modify frozen %s", className), currentNode);
207+
}
208+
209+
@TruffleBoundary
210+
public DynamicObject frozenError(String message, Node currentNode) {
211+
DynamicObject exceptionClass = context.getCoreLibrary().getFrozenErrorClass();
212+
DynamicObject errorMessage = StringOperations.createString(context, StringOperations.encodeRope(message, UTF8Encoding.INSTANCE));
213+
return ExceptionOperations.createRubyException(context, exceptionClass, errorMessage, currentNode, null);
207214
}
208215

216+
// RuntimeError
217+
209218
public DynamicObject runtimeErrorNotConstant(Node currentNode) {
210219
return runtimeError("Truffle::Graal.assert_constant can only be called lexically", currentNode);
211220
}

0 commit comments

Comments
 (0)