@@ -15,14 +15,11 @@ newtype TType =
1515 TTrait ( Trait t ) or
1616 TArrayType ( ) or // todo: add size?
1717 TRefType ( ) or // todo: add mut?
18- TImplTraitType ( int bounds ) {
19- bounds = any ( ImplTraitTypeRepr impl ) .getTypeBoundList ( ) .getNumberOfBounds ( )
20- } or
18+ TImplTraitType ( ImplTraitTypeRepr impl ) or
2119 TTypeParamTypeParameter ( TypeParam t ) or
2220 TAssociatedTypeTypeParameter ( TypeAlias t ) { any ( TraitItemNode trait ) .getAnAssocItem ( ) = t } or
2321 TRefTypeParameter ( ) or
24- TSelfTypeParameter ( Trait t ) or
25- TImplTraitTypeParameter ( ImplTraitType t , int i ) { i in [ 0 .. t .getNumberOfBounds ( ) - 1 ] }
22+ TSelfTypeParameter ( Trait t )
2623
2724/**
2825 * A type without type arguments.
@@ -184,30 +181,50 @@ class RefType extends Type, TRefType {
184181}
185182
186183/**
187- * An [` impl Trait` ][1] type.
184+ * An [impl Trait][1] type.
188185 *
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.
191188 *
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
193190 */
194191class ImplTraitType extends Type , TImplTraitType {
195- private int bounds ;
192+ ImplTraitTypeRepr impl ;
196193
197- ImplTraitType ( ) { this = TImplTraitType ( bounds ) }
194+ ImplTraitType ( ) { this = TImplTraitType ( impl ) }
198195
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 ( ) ;
201201
202202 override StructField getStructField ( string name ) { none ( ) }
203203
204204 override TupleField getTupleField ( int i ) { none ( ) }
205205
206- override TypeParameter getTypeParameter ( int i ) { result = TImplTraitTypeParameter ( this , i ) }
206+ override TypeParameter getTypeParameter ( int i ) { none ( ) }
207207
208- override string toString ( ) { result = " impl Trait ..." }
208+ override string toString ( ) { result = impl . toString ( ) }
209209
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 }
211228}
212229
213230/** A type parameter. */
@@ -219,7 +236,7 @@ abstract class TypeParameter extends Type {
219236 override TypeParameter getTypeParameter ( int i ) { none ( ) }
220237}
221238
222- private class RawTypeParameter = @type_param or @trait or @type_alias;
239+ private class RawTypeParameter = @type_param or @trait or @type_alias or @impl_trait_type_repr ;
223240
224241private predicate id ( RawTypeParameter x , RawTypeParameter y ) { x = y }
225242
@@ -316,23 +333,34 @@ class SelfTypeParameter extends TypeParameter, TSelfTypeParameter {
316333}
317334
318335/**
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
320351 */
321- class ImplTraitTypeParameter extends TypeParameter , TImplTraitTypeParameter {
322- private ImplTraitType implTraitType ;
323- private int i ;
352+ class ImplTraitTypeTypeParameter extends ImplTraitType , TypeParameter {
353+ private Function function ;
324354
325- ImplTraitTypeParameter ( ) { this = TImplTraitTypeParameter ( implTraitType , i ) }
355+ ImplTraitTypeTypeParameter ( ) { impl = function . getParamList ( ) . getAParam ( ) . getTypeRepr ( ) }
326356
327- /** Gets the `impl Trait` type that this parameter belongs to. */
328- ImplTraitType getImplTraitType ( ) { result = implTraitType }
357+ override Function getFunction ( ) { result = function }
329358
330- /** Gets the index of this type parameter. */
331- int getIndex ( ) { result = i }
359+ override StructField getStructField ( string name ) { none ( ) }
332360
333- override string toString ( ) { result = "impl Trait<" + i . toString ( ) + ">" }
361+ override TupleField getTupleField ( int i ) { none ( ) }
334362
335- override Location getLocation ( ) { result instanceof EmptyLocation }
363+ override TypeParameter getTypeParameter ( int i ) { none ( ) }
336364}
337365
338366/**
@@ -370,3 +398,7 @@ final class SelfTypeBoundTypeAbstraction extends TypeAbstraction, Name {
370398
371399 override TypeParamTypeParameter getATypeParameter ( ) { none ( ) }
372400}
401+
402+ final class ImplTraitTypeReprAbstraction extends TypeAbstraction , ImplTraitTypeRepr {
403+ override TypeParamTypeParameter getATypeParameter ( ) { none ( ) }
404+ }
0 commit comments