@@ -55,13 +55,13 @@ abstract class ConstantFolder {
55
55
}
56
56
57
57
/** If tree is a constant operation, replace with result. */
58
- def apply (tree : Tree , site : Symbol ): Tree = {
58
+ def apply (tree : Tree , site : Symbol ): Tree = if (isPastTyper) tree else
59
59
try {
60
60
tree match {
61
- case Apply (Select (FoldableTerm (x), op), List (FoldableTerm (y))) => fold(tree, foldBinop(op, x, y), true )
62
- case Apply (Select (ConstantTerm (x), op), List (ConstantTerm (y))) => fold(tree, foldBinop(op, x, y), false )
63
- case Select (FoldableTerm (x), op) => fold(tree, foldUnop(op, x), true )
64
- case Select (ConstantTerm (x), op) => fold(tree, foldUnop(op, x), false )
61
+ case Apply (Select (FoldableTerm (x), op), List (FoldableTerm (y))) => fold(tree, foldBinop(op, x, y), foldable = true )
62
+ case Apply (Select (ConstantTerm (x), op), List (ConstantTerm (y))) => fold(tree, foldBinop(op, x, y), foldable = false )
63
+ case Select (FoldableTerm (x), op) => fold(tree, foldUnop(op, x), foldable = true )
64
+ case Select (ConstantTerm (x), op) => fold(tree, foldUnop(op, x), foldable = false )
65
65
case _ => tree
66
66
}
67
67
} catch {
@@ -70,39 +70,39 @@ abstract class ConstantFolder {
70
70
runReporting.warning(tree.pos, s " Evaluation of a constant expression results in an arithmetic error: ${e.getMessage}" , WarningCategory .LintConstant , site)
71
71
tree
72
72
}
73
- }
74
73
75
- /** If tree is a constant value that can be converted to type `pt`, perform
76
- * the conversion.
74
+ /** If tree is a constant value that can be converted to type `pt`, perform the conversion.
77
75
*/
78
76
def apply (tree : Tree , pt : Type , site : Symbol ): Tree = {
79
77
val orig = apply(tree, site)
80
78
orig.tpe match {
81
- case tp@ ConstantType (x) => fold(orig, x convertTo pt, isConstantType(tp))
79
+ case tp@ ConstantType (x) => fold(orig, x. convertTo(pt), foldable = isConstantType(tp))
82
80
case _ => orig
83
81
}
84
82
}
85
83
84
+ /** Set the computed constant type.
85
+ */
86
86
private def fold (orig : Tree , folded : Constant , foldable : Boolean ): Tree =
87
87
if ((folded eq null ) || folded.tag == UnitTag ) orig
88
- else if (foldable) orig setType FoldableConstantType (folded)
88
+ else if (foldable) orig setType FoldableConstantType (folded)
89
89
else orig setType LiteralType (folded)
90
90
91
91
private def foldUnop (op : Name , x : Constant ): Constant = (op, x.tag) match {
92
92
case (nme.UNARY_! , BooleanTag ) => Constant (! x.booleanValue)
93
93
94
- case (nme.UNARY_~ , IntTag ) => Constant (~ x.intValue)
95
- case (nme.UNARY_~ , LongTag ) => Constant (~ x.longValue)
94
+ case (nme.UNARY_~ , IntTag ) => Constant (~ x.intValue)
95
+ case (nme.UNARY_~ , LongTag ) => Constant (~ x.longValue)
96
96
97
- case (nme.UNARY_+ , IntTag ) => Constant (+ x.intValue)
98
- case (nme.UNARY_+ , LongTag ) => Constant (+ x.longValue)
99
- case (nme.UNARY_+ , FloatTag ) => Constant (+ x.floatValue)
100
- case (nme.UNARY_+ , DoubleTag ) => Constant (+ x.doubleValue)
97
+ case (nme.UNARY_+ , IntTag ) => Constant (+ x.intValue)
98
+ case (nme.UNARY_+ , LongTag ) => Constant (+ x.longValue)
99
+ case (nme.UNARY_+ , FloatTag ) => Constant (+ x.floatValue)
100
+ case (nme.UNARY_+ , DoubleTag ) => Constant (+ x.doubleValue)
101
101
102
- case (nme.UNARY_- , IntTag ) => Constant (- x.intValue)
103
- case (nme.UNARY_- , LongTag ) => Constant (- x.longValue)
104
- case (nme.UNARY_- , FloatTag ) => Constant (- x.floatValue)
105
- case (nme.UNARY_- , DoubleTag ) => Constant (- x.doubleValue)
102
+ case (nme.UNARY_- , IntTag ) => Constant (- x.intValue)
103
+ case (nme.UNARY_- , LongTag ) => Constant (- x.longValue)
104
+ case (nme.UNARY_- , FloatTag ) => Constant (- x.floatValue)
105
+ case (nme.UNARY_- , DoubleTag ) => Constant (- x.doubleValue)
106
106
107
107
case _ => null
108
108
}
0 commit comments