Skip to content

Commit ac8dc5e

Browse files
committed
Rust: Add Impl::getSelf() and Impl::getTrait()
1 parent a7d4477 commit ac8dc5e

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

rust/ql/lib/codeql/rust/elements/internal/ImplImpl.qll

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
* INTERNAL: Do not use.
55
*/
66

7+
private import rust
78
private import codeql.rust.elements.internal.generated.Impl
9+
private import codeql.rust.internal.PathResolution as PathResolution
10+
private import codeql.rust.internal.typeinference.Type
11+
private import codeql.rust.internal.typeinference.TypeMention
812

913
/**
1014
* INTERNAL: This module contains the customizable definition of `Impl` and should not
@@ -39,5 +43,39 @@ module Impl {
3943
*/
4044
pragma[nomagic]
4145
predicate isInherent() { not this.hasTraitTy() }
46+
47+
/**
48+
* Gets the type being implemented.
49+
*
50+
* For example, in
51+
*
52+
* ```rust
53+
* impl MyType { ... }
54+
*
55+
* impl MyTrait for MyType { ... }
56+
* ```
57+
*
58+
* the type being implemented is in both cases`MyType`.
59+
*/
60+
TypeItem getSelf() {
61+
result = this.getSelfTy().(TypeMention).getType().(DataType).getTypeItem()
62+
}
63+
64+
/**
65+
* Gets the trait being implemented, if any.
66+
*
67+
* For example, in
68+
*
69+
* ```rust
70+
* impl MyType { ... }
71+
*
72+
* impl MyTrait for MyType { ... }
73+
* ```
74+
*
75+
* the trait being implemented is in the second case `MyTrait`.
76+
*/
77+
Trait getTrait() {
78+
result = PathResolution::resolvePath(this.getTraitTy().(PathTypeRepr).getPath())
79+
}
4280
}
4381
}

0 commit comments

Comments
 (0)