@@ -15,14 +15,11 @@ newtype TType =
15
15
TTrait ( Trait t ) or
16
16
TArrayType ( ) or // todo: add size?
17
17
TRefType ( ) or // todo: add mut?
18
- TImplTraitType ( int bounds ) {
19
- bounds = any ( ImplTraitTypeRepr impl ) .getTypeBoundList ( ) .getNumberOfBounds ( )
20
- } or
18
+ TImplTraitType ( ImplTraitTypeRepr impl ) or
21
19
TTypeParamTypeParameter ( TypeParam t ) or
22
20
TAssociatedTypeTypeParameter ( TypeAlias t ) { any ( TraitItemNode trait ) .getAnAssocItem ( ) = t } or
23
21
TRefTypeParameter ( ) or
24
- TSelfTypeParameter ( Trait t ) or
25
- TImplTraitTypeParameter ( ImplTraitType t , int i ) { i in [ 0 .. t .getNumberOfBounds ( ) - 1 ] }
22
+ TSelfTypeParameter ( Trait t )
26
23
27
24
/**
28
25
* A type without type arguments.
@@ -184,30 +181,50 @@ class RefType extends Type, TRefType {
184
181
}
185
182
186
183
/**
187
- * An [` impl Trait` ][1] type.
184
+ * An [impl Trait][1] type.
188
185
*
189
- * We represent `impl Trait` types as generic types with as many type parameters
190
- * as there are bounds.
186
+ * Each syntactic `impl Trait` type gives rise to its own type, even if
187
+ * two `impl Trait` types have the same bounds.
191
188
*
192
- * [1] https://doc.rust-lang.org/book/ch10-02-traits .html#traits-as-parameters
189
+ * [1]: https://doc.rust-lang.org/reference/types/impl-trait .html
193
190
*/
194
191
class ImplTraitType extends Type , TImplTraitType {
195
- private int bounds ;
192
+ ImplTraitTypeRepr impl ;
196
193
197
- ImplTraitType ( ) { this = TImplTraitType ( bounds ) }
194
+ ImplTraitType ( ) { this = TImplTraitType ( impl ) }
198
195
199
- /** Gets the number of bounds of this `impl Trait` type. */
200
- int getNumberOfBounds ( ) { result = bounds }
196
+ /** Gets the underlying AST node. */
197
+ ImplTraitTypeRepr getImplTraitTypeRepr ( ) { result = impl }
198
+
199
+ /** Gets the function that this `impl Trait` belongs to. */
200
+ abstract Function getFunction ( ) ;
201
201
202
202
override StructField getStructField ( string name ) { none ( ) }
203
203
204
204
override TupleField getTupleField ( int i ) { none ( ) }
205
205
206
- override TypeParameter getTypeParameter ( int i ) { result = TImplTraitTypeParameter ( this , i ) }
206
+ override TypeParameter getTypeParameter ( int i ) { none ( ) }
207
207
208
- override string toString ( ) { result = " impl Trait ..." }
208
+ override string toString ( ) { result = impl . toString ( ) }
209
209
210
- override Location getLocation ( ) { result instanceof EmptyLocation }
210
+ override Location getLocation ( ) { result = impl .getLocation ( ) }
211
+ }
212
+
213
+ /**
214
+ * An [impl Trait in return position][1] type, for example:
215
+ *
216
+ * ```rust
217
+ * fn foo() -> impl Trait
218
+ * ```
219
+ *
220
+ * [1]: https://doc.rust-lang.org/reference/types/impl-trait.html#r-type.impl-trait.return
221
+ */
222
+ class ImplTraitReturnType extends ImplTraitType {
223
+ private Function function ;
224
+
225
+ ImplTraitReturnType ( ) { impl = function .getRetType ( ) .getTypeRepr ( ) }
226
+
227
+ override Function getFunction ( ) { result = function }
211
228
}
212
229
213
230
/** A type parameter. */
@@ -316,23 +333,34 @@ class SelfTypeParameter extends TypeParameter, TSelfTypeParameter {
316
333
}
317
334
318
335
/**
319
- * An `impl Trait` type parameter.
336
+ * An [impl Trait in argument position][1] type, for example:
337
+ *
338
+ * ```rust
339
+ * fn foo(arg: impl Trait)
340
+ * ```
341
+ *
342
+ * Such types are syntactic sugar for type parameters, that is
343
+ *
344
+ * ```rust
345
+ * fn foo<T: Trait>(arg: T)
346
+ * ```
347
+ *
348
+ * so we model them as type parameters.
349
+ *
350
+ * [1]: https://doc.rust-lang.org/reference/types/impl-trait.html#r-type.impl-trait.param
320
351
*/
321
- class ImplTraitTypeParameter extends TypeParameter , TImplTraitTypeParameter {
322
- private ImplTraitType implTraitType ;
323
- private int i ;
352
+ class ImplTraitTypeTypeParameter extends ImplTraitType , TypeParameter {
353
+ private Function function ;
324
354
325
- ImplTraitTypeParameter ( ) { this = TImplTraitTypeParameter ( implTraitType , i ) }
355
+ ImplTraitTypeTypeParameter ( ) { impl = function . getParamList ( ) . getAParam ( ) . getTypeRepr ( ) }
326
356
327
- /** Gets the `impl Trait` type that this parameter belongs to. */
328
- ImplTraitType getImplTraitType ( ) { result = implTraitType }
357
+ override Function getFunction ( ) { result = function }
329
358
330
- /** Gets the index of this type parameter. */
331
- int getIndex ( ) { result = i }
359
+ override StructField getStructField ( string name ) { none ( ) }
332
360
333
- override string toString ( ) { result = "impl Trait<" + i . toString ( ) + ">" }
361
+ override TupleField getTupleField ( int i ) { none ( ) }
334
362
335
- override Location getLocation ( ) { result instanceof EmptyLocation }
363
+ override TypeParameter getTypeParameter ( int i ) { none ( ) }
336
364
}
337
365
338
366
/**
@@ -370,3 +398,7 @@ final class SelfTypeBoundTypeAbstraction extends TypeAbstraction, Name {
370
398
371
399
override TypeParamTypeParameter getATypeParameter ( ) { none ( ) }
372
400
}
401
+
402
+ final class ImplTraitTypeReprAbstraction extends TypeAbstraction , ImplTraitTypeRepr {
403
+ override TypeParamTypeParameter getATypeParameter ( ) { none ( ) }
404
+ }
0 commit comments