Skip to content

Commit 50970c1

Browse files
Merge pull request #14302 from dotty-staging/add-macro-forward-compat-tests
Add forward compat tests for macros
2 parents 74d1b82 + 98be6b9 commit 50970c1

File tree

11 files changed

+176
-0
lines changed

11 files changed

+176
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.quoted.*
2+
3+
object Macros:
4+
5+
inline def power(x: Double, inline n: Int) = ${ powerCode('x, 'n) }
6+
7+
private def powerCode(x: Expr[Double], n: Expr[Int])(using Quotes): Expr[Double] =
8+
unrolledPowerCode(x, n.valueOrError)
9+
10+
private def unrolledPowerCode(x: Expr[Double], n: Int)(using Quotes): Expr[Double] =
11+
if n == 0 then '{ 1.0 } // tests simple quotes without splices
12+
else if n % 2 == 1 then '{ $x * ${ unrolledPowerCode(x, n - 1) } } // tests simple splices
13+
else '{ val y = $x * $x; ${ unrolledPowerCode('y, n / 2) } } // tests splice with term capture
14+
15+
16+
inline def let[T, U](x: T)(inline body: T => U): U = ${ letCode('x, 'body) }
17+
18+
private def letCode[T: Type, U: Type](x: Expr[T], body: Expr[T => U])(using Quotes): Expr[U] =
19+
// tests use of Type
20+
'{ val y: T = $x; $body(y): U }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Macros.*
2+
3+
def powerTest(x: Double): Unit =
4+
power(x, 0)
5+
power(x, 1)
6+
power(x, 5)
7+
power(x, 10)
8+
9+
def letTest: Unit =
10+
let(0) { _ + 1 }
11+
let(0) { _.toString }
12+
let((4, 'a')) { _.swap }
13+
let(new Foo) { _.hashCode }
14+
15+
class Foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Disabled until https://github.com/lampepfl/dotty/issues/14306 is fixed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.quoted.*
2+
3+
object Macros:
4+
5+
inline def power(x: Double, inline n: Int) = ${ powerCode('x, 'n) }
6+
7+
private def powerCode(x: Expr[Double], n: Expr[Int])(using Quotes): Expr[Double] =
8+
unrolledPowerCode(x, n.valueOrError)
9+
10+
private def unrolledPowerCode(x: Expr[Double], n: Int)(using Quotes): Expr[Double] =
11+
if n == 0 then '{ 1.0 } // tests simple quotes without splices
12+
else if n % 2 == 1 then '{ $x * ${ unrolledPowerCode(x, n - 1) } } // tests simple splices
13+
else '{ val y = $x * $x; ${ unrolledPowerCode('y, n / 2) } } // tests splice with term capture
14+
15+
16+
inline def let[T, U](x: T)(inline body: T => U): U = ${ letCode('x, 'body) }
17+
18+
private def letCode[T: Type, U: Type](x: Expr[T], body: Expr[T => U])(using Quotes): Expr[U] =
19+
// tests use of Type
20+
'{ val y: T = $x; $body(y): U }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Macros.*
2+
3+
def powerTest(x: Double): Unit =
4+
power(x, 0)
5+
power(x, 1)
6+
power(x, 5)
7+
power(x, 10)
8+
9+
def letTest: Unit =
10+
let(0) { _ + 1 }
11+
let(0) { _.toString }
12+
let((4, 'a')) { _.swap }
13+
let(new Foo) { _.hashCode }
14+
15+
class Foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.quoted.*
2+
3+
object Macros:
4+
5+
inline def power(x: Double, inline n: Int) = ${ powerCode('x, 'n) }
6+
7+
private def powerCode(x: Expr[Double], n: Expr[Int])(using Quotes): Expr[Double] =
8+
unrolledPowerCode(x, n.valueOrError)
9+
10+
private def unrolledPowerCode(x: Expr[Double], n: Int)(using Quotes): Expr[Double] =
11+
if n == 0 then '{ 1.0 } // tests simple quotes without splices
12+
else if n % 2 == 1 then '{ $x * ${ unrolledPowerCode(x, n - 1) } } // tests simple splices
13+
else '{ val y = $x * $x; ${ unrolledPowerCode('y, n / 2) } } // tests splice with term capture
14+
15+
16+
inline def let[T, U](x: T)(inline body: T => U): U = ${ letCode('x, 'body) }
17+
18+
private def letCode[T: Type, U: Type](x: Expr[T], body: Expr[T => U])(using Quotes): Expr[U] =
19+
// tests use of Type
20+
'{ val y: T = $x; $body(y): U }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Macros.*
2+
3+
def powerTest(x: Double): Unit =
4+
power(x, 0)
5+
power(x, 1)
6+
power(x, 5)
7+
power(x, 10)
8+
9+
def letTest: Unit =
10+
let(0) { _ + 1 }
11+
let(0) { _.toString }
12+
let((4, 'a')) { _.swap }
13+
let(new Foo) { _.hashCode }
14+
15+
class Foo
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.quoted.*
2+
3+
object Macros:
4+
5+
inline def power(x: Double, inline n: Int) = ${ powerCode('x, 'n) }
6+
7+
private def powerCode(x: Expr[Double], n: Expr[Int])(using Quotes): Expr[Double] =
8+
unrolledPowerCode(x, n.valueOrError)
9+
10+
private def unrolledPowerCode(x: Expr[Double], n: Int)(using Quotes): Expr[Double] =
11+
if n == 0 then '{ 1.0 } // tests simple quotes without splices
12+
else if n % 2 == 1 then '{ $x * ${ unrolledPowerCode(x, n - 1) } } // tests simple splices
13+
else '{ val y = $x * $x; ${ unrolledPowerCode('y, n / 2) } } // tests splice with term capture
14+
15+
16+
inline def let[T, U](x: T)(inline body: T => U): U = ${ letCode('x, 'body) }
17+
18+
private def letCode[T: Type, U: Type](x: Expr[T], body: Expr[T => U])(using Quotes): Expr[U] =
19+
// tests use of Type
20+
'{ val y: T = $x; $body(y): U }
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Macros.*
2+
3+
def powerTest(x: Double): Unit =
4+
power(x, 0)
5+
power(x, 1)
6+
power(x, 5)
7+
power(x, 10)
8+
9+
def letTest: Unit =
10+
let(0) { _ + 1 }
11+
let(0) { _.toString }
12+
let((4, 'a')) { _.swap }
13+
let(new Foo) { _.hashCode }
14+
15+
class Foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.quoted.*
2+
3+
object Macros:
4+
5+
inline def power(x: Double, inline n: Int) = ${ powerCode('x, 'n) }
6+
7+
private def powerCode(x: Expr[Double], n: Expr[Int])(using Quotes): Expr[Double] =
8+
unrolledPowerCode(x, n.valueOrError)
9+
10+
private def unrolledPowerCode(x: Expr[Double], n: Int)(using Quotes): Expr[Double] =
11+
if n == 0 then '{ 1.0 } // tests simple quotes without splices
12+
else if n % 2 == 1 then '{ $x * ${ unrolledPowerCode(x, n - 1) } } // tests simple splices
13+
else '{ val y = $x * $x; ${ unrolledPowerCode('y, n / 2) } } // tests splice with term capture
14+
15+
16+
inline def let[T, U](x: T)(inline body: T => U): U = ${ letCode('x, 'body) }
17+
18+
private def letCode[T: Type, U: Type](x: Expr[T], body: Expr[T => U])(using Quotes): Expr[U] =
19+
// tests use of Type
20+
'{ val y: T = $x; $body(y): U }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Macros.*
2+
3+
def powerTest(x: Double): Unit =
4+
power(x, 0)
5+
power(x, 1)
6+
power(x, 5)
7+
power(x, 10)
8+
9+
def letTest: Unit =
10+
let(0) { _ + 1 }
11+
let(0) { _.toString }
12+
let((4, 'a')) { _.swap }
13+
let(new Foo) { _.hashCode }
14+
15+
class Foo

0 commit comments

Comments
 (0)