Skip to content

Commit 2c7993d

Browse files
committed
self
1 parent c3a360b commit 2c7993d

File tree

5 files changed

+68
-63
lines changed

5 files changed

+68
-63
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ abstract private class ImplOrTraitItemNode extends ItemNode {
238238
}
239239

240240
class ImplItemNode extends ImplOrTraitItemNode instanceof Impl {
241-
ItemNode resolveSelfTy() { result = resolvePath(super.getSelfTy().(PathTypeRepr).getPath()) }
241+
Path getSelfPath() { result = super.getSelfTy().(PathTypeRepr).getPath() }
242+
243+
ItemNode resolveSelfTy() { result = resolvePath(this.getSelfPath()) }
242244

243245
override string getName() { result = "(impl)" }
244246

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

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ class StructType extends Type, TStruct {
144144
// todo: assumes all `impl` blocks are in scope
145145
exists(ImplItemNode i |
146146
struct = i.resolveSelfTy() and
147-
result = i.getASuccessor(name)
147+
result = i.getASuccessor(name) and
148+
// todo: generics are not supported
149+
not i.getSelfPath().getPart().hasGenericArgList()
148150
)
149151
}
150152

@@ -173,7 +175,9 @@ class EnumType extends Type, TEnum {
173175
// todo: assumes all `impl` blocks are in scope
174176
exists(ImplItemNode i |
175177
enum = i.resolveSelfTy() and
176-
result = i.getASuccessor(name)
178+
result = i.getASuccessor(name) and
179+
// todo: generics are not supported
180+
not i.getSelfPath().getPart().hasGenericArgList()
177181
)
178182
}
179183

@@ -386,34 +390,6 @@ private module BaseTypes {
386390
)
387391
}
388392

389-
// pragma[nomagic]
390-
// predicate arrayBaseTypeHasTypeParameterAt(Type base, TypePath path) {
391-
// exists(UnboundType::TsIListInterfaceType immediateBase |
392-
// base = immediateBase and
393-
// path = "0"
394-
// or
395-
// // transitive base class
396-
// exists(TypeRepr baseTypeMention |
397-
// baseTypeMentionHasTypeParameterAt(immediateBase, baseTypeMention, path, _) and
398-
// base = baseTypeMention.getType()
399-
// )
400-
// )
401-
// }
402-
// pragma[nomagic]
403-
// predicate arrayBaseTypeHasNonTypeParameterAt(Type base, TypePath path, Type t) {
404-
// not t instanceof TypeParameter and
405-
// exists(UnboundType::TsIListInterfaceType immediateBase |
406-
// base = immediateBase and
407-
// t = base and
408-
// path = ""
409-
// or
410-
// // transitive base class
411-
// exists(TypeRepr baseTypeMention |
412-
// baseTypeMentionHasNonTypeParameterAt(immediateBase, baseTypeMention, path, t) and
413-
// base = baseTypeMention.getType()
414-
// )
415-
// )
416-
// }
417393
/**
418394
* Holds if `base` is a (transitive) base type mention of `sub`, and
419395
* non-type-parameter `t` is mentioned (implicitly) at `path` inside `base`.
@@ -534,7 +510,10 @@ private signature module MatchingInputSig {
534510
}
535511

536512
bindingset[this]
537-
class Pos;
513+
class Pos {
514+
bindingset[this]
515+
string toString();
516+
}
538517

539518
Pos getReturnPos();
540519

@@ -614,14 +593,26 @@ private module Matching<MatchingInputSig Input> {
614593
}
615594

616595
private module BaseTypeAtInput implements BaseTypeAtInputSig {
617-
newtype Node =
618-
additional MkNode(Access a, Pos pos) {
596+
private newtype TNode =
597+
MkNode(Access a, Pos pos) {
619598
exists(Decl target |
620599
argumentTypeAt(a, pos, target, _, _) and
621600
declType(target, _, _, any(TypeParameter tp))
622601
)
623602
}
624603

604+
additional Node mkNode(Access a, Pos pos) { result = MkNode(a, pos) }
605+
606+
class Node extends MkNode {
607+
Access getAccess() { this = MkNode(result, _) }
608+
609+
Pos getPos() { this = MkNode(_, result) }
610+
611+
string toString() { result = this.getAccess().toString() + ", " + this.getPos().toString() }
612+
613+
Location getLocation() { result = this.getAccess().getLocation() }
614+
}
615+
625616
Type resolveType(Node n, TypePath path) {
626617
exists(Access a, Pos pos |
627618
n = MkNode(a, pos) and
@@ -648,7 +639,7 @@ private module Matching<MatchingInputSig Input> {
648639
) {
649640
exists(TypeRepr_ tm |
650641
target(a, target) and
651-
NodeHasBaseTypeAt<BaseTypeAtInput>::hasBaseType(BaseTypeAtInput::MkNode(a, pos), tm, path, t) and
642+
NodeHasBaseTypeAt<BaseTypeAtInput>::hasBaseType(BaseTypeAtInput::mkNode(a, pos), tm, path, t) and
652643
base = tm.resolveType()
653644
)
654645
// or
@@ -740,10 +731,21 @@ private Type resolveVariableType(AstNode n, TypePath path) {
740731
}
741732

742733
pragma[nomagic]
743-
private Type resolveSelfType(Impl i, TypePath path) {
734+
private Type resolveImplSelfType(Impl i, TypePath path) {
744735
result = i.getSelfTy().(TypeRepr_).resolveTypeAt(path)
745736
}
746737

738+
pragma[nomagic]
739+
private Type resolveTraitSelfType(Trait t, TypePath path) {
740+
result = TTrait(t) and
741+
path.isEmpty()
742+
or
743+
exists(int i |
744+
result = TTypeParameter(t.getGenericParamList().getTypeParam(i)) and
745+
path = typePath(i)
746+
)
747+
}
748+
747749
pragma[nomagic]
748750
private Type resolveTargetTyped(AstNode n, TypePath path) {
749751
isTargetTyped(n) and
@@ -753,9 +755,13 @@ private Type resolveTargetTyped(AstNode n, TypePath path) {
753755
result = resolveType(let.getInitializer(), path)
754756
)
755757
or
756-
exists(ImplItemNode i, Function f, SelfParam p, TypePath suffix, Type res |
758+
exists(ItemNode i, Function f, SelfParam p, TypePath suffix, Type res |
757759
n = p and
758-
res = resolveSelfType(i, suffix) and
760+
(
761+
res = resolveImplSelfType(i, suffix)
762+
or
763+
res = resolveTraitSelfType(i, suffix)
764+
) and
759765
f = i.getASuccessor(_) and
760766
p = f.getParamList().getSelfParam()
761767
|
Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +0,0 @@
1-
multipleStaticCallTargets
2-
| main.rs:61:26:61:31 | x.m1(...) | main.rs:40:9:42:9 | fn m1 |
3-
| main.rs:61:26:61:31 | x.m1(...) | main.rs:46:9:48:9 | fn m1 |
4-
| main.rs:62:26:62:31 | y.m1(...) | main.rs:40:9:42:9 | fn m1 |
5-
| main.rs:62:26:62:31 | y.m1(...) | main.rs:46:9:48:9 | fn m1 |
6-
| main.rs:114:26:114:31 | x.m1(...) | main.rs:99:9:101:9 | fn m1 |
7-
| main.rs:114:26:114:31 | x.m1(...) | main.rs:105:9:107:9 | fn m1 |
8-
| main.rs:115:26:115:31 | y.m1(...) | main.rs:99:9:101:9 | fn m1 |
9-
| main.rs:115:26:115:31 | y.m1(...) | main.rs:105:9:107:9 | fn m1 |

rust/ql/test/library-tests/type-inference/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ mod m2 {
5858
let x = MyThing { a: S1 };
5959
let y = MyThing { a: S2 };
6060

61-
println!("{:?}", x.m1()); // `x.m1` missing type at path 0, instead at path ""
62-
println!("{:?}", y.m1().a); // `y.m2` missing type at path 0, instead at path ""
61+
println!("{:?}", x.m1()); // missing
62+
println!("{:?}", y.m1().a); // missing
6363

6464
let x = MyThing { a: S1 };
6565
let y = MyThing { a: S2 };
6666

67-
println!("{:?}", x.m2()); // `x.m2` missing type
68-
println!("{:?}", y.m2()); // `y.m2` missing type
67+
println!("{:?}", x.m2()); // missing
68+
println!("{:?}", y.m2()); // missing
6969
}
7070
}
7171

@@ -87,7 +87,7 @@ mod m3 {
8787
where
8888
Self: Sized,
8989
{
90-
self // `self` missing type
90+
self
9191
}
9292
}
9393

@@ -111,8 +111,8 @@ mod m3 {
111111
let x = MyThing { a: S1 };
112112
let y = MyThing { a: S2 };
113113

114-
println!("{:?}", x.m1()); // `x.m1` missing type at path 0, instead at path ""
115-
println!("{:?}", y.m1().a); // `y.m1` missing type at path 0, instead at path ""
114+
println!("{:?}", x.m1()); // missing
115+
println!("{:?}", y.m1().a); // missing
116116

117117
let x = MyThing { a: S1 };
118118
let y = MyThing { a: S2 };
@@ -140,7 +140,7 @@ mod m4 {
140140
where
141141
Self: Sized,
142142
{
143-
self // `self` missing type
143+
self
144144
}
145145
}
146146

rust/ql/test/library-tests/type-inference/type-inference.expected

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,8 @@
4242
| main.rs:59:30:59:31 | S2 | | main.rs:36:5:37:14 | struct S2 |
4343
| main.rs:61:26:61:26 | x | | main.rs:29:5:32:5 | struct MyThing |
4444
| main.rs:61:26:61:26 | x | 0 | main.rs:34:5:35:14 | struct S1 |
45-
| main.rs:61:26:61:31 | x.m1(...) | | main.rs:29:5:32:5 | struct MyThing |
46-
| main.rs:61:26:61:31 | x.m1(...) | | main.rs:34:5:35:14 | struct S1 |
4745
| main.rs:62:26:62:26 | y | | main.rs:29:5:32:5 | struct MyThing |
4846
| main.rs:62:26:62:26 | y | 0 | main.rs:36:5:37:14 | struct S2 |
49-
| main.rs:62:26:62:31 | y.m1(...) | | main.rs:29:5:32:5 | struct MyThing |
50-
| main.rs:62:26:62:31 | y.m1(...) | | main.rs:34:5:35:14 | struct S1 |
5147
| main.rs:64:13:64:13 | x | | main.rs:29:5:32:5 | struct MyThing |
5248
| main.rs:64:13:64:13 | x | 0 | main.rs:34:5:35:14 | struct S1 |
5349
| main.rs:64:17:64:33 | MyThing {...} | | main.rs:29:5:32:5 | struct MyThing |
@@ -62,6 +58,12 @@
6258
| main.rs:67:26:67:26 | x | 0 | main.rs:34:5:35:14 | struct S1 |
6359
| main.rs:68:26:68:26 | y | | main.rs:29:5:32:5 | struct MyThing |
6460
| main.rs:68:26:68:26 | y | 0 | main.rs:36:5:37:14 | struct S2 |
61+
| main.rs:84:15:84:18 | SelfParam | | main.rs:83:5:92:5 | trait MyTrait |
62+
| main.rs:84:15:84:18 | SelfParam | 0 | main.rs:83:19:83:19 | A |
63+
| main.rs:86:15:86:18 | SelfParam | | main.rs:83:5:92:5 | trait MyTrait |
64+
| main.rs:86:15:86:18 | SelfParam | 0 | main.rs:83:19:83:19 | A |
65+
| main.rs:90:13:90:16 | self | | main.rs:83:5:92:5 | trait MyTrait |
66+
| main.rs:90:13:90:16 | self | 0 | main.rs:83:19:83:19 | A |
6567
| main.rs:94:43:94:43 | x | | main.rs:94:26:94:40 | T2 |
6668
| main.rs:95:9:95:9 | x | | main.rs:94:26:94:40 | T2 |
6769
| main.rs:99:15:99:18 | SelfParam | | main.rs:73:5:76:5 | struct MyThing |
@@ -88,12 +90,8 @@
8890
| main.rs:112:30:112:31 | S2 | | main.rs:80:5:81:14 | struct S2 |
8991
| main.rs:114:26:114:26 | x | | main.rs:73:5:76:5 | struct MyThing |
9092
| main.rs:114:26:114:26 | x | 0 | main.rs:78:5:79:14 | struct S1 |
91-
| main.rs:114:26:114:31 | x.m1(...) | | main.rs:73:5:76:5 | struct MyThing |
92-
| main.rs:114:26:114:31 | x.m1(...) | | main.rs:78:5:79:14 | struct S1 |
9393
| main.rs:115:26:115:26 | y | | main.rs:73:5:76:5 | struct MyThing |
9494
| main.rs:115:26:115:26 | y | 0 | main.rs:80:5:81:14 | struct S2 |
95-
| main.rs:115:26:115:31 | y.m1(...) | | main.rs:73:5:76:5 | struct MyThing |
96-
| main.rs:115:26:115:31 | y.m1(...) | | main.rs:78:5:79:14 | struct S1 |
9795
| main.rs:117:13:117:13 | x | | main.rs:73:5:76:5 | struct MyThing |
9896
| main.rs:117:13:117:13 | x | 0 | main.rs:78:5:79:14 | struct S1 |
9997
| main.rs:117:17:117:33 | MyThing {...} | | main.rs:73:5:76:5 | struct MyThing |
@@ -108,6 +106,12 @@
108106
| main.rs:120:40:120:40 | x | 0 | main.rs:78:5:79:14 | struct S1 |
109107
| main.rs:121:40:121:40 | y | | main.rs:73:5:76:5 | struct MyThing |
110108
| main.rs:121:40:121:40 | y | 0 | main.rs:80:5:81:14 | struct S2 |
109+
| main.rs:137:15:137:18 | SelfParam | | main.rs:136:5:145:5 | trait MyTrait |
110+
| main.rs:137:15:137:18 | SelfParam | 0 | main.rs:136:19:136:19 | A |
111+
| main.rs:139:15:139:18 | SelfParam | | main.rs:136:5:145:5 | trait MyTrait |
112+
| main.rs:139:15:139:18 | SelfParam | 0 | main.rs:136:19:136:19 | A |
113+
| main.rs:143:13:143:16 | self | | main.rs:136:5:145:5 | trait MyTrait |
114+
| main.rs:143:13:143:16 | self | 0 | main.rs:136:19:136:19 | A |
111115
| main.rs:147:43:147:43 | x | | main.rs:147:26:147:40 | T2 |
112116
| main.rs:148:9:148:9 | x | | main.rs:147:26:147:40 | T2 |
113117
| main.rs:152:15:152:18 | SelfParam | | main.rs:126:5:129:5 | struct MyThing |
@@ -143,6 +147,8 @@
143147
| main.rs:167:40:167:40 | x | 0 | main.rs:131:5:132:14 | struct S1 |
144148
| main.rs:168:40:168:40 | y | | main.rs:126:5:129:5 | struct MyThing |
145149
| main.rs:168:40:168:40 | y | 0 | main.rs:133:5:134:14 | struct S2 |
150+
| main.rs:176:15:176:18 | SelfParam | | main.rs:173:5:185:5 | trait MyTrait |
151+
| main.rs:178:15:178:18 | SelfParam | | main.rs:173:5:185:5 | trait MyTrait |
146152
| main.rs:193:15:193:18 | SelfParam | | main.rs:187:5:188:13 | struct S |
147153
| main.rs:194:13:194:13 | S | | main.rs:187:5:188:13 | struct S |
148154
| main.rs:199:13:199:13 | x | | main.rs:187:5:188:13 | struct S |

0 commit comments

Comments
 (0)