Skip to content

Commit 6c34ed2

Browse files
committed
[GR-20288] Don't omit backtrace when rescue $ERROR_INFO (#1660).
PullRequest: truffleruby/1207
2 parents a574467 + 2948aed commit 6c34ed2

File tree

7 files changed

+25
-6
lines changed

7 files changed

+25
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Bug fixes:
5454
* Make top-level methods available in `Context#getBindings()` (#1838).
5555
* Made `Kernel#caller_locations` accept a range argument, and return `nil` when appropriate.
5656
* Made `rb_respond_to` work with primitives (#1869, @chrisseaton).
57+
* Fixed issue with missing backtrace for `rescue $ERROR_INFO` (#1660).
5758

5859
Compatibility:
5960

spec/ruby/language/alias_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,9 @@ def test_with_check(*args)
255255
code = '$a = 1; $b = 2; alias $b $a; p [$a, $b]; $b = 3; p [$a, $b]'
256256
ruby_exe(code).should == "[1, 1]\n[3, 3]\n"
257257
end
258+
259+
it "supports aliasing twice the same global variables" do
260+
code = '$a = 1; alias $b $a; alias $b $a; p [$a, $b]'
261+
ruby_exe(code).should == "[1, 1]\n"
262+
end
258263
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require_relative '../../spec_helper'
2+
require 'English'
3+
4+
describe "English" do
5+
it "aliases $! to $ERROR_INFO and $ERROR_INFO still returns an Exception with a backtrace" do
6+
exception = (1 / 0 rescue $ERROR_INFO)
7+
exception.should be_kind_of(Exception)
8+
exception.backtrace.should be_kind_of(Array)
9+
end
10+
11+
it "aliases $@ to $ERROR_POSITION and $ERROR_POSITION still returns a backtrace" do
12+
(1 / 0 rescue $ERROR_POSITION).should be_kind_of(Array)
13+
end
14+
end

spec/tags/language/alias_tags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
slow:The alias keyword on top level defines the alias on Object
22
slow:The alias keyword can create a new global variable, synonym of the original
33
slow:The alias keyword can override an existing global variable and make them synonyms
4+
slow:The alias keyword supports aliasing twice the same global variables

src/main/java/org/truffleruby/language/globals/GlobalVariables.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public void alias(String oldName, String newName) {
7272
final GlobalVariableStorage storage = getStorage(oldName);
7373

7474
final GlobalVariableStorage previousStorage = variables.put(newName, storage);
75-
if (previousStorage != null) {
75+
// If previousStorage == storage, we already have that alias and should not invalidate
76+
if (previousStorage != null && previousStorage != storage) {
7677
previousStorage.getValidAssumption().invalidate();
7778
}
7879
}

src/main/java/org/truffleruby/parser/BodyTranslator.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,10 +2840,7 @@ public RubyNode visitRescueNode(RescueParseNode node) {
28402840
boolean canOmitBacktrace = false;
28412841

28422842
if (context.getOptions().BACKTRACES_OMIT_UNUSED && rescueBody != null &&
2843-
rescueBody.getBodyNode() instanceof SideEffectFree /* allow `expression rescue $!` pattern */ &&
2844-
(!(rescueBody.getBodyNode() instanceof GlobalVarParseNode) ||
2845-
!((GlobalVarParseNode) rescueBody.getBodyNode()).getName().equals("$!")) &&
2846-
rescueBody.getOptRescueNode() == null) {
2843+
rescueBody.getBodyNode() instanceof SideEffectFree && rescueBody.getOptRescueNode() == null) {
28472844
canOmitBacktrace = true;
28482845
}
28492846

src/main/java/org/truffleruby/parser/ast/GlobalVarParseNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
/**
4141
* access to a global variable.
4242
*/
43-
public class GlobalVarParseNode extends ParseNode implements INameNode, SideEffectFree {
43+
public class GlobalVarParseNode extends ParseNode implements INameNode {
4444
private String name;
4545

4646
public GlobalVarParseNode(SourceIndexLength position, String name) {

0 commit comments

Comments
 (0)