Skip to content

Commit 04fec21

Browse files
committed
Unified: Do not generate inherited constructors after all
1 parent da6b3e5 commit 04fec21

5 files changed

Lines changed: 6 additions & 71 deletions

File tree

‎unified/ql/lib/codeql/unified/internal/CallableEx.qll‎

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,14 @@ private import unified
66
private import CallableExPlugin
77
private import NameBinding
88

9-
private predicate mayHaveImplicitConstructor(ClassLikeDeclaration cls, string name) {
10-
mayHaveConstructor(cls, name) and
11-
forall(ConstructorDeclaration c | c = cls.getAMember() | not constructorPreventsImplicit(c))
12-
}
13-
14-
private predicate mayInheritConstructorFrom(ClassLikeDeclaration cls, ClassLikeDeclaration base) {
15-
mayInheritConstructor(cls) and
16-
mayHaveImplicitConstructor(cls, _) and
17-
base.getNameNode() = getStaticBindingTargetFromRef(cls.getABaseType().getType()) and
18-
mayHaveConstructor(base, _)
19-
}
20-
219
private newtype TCallableEx =
2210
TAstCallableEx(Callable c) or
2311
TDefaultConstructor(ClassLikeDeclaration cls, string name) {
24-
mayHaveImplicitConstructor(cls, name) and
25-
forall(ClassLikeDeclaration base | mayInheritConstructorFrom(cls, base) |
26-
not mayHaveConstructor(cls, _)
12+
mayHaveConstructor(cls, name) and
13+
not cls.getAMember() instanceof ConstructorDeclaration and
14+
not exists(ClassLikeDeclaration base |
15+
base.getNameNode() = getStaticBindingTargetFromRef(cls.getABaseType().getType()) and
16+
mayHaveConstructor(base, _)
2717
)
2818
}
2919

‎unified/ql/lib/codeql/unified/internal/CallableExPlugin.qll‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,20 @@ class CallableExPlugin extends Unit {
1313
*/
1414
predicate mayHaveConstructor(ClassLikeDeclaration cls, string name) { none() }
1515

16-
/**
17-
* Holds if the presence of the constructor `c` prevents the class from having
18-
* an implicit (default or inherited) constructor.
19-
*/
20-
bindingset[c]
21-
predicate constructorPreventsImplicit(ConstructorDeclaration c) { none() }
22-
2316
/**
2417
* Holds if a default constructor for `cls` would need to have a parameter
2518
* at index `i` with the given name. `i = 0` should be the implicit receiver
2619
* parameter.
2720
*/
2821
bindingset[cls]
2922
predicate defaultConstructorParameter(ClassLikeDeclaration cls, int i, string name) { none() }
30-
31-
/**
32-
* Holds if the class-like declaration `cls` may inherit a constructor from
33-
* a base class, provided that no explicit constructor exists.
34-
*/
35-
bindingset[cls]
36-
predicate mayInheritConstructor(ClassLikeDeclaration cls) { none() }
3723
}
3824

3925
predicate mayHaveConstructor(ClassLikeDeclaration cls, string name) {
4026
any(CallableExPlugin p).mayHaveConstructor(cls, name)
4127
}
4228

43-
bindingset[c]
44-
predicate constructorPreventsImplicit(ConstructorDeclaration c) {
45-
any(CallableExPlugin p).constructorPreventsImplicit(c)
46-
}
47-
4829
bindingset[cls]
4930
predicate defaultConstructorParameter(ClassLikeDeclaration cls, int i, string name) {
5031
any(CallableExPlugin p).defaultConstructorParameter(cls, i, name)
5132
}
52-
53-
bindingset[cls]
54-
predicate mayInheritConstructor(ClassLikeDeclaration cls) {
55-
any(CallableExPlugin p).mayInheritConstructor(cls)
56-
}

‎unified/ql/lib/codeql/unified/internal/CallableExPluginSwift.qll‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ class CallableExPluginSwift extends CallableExPlugin {
77
name = "init"
88
}
99

10-
bindingset[c]
11-
override predicate constructorPreventsImplicit(ConstructorDeclaration c) {
12-
not c.hasModifier("convenience")
13-
}
14-
1510
bindingset[cls]
1611
override predicate defaultConstructorParameter(ClassLikeDeclaration cls, int i, string name) {
1712
i = 0 and
@@ -34,7 +29,4 @@ class CallableExPluginSwift extends CallableExPlugin {
3429
s order by j
3530
)
3631
}
37-
38-
bindingset[cls]
39-
override predicate mayInheritConstructor(ClassLikeDeclaration cls) { any() }
4032
}

‎unified/ql/test/library-tests/constructors/constructors.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class C6<T1, T2> {
3131
class C7<T3, T4>: C6<T4, T3> {} // inherits `init(x: T4, y: T3)` and `convenience init(x: T4)`
3232

3333
class C8: C7<Int, String> { // inherits `init(x: String, y: Int)`
34-
convenience init(x: String) { // todo: currently also inherits `convenience init` from C6
34+
convenience init(x: String) {
3535
self.init(x: x, y: 0)
3636
}
3737
}

‎unified/ql/test/library-tests/constructors/test.expected‎

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,3 @@ callableExDefaultConstructorParameter
77
| constructors.swift:39:1:45:1 | S1 [default constructor] | 1 | constructors.swift:39:1:45:1 | f1 [S1 default constructor] |
88
| constructors.swift:39:1:45:1 | S1 [default constructor] | 2 | constructors.swift:39:1:45:1 | f2 [S1 default constructor] |
99
| constructors.swift:39:1:45:1 | S1 [default constructor] | 3 | constructors.swift:39:1:45:1 | f4 [S1 default constructor] |
10-
callableExInheritedConstructor
11-
| constructors.swift:3:1:3:15 | C2 [inherited from C1] | constructors.swift:1:1:1:11 | C1 [default constructor] |
12-
| constructors.swift:19:1:19:15 | C5 [inherited from C4] | constructors.swift:10:5:12:5 | init |
13-
| constructors.swift:19:1:19:15 | C5 [inherited from C4] | constructors.swift:14:5:16:5 | convenience init |
14-
| constructors.swift:31:1:31:31 | C7 [inherited from C6] | constructors.swift:22:5:22:25 | init |
15-
| constructors.swift:31:1:31:31 | C7 [inherited from C6] | constructors.swift:26:5:28:5 | convenience init |
16-
| constructors.swift:33:1:37:1 | C8 [inherited from C7] | constructors.swift:31:1:31:31 | C7 [inherited from C6] |
17-
| constructors.swift:33:1:37:1 | C8 [inherited from C7] | constructors.swift:31:1:31:31 | C7 [inherited from C6] |
18-
callableExInheritedConstructorParameter
19-
| constructors.swift:3:1:3:15 | C2 [inherited from C1] | 0 | constructors.swift:3:1:3:15 | parameter 0 of init [inherited from C1] | constructors.swift:1:1:1:11 | self [C1 default constructor] |
20-
| constructors.swift:19:1:19:15 | C5 [inherited from C4] | 0 | constructors.swift:19:1:19:15 | parameter 0 of init [inherited from C4] | constructors.swift:10:5:12:5 | self |
21-
| constructors.swift:19:1:19:15 | C5 [inherited from C4] | 0 | constructors.swift:19:1:19:15 | parameter 0 of init [inherited from C4] | constructors.swift:14:5:16:5 | self |
22-
| constructors.swift:19:1:19:15 | C5 [inherited from C4] | 1 | constructors.swift:19:1:19:15 | parameter 1 of init [inherited from C4] | constructors.swift:10:10:10:17 | Parameter |
23-
| constructors.swift:31:1:31:31 | C7 [inherited from C6] | 0 | constructors.swift:31:1:31:31 | parameter 0 of init [inherited from C6] | constructors.swift:22:5:22:25 | self |
24-
| constructors.swift:31:1:31:31 | C7 [inherited from C6] | 0 | constructors.swift:31:1:31:31 | parameter 0 of init [inherited from C6] | constructors.swift:26:5:28:5 | self |
25-
| constructors.swift:31:1:31:31 | C7 [inherited from C6] | 1 | constructors.swift:31:1:31:31 | parameter 1 of init [inherited from C6] | constructors.swift:22:10:22:14 | Parameter |
26-
| constructors.swift:31:1:31:31 | C7 [inherited from C6] | 1 | constructors.swift:31:1:31:31 | parameter 1 of init [inherited from C6] | constructors.swift:26:22:26:26 | Parameter |
27-
| constructors.swift:31:1:31:31 | C7 [inherited from C6] | 2 | constructors.swift:31:1:31:31 | parameter 2 of init [inherited from C6] | constructors.swift:22:17:22:21 | Parameter |
28-
| constructors.swift:33:1:37:1 | C8 [inherited from C7] | 0 | constructors.swift:33:1:37:1 | parameter 0 of init [inherited from C7] | constructors.swift:22:5:22:25 | self |
29-
| constructors.swift:33:1:37:1 | C8 [inherited from C7] | 0 | constructors.swift:33:1:37:1 | parameter 0 of init [inherited from C7] | constructors.swift:26:5:28:5 | self |
30-
| constructors.swift:33:1:37:1 | C8 [inherited from C7] | 1 | constructors.swift:33:1:37:1 | parameter 1 of init [inherited from C7] | constructors.swift:22:10:22:14 | Parameter |
31-
| constructors.swift:33:1:37:1 | C8 [inherited from C7] | 1 | constructors.swift:33:1:37:1 | parameter 1 of init [inherited from C7] | constructors.swift:26:22:26:26 | Parameter |
32-
| constructors.swift:33:1:37:1 | C8 [inherited from C7] | 2 | constructors.swift:33:1:37:1 | parameter 2 of init [inherited from C7] | constructors.swift:22:17:22:21 | Parameter |

0 commit comments

Comments
 (0)