Skip to content

Commit b98238d

Browse files
committed
Swift: Add type inference tests
1 parent 79baaad commit b98238d

3 files changed

Lines changed: 1463 additions & 0 deletions

File tree

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

Whitespace-only changes.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import swift
2+
import TestUtils
3+
import utils.test.InlineExpectationsTest
4+
5+
pragma[nomagic]
6+
private predicate declHasPotentialCommentAt(Decl d, string path, int line) {
7+
d.getLocation().hasLocationInfo(path, line + 1, _, _, _)
8+
}
9+
10+
pragma[nomagic]
11+
private SingleLineComment getPrecedingComment(Decl d) {
12+
exists(string path, int line |
13+
declHasPotentialCommentAt(d, path, line) and
14+
result.getLocation().hasLocationInfo(path, line, _, _, _)
15+
)
16+
}
17+
18+
module ResolveTest implements TestSig {
19+
string getARelevantTag() { result = "target" }
20+
21+
private predicate declHasName(Decl c, string value) {
22+
exists(string s |
23+
s = getPrecedingComment(c).getText() and
24+
value = s.substring(3, s.length() - 1)
25+
)
26+
or
27+
not exists(getPrecedingComment(c)) and
28+
value = [c.(EnumElementDecl).getName(), c.(Function).getName()]
29+
}
30+
31+
predicate hasActualResult(Location location, string element, string tag, string value) {
32+
exists(AstNode source, Decl target |
33+
location = source.getLocation() and
34+
element = source.toString() and
35+
target =
36+
[
37+
source.(CallExpr).getStaticTarget().(Decl), source.(MethodLookupExpr).getMember(),
38+
source.(EnumElementPattern).getElement()
39+
] and
40+
declHasName(target, value) and
41+
tag = "target" and
42+
toBeTested(source) and
43+
toBeTested(target)
44+
)
45+
}
46+
}
47+
48+
private Type getTypeAt(Type t, string path) {
49+
path = "" and
50+
result = t.getUnderlyingType()
51+
or
52+
exists(BoundGenericType b, GenericTypeDecl decl |
53+
b = t and
54+
decl = b.getDeclaration()
55+
|
56+
exists(string prefix, string suffix, int i |
57+
result = getTypeAt(b.getArgType(i).getUnderlyingType(), suffix) and
58+
prefix = decl.getName() + "<" + i + ">" and
59+
if suffix = "" then path = prefix else path = prefix + "." + suffix
60+
)
61+
)
62+
}
63+
64+
module TypeTest implements TestSig {
65+
string getARelevantTag() { result = ["type", "certainType"] }
66+
67+
predicate tagIsOptional(string expectedTag) { expectedTag = "type" }
68+
69+
predicate hasActualResult(Location location, string element, string tag, string value) { none() }
70+
71+
predicate hasOptionalResult(Location location, string element, string tag, string value) {
72+
exists(Locatable e, string path, Type t, Type t0 |
73+
t = [e.(Expr).getType(), e.(VarDecl).getType()] and
74+
tag = "type" and
75+
location = e.getLocation() and
76+
t0 = getTypeAt(t, path) and
77+
exists(string name, string at |
78+
name = t0.(AnyGenericType).getDeclaration().getName()
79+
or
80+
not t0 instanceof AnyGenericType and
81+
name = t0.getName()
82+
|
83+
(if path = "" then at = "" else at = "@" + path) and
84+
value = element + at + ":" + name and
85+
element = e.toString()
86+
)
87+
)
88+
}
89+
}
90+
91+
import MakeTest<MergeTests<ResolveTest, TypeTest>>

0 commit comments

Comments
 (0)