@@ -8,11 +8,7 @@ private import codeql.rust.elements.internal.generated.Raw
88private import codeql.rust.elements.internal.generated.Synth
99private import codeql.rust.frameworks.stdlib.Stdlib
1010private import codeql.rust.frameworks.stdlib.Builtins as Builtins
11-
12- /** Gets a type alias of `trait` or of a supertrait of `trait`. */
13- private TypeAlias getTraitTypeAlias ( Trait trait ) {
14- result = trait .getSupertrait * ( ) .getAssocItemList ( ) .getAnAssocItem ( )
15- }
11+ private import AssociatedType
1612
1713/**
1814 * Holds if a dyn trait type for the trait `trait` should have a type parameter
@@ -31,7 +27,7 @@ private TypeAlias getTraitTypeAlias(Trait trait) {
3127 */
3228private predicate dynTraitTypeParameter ( Trait trait , AstNode n ) {
3329 trait = any ( DynTraitTypeRepr dt ) .getTrait ( ) and
34- n = [ trait .getGenericParamList ( ) .getATypeParam ( ) .( AstNode ) , getTraitTypeAlias ( trait ) ]
30+ n = [ trait .getGenericParamList ( ) .getATypeParam ( ) .( AstNode ) , getTraitAssocType ( trait ) ]
3531}
3632
3733cached
@@ -43,8 +39,11 @@ newtype TType =
4339 TNeverType ( ) or
4440 TUnknownType ( ) or
4541 TTypeParamTypeParameter ( TypeParam t ) or
46- TAssociatedTypeTypeParameter ( Trait trait , TypeAlias typeAlias ) {
47- getTraitTypeAlias ( trait ) = typeAlias
42+ TAssociatedTypeTypeParameter ( Trait trait , AssocType typeAlias ) {
43+ getTraitAssocType ( trait ) = typeAlias
44+ } or
45+ TTypeParamAssociatedTypeTypeParameter ( TypeParam tp , AssocType assoc ) {
46+ tpAssociatedType ( tp , assoc , _)
4847 } or
4948 TDynTraitTypeParameter ( Trait trait , AstNode n ) { dynTraitTypeParameter ( trait , n ) } or
5049 TImplTraitTypeParameter ( ImplTraitTypeRepr implTrait , TypeParam tp ) {
@@ -464,6 +463,52 @@ class AssociatedTypeTypeParameter extends TypeParameter, TAssociatedTypeTypePara
464463 override Location getLocation ( ) { result = typeAlias .getLocation ( ) }
465464}
466465
466+ /**
467+ * A type parameter corresponding to an associated type accessed on a type
468+ * parameter, for example `T::AssociatedType` where `T` is a type parameter.
469+ *
470+ * These type parameters are created when a function signature accesses an
471+ * associated type on a type parameter. For example, in
472+ * ```rust
473+ * fn foo<T: SomeTrait>(arg: T::Assoc) { }
474+ * ```
475+ * we create a `TypeParamAssociatedTypeTypeParameter` for `Assoc` on `T` and the
476+ * mention `T::Assoc` resolves to this type parameter. If denoting the type
477+ * parameter by `T_Assoc` then the above function is treated as if it was
478+ * ```rust
479+ * fn foo<T: SomeTrait<Assoc = T_Assoc>, T_Assoc>(arg: T_Assoc) { }
480+ * ```
481+ */
482+ class TypeParamAssociatedTypeTypeParameter extends TypeParameter ,
483+ TTypeParamAssociatedTypeTypeParameter
484+ {
485+ private TypeParam typeParam ;
486+ private AssocType assoc ;
487+
488+ TypeParamAssociatedTypeTypeParameter ( ) {
489+ this = TTypeParamAssociatedTypeTypeParameter ( typeParam , assoc )
490+ }
491+
492+ /** Gets the type parameter that this associated type is accessed on. */
493+ TypeParam getTypeParam ( ) { result = typeParam }
494+
495+ /** Gets the associated type alias. */
496+ AssocType getTypeAlias ( ) { result = assoc }
497+
498+ /** Gets a path that accesses this type parameter. */
499+ Path getAPath ( ) { tpAssociatedType ( typeParam , assoc , result ) }
500+
501+ override ItemNode getDeclaringItem ( ) { result .getTypeParam ( _) = typeParam }
502+
503+ override string toString ( ) {
504+ result =
505+ typeParam .toString ( ) + "::" + assoc .getName ( ) .getText ( ) + "[" +
506+ assoc .getTrait ( ) .getName ( ) .getText ( ) + "]"
507+ }
508+
509+ override Location getLocation ( ) { result = typeParam .getLocation ( ) }
510+ }
511+
467512/** Gets the associated type type-parameter corresponding directly to `typeAlias`. */
468513AssociatedTypeTypeParameter getAssociatedTypeTypeParameter ( TypeAlias typeAlias ) {
469514 result .isDirect ( ) and result .getTypeAlias ( ) = typeAlias
0 commit comments