Skip to content

Commit 72d6dc7

Browse files
committed
Fix #7204: Add Typeable instances to TASTy reflection
1 parent 90a55c0 commit 72d6dc7

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

library/src/scala/tasty/reflect/Core.scala

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,72 +120,95 @@ trait Core {
120120

121121
/** Tree representing a pacakage clause in the source code */
122122
type PackageClause = internal.PackageClause
123+
given (given ctx: Context): Typeable[Tree, PackageClause] = internal.matchPackageClause(_)
123124

124125
/** Tree representing a statement in the source code */
125126
type Statement = internal.Statement
127+
given (given ctx: Context): Typeable[Tree, Statement] = internal.matchStatement(_)
126128

127129
/** Tree representing an import in the source code */
128130
type Import = internal.Import
131+
given (given ctx: Context): Typeable[Tree, Import] = internal.matchImport(_)
129132

130133
/** Tree representing a definition in the source code. It can be `PackageDef`, `ClassDef`, `TypeDef`, `DefDef` or `ValDef` */
131134
type Definition = internal.Definition
135+
given (given ctx: Context): Typeable[Tree, Definition] = internal.matchDefinition(_)
132136

133137
/** Tree representing a package definition. This includes definitions in all source files */
134138
type PackageDef = internal.PackageDef
139+
given (given ctx: Context): Typeable[Tree, PackageDef] = internal.matchPackageDef(_)
135140

136141
/** Tree representing a class definition. This includes annonymus class definitions and the class of a module object */
137142
type ClassDef = internal.ClassDef
143+
given (given ctx: Context): Typeable[Tree, ClassDef] = internal.matchClassDef(_)
138144

139145
/** Tree representing a type (paramter or member) definition in the source code */
140146
type TypeDef = internal.TypeDef
147+
given (given ctx: Context): Typeable[Tree, TypeDef] = internal.matchTypeDef(_)
141148

142149
/** Tree representing a method definition in the source code */
143150
type DefDef = internal.DefDef
151+
given (given ctx: Context): Typeable[Tree, DefDef] = internal.matchDefDef(_)
144152

145153
/** Tree representing a value definition in the source code This inclues `val`, `lazy val`, `var`, `object` and parameter defintions. */
146154
type ValDef = internal.ValDef
155+
given (given ctx: Context): Typeable[Tree, ValDef] = internal.matchValDef(_)
147156

148157
/** Tree representing an expression in the source code */
149158
type Term = internal.Term
159+
given (given ctx: Context): Typeable[Tree, Term] = internal.matchTerm(_)
150160

151161
/** Tree representing a reference to definition */
152162
type Ref = internal.Ref
163+
given (given ctx: Context): Typeable[Tree, Ref] = internal.matchRef(_)
153164

154165
/** Tree representing a reference to definition with a given name */
155166
type Ident = internal.Ident
167+
given (given ctx: Context): Typeable[Tree, Ident] = internal.matchIdent(_)
156168

157169
/** Tree representing a selection of definition with a given name on a given prefix */
158170
type Select = internal.Select
171+
given (given ctx: Context): Typeable[Tree, Select] = internal.matchSelect(_)
159172

160173
/** Tree representing a literal value in the source code */
161174
type Literal = internal.Literal
175+
given (given ctx: Context): Typeable[Tree, Literal] = internal.matchLiteral(_)
162176

163177
/** Tree representing `this` in the source code */
164178
type This = internal.This
179+
given (given ctx: Context): Typeable[Tree, This] = internal.matchThis(_)
165180

166181
/** Tree representing `new` in the source code */
167182
type New = internal.New
183+
given (given ctx: Context): Typeable[Tree, New] = internal.matchNew(_)
168184

169185
/** Tree representing an argument passed with an explicit name. Such as `arg1 = x` in `foo(arg1 = x)` */
170186
type NamedArg = internal.NamedArg
187+
given (given ctx: Context): Typeable[Tree, NamedArg] = internal.matchNamedArg(_)
171188

172189
/** Tree an application of arguments. It represents a single list of arguments, multiple argument lists will have nested `Apply`s */
173190
type Apply = internal.Apply
191+
given (given ctx: Context): Typeable[Tree, Apply] = internal.matchApply(_)
174192

175193
/** Tree an application of type arguments */
176194
type TypeApply = internal.TypeApply
195+
given (given ctx: Context): Typeable[Tree, TypeApply] = internal.matchTypeApply(_)
177196

178197
/** Tree representing `super` in the source code */
179198
type Super = internal.Super
199+
given (given ctx: Context): Typeable[Tree, Super] = internal.matchSuper(_)
180200

181201
/** Tree representing a type ascription `x: T` in the source code */
182202
type Typed = internal.Typed
203+
given (given ctx: Context): Typeable[Tree, Typed] = internal.matchTyped(_)
183204

184205
/** Tree representing an assignment `x = y` in the source code */
185206
type Assign = internal.Assign
207+
given (given ctx: Context): Typeable[Tree, Assign] = internal.matchAssign(_)
186208

187209
/** Tree representing a block `{ ... }` in the source code */
188210
type Block = internal.Block
211+
given (given ctx: Context): Typeable[Tree, Block] = internal.matchBlock(_)
189212

190213
/** A lambda `(...) => ...` in the source code is represented as
191214
* a local method and a closure:
@@ -197,87 +220,114 @@ trait Core {
197220
*
198221
*/
199222
type Closure = internal.Closure
223+
given (given ctx: Context): Typeable[Tree, Closure] = internal.matchClosure(_)
200224

201225
/** Tree representing an if/then/else `if (...) ... else ...` in the source code */
202226
type If = internal.If
227+
given (given ctx: Context): Typeable[Tree, If] = internal.matchIf(_)
203228

204229
/** Tree representing a pattern match `x match { ... }` in the source code */
205230
type Match = internal.Match
231+
given (given ctx: Context): Typeable[Tree, Match] = internal.matchMatch(_)
206232

207233
/** Tree representing a pattern match `delegate match { ... }` in the source code */ // TODO: drop
208234
type ImpliedMatch = internal.ImpliedMatch
235+
given (given ctx: Context): Typeable[Tree, ImpliedMatch] = internal.matchImplicitMatch(_)
209236

210237
/** Tree representing a try catch `try x catch { ... } finally { ... }` in the source code */
211238
type Try = internal.Try
239+
given (given ctx: Context): Typeable[Tree, Try] = internal.matchTry(_)
212240

213241
/** Tree representing a `return` in the source code */
214242
type Return = internal.Return
243+
given (given ctx: Context): Typeable[Tree, Return] = internal.matchReturn(_)
215244

216245
/** Tree representing a variable argument list in the source code */
217246
type Repeated = internal.Repeated
247+
given (given ctx: Context): Typeable[Tree, Repeated] = internal.matchRepeated(_)
218248

219249
/** Tree representing the scope of an inlined tree */
220250
type Inlined = internal.Inlined
251+
given (given ctx: Context): Typeable[Tree, Inlined] = internal.matchInlined(_)
221252

222253
/** Tree representing a selection of definition with a given name on a given prefix and number of nested scopes of inlined trees */
223254
type SelectOuter = internal.SelectOuter
255+
given (given ctx: Context): Typeable[Tree, SelectOuter] = internal.matchSelectOuter(_)
224256

225257
/** Tree representing a while loop */
226258
type While = internal.While
259+
given (given ctx: Context): Typeable[Tree, While] = internal.matchWhile(_)
227260

228261
/** Type tree representing a type written in the source */
229262
type TypeTree = internal.TypeTree
263+
given (given ctx: Context): Typeable[Tree, TypeTree] = internal.matchTypeTree(_)
230264

231265
/** Type tree representing an inferred type */
232266
type Inferred = internal.Inferred
267+
given (given ctx: Context): Typeable[Tree, Inferred] = internal.matchInferred(_)
233268

234269
/** Type tree representing a reference to definition with a given name */
235270
type TypeIdent = internal.TypeIdent
271+
given (given ctx: Context): Typeable[Tree, TypeIdent] = internal.matchTypeIdent(_)
236272

237273
/** Type tree representing a selection of definition with a given name on a given term prefix */
238274
type TypeSelect = internal.TypeSelect
275+
given (given ctx: Context): Typeable[Tree, TypeSelect] = internal.matchTypeSelect(_)
239276

240277
/** Type tree representing a selection of definition with a given name on a given type prefix */
241278
type Projection = internal.Projection
279+
given (given ctx: Context): Typeable[Tree, Projection] = internal.matchProjection(_)
242280

243281
/** Type tree representing a singleton type */
244282
type Singleton = internal.Singleton
283+
given (given ctx: Context): Typeable[Tree, Singleton] = internal.matchSingleton(_)
245284

246285
/** Type tree representing a type refinement */
247286
type Refined = internal.Refined
287+
given (given ctx: Context): Typeable[Tree, Refined] = internal.matchRefined(_)
248288

249289
/** Type tree representing a type application */
250290
type Applied = internal.Applied
291+
given (given ctx: Context): Typeable[Tree, Applied] = internal.matchApplied(_)
251292

252293
/** Type tree representing an annotated type */
253294
type Annotated = internal.Annotated
295+
given (given ctx: Context): Typeable[Tree, Annotated] = internal.matchAnnotated(_)
254296

255297
/** Type tree representing a type match */
256298
type MatchTypeTree = internal.MatchTypeTree
299+
given (given ctx: Context): Typeable[Tree, MatchTypeTree] = internal.matchMatchTypeTree(_)
257300

258301
/** Type tree representing a by name parameter */
259302
type ByName = internal.ByName
303+
given (given ctx: Context): Typeable[Tree, ByName] = internal.matchByName(_)
260304

261305
/** Type tree representing a lambda abstraction type */
262306
type LambdaTypeTree = internal.LambdaTypeTree
307+
given (given ctx: Context): Typeable[Tree, LambdaTypeTree] = internal.matchLambdaTypeTree(_)
263308

264309
/** Type tree representing a type binding */
265310
type TypeBind = internal.TypeBind
311+
given (given ctx: Context): Typeable[Tree, TypeBind] = internal.matchTypeBind(_)
266312

267313
/** Type tree within a block with aliases `{ type U1 = ... ; T[U1, U2] }` */
268314
type TypeBlock = internal.TypeBlock
315+
given (given ctx: Context): Typeable[Tree, TypeBlock] = internal.matchTypeBlock(_)
269316

270317
/** Type tree representing a type bound written in the source */
271318
type TypeBoundsTree = internal.TypeBoundsTree
319+
given (given ctx: Context): Typeable[Tree, TypeBoundsTree] = internal.matchTypeBoundsTree(_)
272320

273321
/** Type tree representing wildcard type bounds written in the source.
274322
* The wildcard type `_` (for example in in `List[_]`) will be a type tree that
275323
* represents a type but has `TypeBound`a inside.
276324
*/
277325
type WildcardTypeTree = internal.WildcardTypeTree
326+
given (given ctx: Context): Typeable[Tree, WildcardTypeTree] = internal.matchWildcardTypeTree(_)
278327

279328
/** Branch of a pattern match or catch clause */
280329
type CaseDef = internal.CaseDef
330+
given (given ctx: Context): Typeable[Tree, CaseDef] = internal.matchCaseDef(_)
281331

282332
/** Branch of a type pattern match */
283333
type TypeCaseDef = internal.TypeCaseDef

tests/run-macros/i5715/Macro_1.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ object scalatest {
88
import qctx.tasty.{_, given}
99

1010
cond.unseal.underlyingArgument match {
11-
case app @ Apply(sel @ Select(lhs, op), rhs :: Nil) =>
12-
val IsSelect(select) = sel
11+
case app @ Apply(select @ Select(lhs, op), rhs :: Nil) =>
1312
val cond = Apply(Select.copy(select)(lhs, "exists"), rhs :: Nil).seal.cast[Boolean]
1413
'{ scala.Predef.assert($cond) }
1514
case _ =>

tests/run-macros/reflect-select-copy/assert_1.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ object scalatest {
88
import qctx.tasty.{_, given}
99

1010
cond.unseal.underlyingArgument match {
11-
case Apply(sel @ Select(lhs, op), rhs :: Nil) =>
12-
val IsSelect(select) = sel
11+
case Apply(select @ Select(lhs, op), rhs :: Nil) =>
1312
val cond = Apply(Select.copy(select)(lhs, ">"), rhs :: Nil).seal.cast[Boolean]
1413
'{ scala.Predef.assert($cond) }
1514
case _ =>

tests/run-macros/reflect-select-copy/reflect-select-copy/assert_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ object scalatest {
2424
}
2525
}
2626
}.seal.cast[Unit]
27-
case Apply(f @ Apply(IsSelect(sel @ Select(Apply(qual, lhs :: Nil), op)), rhs :: Nil), implicits)
27+
case Apply(f @ Apply(sel @ Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
2828
if isImplicitMethodType(f.tpe) =>
2929
let(lhs) { left =>
3030
let(rhs) { right =>

tests/run-macros/tasty-extractors-3/quoted_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ object Macros {
1313
val buff = new StringBuilder
1414
val traverser = new TreeTraverser {
1515
override def traverseTree(tree: Tree)(implicit ctx: Context): Unit = tree match {
16-
case IsTypeBoundsTree(tree) =>
16+
case tree: TypeBoundsTree =>
1717
buff.append(tree.tpe.showExtractors)
1818
buff.append("\n\n")
1919
traverseTreeChildren(tree)
20-
case IsTypeTree(tree) =>
20+
case tree: TypeTree =>
2121
buff.append(tree.tpe.showExtractors)
2222
buff.append("\n\n")
2323
traverseTreeChildren(tree)

0 commit comments

Comments
 (0)