@@ -15,6 +15,7 @@ newtype TType =
1515 TTrait ( Trait t ) or
1616 TArrayType ( ) or // todo: add size?
1717 TRefType ( ) or // todo: add mut?
18+ TImplTraitType ( ImplTraitTypeRepr impl ) or
1819 TTypeParamTypeParameter ( TypeParam t ) or
1920 TAssociatedTypeTypeParameter ( TypeAlias t ) { any ( TraitItemNode trait ) .getAnAssocItem ( ) = t } or
2021 TRefTypeParameter ( ) or
@@ -115,6 +116,9 @@ class TraitType extends Type, TTrait {
115116
116117 TraitType ( ) { this = TTrait ( trait ) }
117118
119+ /** Gets the underlying trait. */
120+ Trait getTrait ( ) { result = trait }
121+
118122 override StructField getStructField ( string name ) { none ( ) }
119123
120124 override TupleField getTupleField ( int i ) { none ( ) }
@@ -176,6 +180,53 @@ class RefType extends Type, TRefType {
176180 override Location getLocation ( ) { result instanceof EmptyLocation }
177181}
178182
183+ /**
184+ * An [impl Trait][1] type.
185+ *
186+ * Each syntactic `impl Trait` type gives rise to its own type, even if
187+ * two `impl Trait` types have the same bounds.
188+ *
189+ * [1]: https://doc.rust-lang.org/reference/types/impl-trait.html
190+ */
191+ class ImplTraitType extends Type , TImplTraitType {
192+ ImplTraitTypeRepr impl ;
193+
194+ ImplTraitType ( ) { this = TImplTraitType ( impl ) }
195+
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+
202+ override StructField getStructField ( string name ) { none ( ) }
203+
204+ override TupleField getTupleField ( int i ) { none ( ) }
205+
206+ override TypeParameter getTypeParameter ( int i ) { none ( ) }
207+
208+ override string toString ( ) { result = impl .toString ( ) }
209+
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 }
228+ }
229+
179230/** A type parameter. */
180231abstract class TypeParameter extends Type {
181232 override StructField getStructField ( string name ) { none ( ) }
@@ -185,7 +236,7 @@ abstract class TypeParameter extends Type {
185236 override TypeParameter getTypeParameter ( int i ) { none ( ) }
186237}
187238
188- 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 ;
189240
190241private predicate id ( RawTypeParameter x , RawTypeParameter y ) { x = y }
191242
@@ -281,6 +332,37 @@ class SelfTypeParameter extends TypeParameter, TSelfTypeParameter {
281332 override Location getLocation ( ) { result = trait .getLocation ( ) }
282333}
283334
335+ /**
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
351+ */
352+ class ImplTraitTypeTypeParameter extends ImplTraitType , TypeParameter {
353+ private Function function ;
354+
355+ ImplTraitTypeTypeParameter ( ) { impl = function .getParamList ( ) .getAParam ( ) .getTypeRepr ( ) }
356+
357+ override Function getFunction ( ) { result = function }
358+
359+ override StructField getStructField ( string name ) { none ( ) }
360+
361+ override TupleField getTupleField ( int i ) { none ( ) }
362+
363+ override TypeParameter getTypeParameter ( int i ) { none ( ) }
364+ }
365+
284366/**
285367 * A type abstraction. I.e., a place in the program where type variables are
286368 * introduced.
@@ -316,3 +398,7 @@ final class SelfTypeBoundTypeAbstraction extends TypeAbstraction, Name {
316398
317399 override TypeParamTypeParameter getATypeParameter ( ) { none ( ) }
318400}
401+
402+ final class ImplTraitTypeReprAbstraction extends TypeAbstraction , ImplTraitTypeRepr {
403+ override TypeParamTypeParameter getATypeParameter ( ) { none ( ) }
404+ }
0 commit comments