Skip to content

Commit ac301a4

Browse files
committed
Fix for Checked failing in a border case in scala 3
Signed-off-by: Carlos Quiroz <[email protected]>
1 parent bc17356 commit ac301a4

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

core/src/main/scala/spire/math/Algebraic.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ object Algebraic extends AlgebraicInstances {
13501350

13511351
@nowarn
13521352
// TODO Restore the checked call
1353-
def apply(expr: Algebraic.Expr): Bound = {
1353+
def apply(expr: Algebraic.Expr): Bound = checked {
13541354
// Unfortunately, we must call degreeBound early, to avoid many redundant
13551355
// traversals of the Expr tree. Getting this out of the way early on
13561356
// means that we will traverse the tree once and populate the degreeBound

macros/src/main/scala-3/spire/macros/Checked.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ object Checked:
7676
'{
7777
val z = ${toInt(checkedImpl(x.asExprOf[Any], fallback))}
7878
if (z == ${numLimit}) $fallback else -z
79-
}.asExprOf[A].asTerm
79+
}.asExprOf[A].asTerm.changeOwner(owner)
8080
else if (isLong)
8181
'{
8282
val z = ${toLong(checkedImpl(x.asExprOf[Any], fallback))}
8383
if (z == ${numLimit}) $fallback else -z
84-
}.asTerm
84+
}.asTerm.changeOwner(owner)
8585
else super.transformTerm(tree)(owner)
8686
// NOTE I couldn't find a way to unify the long and int branches. Suggestions are welcome
8787
case Apply(Select(x, "*"), List(y)) =>
@@ -93,14 +93,14 @@ object Checked:
9393
val yt = ${toInt(checkedImpl(y.asExprOf[Any], fallback))}
9494
val z = xt * yt
9595
if (xt == 0 || (yt == z / xt && !(xt == -1 && yt == $numLimit))) z else $fallback
96-
}.asTerm
96+
}.asTerm.changeOwner(owner)
9797
} else if (isLong) {
9898
'{
9999
val xt = ${toLong(checkedImpl(x.asExprOf[Any], fallback))}
100100
val yt = ${toLong(checkedImpl(y.asExprOf[Any], fallback))}
101101
val z = xt * yt
102102
if (xt == 0 || (yt == z / xt && !(xt == -1 && yt == $numLimit))) z else $fallback
103-
}.asTerm
103+
}.asTerm.changeOwner(owner)
104104
} else
105105
super.transformTerm(tree)(owner)
106106
case Apply(Select(x, "+"), List(y)) =>
@@ -112,14 +112,14 @@ object Checked:
112112
val yt = ${toInt(checkedImpl(y.asExprOf[Any], fallback))}
113113
val z = xt + yt
114114
if ((~(xt ^ yt) & (xt ^ z)) < 0) $fallback else z
115-
}.asTerm
115+
}.asTerm.changeOwner(owner)
116116
else if (isLong)
117117
'{
118118
val xt = ${toLong(checkedImpl(x.asExprOf[Any], fallback))}
119119
val yt = ${toLong(checkedImpl(y.asExprOf[Any], fallback))}
120120
val z = xt + yt
121121
if ((~(xt ^ yt) & (xt ^ z)) < 0) $fallback else z
122-
}.asTerm
122+
}.asTerm.changeOwner(owner)
123123
else super.transformTerm(tree)(owner)
124124
case Apply(Select(x, "-"), List(y)) =>
125125
val isInt = isIntType(x.asExpr) && isIntType(y.asExpr)
@@ -130,14 +130,14 @@ object Checked:
130130
val yt = ${toInt(checkedImpl(y.asExprOf[Any], fallback))}
131131
val z = xt - yt
132132
if (((xt ^ yt) & (xt ^ z)) < 0) $fallback else z
133-
}.asTerm
133+
}.asTerm.changeOwner(owner)
134134
else if (isLong)
135135
'{
136136
val xt = ${toLong(checkedImpl(x.asExprOf[Any], fallback))}
137137
val yt = ${toLong(checkedImpl(y.asExprOf[Any], fallback))}
138138
val z = xt - yt
139139
if (((xt ^ yt) & (xt ^ z)) < 0) $fallback else z
140-
}.asTerm
140+
}.asTerm.changeOwner(owner)
141141
else super.transformTerm(tree)(owner)
142142
case Apply(Select(x, "/"), List(y)) =>
143143
val isInt = isIntType(x.asExpr) && isIntType(y.asExpr)
@@ -148,19 +148,19 @@ object Checked:
148148
val yt = ${toInt(checkedImpl(y.asExprOf[Any], fallback))}
149149
val z = xt / yt
150150
if (yt == -1 && xt == $numLimit) $fallback else z
151-
}.asTerm
151+
}.asTerm.changeOwner(owner)
152152
else if (isLong)
153153
'{
154154
val xt = ${toLong(checkedImpl(x.asExprOf[Any], fallback))}
155155
val yt = ${toLong(checkedImpl(y.asExprOf[Any], fallback))}
156156
val z = xt / yt
157157
if (yt == -1 && xt == $numLimit) $fallback else z
158-
}.asTerm
158+
}.asTerm.changeOwner(owner)
159159
else super.transformTerm(tree)(owner)
160160
case _ =>
161161
super.transformTerm(tree)(owner)
162162

163-
val result = acc.transformTerm(tree)(tree.symbol).asExprOf[A]
163+
val result = acc.transformTerm(tree)(Symbol.spliceOwner).asExprOf[A]
164164
// report.info(result.show)
165165
result
166166

macros/src/test/scala/spire/macros/CheckedScalaCheckSuite.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ class CheckedScalaCheckSuite extends munit.ScalaCheckSuite {
143143
assertEquals(3L, c5)
144144
val ag = A(Long.MaxValue, Long.MaxValue)
145145
intercept[ArithmeticException] { checked(ag.p * 2L) }
146-
// Border case failing in scala 3
147-
// intercept[ArithmeticException] { checked(List(1L, 2L).map{ k =>
148-
// ag.p * k
149-
// })
150-
// }
146+
// Border case failing in earlier versions of the scala 3 macro
147+
intercept[ArithmeticException] { checked(List(1L, 2L).map{ k =>
148+
ag.p * k
149+
})
150+
}
151151
}
152152

153153
property("Long negate overflow throws arithmetic exception") {

0 commit comments

Comments
 (0)