-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathi15503f.scala
102 lines (86 loc) · 3.1 KB
/
i15503f.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//> using options -Wunused:implicits
/* This goes around the "trivial method" detection */
val default_int = 1
object Xd {
private def f1(a: Int) = a // OK
private def f2(a: Int) = 1 // OK
private def f3(a: Int)(using Int) = a // warn
private def f4(a: Int)(using Int) = default_int // warn
private def f6(a: Int)(using Int) = summon[Int] // OK
private def f7(a: Int)(using Int) = summon[Int] + a // OK
private def f8(a: Int)(using foo: Int) = a // warn
private def f9(a: Int)(using Int) = ??? // OK trivial
private def g1(a: Int)(implicit foo: Int) = a // warn
}
trait T
object T:
def hole(using T) = ()
class C(using T) // no warn marker trait is evidence only
class D(using T):
def t = T.hole // nowarn
object Example:
import scala.quoted.*
given OptionFromExpr[T](using Type[T], FromExpr[T]): FromExpr[Option[T]] with
def unapply(x: Expr[Option[T]])(using Quotes) = x match
case '{ Option[T](${Expr(y)}) } => Some(Option(y))
case '{ None } => Some(None)
case '{ ${Expr(opt)} : Some[T] } => Some(opt)
case _ => None
object ExampleWithoutWith:
import scala.quoted.*
given [T] => (Type[T], FromExpr[T]) => FromExpr[Option[T]]:
def unapply(x: Expr[Option[T]])(using Quotes) = x match
case '{ Option[T](${Expr(y)}) } => Some(Option(y))
case '{ None } => Some(None)
case '{ ${Expr(opt)} : Some[T] } => Some(opt)
case _ => None
//nowarning names on matches of quote trees requires consulting non-abstract types in QuotesImpl
object Unmatched:
import scala.quoted.*
def transform[T](e: Expr[T])(using Quotes): Expr[T] =
import quotes.reflect.*
def f(tree: Tree) =
tree match
case Ident(name) =>
case _ =>
e
trait Ctx:
val state: Int
case class K(i: Int)(using val ctx: Ctx) // nowarn
class L(val i: Int)(using val ctx: Ctx) // nowarn
class M(val i: Int)(using ctx: Ctx) // warn
package givens:
trait X:
def doX: Int
trait Y:
def doY: String
given X:
def doX = 7
given X => Y: // warn protected param to given class
def doY = "7"
/* desugared. It is protected so that its type can be used in member defs without leaking.
* possibly it should be protected only for named parameters.
given class given_Y(using x$1: givens.X) extends Object(), givens.Y {
protected given val x$1: givens.X
def doY: String = "7"
}
final given def given_Y(using x$1: givens.X): givens.given_Y =
new givens.given_Y(using x$1)()
*/
given namely: (x: X) => Y: // warn protected param to given class
def doY = "8"
end givens
object i22895:
trait Test[F[_], Ev] {
def apply[A, B](fa: F[A])(f: A => B)(using ev: Ev): F[B]
}
given testId: Test[[a] =>> a, Unit] =
new Test[[a] =>> a, Unit] {
def apply[A, B](fa: A)(f: A => B)(using ev: Unit): B = f(fa) // nowarn override
}
class C:
def f(using s: String) = s.toInt
class D(i: Int) extends C:
override def f(using String) = compute(i) // nowarn override
def g(using sss: String) = compute(i) // warn
def compute(i: Int) = i * 42 // returning a class param is deemed trivial, make it non-trivial