Permalink
Browse files

Use |deep_copy| in ternary operators when needed

Deep copying wasn't performed in assignments where the RHS was a ternary
operator, even when some of its branches would normally be deeply copied
if they stood alone. Similarly with |return| statements. This commit
fixes the problem.

Fixes #47.
  • Loading branch information...
1 parent 52433bc commit f96a2a7174c04f2906570003d4a0ea238437a27f @dmajda dmajda committed Oct 18, 2013
Showing with 38 additions and 2 deletions.
  1. +8 −0 lib/y2r/ast/ycp.rb
  2. +30 −2 spec/y2r_spec.md
View
@@ -2501,6 +2501,14 @@ def empty?
end
class YETriple < Node
+ def compile_as_copy_if_needed(context)
+ Ruby::TernaryOperator.new(
+ :condition => cond.compile(context),
+ :then => self.true.compile_as_copy_if_needed(context),
+ :else => self.false.compile_as_copy_if_needed(context)
+ )
+ end
+
def compile(context)
Ruby::TernaryOperator.new(
:condition => cond.compile(context),
View
@@ -1646,11 +1646,25 @@ returned variables. The only exception is immutable types (like `boolean`).
return r;
}
+ // return value with a mutable type inside a ternary operator
+ list f2() {
+ list r1 = [];
+ list r2 = [];
+ return true ? r1 : r2;
+ }
+
// return value with an immutable type
- integer f2() {
+ integer f3() {
integer r = 42;
return r;
}
+
+ // return value with an immutable type inside a ternary operator
+ integer f4() {
+ integer r1 = 42;
+ integer r2 = 43;
+ return true ? r1 : r2;
+ }
}
```
@@ -1667,11 +1681,25 @@ module Yast
deep_copy(r)
end
- # return value with an immutable type
+ # return value with a mutable type inside a ternary operator
def f2
+ r1 = []
+ r2 = []
+ true ? deep_copy(r1) : deep_copy(r2)
+ end
+
+ # return value with an immutable type
+ def f3
r = 42
r
end
+
+ # return value with an immutable type inside a ternary operator
+ def f4
+ r1 = 42
+ r2 = 43
+ true ? r1 : r2
+ end
end
end

0 comments on commit f96a2a7

Please sign in to comment.